home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / PolyMazesDemo-c / maze.c original < prev    next >
Encoding:
Text File  |  1994-12-04  |  4.7 KB  |  173 lines  |  [TEXT/EDIT]

  1. /* Written 12:00 pm  Jul 16, 1985 by sdh@joevax.UUCP in uiucdcs:net.sources.mac */
  2. /* ---------- "Source to 3D maze display" ---------- */
  3.  
  4. This is the source code for the maze display program. It was done in
  5. Aztec C version 1.06D. Note: the tabs should be set to 4 spaces.
  6.  
  7. Steve Hawley
  8. {joevax,mouton,alice}!sdh
  9. Date: 16 July 1985 1131-EDT (Tuesday)
  10.  
  11.  
  12. #include <quickdraw.h>
  13. #include <window.h>
  14. #include <inits.h>
  15. #include <event.h>
  16. #include <menu.h>
  17. #include <osutil.h>
  18.  
  19. /* Program to display a 2D maze in 3D with hidden lines and limited shading */
  20. /* Steve Hawley 7/15/85. Written in Aztec C, version 1.06D */
  21.  
  22.  
  23.  
  24. WindowRecord    wRecord;
  25. WindowPtr    myWindow;
  26. EventRecord myEvent;
  27.  
  28. static char maze[22][17] ={ {1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1},
  29.                             {1,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,1},
  30.                             {1,0,1,1,1,0,1,1,0,1,0,1,1,0,1,1,1},
  31.                             {1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1},
  32.                             {1,1,0,1,0,1,1,0,1,1,0,1,1,1,1,0,1},
  33.                             {1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
  34.                             {1,0,1,1,0,1,0,1,1,1,1,1,0,1,1,1,1},
  35.                             {1,0,0,1,0,1,0,1,0,0,0,1,0,0,0,0,1},
  36.                             {1,1,0,0,0,1,1,1,0,1,0,1,1,1,0,1,1},
  37.                             {1,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1},
  38.                             {1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1},
  39.                             {1,0,0,0,0,0,0,0,0,1,0,1,0,1,0,0,1},
  40.                             {1,0,1,1,1,1,1,1,0,1,0,1,0,0,0,1,1},
  41.                             {1,0,0,0,0,0,0,1,0,1,0,1,0,1,0,0,1},
  42.                             {1,0,1,1,0,1,1,1,0,1,0,1,1,1,1,0,1},
  43.                             {1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0,1},
  44.                             {1,0,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
  45.                             {1,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,1},
  46.                             {1,0,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1},
  47.                             {1,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,1},
  48.                             {1,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1},
  49.                             {1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1}};
  50.                             /* the maze. 1's = blocks, 0's = space */
  51.  
  52. sqre3d (col, row)
  53.     int col, row;
  54. /* Displays a given block from the maze. Decides on which side of center the block is found, and */
  55. /* draws the visible faces, which are defined as polygons. The faces are projected using the     */
  56. /* formulae: x' = x/z; y' = y/z; to achieve one point perspective. */
  57. {
  58.     int x1,x2,y1,y2,z1,z2;
  59.     PolyHandle sideleft, siderite, top, front;
  60.     x1 = (col - 8) * 100;
  61.     x2 = x1 + 100;
  62.     z1 = (23 - row);
  63.     z2 = z1 -1;
  64.     y1 = 150;
  65.     y2 = 250;
  66.     
  67.     sideleft = OpenPoly();
  68.     MoveTo( (x1/z1) + 200, (y1/z1) + 60);
  69.     LineTo( (x1/z1) + 200, (y2/z1) + 60);
  70.     LineTo( (x1/z2) + 200, (y2/z2) + 60);
  71.     LineTo( (x1/z2) + 200, (y1/z2) + 60);
  72.     LineTo( (x1/z1) + 200, (y1/z1) + 60);
  73.     ClosePoly();
  74.     
  75.     siderite = OpenPoly();
  76.     MoveTo( (x2/z1) + 200, (y1/z1) + 60);
  77.     LineTo( (x2/z1) + 200, (y2/z1) + 60);
  78.     LineTo( (x2/z2) + 200, (y2/z2) + 60);
  79.     LineTo( (x2/z2) + 200, (y1/z2) + 60);
  80.     LineTo( (x2/z1) + 200, (y1/z1) + 60);
  81.     ClosePoly();
  82.     
  83.     top = OpenPoly();
  84.     MoveTo( (x1/z1) + 200, (y1/z1) + 60);
  85.     LineTo( (x2/z1) + 200, (y1/z1) + 60);
  86.     LineTo( (x2/z2) + 200, (y1/z2) + 60);
  87.     LineTo( (x1/z2) + 200, (y1/z2) + 60);
  88.     LineTo( (x1/z1) + 200, (y1/z1) + 60);
  89.     ClosePoly();
  90.     
  91.     front = OpenPoly();
  92.     MoveTo( (x1/z2) + 200, (y1/z2) + 60);
  93.     LineTo( (x2/z2) + 200, (y1/z2) + 60);
  94.     LineTo( (x2/z2) + 200, (y2/z2) + 60);
  95.     LineTo( (x1/z2) + 200, (y2/z2) + 60);
  96.     LineTo( (x1/z2) + 200, (y1/z2) + 60);
  97.     ClosePoly();
  98.     
  99.     if (x2 < 0 && maze[row][col +1] != 1)/*decide to draw right side (dark gray)*/
  100.     {
  101.         PenPat(dkGray);
  102.         ErasePoly(siderite);
  103.         PaintPoly(siderite);
  104.         PenPat(black);
  105.         FramePoly(siderite);
  106.     }
  107.     
  108.     if (x1 > 0 && maze[row][col-1] != 1) /*decide to draw left face (light gray)*/
  109.     {
  110.         PenPat(ltGray);
  111.         ErasePoly(sideleft);
  112.         PaintPoly(sideleft);
  113.         PenPat(black);
  114.         FramePoly(sideleft);
  115.     }
  116.  
  117.     PenPat(white);    /* Draw the top (white) */
  118.     ErasePoly(top);
  119.     PaintPoly(top);
  120.     PenPat(black);
  121.     FramePoly(top);
  122.     
  123.     PenPat(gray);    /*draw the front (gray) */
  124.     ErasePoly(front);
  125.     PaintPoly(front);
  126.     PenPat(black);
  127.     FramePoly(front);
  128.     
  129.     PenNormal(); /*restore pen characteristics */
  130.     
  131.     KillPoly(top); /* dispose of all the polygons to free up memory*/
  132.     KillPoly(front);
  133.     KillPoly(sideleft);
  134.     KillPoly(siderite);
  135. }
  136.  
  137. display ()
  138. /* Draws out all the blocks, starting from the back, moving left to center, then right to center */
  139. {
  140.     Rect trect;
  141.     int row, col;
  142.     
  143.     for (row = 0; row < 22; row++)
  144.     {
  145.         for (col = 0; col < 8; col++)
  146.             if (maze[row][col] == 1) sqre3d(col,row);
  147.         for (col = 16; col > 7; col--)
  148.             if (maze[row][col] == 1) sqre3d(col, row);
  149.     }
  150. }
  151.  
  152. main ()
  153. {
  154.     Rect screen;
  155.     
  156.     InitGraf(&thePort);    /* do inits */
  157.     InitFonts();
  158.     FlushEvents(everyEvent,0);
  159.     InitWindows();
  160.     SetCursor(&arrow);
  161.     ShowCursor();
  162.     
  163.     SetRect(&screen, 4, 40, 508, 338); /*set up window */
  164.     myWindow = NewWindow(&wRecord, &screen, ctop("Something Amazing"),1,0,(char*)-1,1,(long)0);
  165.     SetPort(myWindow);
  166.     ShowWindow(myWindow);
  167.     
  168.     display(); /*draw maze*/
  169.     while(!Button()) /*wait for button to be pressed and handle events like clover-shift-4 */
  170.         GetNextEvent(everyEvent, &myEvent);
  171. }
  172. /* End of text from uiucdcs:net.sources.mac */
  173.