[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Draw Example

The following is the version of Greet program that simply illustrates the use of the Draw module:

Program Listing 3-14 drawgreet.b

  1. implement DrawGreet;
  2. include "sys.m";
  3. include "draw.m";
  4. sys: Sys;
  5. draw: Draw;
  6. Display, Font, Rect, Point, Image, Screen: import draw;
  7. display: ref Display;
  8. disp, img: ref Image;
  9. font: Font;
  10. DrawGreet : module {
  11. init: fn(ctxt: ref Draw->Context, argv: list of string);
  12. };
  13. init(ctxt: ref Draw->Context, argv: list of string) {
  14. sys = load Sys Sys->PATH;
  15. draw = load Draw Draw->PATH;
  16. display = Display.allocate(nil);
  17. disp = display.image;
  18. spawn refresh(display);
  19. white := display.color(Draw->White); #predefined color
  20. yellow := display.color(Draw->Yellow); #predefined color
  21. black := display.color(Draw->Black); #predefined color
  22. ones := display.ones; # pixel mask
  23. font = Font.open(display,
    "/fonts/lucidasans/unicode.9x24.font");
  24. if ((tl argv) != nil)
  25. str := "Hello, " + (hd(tl argv)) + "!";
  26. else
  27. str = "Hello, World!";
  28. strctr := (font.width(str) / 2);
  29. img := display.open("/icons/logon.bit");
  30. imgxctr := img.r.max.x / 2;
  31. dispctr := disp.r.max.x / 2;
  32. disp.draw(disp.r, white, ones, (0,0));
  33. disp.draw(disp.r, img, ones, (-(dispctr-imgxctr),-10));
  34. disp.text(((dispctr - strctr), img.r.max.y+20), black,
    (0,0), font, str);
  35. sys->sleep(10000);
  36. disp.draw(disp.r, white, ones, (0,0));
  37. }
  38. refresh(display: ref Display) {
  39. display.startrefresh();
  40. }

Line 7 declares the handle to the Draw module and Line 8 imports the Draw data structures into the current scope. Of particular note are the Display, Image, and Font types.

The Display type represents the physical display represented by the draw device mounted in <inferno_root>/dev/draw. Line 21 allocates this display using the Display.allocate function (the default is specified by the empty string argument nil).

The Image type provides the basic operations for a group of pixels and the building blocks for higher-level objects such as pictures (graphic image files), windows, and fonts. Line 22 assigns the visible contents (the Draw canvas) to disp. Line 37 opens a bitmap image, logon.bit, in the <inferno_root>/icons/ directory and assigns it to img.

The Font type defines the appearance of characters drawn with the Image.font function. Fonts are read from files that describe their size, style, and the portion of the Unicode character set they represent. Line 27 opens a font file, unicode.9x24.font, in the <inferno_root>/fonts/lucidasans/ directory and assigns it to font.

Line 23 refreshes the display just allocated using the recommended technique of spawning a local function, refresh (Lines 49 through 51) that calls the Display.startrefresh function in the Draw module.

Line 42 draws the display area (the rectangle specified by disp.r) using the color white. Line 43 draws the bitmap image, img, on the display. Line 44 draws the text in str using font. Line 45 pauses before the display is cleared, Line 46.

After compiling this program, you could run it from the Inferno console (not from a shell under Window Manager):

	inferno$ drawgreet Inferno
morea3.gif


[Contents]
[Prev] [Next] [Limbo Basics] [Limbo Programming] [Language Definition]

Copyright © 1998, Lucent Technologies, Inc. All rights reserved.