home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_03 / colorrot.c next >
Encoding:
C/C++ Source or Header  |  1994-04-26  |  5.9 KB  |  259 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <io.h>
  5. #include <conio.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <bios.h>
  10. #include <fcntl.h>
  11. #include <memory.h>
  12. #include <math.h>
  13. #include <string.h>
  14.  
  15. // D E F I N E S  ////////////////////////////////////////////////////////////
  16.  
  17. #define VGA256            0x13
  18. #define TEXT_MODE         0x03
  19.  
  20. #define PALETTE_MASK        0x3c6
  21. #define PALETTE_REGISTER_RD 0x3c7
  22. #define PALETTE_REGISTER_WR 0x3c8
  23. #define PALETTE_DATA        0x3c9
  24.  
  25. #define SCREEN_WIDTH      (unsigned int)320
  26. #define SCREEN_HEIGHT     (unsigned int)200
  27.  
  28. // S T R U C T U R E S ///////////////////////////////////////////////////////
  29.  
  30. // this structure holds a RGB triple in three bytes
  31.  
  32. typedef struct RGB_color_typ
  33.         {
  34.  
  35.         unsigned char red;    // red   component of color 0-63
  36.         unsigned char green;  // green component of color 0-63
  37.         unsigned char blue;   // blue  component of color 0-63
  38.  
  39.         } RGB_color, *RGB_color_ptr;
  40.  
  41.  
  42. // P R O T O T Y P E S ///////////////////////////////////////////////////////
  43.  
  44. void Set_Palette_Register(int index, RGB_color_ptr color);
  45.  
  46. void Get_Palette_Register(int index, RGB_color_ptr color);
  47.  
  48. void Create_Cool_Palette(void);
  49.  
  50. void H_Line(int x1,int x2,int y,unsigned int color);
  51.  
  52. // G L O B A L S  ////////////////////////////////////////////////////////////
  53.  
  54. unsigned char far *video_buffer = (char far *)0xA0000000L; // vram byte ptr
  55.  
  56. // F U N C T I O N S /////////////////////////////////////////////////////////
  57.  
  58. void Set_Palette_Register(int index, RGB_color_ptr color)
  59. {
  60.  
  61. // this function sets a single color look up table value indexed by index
  62. // with the value in the color structure
  63.  
  64. // tell VGA card we are going to update a pallete register
  65.  
  66. _outp(PALETTE_MASK,0xff);
  67.  
  68. // tell vga card which register we will be updating
  69.  
  70. _outp(PALETTE_REGISTER_WR, index);
  71.  
  72. // now update the RGB triple, note the same port is used each time
  73.  
  74. _outp(PALETTE_DATA,color->red);
  75. _outp(PALETTE_DATA,color->green);
  76. _outp(PALETTE_DATA,color->blue);
  77.  
  78. } // end Set_Palette_Color
  79.  
  80. ///////////////////////////////////////////////////////////////////////////////
  81.  
  82. void Get_Palette_Register(int index, RGB_color_ptr color)
  83. {
  84.  
  85. // this function gets the data out of a color lookup regsiter and places it
  86. // into color
  87.  
  88. // set the palette mask register
  89.  
  90. _outp(PALETTE_MASK,0xff);
  91.  
  92. // tell vga card which register we will be reading
  93.  
  94. _outp(PALETTE_REGISTER_RD, index);
  95.  
  96. // now extract the data
  97.  
  98. color->red   = _inp(PALETTE_DATA);
  99. color->green = _inp(PALETTE_DATA);
  100. color->blue  = _inp(PALETTE_DATA);
  101.  
  102. } // end Get_Palette_Color
  103.  
  104. ///////////////////////////////////////////////////////////////////////////////
  105.  
  106. void Create_Cool_Palette(void)
  107. {
  108. // this function creates a cool palette. 64 shades of gray, 64 of red,
  109. // 64 of green and finally 64 of blue.
  110.  
  111. RGB_color color;
  112.  
  113. int index;
  114.  
  115. // swip thru the color registers and create 4 banks of 64 colors
  116.  
  117. for (index=0; index < 64; index++)
  118.     {
  119.  
  120.     // grays
  121.  
  122.     color.red   = index;
  123.     color.green = index;
  124.     color.blue  = index;
  125.  
  126.     Set_Palette_Register(index, (RGB_color_ptr)&color);
  127.  
  128.     // reds
  129.  
  130.     color.red   = index;
  131.     color.green = 0;
  132.     color.blue  = 0;
  133.  
  134.     Set_Palette_Register(index+64, (RGB_color_ptr)&color);
  135.  
  136.     // greens
  137.  
  138.     color.red   = 0;
  139.     color.green = index;
  140.     color.blue  = 0;
  141.  
  142.     Set_Palette_Register(index+128, (RGB_color_ptr)&color);
  143.  
  144.     // blues
  145.  
  146.     color.red   = 0;
  147.     color.green = 0;
  148.     color.blue  = index;
  149.  
  150.     Set_Palette_Register(index+192, (RGB_color_ptr)&color);
  151.  
  152.     } // end index
  153.  
  154. // make color 0 black
  155.  
  156. color.red   = 0;
  157. color.green = 0;
  158. color.blue  = 0;
  159.  
  160. Set_Palette_Register(0, (RGB_color_ptr)&color);
  161.  
  162. } // end Create_Cool_Palette
  163.  
  164. //////////////////////////////////////////////////////////////////////////////
  165.  
  166. void Set_Video_Mode(int mode)
  167. {
  168.  
  169. // use the video interrupt 10h to set the video mode to the sent value
  170.  
  171. union REGS inregs,outregs;
  172.  
  173. inregs.h.ah = 0;                    // set video mode sub-function
  174. inregs.h.al = (unsigned char)mode;  // video mode to change to
  175.  
  176. _int86(0x10, &inregs, &outregs);
  177.  
  178. } // end Set_Video_Mode
  179.  
  180. //////////////////////////////////////////////////////////////////////////////
  181.  
  182. void H_Line(int x1,int x2,int y,unsigned int color)
  183. {
  184. // draw a horizontal line useing the memset function
  185. // note x2 > x1
  186.  
  187. _fmemset((char far *)(video_buffer + ((y<<8) + (y<<6)) + x1),color,x2-x1+1);
  188.  
  189. } // end H_Line
  190.  
  191. //M A I N /////////////////////////////////////////////////////////////////////
  192.  
  193. void main(void)
  194. {
  195.  
  196. int index,           // loop var
  197.     x1=150,          // x1 & x2 are the edges of the current piece of the road
  198.     x2=170,
  199.     y=0,             // y is the current y position of the piece of road
  200.     curr_color=1;    // the current color being drawn
  201.  
  202. RGB_color color,color_1;
  203.  
  204. // set video mode to 320x200 256 color mode
  205.  
  206. Set_Video_Mode(VGA256);
  207.  
  208. // create the color palette
  209.  
  210. Create_Cool_Palette();
  211.  
  212. printf("Press any key to exit.");
  213.  
  214. // draw a road to nowhere
  215.  
  216. for (y=80; y<200; y++)
  217.     {
  218.     // draw next horizontal piece of road
  219.  
  220.     H_Line(x1,x2,y,curr_color);
  221.  
  222.     // make the road wider
  223.  
  224.     if (--x1 < 0)
  225.        x1=0;
  226.  
  227.     if (++x2 > 319)
  228.        x2=319;
  229.  
  230.     // next color please
  231.  
  232.     if (++curr_color>255)
  233.        curr_color=1;
  234.  
  235.     } // end for
  236.  
  237. // wait for user to hit a key
  238.  
  239. while(!kbhit())
  240.      {
  241.      Get_Palette_Register(1,(RGB_color_ptr)&color_1);
  242.  
  243.      for (index=1; index<=254; index++)
  244.          {
  245.          Get_Palette_Register(index+1,(RGB_color_ptr)&color);
  246.          Set_Palette_Register(index,(RGB_color_ptr)&color);
  247.  
  248.          } // end for
  249.  
  250.          Set_Palette_Register(255,(RGB_color_ptr)&color_1);
  251.  
  252.      } // end while
  253.  
  254. // go back to text mode
  255.  
  256. Set_Video_Mode(TEXT_MODE);
  257.  
  258. } // end main
  259.