Drawing your own images

You can do more than place PNG and JPEG images in graphics windows; you can draw your own. The main command for this is glk_window_fill_rect(), which takes six arguments:

1) The name of the window you want to draw to.

2) The color you want your rectangle to be. This is encoded as a hex number (footnote), as follows. First, type a dollar sign (which indicates that a hex number is to follow.) Next, type a two-hex-digit number, from 00 to FF, representing the amount of red to appear in the color. Then comes a two-hex-digit number to represent the amount of green, and then a two-hex-digit number to represent the amount of blue. Thus, $000000 would be black, $FFFFFF would be white, $FF0000 would be bright red, $FFC000 would be a nice goldenrod, $C0C0FF would be baby blue, and so on.

3) The X-coordinate of the top left corner of the rectangle.

4) The Y-coordinate of the top left corner of the rectangle.

5) The width of the rectangle. If you want to draw a single pixel or a vertical line, this should be 1.

6) The height of the rectangle. If you want to draw a single pixel or a horizontal line, this should be 1.

And that's pretty much it. The other trick you can pull is to fill an entire window with a solid color, using glk_window_set_background_color() (which takes two arguments, the window in question and the color to declare as its background, encoded as above) and then following up with a glk_window_clear() (which takes one argument, the window to be cleared.) But if you want to draw more complex shapes than points, lines and rectangles, you will, at least presently, have to build them out of points, lines and rectangles. In most cases, a much better bet will be to use a graphics program to create a PNG or JPEG image.


Next section: Sound and music
Or return to the table of contents