home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_06 / vertical.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-23  |  2.3 KB  |  97 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.  
  20. // D E F I N E S /////////////////////////////////////////////////////////////
  21.  
  22. #define VGA_INPUT_STATUS_1   0x3DA // vga status reg 1, bit 3 is the vsync
  23.                                    // when 1 - retrace in progress
  24.                                    // when 0 - no retrace
  25.  
  26. #define VGA_VSYNC_MASK 0x08        // masks off unwanted bit of status reg
  27.  
  28.  
  29.  
  30. // F U N C T I O N S ////////////////////////////////////////////////////////
  31.  
  32. void Wait_For_Vsync(void)
  33. {
  34. // this function waits for the start of a vertical retrace, if a vertical
  35. // retrace is in progress then it waits until the next one
  36.  
  37. while(_inp(VGA_INPUT_STATUS_1) & VGA_VSYNC_MASK)
  38.      {
  39.      // do nothing, vga is in retrace
  40.      } // end while
  41.  
  42. // now wait for vysnc and exit
  43.  
  44. while(!(_inp(VGA_INPUT_STATUS_1) & VGA_VSYNC_MASK))
  45.      {
  46.      // do nothing, wait for start of retrace
  47.      } // end while
  48.  
  49. // at this point a vertical retrace is occuring, so return back to caller
  50.  
  51. } // end Wait_For_Vsync
  52.  
  53. // M A I N //////////////////////////////////////////////////////////////////
  54.  
  55. void main(void)
  56. {
  57. char buffer[128];      // used as temporary string buffer
  58. long number_vsyncs=0;  // tracks number of retrace cycles
  59.  
  60. // set video mode to 320x200 256 color mode
  61.  
  62. Set_Video_Mode(VGA256);
  63.  
  64. // wait till user hits a key
  65.  
  66. while(!kbhit())
  67.      {
  68.  
  69.      // wait for a vsync
  70.  
  71.      Wait_For_Vsync();
  72.  
  73.      // do graphics or whatever now that we know electron gun is retracing
  74.      // we only have 1/70 of a second though! Usually, we would copy the
  75.      // double buffer to the video ram
  76.  
  77.      Plot_Pixel_Fast(rand()%320, rand()%200,rand()%256);
  78.  
  79.      // tally vsyncs
  80.  
  81.      number_vsyncs++;
  82.  
  83.      // print to screen
  84.  
  85.      sprintf(buffer,"Number of Vsync's = %ld   ",number_vsyncs);
  86.  
  87.      Blit_String(8,8,9,buffer,0);
  88.  
  89.      } // end while
  90.  
  91. // reset the video mode back to text
  92.  
  93. Set_Video_Mode(TEXT_MODE);
  94.  
  95. } // end main
  96.  
  97.