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

  1. /* 3D animation program to rotate 12 cubes. Uses fixed point.
  2. Tested with Borland C++ 4.02 in small model by Jim Mischel 12/16/94.
  3. */
  4.  
  5. #include <conio.h>
  6. #include <dos.h>
  7. #include "polygon.h"
  8.  
  9. /* Base offset of page to which to draw */
  10. unsigned int CurrentPageBase = 0;
  11. /* Clip rectangle; clips to the screen */
  12. int ClipMinX = 0, ClipMinY = 0;
  13. int ClipMaxX = SCREEN_WIDTH, ClipMaxY = SCREEN_HEIGHT;
  14. static unsigned int PageStartOffsets[2] =
  15.    {PAGE0_START_OFFSET,PAGE1_START_OFFSET};
  16. int DisplayedPage, NonDisplayedPage;
  17. int RecalcAllXforms = 1, NumObjects = 0;
  18. Xform WorldViewXform;   /* initialized from floats */
  19. /* Pointers to objects */
  20. Object *ObjectList[MAX_OBJECTS];
  21.  
  22. void main() {
  23.    int Done = 0, i;
  24.    Object *ObjectPtr;
  25.    union REGS regset;
  26.  
  27.    InitializeFixedPoint(); /* set up fixed-point data */
  28.    InitializeCubes();    /* set up cubes and add them to object list; other 
  29.                          objects would be initialized now, if there were any */
  30.    Set320x240Mode(); /* set the screen to mode X */
  31.    ShowPage(PageStartOffsets[DisplayedPage = 0]);
  32.    /* Keep transforming the cube, drawing it to the undisplayed page,
  33.       and flipping the page to show it */
  34.    do {
  35.       /* For each object, regenerate viewing info, if necessary */
  36.       for (i=0; i<NumObjects; i++) {
  37.          if ((ObjectPtr = ObjectList[i])->RecalcXform ||
  38.                RecalcAllXforms) {
  39.             ObjectPtr->RecalcFunc(ObjectPtr);
  40.             ObjectPtr->RecalcXform = 0;
  41.          }
  42.       }
  43.       RecalcAllXforms = 0;
  44.       CurrentPageBase =    /* select other page for drawing to */
  45.             PageStartOffsets[NonDisplayedPage = DisplayedPage ^ 1];
  46.       /* For each object, clear the portion of the non-displayed page
  47.          that was drawn to last time, then reset the erase extent */
  48.       for (i=0; i<NumObjects; i++) {
  49.          ObjectPtr = ObjectList[i];
  50.          FillRectangleX(ObjectPtr->EraseRect[NonDisplayedPage].Left,
  51.             ObjectPtr->EraseRect[NonDisplayedPage].Top,
  52.             ObjectPtr->EraseRect[NonDisplayedPage].Right,
  53.             ObjectPtr->EraseRect[NonDisplayedPage].Bottom,
  54.             CurrentPageBase, 0);
  55.          ObjectPtr->EraseRect[NonDisplayedPage].Left =
  56.               ObjectPtr->EraseRect[NonDisplayedPage].Top = 0x7FFF;
  57.          ObjectPtr->EraseRect[NonDisplayedPage].Right =
  58.                ObjectPtr->EraseRect[NonDisplayedPage].Bottom = 0;
  59.       }
  60.       /* Draw all objects */
  61.       for (i=0; i<NumObjects; i++)
  62.          ObjectList[i]->DrawFunc(ObjectList[i]);
  63.       /* Flip to display the page into which we just drew */
  64.       ShowPage(PageStartOffsets[DisplayedPage = NonDisplayedPage]);
  65.       /* Move and reorient each object */
  66.       for (i=0; i<NumObjects; i++)
  67.          ObjectList[i]->MoveFunc(ObjectList[i]);
  68.       if (kbhit())
  69.          if (getch() == 0x1B) Done = 1;   /* Esc to exit */
  70.    } while (!Done);
  71.    /* Return to text mode and exit */
  72.    regset.x.ax = 0x0003;   /* AL = 3 selects 80x25 text mode */
  73.    int86(0x10, ®set, ®set);
  74.    exit(1);
  75. }
  76.  
  77.