home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap15 / vgamap.c < prev   
Encoding:
C/C++ Source or Header  |  1988-04-07  |  2.7 KB  |  95 lines

  1. /*  vgamap.c  -- remaps the vga mode 19 palette        */
  2. /* Program list: vgamap.c (for rand())                 */
  3. #include <stdio.h>
  4. #include <graph.h>
  5. #include <conio.h>
  6. #define ESC '\033'
  7. #define PALSIZE 256
  8. #define ROWS 16
  9. #define COLS 16
  10. #define MIDBLUE 0x190000L
  11. long newpal[PALSIZE]; /* array of color values */
  12.  
  13. main()
  14. {
  15.     struct videoconfig vc;
  16.     int mode = _MRES256COLOR;
  17.     short xmax, ymax;
  18.     short xcs[ROWS][COLS];
  19.     short ycs[ROWS][COLS];
  20.     short row, col;
  21.     long colorval;       /* VGA color value */
  22.     long index;          /* looping index   */
  23.     short palval;        /* palette value */
  24.     int c_base;  /* color base -- blue, green, or red */
  25.     int ch;
  26.  
  27.     if (_setvideomode(mode) == 0)
  28.         {
  29.         fprintf(stderr, "%d mode not supported\n", mode);
  30.         exit(1);
  31.         }
  32.     _getvideoconfig(&vc);
  33.     xmax = vc.numxpixels - 1;
  34.     ymax = vc.numypixels - 1;
  35.     for (col = 0; col < COLS; col++)
  36.         for (row = 0; row < ROWS; row++)
  37.             {
  38.             xcs[row][col] =  col * xmax / COLS + 5;
  39.             ycs[row][col] =  row * ymax / ROWS + 5;
  40.             }
  41.     _setcolor(1);
  42.     _rectangle(_GBORDER, 0, 0, xmax, ymax);
  43.     for (col = 1; col < COLS ; col++)
  44.         {
  45.         _moveto(col * (xmax + 1) / COLS, 0);
  46.         _lineto(col * (xmax + 1) / COLS, ymax);
  47.         }
  48.     for (row = 1; row < ROWS;  row++)
  49.         {
  50.         _moveto(0, row * (ymax + 1) / ROWS);
  51.         _lineto(xmax, row * (ymax + 1) / ROWS);
  52.         }
  53.  
  54.     for (col = 0; col < COLS; col++)
  55.         for (row = 0; row < ROWS; row++)
  56.             {
  57.             _setcolor(row * ROWS + col);
  58.             _floodfill(xcs[row][col], ycs[row][col],1);
  59.              }
  60.     getch();
  61.  
  62.     /*  initialize newpal[] to 64 shades of blue, 64
  63.         shades of green, 64 shades of red, and 64 shades
  64.         of magenta */
  65.     for (index = 0; index < 64; index++)
  66.         {
  67.         newpal[index] = index << 16;
  68.         newpal[index + 64] = index << 8;
  69.         newpal[index + 128] = index;
  70.         newpal[index + 192] = index | MIDBLUE;
  71.         }
  72.     _remapallpalette(newpal);
  73.     getch();
  74.  
  75.     /* set squares and colors randomly -- ESC
  76.        terminates loop, and other keystrokes toggle
  77.        it on and off */
  78.     do
  79.         {
  80.         while (!kbhit())
  81.             {
  82.             palval = rand() % PALSIZE;
  83.             colorval = 0L;
  84.             for (c_base = 0; c_base < 3; c_base++)
  85.                 colorval += ((long) rand() % 64) <<
  86.                              (c_base * 8);
  87.             _remappalette (palval, colorval);
  88.             }
  89.         ch = getch();
  90.         if (ch != ESC)
  91.             ch = getch();
  92.         } while (ch != ESC);
  93.     _setvideomode(_DEFAULTMODE);
  94. }
  95.