[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
GL3.2 for AIX: Graphics Library (GL) Technical Reference

iobounce.c Example C Language Program

/*
iobounce.c:
  A "pool" ball that "bounces" around a 2-d "surface".
  RIGHTMOUSE stops ball
  MIDDLEMOUSE increases y velocity
  LEFTMOUSE increases x velocity
*/
#include <gl/gl.h>
#include <gl/device.h>
#define XMIN 100
#define YMIN 100
#define XMAX 900
#define YMAX 700
long xvelocity = 0, yvelocity = 0;
main()
{
   Device dev;
   short val;    
   initialize();    
   while (TRUE) {       
      while (qtest()) {          
         dev = qread(&val);          
         switch (dev) {
         case LEFTMOUSE:          /* increase xvelocity */
            if (xvelocity >= 0)
               xvelocity++;
            else
               xvelocity--;
            break;
         case MIDDLEMOUSE:        /* increase yvelocity */
            if (yvelocity >= 0)
               yvelocity++;
            else
               yvelocity--;
            break;
         case RIGHTMOUSE:         /* stop ball */
            xvelocity = yvelocity = 0;
            break;                
         case ESCKEY:
            gexit();
            exit(0);
         }
      }
      drawball();
   }
}
initialize()
{
   int gid;    
   prefposition(XMAXSCREEN/4, XMAXSCREEN*3/4, YMAXSCREEN/4, 
       YMAXSCREEN*3/4);
   gid = winopen("iobounce");    
   doublebuffer();
   gconfig();
   shademodel(FLAT);    
   ortho2(XMIN - 0.5, XMAX + 0.5, YMIN - 0.5, YMAX + 0.5);    
   qdevice(ESCKEY);
   qdevice(REDRAW);
   qdevice(LEFTMOUSE);
   qdevice(MIDDLEMOUSE);
   qdevice(RIGHTMOUSE);
   qenter(REDRAW,gid);
}
drawball()
{
   static xpos = 500,ypos = 500;
   long radius = 10;    
   color(BLACK);
   clear();
   xpos += xvelocity;
   ypos += yvelocity;
   if (xpos > XMAX - radius ||
       xpos < XMIN + radius) {
      xpos -= xvelocity;
      xvelocity = -xvelocity;
   }
   if (ypos > YMAX - radius ||
       ypos < YMIN + radius) {
      ypos -= yvelocity;
      yvelocity = -yvelocity;
   }
   color(YELLOW);
   circfi(xpos, ypos, radius);
   swapbuffers();
}

Related Information


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]