home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Dave Mark / Mac C Primer 1 (SC++7) / 3.4 - Flying Line / FlyingLine.c next >
Encoding:
C/C++ Source or Header  |  1994-05-13  |  4.6 KB  |  220 lines  |  [TEXT/KAHL]

  1. /********************************************************/
  2. /*                                                        */
  3. /*  Flying Line Code from Chapter Three of                */
  4. /*                                                        */
  5. /*    *** The Macintosh Programming Primer, 2nd Ed. ***    */
  6. /*                                                      */
  7. /*    Copyright 1992, Dave Mark and Cartwright Reed       */
  8. /*                                                        */
  9. /********************************************************/
  10.  
  11.  
  12. #define    kNumLines            50    /*  Try 100 or 150  */
  13. #define    kMoveToFront        (WindowPtr)-1L
  14. #define kRandomUpperLimit    32768
  15.  
  16. #define    kEmptyString        "\p"
  17. #define    kEmptyTitle            kEmptyString
  18. #define    kVisible            true
  19. #define    kNoGoAway            false
  20. #define    kNilRefCon            (long)nil
  21.  
  22.  
  23. /*************/
  24. /*  Globals  */
  25. /*************/
  26.  
  27. Rect        gLines[ kNumLines ];
  28. short        gDeltaTop=3, gDeltaBottom=3;  /* These four are the  */
  29. short        gDeltaLeft=2, gDeltaRight=6;  /* key to flying line!  */
  30. short        gOldMBarHeight;
  31.  
  32.  
  33. /***************/
  34. /*  Functions  */
  35. /***************/
  36.  
  37. void    ToolBoxInit( void );
  38. void    WindowInit( void );
  39. void    LinesInit( void );
  40. void    MainLoop( void );
  41. void    RandomRect( Rect *myRectPtr );
  42. short    Randomize( short range );
  43. void    RecalcLine( short i );
  44. void    DrawLine( short i );
  45.  
  46.  
  47. /****************** main ***************************/
  48.  
  49. void    main( void )
  50. {
  51.     ToolBoxInit();
  52.     WindowInit();
  53.     LinesInit();
  54.     MainLoop();
  55. }
  56.  
  57.  
  58. /****************** ToolBoxInit *********************/
  59.  
  60. void    ToolBoxInit( void )
  61. {
  62.     InitGraf( &qd.thePort );
  63.     InitFonts();
  64.     InitWindows();
  65.     InitMenus();
  66.     TEInit();
  67.     InitDialogs( 0L );
  68.     InitCursor();
  69. }
  70.  
  71.  
  72. /****************** WindowInit ***********************/
  73.  
  74. void    WindowInit( void )
  75. {
  76.     Rect        mBarRect;
  77.     RgnHandle    mBarRgn;
  78.     WindowPtr    window;
  79.  
  80.     gOldMBarHeight = LMGetMBarHeight();
  81.     LMSetMBarHeight( 0 );
  82.  
  83.     window = NewWindow( nil, &(qd.screenBits.bounds),
  84.         kEmptyTitle, kVisible, plainDBox, kMoveToFront,
  85.         kNoGoAway, kNilRefCon );
  86.  
  87.     SetRect( &mBarRect, qd.screenBits.bounds.left,
  88.                 qd.screenBits.bounds.top,
  89.                 qd.screenBits.bounds.right,
  90.                 qd.screenBits.bounds.top+gOldMBarHeight );
  91.  
  92.     mBarRgn = NewRgn();
  93.     RectRgn( mBarRgn, &mBarRect );
  94.     UnionRgn( window->visRgn, mBarRgn, window->visRgn );
  95.     DisposeRgn( mBarRgn );
  96.     SetPort( window );
  97.     FillRect( &(window->portRect), &qd.black );
  98.                                 /* Change black to ltGray, */
  99.     PenMode( patXor );    /*   <--- and comment out this line  */
  100. }
  101.  
  102.  
  103. /****************** LinesInit **********************/
  104.  
  105. void    LinesInit( void )
  106. {
  107.     short i;
  108.     
  109.     HideCursor();
  110.     GetDateTime( (unsigned long *)(&qd.randSeed) );
  111.     RandomRect( &(gLines[ 0 ]) );
  112.     DrawLine( 0 );
  113.     
  114.     for ( i=1; i<kNumLines; i++ )
  115.     {
  116.         gLines[ i ] = gLines[ i-1 ];
  117.         RecalcLine( i );
  118.         DrawLine( i );
  119.     }
  120. }
  121.  
  122.  
  123. /****************** MainLoop ***********************/
  124.  
  125. void    MainLoop( void )
  126. {
  127.     short i;
  128.     
  129.     while ( ! Button() )
  130.     {
  131.         DrawLine( kNumLines - 1 );
  132.         for ( i=kNumLines-1; i>0; i-- )
  133.             gLines[ i ] = gLines[ i-1 ];
  134.         RecalcLine( 0 );
  135.         DrawLine( 0 );
  136.     }
  137.     LMSetMBarHeight( gOldMBarHeight );
  138. }
  139.  
  140.  
  141. /****************** RandomRect *********************/
  142.  
  143. void    RandomRect( Rect *myRectPtr )
  144. {
  145.     WindowPtr    myWindow;
  146.  
  147.     myWindow = FrontWindow();
  148.     
  149.     myRectPtr->left = Randomize( myWindow->portRect.right
  150.         - myWindow->portRect.left );
  151.     myRectPtr->right = Randomize( myWindow->portRect.right
  152.         - myWindow->portRect.left );
  153.     myRectPtr->top = Randomize( myWindow->portRect.bottom
  154.         - myWindow->portRect.top );
  155.     myRectPtr->bottom = Randomize( myWindow->portRect.bottom
  156.         - myWindow->portRect.top );
  157. }
  158.  
  159.  
  160. /****************** Randomize **********************/
  161.  
  162. short    Randomize( short range )
  163. {
  164.     long    rawResult;
  165.     
  166.     rawResult = Random();
  167.     if ( rawResult < 0 ) rawResult *= -1;
  168.     return( (rawResult * range) / 32768 );
  169. }
  170.  
  171.  
  172. /****************** RecalcLine *********************/
  173.  
  174. void    RecalcLine( short i )
  175. {
  176.     WindowPtr    myWindow;
  177.  
  178.     myWindow = FrontWindow();
  179.     
  180.     gLines[ i ].top += gDeltaTop;
  181.     if ( ( gLines[ i ].top < myWindow->portRect.top ) ||
  182.          ( gLines[ i ].top > myWindow->portRect.bottom ) )
  183.     {
  184.         gDeltaTop *= -1;    
  185.         gLines[ i ].top += 2*gDeltaTop;
  186.     }
  187.     
  188.     gLines[ i ].bottom += gDeltaBottom;
  189.     if ( ( gLines[ i ].bottom < myWindow->portRect.top ) ||
  190.          ( gLines[ i ].bottom > myWindow->portRect.bottom ) )
  191.     {
  192.         gDeltaBottom *= -1;    
  193.         gLines[ i ].bottom += 2*gDeltaBottom;
  194.     }
  195.     
  196.     gLines[ i ].left += gDeltaLeft;
  197.     if ( ( gLines[ i ].left < myWindow->portRect.left ) ||
  198.          ( gLines[ i ].left > myWindow->portRect.right ) )
  199.     {
  200.         gDeltaLeft *= -1;    
  201.         gLines[ i ].left += 2*gDeltaLeft;
  202.     }
  203.     
  204.     gLines[ i ].right += gDeltaRight;
  205.     if ( ( gLines[ i ].right < myWindow->portRect.left ) ||
  206.          ( gLines[ i ].right > myWindow->portRect.right ) )
  207.     {
  208.         gDeltaRight *= -1;    
  209.         gLines[ i ].right += 2*gDeltaRight;
  210.     }
  211. }
  212.  
  213.  
  214. /****************** DrawLine ***********************/
  215.  
  216. void    DrawLine( short i )
  217. {
  218.     MoveTo( gLines[ i ].left, gLines[ i ].top );
  219.     LineTo( gLines[ i ].right, gLines[ i ].bottom );
  220. }