home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / EGA_VGA / XGADEMO.ZIP / XGADEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-27  |  5.2 KB  |  203 lines

  1. /*
  2.     XGAMODES package demo program
  3.     This program just demonstrates the use of the XGAMODES routines
  4.  
  5.     by Bert Tyler of Tyler Software
  6.     CIS ID:  73477,433
  7.     
  8. */
  9.  
  10. unsigned int intpixels[1024];    /* pixel arrays (can hold a scan line) */
  11. unsigned char pixels[1024];
  12.  
  13. unsigned char palette[512][3];    /* palette values */
  14.  
  15. int debug = 0;            /* for my own testing purposes */
  16.  
  17. main() {
  18. int capabilities, i, j;
  19.  
  20. /*  so, does this guy really have an XGA adapter? */
  21. if (! (capabilities = xga_detect())) {
  22.     puts("...but you have no XGA adapter\n");
  23.     exit(1);
  24.     }
  25.  
  26. /* make a fancy palette with 64 shades of red, green, blue, then grey */
  27. /* then double it up to make color-cycling easier */
  28. for (i = 0; i < 256; i++)
  29.     for (j = 0; j < 3; j++)
  30.         palette[i][j] = 0;
  31. for (i = 0; i < 64; i++) {
  32.     j = i << 2;
  33.     palette[i    ][0] = palette[i+64 ][1] = palette[i+128][2] = j; 
  34.     palette[i+192][0] = palette[i+192][1] = palette[i+192][2] = j; 
  35.     }
  36. for (i = 0; i < 256; i++)
  37.     for (j = 0; j < 3; j++)
  38.         palette[i+256][j] = palette[i][j];
  39.  
  40. /* loop until he chooses option 0 (or presses the ESC key) */
  41. puts("\n\n\n\n\n\n\n\n\n\n\n\n");
  42. for (;;) {
  43.     /* tell the user what his hardware looks like */
  44.     if ((capabilities & 8) == 0)
  45.         puts("\nYou have an XGA adapter with 512K of memory");
  46.     else
  47.         puts("\nYou have an XGA adapter with 1MB of memory");
  48.     if ((capabilities & 4) == 0)
  49.         puts("     It is attached to a low-rez monitor");
  50.     else
  51.         puts("     It is attached to a high-rez monitor");
  52.     if ((capabilities & 2) == 0)
  53.         puts("     The monitor is a monochrome monitor");
  54.     else
  55.         puts("     The monitor is a color monitor");
  56.     if ((capabilities & 16) == 0)
  57.         puts("     The XGA is currently also your VGA adapter");
  58.     else
  59.         puts("     The XGA is part of a dual-monitor setup");
  60.  
  61.     puts("\n\n What do you want to do?");
  62.     puts("   0 = end this demo");
  63.     puts("   1 = demo 132-column    text     mode");
  64.     puts("   2 = demo 1024x768x256  graphics mode");
  65.     puts("   3 = demo 1024x768x16   graphics mode");
  66.     puts("   4 = demo 640x480x256   graphics mode");
  67.     puts("   5 = demo 640x480x65536 graphics mode");
  68.  
  69.     i = getch();
  70.     j = i - '0';
  71.     switch(i) {
  72.         case '0':
  73.         case 27 :
  74.             exit(0);        /* end-of-program */
  75.             break;
  76.         case '1':
  77.             if ((capabilities & 16) == 0) {
  78.                 xga_mode(1);    /* 132-char text mode */
  79.                 puts("\n\nThis is the XGA's DOS-compatible 132-column text mode");
  80.                 puts("C programs can write to the screen with dull-normal 'printf()' and 'puts()' statements");
  81.                 }
  82.             else
  83.                 puts("That can only be done on a single-monitor setup\n");
  84.             puts("\n\n Press any key to continue...");
  85.             break;
  86.         case '2':
  87.         case '3':
  88.         case '4':
  89.         case '5':
  90.             if (!xga_mode(j)) {    /* graphics modes */
  91.                 puts("\n\n Your hardware can't run that mode");
  92.                 puts("\n\n Press any key to continue...");
  93.                 }
  94.             else    {
  95.                 if (j == 2) demoscreen(1024, 768, 256);
  96.                 if (j == 3) demoscreen(1024, 768, 16);
  97.                 if (j == 4) demoscreen(640,  480, 256);
  98.                 if (j == 5) demoscreen(640,  480, -1);
  99.                 }
  100.             break;
  101.         /* debugging code leftover from testing this stuff */
  102.         case 'd':
  103.         case 'D':
  104.             debug = 1 - debug;    /* flip-flop debug flag */
  105.             break;
  106.         default :
  107.             break;
  108.         }
  109. /*
  110.     puts("\007");
  111. */
  112.     getch();
  113.     xga_mode(0);    /* switch back to VGA-compatible text mode */
  114.  
  115.     /* debugging code leftover from testing this stuff */
  116.     if (debug && j >= 2 && j <= 5) {    /* prove we can read */
  117.         for (i = 0; i < 100; i+= 10)    /*    pixels, too */
  118.         printf(" %d %d %d %d %d %d %d %d %d %d\n",
  119.             intpixels[i+0],intpixels[i+1],intpixels[i+2],
  120.             intpixels[i+3],intpixels[i+4],intpixels[i+5],
  121.             intpixels[i+6],intpixels[i+7],intpixels[i+8],
  122.             intpixels[i+9]);
  123.         getch();
  124.         }
  125.  
  126.     }
  127.  
  128. }
  129.  
  130. demoscreen(int rows, int cols, int colors)
  131. {
  132. unsigned int i, j, k, l, r, g, b;
  133.  
  134. /* generate a simple image - then, for 16/256 color modes, color-cycle it */
  135. /* exit when a key is pressed */
  136.  
  137. if (colors > 0) {            /* 16 and 256 color modes */
  138.     xga_setpalette(&palette[16]);        /* set the palette */
  139.     k = 0;
  140.     for (j = 0; j < cols; j++) {    /* generate a test pattern */
  141.         if (++k >= colors) k = 0;
  142.         l = k;
  143.         for (i = 0; i < rows; i++) {
  144.             if (++l >= colors) l = 0;
  145.             pixels[i] = l;
  146. /*
  147.             xga_putpixel(i, j, l);
  148. */
  149.             }
  150.         xga_putline(j, 0, rows-1, pixels);
  151.         if (kbhit()) {        /* user bail-out */
  152.             break;
  153.             }
  154.         }
  155.  
  156. /*    testing - cycle the palette until a key is hit */
  157.     i = 16;
  158.     while (kbhit() == 0) {
  159.         if (++i >= 256) i = 0;
  160.         xga_setpalette(&palette[i]);
  161.         }
  162.     }
  163. else    {                /* 65536 "true color" mode */
  164.     g = 0;
  165.     for (j = 0; j < cols; j++) {    /* generate a test pattern */
  166.         if (++g >= 64) g = 0;
  167.         r = 0;
  168.         b = 0;
  169.         if (j >= 256) b = 16;
  170.         for (i = 0; i < rows; i++) {
  171.             if (++r >= 32) {
  172.                 r = 0;
  173.                 if (++b >= 32) b = 0;
  174.                 }
  175.             l = (r << 11) + (g << 5) + b;
  176.             intpixels[i] = l;
  177. /*
  178.             xga_putpixel(i,j,l);
  179. */
  180.             }
  181.         xga_putline(j, 0, rows-1, intpixels);
  182.         if (kbhit()) {        /* user bail-out */
  183.             break;
  184.             }
  185.         }
  186.  
  187.     }
  188.  
  189. /* read back a row, just for test purposes */
  190. /*
  191. for (i = 0; i < rows; i++)
  192.     intpixels[i] = xga_getpixel(i, 2);
  193. */
  194. if (colors > 0) {
  195.     xga_getline(2, 0, rows-1, pixels);
  196.     for (i = 0; i < rows; i++)
  197.         intpixels[i] = pixels[i];
  198.     }
  199. else
  200.     xga_getline(2, 0, rows-1, intpixels);
  201.  
  202. }
  203.