home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter53 / xsharp15.exe / XSHARP.C < prev    next >
C/C++ Source or Header  |  1994-12-16  |  4KB  |  110 lines

  1. /* 3D animation program to rotate 12 cubes. Uses fixed point.
  2.    All C code tested with Borland C++ 4.02 in C compilation mode
  3.    and the small model.
  4.    Checked by Jim Mischel 12/16/94. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <conio.h>
  8. #include <dos.h>
  9. #include "polygon.h"
  10.  
  11. #define FIXED_REPS   0     /* set to # of reps to stop after, or
  12.                               to 0 for free-running */
  13.  
  14. /* Base offset of page to which to draw */
  15. unsigned int CurrentPageBase = 0;
  16.  
  17. /* Clip rectangle; clips to the screen */
  18. int ClipMinX = 0, ClipMinY = 0;
  19. int ClipMaxX = SCREEN_WIDTH, ClipMaxY = SCREEN_HEIGHT;
  20.  
  21. static unsigned int PageStartOffsets[2] =
  22.    {PAGE0_START_OFFSET,PAGE1_START_OFFSET};
  23. int DisplayedPage, NonDisplayedPage;
  24. int RecalcAllXforms = 1, NumObjects;
  25.  
  26. Xform WorldViewXform;   /* initialized from floats */
  27.  
  28. /* Object list start and end */
  29. Object ObjectListStart;
  30. Object ObjectListEnd;
  31.  
  32. void main()
  33. {
  34.    int Done = 0, i;
  35.    Object *ObjectPtr;
  36.    union REGS regset;
  37. #if FIXED_REPS
  38.    int Reps = FIXED_REPS;
  39. #endif
  40.  
  41.    InitializeObjectList();
  42.    InitializeFixedPoint(); /* set up fixed-point data */
  43.    InitializeCubes();      /* set up the cubes and add them to the
  44.                               object list */
  45.    InitializeBalls();      /* ditto for the balls */
  46.    Set320x240Mode(); /* set the screen to mode X */
  47.    ShowPage(PageStartOffsets[DisplayedPage = 0]);
  48.  
  49.    /* Keep transforming the cube, drawing it to the undisplayed page,
  50.       and flipping the page to show it */
  51.    do {
  52.  
  53. #if FIXED_REPS
  54.          if ((--Reps) == 0) Done = 1;
  55. #endif
  56.  
  57.       /* For each object, regenerate viewing info, if necessary */
  58.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  59.             i++, ObjectPtr = ObjectPtr->NextObject) {
  60.          if (ObjectPtr->RecalcXform || RecalcAllXforms) {
  61.             ObjectPtr->RecalcFunc(ObjectPtr);
  62.             ObjectPtr->RecalcXform = 0;
  63.          }
  64.       }
  65.       RecalcAllXforms = 0;
  66.  
  67.       CurrentPageBase =    /* select other page for drawing to */
  68.             PageStartOffsets[NonDisplayedPage = DisplayedPage ^ 1];
  69.  
  70.       /* For each object, clear the portion of the non-displayed page
  71.          that was drawn to last time, then reset the erase extent */
  72.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  73.             i++, ObjectPtr = ObjectPtr->NextObject) {
  74.          FillRectangleX(ObjectPtr->EraseRect[NonDisplayedPage].Left,
  75.             ObjectPtr->EraseRect[NonDisplayedPage].Top,
  76.             ObjectPtr->EraseRect[NonDisplayedPage].Right,
  77.             ObjectPtr->EraseRect[NonDisplayedPage].Bottom,
  78.             CurrentPageBase, 0);
  79.          ObjectPtr->EraseRect[NonDisplayedPage].Left =
  80.               ObjectPtr->EraseRect[NonDisplayedPage].Top = 0x7FFF;
  81.          ObjectPtr->EraseRect[NonDisplayedPage].Right =
  82.                ObjectPtr->EraseRect[NonDisplayedPage].Bottom = 0;
  83.       }
  84.  
  85.       /* Sort the objects so we can draw them back to front */
  86.       SortObjects();
  87.  
  88.       /* Draw all objects */
  89.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  90.             i++, ObjectPtr = ObjectPtr->NextObject)
  91.          ObjectPtr->DrawFunc(ObjectPtr);
  92.  
  93.       /* Flip to display the page into which we just drew */
  94.       ShowPage(PageStartOffsets[DisplayedPage = NonDisplayedPage]);
  95.  
  96.       /* Move and reorient each object */
  97.       for (i=0, ObjectPtr = ObjectListStart.NextObject; i<NumObjects;
  98.             i++, ObjectPtr = ObjectPtr->NextObject)
  99.          ObjectPtr->MoveFunc(ObjectPtr);
  100.  
  101.       /* Check the keyboard */
  102.       if (kbhit())
  103.          if (getch() == 0x1B) Done = 1;   /* Esc to exit */
  104.    } while (!Done);
  105.    /* Return to text mode and exit */
  106.    regset.x.ax = 0x0003;   /* AL = 3 selects 80x25 text mode */
  107.    int86(0x10, ®set, ®set);
  108.    exit(1);
  109. }
  110.