home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_06 / buffer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-20  |  4.1 KB  |  177 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 <malloc.h>
  13. #include <math.h>
  14. #include <string.h>
  15.  
  16. #include "graph3.h"
  17. #include "graph4.h"
  18.  
  19. // G L O B A L S  ////////////////////////////////////////////////////////////
  20.  
  21. unsigned char far *double_buffer = NULL;
  22.  
  23. unsigned int buffer_height        = SCREEN_HEIGHT;
  24.  
  25. unsigned int buffer_size = SCREEN_WIDTH*SCREEN_HEIGHT/2;
  26.  
  27. // F U N C T I O N S /////////////////////////////////////////////////////////
  28.  
  29. void Show_Double_Buffer(char far *buffer)
  30. {
  31. // this functions copies the doubele buffer into the video buffer
  32.  
  33. _asm
  34.    {
  35.    push ds               ; save DS on stack
  36.    mov cx,buffer_size    ; this is the size of buffer in WORDS
  37.    les di,video_buffer   ; es:di is destination of memory move
  38.    lds si,buffer         ; ds:si is source of memory move
  39.    cld                   ; make sure to move in the right direction
  40.    rep movsw             ; move all the words
  41.    pop ds                ; restore the data segment
  42.    } // end asm
  43.  
  44. } // end Show_Double_Buffer
  45.  
  46. //////////////////////////////////////////////////////////////////////////////
  47.  
  48. int Create_Double_Buffer(int num_lines)
  49. {
  50.  
  51. // allocate enough memory to hold the double buffer
  52.  
  53. if ((double_buffer = (unsigned char far *)_fmalloc(SCREEN_WIDTH * (num_lines + 1)))==NULL)
  54.    return(0);
  55.  
  56. // set the height of the buffer and compute it's size
  57.  
  58. buffer_height = num_lines;
  59.  
  60. buffer_size = SCREEN_WIDTH * num_lines/2;
  61.  
  62. // fill the buffer with black
  63.  
  64. _fmemset(double_buffer, 0, SCREEN_WIDTH * num_lines);
  65.  
  66. // everything was ok
  67.  
  68. return(1);
  69.  
  70. } // end Init_Double_Buffer
  71.  
  72. ///////////////////////////////////////////////////////////////////////////////
  73.  
  74. void Fill_Double_Buffer(int color)
  75. {
  76. // this function fills in the double buffer with the sent color
  77.  
  78. _fmemset(double_buffer, color, SCREEN_WIDTH * buffer_height);
  79.  
  80. } // end Fill_Double_Buffer
  81.  
  82. //////////////////////////////////////////////////////////////////////////////
  83.  
  84. void Delete_Double_Buffer(void)
  85. {
  86. // this function free's up the memory allocated by the double buffer
  87. // make sure to use FAR version
  88.  
  89. if (double_buffer)
  90.   _ffree(double_buffer);
  91.  
  92. } // end Delete_Double_Buffer
  93.  
  94. //////////////////////////////////////////////////////////////////////////////
  95.  
  96. void Plot_Pixel_Fast_DB(int x,int y,unsigned char color)
  97. {
  98.  
  99. // plots the pixel in the desired color a little quicker using binary shifting
  100. // to accomplish the multiplications
  101.  
  102. // use the fact that 320*y = 256*y + 64*y = y<<8 + y<<6
  103.  
  104. double_buffer[((y<<8) + (y<<6)) + x] = color;
  105.  
  106. } // end Plot_Pixel_Fast_DB
  107.  
  108. //////////////////////////////////////////////////////////////////////////////
  109.  
  110. void main(void)
  111. {
  112. // this program creates a kaleidoscope of colors
  113.  
  114. int x,y,fcolor=1,index;
  115.  
  116. // set video mode to 320x200 256 color mode
  117.  
  118. Set_Video_Mode(VGA256);
  119.  
  120. // create a double buffer
  121.  
  122. if (!Create_Double_Buffer(SCREEN_HEIGHT))
  123.    {
  124.    printf("\nNot enough memory to create double buffer.");
  125.  
  126.    } // end if
  127.  
  128. // main event loop
  129.  
  130. while(!kbhit())
  131.      {
  132.  
  133.      // clear out the double buffer with black
  134.  
  135.      Fill_Double_Buffer(0);
  136.  
  137.      // next color
  138.  
  139.      if (++fcolor>15) fcolor=1;
  140.  
  141.      // draw something in it
  142.  
  143.      for (index=0; index<200; index++)
  144.          {
  145.          // make a kaleidoscope of color
  146.  
  147.          x = rand()%(SCREEN_WIDTH/2);
  148.          y = rand()%(SCREEN_HEIGHT/2);
  149.  
  150.          Plot_Pixel_Fast_DB(x,y,fcolor);
  151.          Plot_Pixel_Fast_DB((SCREEN_WIDTH-1)-x,y,fcolor);
  152.          Plot_Pixel_Fast_DB(x,(SCREEN_HEIGHT-1)-y,fcolor);
  153.          Plot_Pixel_Fast_DB((SCREEN_WIDTH-1)-x,(SCREEN_HEIGHT-1)-y,fcolor);
  154.  
  155.          } // end for
  156.  
  157.      // copy double buffer to video buffer
  158.  
  159.      Show_Double_Buffer(double_buffer);
  160.  
  161.      // wait a bit so user can see it
  162.  
  163.      Delay(2);
  164.  
  165.      } // end while
  166.  
  167. // reset the video mode back to text
  168.  
  169. Set_Video_Mode(TEXT_MODE);
  170.  
  171. // free the double buffer
  172.  
  173. Delete_Double_Buffer();
  174.  
  175. } // end main
  176.  
  177.