home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Life 1.0d2 / LifeMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-02  |  3.7 KB  |  227 lines  |  [TEXT/MPCC]

  1. // LifeMain.c
  2. // Entry point for Mike's Life.
  3. // Copyright ©1995 Michael D. Crawford.  All Rights Reserved.
  4. // 11 Jun 95 Mike Crawford crawford@scruznet.com
  5. //
  6. // Revision History:
  7. // 11 Jun 95    MDC    New today
  8.  
  9. #include <QDOffscreen.h>
  10. #include "LifeAuthors.h"
  11. #include "LifeMain.h"
  12. #include "LifeMenus.h"
  13. #include "Control.h"
  14. #include "LifeModules.h"
  15.  
  16. void InitManagers( void );
  17. void EventLoop( void );
  18. void DoNullEvent( void );
  19. void DoMouseDown( EventRecord *evtPtr );
  20. void DoKeyDown( EventRecord *evtPtr );
  21. void DoUpdate( EventRecord *evtPtr );
  22. void DoActivate( EventRecord *evtPtr );
  23. void DoContentClick( WindowPtr winPtr, Point globalPt );
  24.  
  25. static Boolean    gTimeToQuit;
  26. static unsigned long    gSleepTime;
  27.  
  28. void main( void )
  29. {
  30.     OSErr    err;
  31.     
  32.     InitManagers();
  33.     
  34.     err = SetupMenus();
  35.     if ( err ){
  36.         ExitToShell();
  37.     }
  38.  
  39.     InitControl();
  40.  
  41.     err = InitModules();
  42.     if ( err )
  43.         return;
  44.  
  45.     gTimeToQuit = false;
  46.     gSleepTime = 0;
  47.     
  48.     InitCursor();
  49.  
  50.     EventLoop();
  51.     
  52.     return;
  53. }
  54.  
  55. void EventLoop( void )
  56. {
  57.     EventRecord    evt;
  58.     
  59.     do {
  60.         WaitNextEvent( everyEvent, &evt, gSleepTime, (RgnHandle)NULL );
  61.         
  62.         switch( evt.what ){
  63.             case nullEvent:
  64.                 DoNullEvent();
  65.                 break;
  66.             case mouseDown:
  67.                 DoMouseDown( &evt );
  68.                 break;
  69.             case mouseUp:
  70.                 break;
  71.             case keyDown:
  72.             case autoKey:
  73.                 DoKeyDown( &evt );
  74.                 break;
  75.             case keyUp:
  76.                 break;
  77.             case updateEvt:
  78.                 DoUpdate( &evt );
  79.                 break;
  80.             case diskEvt:
  81.                 break;
  82.             case activateEvt:
  83.                 DoActivate( &evt );
  84.                 break;
  85.             case osEvt:
  86.                 break;
  87.             default:
  88.                 break;
  89.         }
  90.     }while ( !gTimeToQuit );
  91.     
  92.     return;
  93. }
  94.  
  95. void DoNullEvent( void )
  96. {
  97.     DoLifeNull();
  98.  
  99.     return;
  100. }
  101.  
  102. void DoMouseDown( EventRecord *evtPtr )
  103. {
  104.     short    where;
  105.     WindowPtr    winPtr;
  106.     long        menuCode;
  107.     Rect        dragRect;
  108.     RgnHandle    grayRgn;
  109.  
  110.     where = FindWindow( evtPtr->where, &winPtr );
  111.  
  112.     switch( where ){
  113.         case inDesk:
  114.             break;
  115.         case inMenuBar:
  116.             AdjustMenus();
  117.             menuCode = MenuSelect( evtPtr->where );
  118.             DoMenuChoice( menuCode );
  119.             break;
  120.         case inSysWindow:
  121.             break;
  122.         case inContent:
  123.             DoContentClick( winPtr, evtPtr->where );
  124.             break;
  125.         case inDrag:
  126.             grayRgn = GetGrayRgn();
  127.             dragRect = (*grayRgn)->rgnBBox;
  128.             DragWindow( winPtr, evtPtr->where, &dragRect );
  129.             break;
  130.         case inGrow:
  131.             break;
  132.         case inGoAway:
  133.             if ( TrackGoAway( winPtr, evtPtr->where ) )
  134.                 DoCloseWindow();
  135.             break;
  136.         case inZoomIn:
  137.             break;
  138.         case inZoomOut:
  139.             break;
  140.     }
  141.  
  142.     return;
  143. }
  144.  
  145. void DoKeyDown( EventRecord *evtPtr )
  146. {
  147.     char    theChar;
  148.     short    isCmd;
  149.     long    menuCode;
  150.     
  151.     theChar = evtPtr->message & charCodeMask;
  152.  
  153.     isCmd = evtPtr->modifiers & cmdKey;
  154.     
  155.     if ( isCmd ){
  156.         AdjustMenus();                    // So grayed out items won't show up in MenuKey
  157.  
  158.         menuCode = MenuKey( theChar );
  159.         
  160.         DoMenuChoice( menuCode );
  161.     }
  162.  
  163.     return;
  164. }
  165.  
  166. void DoContentClick( WindowPtr winPtr, Point globalPt )
  167. {
  168.     tLifeWorldPtr    worldPtr;
  169.     Point localPt;
  170.  
  171.     SetPort( winPtr );
  172.     localPt = globalPt;
  173.     GlobalToLocal( &localPt );
  174.  
  175.     worldPtr = WinToLifeWorld( winPtr );
  176.     if ( !worldPtr )
  177.         return;
  178.  
  179.     // Locate the part of the window that we have clicked in
  180.     // STUB I'd like to have a toolbar.
  181.     // STUB Check for scroll bar click
  182.     // STUB and also translate into lifeworld coordinates
  183.     // STUB allow drawing by dragging, and autoscrolling... gets complicated.
  184.  
  185.     ToggleCell( worldPtr, (long)localPt.h, (long)localPt.v );
  186.     DrawCell( winPtr, worldPtr, (long)localPt.h, (long)localPt.v );
  187.     return;
  188. }
  189.  
  190. void DoUpdate( EventRecord *evtPtr )
  191. {
  192.     WindowPtr    win;
  193.     
  194.     win = (WindowPtr)( evtPtr->message );
  195.  
  196.     BeginUpdate( win );
  197.     
  198.     // Draw the window STUB
  199.  
  200.     EndUpdate( win );
  201.  
  202.     return;
  203. }
  204.  
  205. void DoActivate( EventRecord *evtPtr )
  206. {
  207.     return;
  208. }
  209.  
  210. void TimeToQuit( void )
  211. {
  212.     gTimeToQuit = true;
  213.  
  214.     return;
  215. }
  216.  
  217. void InitManagers( void )
  218. {
  219.     InitGraf( &qd.thePort );
  220.     InitWindows();
  221.     InitFonts();
  222.     InitDialogs( (long)NULL );
  223.     
  224.     return;
  225. }
  226.  
  227.