Here is a very simple complete program that draws power and ground circuitry. The interface consists of the keys P draw power rectangles, G draw ground rectangles, C clear the window, and Q quit. To draw a rectangle, press down the left mouse button at one corner, hold it down and slide to the other corner, and release it.
When you use the program, be sure to exit by typing Q. This resets the four lowest entries in the color map (which are used by all the windows) back to the default values. */
#include <gl/gl.h> #include <gl/device.h>
#define BACKGROUND 0 /* = 00 */ #define POWER 1 /* = 01 */ #define GROUND 2 /* = 10 */ #define SHORT 3 /* = 11 */
long xorg, yorg, xsize, ysize; main() { short val, drawtype; short x1, y1, x2, y2; drawtype = GROUND; prefposition (0,800,0,800); winopen("circuit"); winconstraints(); getorigin(&xorg, &yorg); getsize (&xsize,&ysize); ortho2 (-0.5,(Coord)xsize,-0.5,(Coord)ysize); /*Allow any size to work*/ gconfig(); mapcolor(0, 255, 255, 255); mapcolor(1, 0, 0, 255); mapcolor(2, 0, 0, 0); mapcolor(3, 255, 0, 0); qdevice(PKEY); /* draw power rectangles */ qdevice(GKEY); /* draw ground rectangles */ qdevice(CKEY); /* clear screen */ qdevice(QKEY); /* quit */ qdevice(REDRAW); qdevice(LEFTMOUSE); /* mark rectangle corners */ tie(LEFTMOUSE, MOUSEX, MOUSEY); color(0); clear(); while (1) switch (qread(&val)) { case PKEY: drawtype = POWER; break; case GKEY: drawtype = GROUND; break; case REDRAW: reshapeviewport(); getorigin(&xorg, &yorg); getsize (&xsize,&ysize); ortho2 (-0.5,(Coord)xsize,-0.5,(Coord)ysize); clearscreen(); gconfig(); case CKEY: clearscreen(); break; case QKEY: /* restore default colors */ mapcolor(0, 0, 0, 0); mapcolor(1, 255, 0, 0); mapcolor(2, 0, 255, 0); mapcolor(3, 255, 255, 0); return; case LEFTMOUSE: qread(&x1); qread(&y1); qread(&val); qread(&x2); qread(&y2); if (drawtype == POWER) powerrect(x1-xorg, y1-yorg, x2-xorg, y2-yorg); else groundrect(x1-xorg, y1-yorg, x2-xorg, y2-yorg); break; }
} clearscreen() { writemask(3); /* clear window before drawing. */ color(BACKGROUND); clear(); } powerrect(x1, y1, x2, y2) short x1, y1, x2, y2; { writemask(1); color(1); rectfs(x1, y1, x2, y2); }
groundrect(x1, y1, x2, y2) short x1, y1, x2, y2; { writemask(2); color(2); rectfs(x1, y1, x2, y2); }
/* Changes: -The program now starts from the default size window prefposition (0,800,0,800); getsize (&xsize,&ysize); qdevice(REDRAW); -The program now handles resize/redraw case REDRAW: reshapeviewport(); getorigin(&xorg, &yorg); getsize (&xsize,&ysize); ortho2 (-0.5,(Coord)xsize,-0.5,(Coord)ysize); clearscreen(); gconfig(); */
The writemask subroutine .
Writemasks and Logical Operations in GL3.2 Version 4 for AIX: Programming Concepts.