Listing. Making Raster Bars, raster.c

// gcc -Wall -O2 raster.c -lvgagl -lvga -lm -o raster
#include <vga.h>
#include <vgagl.h>
#include <math.h> /* sines! */
#include <stdlib.h> /* malloc */
#define GMODE G320x200x256
GraphicsContext *virtualscreen;
GraphicsContext *physicalscreen;
{
  char *redbox, *orangebox, *yellowbox;
  char *greenbox, *bluebox, *purplebox;
  char c, d; /* to keep track of color */
  short int x; /* a counter for our sine */
  vga_init(); /* start svgalib */
  gl_enableclipping(); /* just in case */
  vga_setmode(GMODE); /* set mode */
  gl_setcontextvga(GMODE);
  physicalscreen = gl_allocatecontext();
  gl_getcontext(physicalscreen);
  gl_setcontextvgavirtual(GMODE);
  virtualscreen = gl_allocatecontext();
  gl_getcontext(virtualscreen);
  /* Allocate the memory for our bars! */
  redbox = malloc(WIDTH*17*BYTESPERPIXEL);
  orangebox = malloc(WIDTH*17*BYTESPERPIXEL);
  yellowbox = malloc(WIDTH*17*BYTESPERPIXEL);
  greenbox = malloc(WIDTH*17*BYTESPERPIXEL);
  bluebox = malloc(WIDTH*17*BYTESPERPIXEL);
  purplebox = malloc(WIDTH*17*BYTESPERPIXEL);
  /* set up the palette! */
  c=0; /* red */
  for (d=1; d<10; d++)
    gl_setpalettecolor(191+c*9+d,
		       7*d, 0, 0);
  c=1; /* orange */
  for (d=1; d<10; d++)
    gl_setpalettecolor(191+c*9+d,
                       7*d, 3.5*d, 0);
  c=2; /* yellow */
  for (d=1; d<10; d++)
    gl_setpalettecolor(191+c*9+d,
                       7*d, 7*d, 0);
  c=3; /* green */
  for (d=1; d<10; d++)
    gl_setpalettecolor(191+c*9+d,
                       0, 7*d, 0);
  c=4; /* blue */
  for (d=1; d<10; d++)
    gl_setpalettecolor(191+c*9+d,
                       0, 0, 7*d);
  c=5; /* purple */
  for (d=1; d<10; d++)
    gl_setpalettecolor(191+c*9+d,
                       7*d, 0, 7*d);
  /* Now is when we draw the bars! */
  for (d=0; d<9; d++) {
    gl_hline(0,d,WIDTH-1,192+d);
    gl_hline(0,16-d,WIDTH-1,192+d);
    gl_hline(0,17+d,WIDTH-1,201+d);
    gl_hline(0,33-d,WIDTH-1,201+d);
    gl_hline(0,34+d,WIDTH-1,210+d);
    gl_hline(0,50-d,WIDTH-1,210+d);
    gl_hline(0,51+d,WIDTH-1,219+d);
    gl_hline(0,67-d,WIDTH-1,219+d);
    gl_hline(0,68+d,WIDTH-1,228+d);
    gl_hline(0,84-d,WIDTH-1,228+d);
    gl_hline(0,85+d,WIDTH-1,237+d);
    gl_hline(0,101-d,WIDTH-1,237+d);
  }
  /* copy the bars to our arrays */
  gl_getbox(0,0,WIDTH,17,redbox);
  gl_getbox(0,16,WIDTH,17,orangebox);
  gl_getbox(0,34,WIDTH,17,yellowbox);
  gl_getbox(0,51,WIDTH,17,greenbox);
  gl_getbox(0,68,WIDTH,17,bluebox);
  gl_getbox(0,85,WIDTH,17,purplebox);
  /* the main loop. This severely and
   * inefficiently burns CPU but we're
   * feeling sadistic on the chip today.
   * 12 raster bars is a lot, so comment
   * out a few if your computer grinds
   * to disastrous effect.
   */
  for (x=d=0; d==0; d=vga_getkey()) {
    x++;
    gl_putbox(0,40*sin((x+0)/24.0)+40,
	      WIDTH, 17, purplebox);
    gl_putbox(0,40*sin((x+10)/24.0)+40,
              WIDTH, 17, bluebox);
    gl_putbox(0,40*sin((x+20)/24.0)+40,
              WIDTH, 17, greenbox);
    gl_putbox(0,40*sin((x+30)/24.0)+40,
              WIDTH, 17, yellowbox);
    gl_putbox(0,40*sin((x+40)/24.0)+40,
              WIDTH, 17, orangebox);
    gl_putbox(0,40*sin((x+50)/24.0)+40,
              WIDTH, 17, redbox);
    gl_putbox(0,-40*sin((x+0)/24.0)+144,
              WIDTH, 17, purplebox);
    gl_putbox(0,-40*sin((x+10)/24.0)+144,
              WIDTH, 17, bluebox);
    gl_putbox(0,-40*sin((x+20)/24.0)+144,
              WIDTH, 17, greenbox);
    gl_putbox(0,-40*sin((x+30)/24.0)+144,
              WIDTH, 17, yellowbox);
    gl_putbox(0,-40*sin((x+40)/24.0)+144,
              WIDTH, 17, orangebox);
    gl_putbox(0,-40*sin((x+50)/24.0)+144,
              WIDTH, 17, redbox);
    gl_copyscreen(physicalscreen);
    gl_clearscreen(0);
    vga_waitretrace();
  }
  return 0; /* it's almost a tradition! */
}
