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

  1. /* Sample program to exercise the polygon-filling routines.
  2.    Compile and link with Borland C++ 4.02 with:
  3.      bcc -ms l21-3.c l21-2.c l21-1.c
  4.    Checked by Jim Mischel 11/30/94.
  5. */
  6.  
  7. #include <conio.h>
  8. #include <dos.h>
  9. #include "polygon.h"
  10.  
  11. /* Draws the polygon described by the point list PointList in color
  12.    Color with all vertices offset by (X,Y) */
  13. #define DRAW_POLYGON(PointList,Color,X,Y)                   \
  14.    Polygon.Length = sizeof(PointList)/sizeof(struct Point); \
  15.    Polygon.PointPtr = PointList;                            \
  16.    FillConvexPolygon(&Polygon, Color, X, Y);
  17.    
  18. void main(void);
  19. extern int FillConvexPolygon(struct PointListHeader *, int, int, int);
  20.  
  21. void main() {
  22.    int i, j;
  23.    struct PointListHeader Polygon;
  24.    static struct Point ScreenRectangle[] =
  25.          {{0,0},{320,0},{320,200},{0,200}};
  26.    static struct Point ConvexShape[] =
  27.          {{0,0},{121,0},{320,0},{200,51},{301,51},{250,51},{319,143},
  28.          {320,200},{22,200},{0,200},{50,180},{20,160},{50,140},
  29.          {20,120},{50,100},{20,80},{50,60},{20,40},{50,20}};
  30.    static struct Point Hexagon[] =
  31.          {{90,-50},{0,-90},{-90,-50},{-90,50},{0,90},{90,50}};
  32.    static struct Point Triangle1[] = {{30,0},{15,20},{0,0}};
  33.    static struct Point Triangle2[] = {{30,20},{15,0},{0,20}};
  34.    static struct Point Triangle3[] = {{0,20},{20,10},{0,0}};
  35.    static struct Point Triangle4[] = {{20,20},{20,0},{0,10}};
  36.    union REGS regset;
  37.  
  38.    /* Set the display to VGA mode 13h, 320x200 256-color mode */
  39.    regset.x.ax = 0x0013;   /* AH = 0 selects mode set function,
  40.                               AL = 0x13 selects mode 0x13
  41.                               when set as parameters for INT 0x10 */
  42.    int86(0x10, ®set, ®set);
  43.  
  44.    /* Clear the screen to cyan */
  45.    DRAW_POLYGON(ScreenRectangle, 3, 0, 0);
  46.  
  47.    /* Draw an irregular shape that meets our definition of convex but
  48.       is not convex by any normal description */
  49.    DRAW_POLYGON(ConvexShape, 6, 0, 0);
  50.    getch();    /* wait for a keypress */
  51.  
  52.    /* Draw adjacent triangles across the top half of the screen */
  53.    for (j=0; j<=80; j+=20) {
  54.       for (i=0; i<290; i += 30) {
  55.          DRAW_POLYGON(Triangle1, 2, i, j);
  56.          DRAW_POLYGON(Triangle2, 4, i+15, j);
  57.       }
  58.    }
  59.  
  60.    /* Draw adjacent triangles across the bottom half of the screen */
  61.    for (j=100; j<=170; j+=20) {
  62.       /* Do a row of pointing-right triangles */
  63.       for (i=0; i<290; i += 20) {
  64.          DRAW_POLYGON(Triangle3, 40, i, j);
  65.       }
  66.       /* Do a row of pointing-left triangles halfway between one row
  67.          of pointing-right triangles and the next, to fit between */
  68.       for (i=0; i<290; i += 20) {
  69.          DRAW_POLYGON(Triangle4, 1, i, j+10);
  70.       }
  71.    }
  72.    getch();    /* wait for a keypress */
  73.  
  74.    /* Finally, draw a series of concentric hexagons of approximately
  75.       the same proportions in the center of the screen */
  76.    for (i=0; i<16; i++) {
  77.       DRAW_POLYGON(Hexagon, i, 160, 100);
  78.       for (j=0; j<sizeof(Hexagon)/sizeof(struct Point); j++) {
  79.          /* Advance each vertex toward the center */
  80.          if (Hexagon[j].X != 0) {
  81.             Hexagon[j].X -= Hexagon[j].X >= 0 ? 3 : -3;
  82.             Hexagon[j].Y -= Hexagon[j].Y >= 0 ? 2 : -2;
  83.          } else {
  84.             Hexagon[j].Y -= Hexagon[j].Y >= 0 ? 3 : -3;
  85.          }
  86.       }
  87.    }
  88.    getch();    /* wait for a keypress */
  89.  
  90.    /* Return to text mode and exit */
  91.    regset.x.ax = 0x0003;   /* AL = 3 selects 80x25 text mode */
  92.    int86(0x10, ®set, ®set);
  93. }
  94.