home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / NewRae 1.0.2 / NewRae.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  7.7 KB  |  339 lines  |  [TEXT/CWIE]

  1. // NewRae
  2. // version 1.0.2
  3. // by Ken Long <kenlong@netcom.com>
  4. // updated for CW7 on 951201
  5.  
  6. //•-----------------------------------------------------------------•//
  7. //• Another old C source, brought back from the dead ---------------•//
  8. //• by Kenneth A. Long, at itty bitty bytes™ -----------------------•//
  9. //• Made to run on Think C™ on 7 April 1994 ------------------------•//
  10. //•-----------------------------------------------------------------•//
  11.  
  12. //o Written 11:22 am Aug 22, 1986 
  13. //o by sdh@joevax.UUCP 
  14. //o Posted to uiucdcsb:net.sources.mac.
  15. //o "source to rae demo"
  16.  
  17. //o rae.c
  18. //o This is the demo "rae" originally written for blit terminals
  19. //o converted to the Macintosh in Aztec C version 1.06H
  20. //o Steve Hawley 8/22/86
  21.  
  22. //o I can't vouch for the code for rae. It was copied directly.
  23. //o I only changed the system commands and initialization sequence.
  24.  
  25. #define kMinX    0
  26. #define kMinY    0
  27. #define kMaxX    qd.screenBits.bounds.right
  28. #define kMaxY    qd.screenBits.bounds.bottom
  29. #define kTop       kMinY
  30. #define kBottom    (kMaxY-1)
  31.  
  32. short   ground[128];
  33. short   X_pos;
  34.  
  35. GrafPort myPort;
  36.  
  37. //o This is the bits for the face that gets bounced around.
  38. //o the original rae used just circles.
  39.  
  40. static unsigned char facebits[32] = {
  41.     0x07, 0xe0, 0x18, 0x18, 0x20, 0x04, 0x42, 0x42, 0x42, 0x42,
  42.     0x82, 0x41, 0x80, 0x01, 0x80, 0x01, 0x84, 0x21, 0x88, 0x11,
  43.     0x94, 0x29, 0x43, 0xc2, 0x40, 0x02, 0x20, 0x04, 0x18, 0x18,
  44.     0x07, 0xe0
  45. };
  46.  
  47. WindowPtr    raeWindow;            //• Added by KAL.
  48. Rect        windowBounds;        //• Added by KAL.
  49.  
  50. struct BitMap face;
  51.  
  52. //• MBar show/hide stuff.
  53.  
  54. RgnHandle        mBarRgn, GrayRgn;
  55. short            *mBarHeightPtr;
  56. short            oldMBarHeight;
  57.  
  58.  
  59. //•-----------------------------------------------------------------•//
  60. //• Prototypes: ----------------------------------------------------•//
  61. //•-----------------------------------------------------------------•//
  62. void Hide_Menu_Bar (void);
  63. void Show_Menu_Bar (void);
  64. void Setup_Window (void);
  65. void Draw_It (short x, short y);
  66. short Friction_Decay (short v);
  67. void Frame_It (void);
  68. void main (void);
  69.  
  70. //•-----------------------------------------------------------------•//
  71. void Hide_Menu_Bar (void) 
  72. {
  73.     Rect    mBarRect;
  74.  
  75.     GrayRgn = GetGrayRgn ();
  76.     mBarHeightPtr = (short *) 0x0BAA;
  77.     oldMBarHeight = *mBarHeightPtr;
  78.     *mBarHeightPtr = 0;
  79.     mBarRect = qd.screenBits.bounds;
  80.     mBarRect.bottom = mBarRect.top + oldMBarHeight;
  81.     mBarRgn = NewRgn ();
  82.     RectRgn (mBarRgn, &mBarRect);
  83.     UnionRgn (GrayRgn, mBarRgn, GrayRgn);
  84.     PaintOne (0L, mBarRgn);
  85. }
  86.  
  87. //•-----------------------------------------------------------------•//
  88. void Show_Menu_Bar (void) 
  89. {
  90.     *mBarHeightPtr = oldMBarHeight;
  91.     DiffRgn (GrayRgn, mBarRgn, GrayRgn);
  92.     DisposeRgn (mBarRgn);
  93. }
  94.  
  95. //•-----------------------------------------------------------------•//
  96. void Setup_Window (void)
  97. {
  98.     SetRect (&windowBounds, 0, 0, kMaxX, kMaxY);
  99.                                 
  100.     raeWindow = NewWindow (0L, &windowBounds, "\p", true, noGrowDocProc, (WindowPtr) -1L, true, 0);
  101.     FillRect (&windowBounds, &qd.black);
  102.     SetPort (raeWindow);
  103. }
  104.  
  105. //•-----------------------------------------------------------------•//
  106. void Draw_It (short x, short y)
  107. {    
  108.     short i;
  109.     Rect trect, myRect;
  110.  
  111.     if (x < kMinX)
  112.         x = kMinX + 8;
  113.     
  114.     //• kTop is 0, so if vert. is less then it's 8 down.
  115.     if (y < kTop)
  116.         y = kTop + 8;
  117.     
  118.     //• kMaxX is screen.right, so if it's more then it's 8 left.
  119.     if (x > kMaxX)
  120.         x = kMaxX - 8;
  121.         
  122.     X_pos = x >> 3;
  123.     if (y > ground[X_pos])
  124.         y = ground[X_pos];
  125.         
  126.     SetRect (&trect, x - 12, y - 8, x + 4, y + 8);
  127.     CopyBits (&face, &myPort.portBits, &face.bounds, &trect, srcXor, 0L);
  128. }
  129.  
  130. //•-----------------------------------------------------------------•//
  131. short Friction_Decay (short v)
  132. {
  133.     if (v == 0)
  134.         return (v);
  135.     if (v > 0)
  136.         return (v - 1 - (v >> 3));
  137.         
  138.     return (v + 1 - (v >> 3));
  139. }
  140.  
  141. //•-----------------------------------------------------------------•//
  142. void Frame_It ()
  143. {
  144.     PenPat (&qd.white);
  145.     MoveTo (kMinX, kMinY); 
  146.     LineTo (kMinX, kMaxY -1);
  147.     MoveTo (kMinX, kMaxY -1); 
  148.     LineTo (kMaxX -1, kMaxY -1);
  149.     MoveTo (kMaxX -1, kMaxY -1);
  150.     LineTo (kMaxX -1, kMinY);
  151.     PenPat (&qd.black);
  152. }
  153.  
  154. //•-----------------------------------------------------------------•//
  155. void main ()
  156. {
  157.     Rect myRect, trect;
  158.  
  159.     register i;
  160.     register time;
  161.     short x, xx, y, yy, xvel, yvel, xacc, yacc, ypos, offset;
  162.     long waste;
  163.     WindowPtr aWindow;
  164.  
  165.     InitGraf (&qd.thePort);
  166.     InitWindows ();
  167.     
  168.     Hide_Menu_Bar ();
  169.     
  170.     Setup_Window ();
  171.     
  172.     OpenPort (&myPort); //o create my own port so I don't need.
  173.              //o windows and can blacken the whole screen.
  174.     
  175.     HideCursor ();
  176.     
  177.     face.baseAddr = (Ptr) &facebits[0]; //o set up bitmap parameters.
  178.     face.rowBytes = 2;
  179.     SetRect (&face.bounds, 0, 0, 16, 16);
  180.     
  181.     Frame_It ();
  182.     
  183.     for (i = 0; i < 128; i++) 
  184.     {
  185.         x = i << 3;
  186.         if (x <= kMinX + 8 || x >= kMaxX - 8)
  187.             ground[i] = 0;
  188.         else 
  189.         {
  190.             ground[i] = kBottom - 9;
  191.             if (i & 01)
  192.                 if (ground[i - 1] == 0)
  193.                     ground[i] = 0;
  194.                 else
  195.                     ground[i] -= 7;
  196.         }
  197.     }
  198.     //• Keep a tickin' while we ain't a clickin'!
  199.     while (! Button ())
  200.     {
  201.         X_pos = Random () & 0177;
  202.         if (ground[X_pos] <= kTop) 
  203.         {
  204.             Delay (1L, &waste); 
  205.             continue;
  206.         }
  207.         x = X_pos << 3;
  208.         xvel = 4 - ((Random () | 01) & 07);
  209.         yacc = 1;
  210.         yvel = 0;
  211.         y = kTop;
  212.         for (time = 0;; time++) 
  213.         {
  214.             if (Button ()) //o Was a read key command.
  215.                 return;
  216.                 
  217.             Draw_It (x, y);
  218.             xx = x; 
  219.             yy = y;     //o Save x and y.
  220.             yvel += yacc;
  221.             y += yvel;
  222.             x += xvel;
  223.             
  224.             //o Bounce?
  225.             if (y > ground[x >> 3]) 
  226.             {
  227.                 if (yvel > 5) 
  228.                 {
  229.                     Draw_It (xx, yy);
  230.                     Draw_It (x, y);
  231.                     Delay  (1L, &waste);
  232.                     Draw_It (x, y);
  233.                     Draw_It (xx, yy);
  234.                 }
  235.                 //o Side collision?
  236.                 if (y <= ground[xx >> 3]) 
  237.                 {
  238.                     x = xx;
  239.                     xvel = -xvel;
  240.                 } 
  241.                 else 
  242.                     //o Bottom?                
  243.                     if (yy <= ground[x >> 3]) 
  244.                     {
  245.                         y = yy;
  246.                         yvel = -yvel;
  247.                     }
  248.                     else 
  249.                         {    //o Corner.
  250.                             x = xx;
  251.                             y = yy;
  252.                             xvel = -xvel;
  253.                             yvel = -yvel;
  254.                     }
  255.                     if ((time & 017) == 0)
  256.                         xvel = Friction_Decay (xvel);
  257.                         
  258.                     if (xvel == 0) 
  259.                     {
  260.                         X_pos = x >> 3;
  261.                         if (ground[X_pos - 1]    <
  262.                             ground[X_pos]        &&
  263.                             ground[X_pos]         <
  264.                             ground[X_pos + 1])
  265.                             xvel = 1;
  266.                         //o Roll left.
  267.                         else 
  268.                             if (ground[X_pos - 1]    > 
  269.                                 ground[X_pos]        &&
  270.                                 ground[X_pos]         > 
  271.                                 ground[X_pos + 1])
  272.                                 xvel = -1;
  273.                             //o on hilltop.
  274.                             else 
  275.                                 if (ground[X_pos - 1]     > 
  276.                                     ground[X_pos]        &&
  277.                                     ground[X_pos]         < 
  278.                                     ground[X_pos + 1]) 
  279.                                 {
  280.                                     if (Random () & 01)
  281.                                         xvel = 1;
  282.                                     else
  283.                                         xvel = -1;
  284.                                 }
  285.                     }    //o if xvel equals zero.
  286.                     yvel = Friction_Decay (yvel);
  287.                 }
  288.             Delay (1L, &waste);
  289.             Draw_It (xx, yy);
  290.             if (xvel == 0 && yvel == 0 && y > ground[x >> 3]-4)
  291.                 break;
  292.         }    //o for time increments.
  293.         
  294.         for (;;) 
  295.         {
  296.             //o find stable position.
  297.             if (ground[X_pos - 1] < ground[X_pos] && ground[X_pos] > ground[X_pos + 1]) 
  298.             {
  299.                 Draw_It (X_pos << 3, ground[X_pos]);
  300.                 ground[X_pos] -= 21;
  301.                 if (ground[X_pos - 1] <= 0)
  302.                     ground[X_pos] -= 7;
  303.                     ground[X_pos + 1] -= 7;
  304.                 break;
  305.             }
  306.             //o roll right.
  307.             if (ground[X_pos - 1]<ground[X_pos] && ground[X_pos]<ground[X_pos+1]) 
  308.             {
  309.                 X_pos++;
  310.                 continue;
  311.             }
  312.             //o roll left.
  313.             if (ground[X_pos - 1] > ground[X_pos] && ground[X_pos] > ground[X_pos + 1]) 
  314.             {
  315.                 X_pos--;
  316.                 continue;
  317.             }
  318.             //o on hilltop, choose at Random.
  319.             if (ground[X_pos - 1] > ground[X_pos] && ground[X_pos] < ground[X_pos + 1]) 
  320.             {
  321.                 if (Random () & 01)
  322.                     X_pos++;
  323.                 else
  324.                     X_pos--;
  325.                 continue;
  326.             }
  327.             //o else botch.
  328.             Draw_It (X_pos << 3, ground[X_pos]);
  329.             PaintRect (&myRect); //o was a call to f_rect ().
  330.             break;
  331.         }    //o for ever.
  332.     }    //• while not button.
  333.     Show_Menu_Bar ();
  334. }
  335.  
  336. //•-----------------------------------------------------------------•//
  337. //• Show's over, folks!
  338. //•-----------------------------------------------------------------•//
  339.