home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter45 / l45-3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-18  |  2.2 KB  |  72 lines

  1. /* Sample program to exercise VGA 640x400 16-color mode page flipping, by 
  2. drawing a horizontal line at the top of page 0 and another at bottom of page 1,
  3. then flipping between them once every 30 frames. 
  4. Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94.
  5. */
  6. #include <dos.h>
  7. #include <conio.h>
  8.  
  9. #define SCREEN_SEGMENT  0xA000
  10. #define SCREEN_HEIGHT   400
  11. #define SCREEN_WIDTH_IN_BYTES 80
  12. #define INPUT_STATUS_1  0x3DA /* color-mode address of Input Status 1
  13.                                  register */
  14. /* The page start addresses must be even multiples of 256, because page 
  15. flipping is performed by changing only the upper start address byte */
  16. #define PAGE_0_START 0
  17. #define PAGE_1_START (400*SCREEN_WIDTH_IN_BYTES)
  18.  
  19. void main(void);
  20. void Wait30Frames(void);
  21. extern void Set640x400(void);
  22.  
  23. void main()
  24. {
  25.    int i;
  26.    unsigned int far *ScreenPtr; 
  27.    union REGS regset;
  28.  
  29.    Set640x400();  /* set to 640x400 16-color mode */
  30.  
  31.    /* Point to first line of page 0 and draw a horizontal line across screen */
  32.    FP_SEG(ScreenPtr) = SCREEN_SEGMENT;
  33.    FP_OFF(ScreenPtr) = PAGE_0_START;
  34.    for (i=0; i<(SCREEN_WIDTH_IN_BYTES/2); i++) *ScreenPtr++ = 0xFFFF;
  35.  
  36.    /* Point to last line of page 1 and draw a horizontal line across screen */
  37.    FP_OFF(ScreenPtr) =
  38.          PAGE_1_START + ((SCREEN_HEIGHT-1)*SCREEN_WIDTH_IN_BYTES);
  39.    for (i=0; i<(SCREEN_WIDTH_IN_BYTES/2); i++) *ScreenPtr++ = 0xFFFF;
  40.  
  41.    /* Now flip pages once every 30 frames until a key is pressed */
  42.    do {
  43.       Wait30Frames();
  44.  
  45.       /* Flip to page 1 */
  46.       outpw(0x3D4, 0x0C | ((PAGE_1_START >> 8) << 8));
  47.  
  48.       Wait30Frames();
  49.  
  50.       /* Flip to page 0 */
  51.       outpw(0x3D4, 0x0C | ((PAGE_0_START >> 8) << 8));
  52.    } while (kbhit() == 0);
  53.  
  54.    getch(); /* clear the key press */
  55.  
  56.    /* Return to text mode and exit */
  57.    regset.x.ax = 0x0003;   /* AL = 3 selects 80x25 text mode */
  58.    int86(0x10, ®set, ®set);
  59. }
  60.  
  61. void Wait30Frames()
  62. {
  63.    int i;
  64.  
  65.    for (i=0; i<30; i++) {
  66.       /* Wait until we're not in vertical sync, so we can catch leading edge */
  67.       while ((inp(INPUT_STATUS_1) & 0x08) != 0) ;
  68.       /* Wait until we are in vertical sync */
  69.       while ((inp(INPUT_STATUS_1) & 0x08) == 0) ;
  70.    }
  71. }
  72.