The following is the version of Greet program that simply illustrates the use of the Draw module:
Program Listing 3-14 drawgreet.b
implement DrawGreet;
include "sys.m";
include "draw.m";
sys: Sys;
draw: Draw;
Display, Font, Rect, Point, Image, Screen: import draw;
display: ref Display;
disp, img: ref Image;
font: Font;
DrawGreet : module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
init(ctxt: ref Draw->Context, argv: list of string) {
sys = load Sys Sys->PATH;
draw = load Draw Draw->PATH;
display = Display.allocate(nil);
disp = display.image;
spawn refresh(display);
white := display.color(Draw->White); #predefined color
yellow := display.color(Draw->Yellow); #predefined color
black := display.color(Draw->Black); #predefined color
ones := display.ones; # pixel mask
font = Font.open(display,
"/fonts/lucidasans/unicode.9x24.font");
if ((tl argv) != nil)
str := "Hello, " + (hd(tl argv)) + "!";
else
str = "Hello, World!";
strctr := (font.width(str) / 2);
img := display.open("/icons/logon.bit");
imgxctr := img.r.max.x / 2;
dispctr := disp.r.max.x / 2;
disp.draw(disp.r, white, ones, (0,0));
disp.draw(disp.r, img, ones, (-(dispctr-imgxctr),-10));
disp.text(((dispctr - strctr), img.r.max.y+20), black,
(0,0), font, str);
sys->sleep(10000);
disp.draw(disp.r, white, ones, (0,0));
}
refresh(display: ref Display) {
display.startrefresh();
}
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![]()