/* select1.c:
Select demo program. the "ship" is the blue rectangle. The "planet" is the red circle. move the ship so it intersects the planet and the ship will crash. */
#include <gl/gl.h> #include <gl/device.h>
#define PLANET 1
main() { short type, val; register short buffer[50], cnt, i; float shipx, shipy, shipz;
for (i = 0; i < 50; i++) buffer[i] = 0; initialize(); while (TRUE) { type = qread(&val); if (val==0) continue; switch (type) { case REDRAW: reshapeviewport(); drawplanet(); break; case ESCKEY: gexit(); exit(); case LEFTMOUSE: /* set ship location to cursor location */ shipz=0; shipx=getvaluator(MOUSEX); shipy=getvaluator(MOUSEY); /* draw the ship */ color(BLUE); rect(shipx, shipy, shipx+20, shipy+10); /* specify the selecting region to be a box surrounding the ship */ pushmatrix(); ortho(shipx, shipx+.05, shipy, shipy+.05, shipz-0.5, shipz+.05); /* clear the name stack */ initnames(); gselect(buffer, 50); /* enter selecting mode */ /* put "1" on the name stack to be saved if PLANET draws into the selecting region */ loadname(1); pushname(2); /* draw the planet */ callobj(PLANET); /* exit selecting mode */ cnt = endselect(buffer); popmatrix(); /* check to see if PLANET was selected */ printf("cnt = %d\n",cnt); for (i = 0;i<4;i++) printf("buffer[%d] = %d\n",i,buffer[i]); if (buffer[1]==1) { printf("CRASH\n"); gexit(); exit(); }
break; default: break; }
}
}
initialize() { int gid; float xmax,ymax;
prefposition(XMAXSCREEN/4, XMAXSCREEN*3/4, YMAXSCREEN/4, YMAXSCREEN*3/4); keepaspect(1,1); gid = winopen("select1"); qdevice(ESCKEY); qdevice(REDRAW); qdevice(LEFTMOUSE); qenter(REDRAW,gid); xmax = .5 + (float) XMAXSCREEN; ymax = .5 + (float) YMAXSCREEN; ortho(xmax/4.0, xmax*3.0/4.0, ymax/4.0, ymax*3.0/4.0, 0.0, -xmax/2.0); createplanet(PLANET); }
drawplanet() { color(BLACK); clear(); color(RED); /* create the planet object */ callobj(PLANET); }
createplanet(x) { makeobj(x); circfi(600,600,20); closeobj(); }
The endselect subroutine , getvaluator subroutine , gselect subroutine .