home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / Trench-c / original trench.c next >
Encoding:
C/C++ Source or Header  |  1994-12-04  |  4.0 KB  |  114 lines  |  [TEXT/MACA]

  1. /* Written 12:00 pm  Jul 16, 1985 by sdh@joevax.UUCP in uiucdcs:net.sources.mac */
  2. /* ---------- "source to trench simulator" ---------- */
  3. /*This is the source to the trench simulator. It was written in MegaMax C*/
  4. /* Note the tabs should be set to 4 spaces. */
  5.  
  6. /* Steve Hawley */
  7. /* {joevax,mouton,alice}!sdh */
  8.  
  9. /* Program to simulate the trench from Star Wars */
  10. /* Written 7/12/85 by Steve Hawley in MegaMax C  */
  11.  
  12. #include <qd.h>            /*QuickDraw header file */
  13. #include <qdvars.h>    /*Quickdraw variables */
  14. #include <event.h>  /*Event manager header */
  15. #include <win.h>        /*Window manager header */
  16.  
  17. windowrecord wrecord; /*Record for window */
  18. windowptr mywindow;        /*pointer to record */
  19. rect screenrect;            /*Rectangle representing window size*/
  20.  
  21. int wlines = 0;                /* phase of depth lines */
  22.  
  23. drawlines(offx, offy, start) /* draws lines to give illusion of depth */
  24.     int offx, offt, start;
  25. {
  26.     int x1 = -200, y1 = -100;
  27.     int z;
  28.  
  29. /* start is the phase (from 0 to 3), The lines are projected by */
  30. /* the formulae x' = x/z; y' = y/z. offx and offy are offsets for*/
  31. /* viewpoint changes */
  32.     
  33.     for (index = 50 - start; index > 0; index -= 4)
  34.     {
  35.         moveto( (x1 - offx)/z, (y1 - offy)/z);
  36.         lineto( (x1 - offx)/z, (-y1 - offy)/z);
  37.         moveto( (-x1 - offx)/z, (y1 - offy)/z);
  38.         lineto( (-x1 - offx)/z, (-y1 - offy)/z);
  39.     }
  40. }
  41.  
  42. setup(offx, offy) /* draws the frame of the trench */
  43. /* offx and offy again represent the viewpoint offsets, and it is */
  44. /* projected using the same formulae as before */
  45.     int offx, offy;
  46. {
  47.  
  48.     int x1 = -200, x2 = -100, y1 = -100;
  49.     
  50.     
  51.     
  52.     moveto(x1 - offx,y1 -offy);
  53.     lineto((x1 - offx)/50, (y1-offy)/50);
  54.     lineto((x1 - offx)/50, (-y1 - offy)/50);
  55.     lineto(x1 - offx, -y1 - offy);
  56.     moveto(x2 - offx, -y1 - offy);
  57.     lineto((x2 - offx)/50, (-y1 - offy)/50);
  58.     moveto(-x1 - offx,y1 -offy);
  59.     lineto((-x1 - offx)/50, (y1-offy)/50);
  60.     lineto((-x1 - offx)/50, (-y1 - offy)/50);
  61.     lineto(-x1 - offx, -y1 - offy);
  62.     moveto(-x2 - offx, -y1 - offy);
  63.     lineto((-x2 - offx)/50, (-y1 - offy)/50);
  64. }
  65.  
  66. main()
  67. {
  68. /* the objects are animated by using exclusive-or drawing. this way, the */
  69. /* same routine to draw can be used to erase, and the new image can be */
  70. /* drawn before the old image is erased to help eliminate flicker. Thus*/
  71. /* the the program needs two copies of the offset parameters, one for the*/
  72. /* new and one for the old. */
  73.     int offxo=0,offxn=0,offyo=0,offyn=0;
  74.     point mouse;
  75.     
  76.     initgraf(&theport);    /* Set up quickdraw */
  77.     flushevents(everyevent, 0); /*kill previous events*/
  78.     initwindows();        /*initialize window manager*/
  79.     setrect(&screenrect, 4, 40, 508, 338); /* Set screen size*/
  80.     mywindow = newwindow(&wrecord, &screenrect, "Trench",1,0,(long)-1,(long)0);
  81. /*Set window parameters */
  82.     setport(mywindow); /*make the window the current grafport */
  83.     showwindow(mywindow); /*display window */
  84.     setorigin(-252, -149);/* set the origin so the center of the screen in (0,0)*/
  85.     penmode(patxor); /* set exclusive-or drawing*/
  86.     setup(offxn,offyn); /*draw initial setup*/
  87.     drawlines(offxn, offyn, wlines); /*draw initiail depth lines */
  88.     while(!button()) /*repeat until button is down*/
  89.     {
  90.         offxo = offxn;   /* Put new offsets in old variables */
  91.         offyo = offyn;
  92.         getmouse(&mouse); /*get the mouses coordinates */
  93.         if ( mouse.vh[1]/2 < offxn)  /* if the horizontal has changed */
  94.             offxn =  mouse.vh[1];             /* store it in the new offset */
  95.         else if ( mouse.vh[1]/2 > offxn)
  96.             offxn = mouse.vh[1]/2;
  97.         if (mouse.vh[0]/2 < offyn)   /* if the vertical has changed */
  98.             offyn = mouse.vh[0]/2;         /* stroe it in the new offset */
  99.         else if (mouse.vh[0]/2 > offyn)
  100.             offyn = mouse.vh[0]/2;
  101.         if ( (offxo != offxn) || (offyo != offyn)) /* if the old offset*/
  102.         {                                                                                /*differs from the new, update */
  103.             setup(offxn, offyn); /*draw new setup */
  104.             setup(offxo, offyo); /*erase old */
  105.         }
  106.         drawlines(offxo, offyo, wlines); /* erase the vertical lines */
  107.         wlines++; /* increment wlines */
  108.         if (wlines > 3) wlines = 0; /* reset wlines if too big */
  109.         drawlines(offxn, offyn, wlines); /* draw new set of lines */
  110.     }
  111. }
  112.     
  113. /* End of text from uiucdcs:net.sources.mac */
  114.