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

overlay.c Example C Language Program

/*
overlay.c
This program demonstrates how to use overlay bitplanes.
*/
#include <gl/gl.h>
#include <gl/device.h>
main ()
{
   Colorindex heat;
   int i, xpos = 1013, ypos = 1013, rampup();
   float xspd = 0.0, yspd = 0.0, yaccel = -1.0;
   float yacc = -.4,  yreflect = -0.6;
   prefposition(0, XMAXSCREEN, 0, YMAXSCREEN);
   winopen ("overlay");
   doublebuffer ();
   overlay (2);
   gconfig ();
   get_cmap();
   drawmode (OVERDRAW);             /* Get into the overlay bitplanes */
   mapcolor (1, 255, 0, 0);
   mapcolor (2, 0, 255, 0);
   mapcolor (3, 0, 255, 255);
   color (1);
   rectfi (200, 200, 300, 300);     /* Draw some rectangles for a ball to roll 
   under. */
   color (2);
   rectfi (500, 500, 600, 600);
   color (3);
   rectfi (800, 800, 900, 900);
   rectfi (850, 500, 950, 600);
   rectfi (750, 400, 850, 500);
   rectfi (650, 300, 750, 400);
   drawmode (NORMALDRAW);
   mapcolor (0, 0, 0, 0);
   mapcolor (1, 255, 255, 0);
   mapcolor (10, 255, 255, 255);
   rampup(12, 82, 255, 255, 0, 255, 0, 0);
   setbell(1);
   while (!getbutton (MIDDLEMOUSE)) {
      for (i = 0; i < 1013 && !getbutton(MIDDLEMOUSE); i = i + 3)
      {
         color (BLUE);                /* Roll the ball up and 
                  to the right */
         clear ();
         if ((i==210) || (i==510) || (i==810))
            ringbell();                /* Ring the bell everytime */
         if ((i>250) && (i<550))      /* the ball gets to the    */
            color (2);                 /* next rectangle.         */
         else if ((i>550) && (i<850))
            color (5);                 /* change the ball's color */
         else if (i>850)
            color (10);
         else
            color (1);
         circfi (i, i, 10);
         swapbuffers ();
      }
      yspd = 0.0;
      for (heat=82, ypos=1013; ypos>=5 && !getbutton(MIDDLEMOUSE);
          ypos+=yspd)
      {
         color (BLUE);        /* drop the ball back to the bottom */
         clear ();
         color (heat--);      /* change the ball's color          */
         yspd += yacc;        /* as it falls.                     */
         circfi (1013, ypos, 10);
         swapbuffers ();
      }
      yspd = -60.0;
      for (xpos=1013, ypos=10; xpos>=-0&& !getbutton(MIDDLEMOUSE);
          xpos -= 5)
      {
         if (ypos <= 10)      /* roll the ball back to the beginning, */
            yspd *= yreflect;  /* and keep updating its'   */
         color (BLUE);        /* bounce-ability per frame */
         clear ();
         color (1);
         ypos += yspd;
         yspd += yaccel;
         circfi (xpos, ypos, 10);
         swapbuffers ();
      }
   }
   drawmode (OVERDRAW);     /* clean up the overlay bitplanes */
   color (0);
   clear();
   drawmode(NORMALDRAW);
   restore_cmap();          /* restore the color map */
   greset();
   gexit ();
}
/*
Make a color ramp.  Make an interpolated ramp from the 1st 
arguement's index to the second one. The 3rd, and 4th are red's 
low and hi indexes (5&6 green's, 7&8 are blue's). 
*/
#define round(n)     ((int) (n + 0.5))
rampup(first_lutv,last_lutv,minR,maxR,minG,maxG,minB,maxB)
unsigned short first_lutv, last_lutv, /* Start & end ramp values. */
minR, maxR,                           /* Low and high red, */
minG, maxG,                           /* green, */
minB, maxB;                           /* and blue values */
{
   unsigned short len_red, len_green,
      len_blue,                         /* Length of each color */
     i;                                /* Counter for number of steps */
   short red, gre, blu;                /* lut values */
   float rdx, gdx, bdx,                /* Sizes of rgb increments */
     r, g, b,                          /* A position on the ramp */
     steps;                            /* No. of steps along the ramp 
                                         at which intensity assignments
                                         will be made */
   /* Determine length of ramp*/
   steps = (float) (last_lutv-first_lutv + 1);
   len_red   = (maxR - minR);          /* determine length of red */
   len_green = (maxG - minG);          /* determine length of green */
   len_blue  = (maxB - minB);          /* determine length of blue */
   rdx = (float) len_red   / steps;    /* compute step   */
   gdx = (float) len_green / steps;    /* sizes of r, g, */
   bdx = (float) len_blue  / steps;    /* and b values   */
   r = minR;                           /* Assign starting */
   g = minG;                           /* indexes for each */
   b = minB;                           /* color value      */
   for (i = first_lutv; i <= last_lutv; i++) {
      red = (short) round(r);           /* Round off the */
      gre = (short) round(g);           /* given r, g,   */
      blu = (short) round(b);           /* and b values  */
      mapcolor(i, red, gre, blu);       /* assign next color 
               map index */
      r += rdx;                         /* Increment the */
      g += gdx;                         /* color indexes */
      b += bdx;
   }
}
#define MAXCOLI 255
static short CarrayR[MAXCOLI+1], CarrayG[MAXCOLI+1], CarrayB[MAXCOLI+1];
unsigned short index;
get_cmap() {
   short rcomp, gcomp, bcomp;
   for (index=0; index<=MAXCOLI; index++) {
      getmcolor(index,&rcomp, &gcomp, &bcomp);
      CarrayR[index] = rcomp;
      CarrayG[index] = gcomp;
      CarrayB[index] = bcomp;
   }
}
restore_cmap() {
   for (index=0; index<=MAXCOLI; index++)
      mapcolor(index,CarrayR[index], 
          CarrayG[index], CarrayB[index]);
}

Related Information

The gconfig subroutine, getmcolor subroutine, overlay subroutine.


[ Previous | Next | Contents | Glossary | Home | Search ]