home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Graphics / QD3D / rollercoaster / Sources / MacApplication.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.9 KB  |  253 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MacApplication.c
  3.     
  4.     Contains:    Contains standard mac event loop & initialization code
  5.     
  6.     Written by:    Scott Kuechle, based on original Gerbils code by Brian Greenstone
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc. All rights reserved
  9.     
  10.     Change History (most recent first)
  11.     
  12.         <1>        9/1/98        srk        first file
  13.  
  14.  
  15. */
  16.  
  17. /************************************************************
  18. *                                                           *
  19. *    INCLUDE FILES                                          *
  20. *                                                           *
  21. *************************************************************/
  22.  
  23.  
  24. #include "MacApplication.h"
  25.  
  26.  
  27. /************************************************************
  28. *                                                           *
  29. *    FUNCTION PROTOTYPES                                    *
  30. *                                                           *
  31. *************************************************************/
  32.  
  33. static void         InitToolbox( void ) ;
  34. static void         MainEventLoop( void ) ;
  35. static void         HandleKeyPress(EventRecord *event) ;
  36. static void         HandleOSEvent(EventRecord *event) ;
  37. static short         HiWrd(long aLong);
  38. static short         LoWrd(long aLong);
  39.  
  40. /************************************************************
  41. *                                                           *
  42. *    GLOBAL VARIABLES FOR THIS SOURCE FILE                  *
  43. *                                                           *
  44. *************************************************************/
  45.  
  46. static DocumentRec    gDocument ;
  47.  
  48. /************************************************************
  49. *                                                           *
  50. *    FUNCTION:  Main                                        *
  51. *                                                           *
  52. *    PURPOSE:   Main routine for Macintosh sample program   *
  53. *                                                           *
  54. *************************************************************/
  55.  
  56. void main(void)
  57. {
  58.     Rect        rBounds = { 50, 50, 450, 450 } ;
  59.     Str255        title = "\pRollerCoaster" ;
  60.  
  61.         InitToolbox() ;
  62.         
  63.             /* set up our globals */
  64.         gDocument.fMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,0) ;
  65.         if (gDocument.fMainWindow == NULL)
  66.         {
  67.             Utils_DisplayFatalErrorMsg("Fatal Error: Unable to allocate a window!");
  68.         }
  69.  
  70.         #if TARGET_OS_MAC
  71.             QD3DSupport_InitDoc3DData( gDocument.fMainWindow, &gDocument );
  72.         #else if TARGET_OS_WIN32
  73.             QD3DSupport_InitDoc3DData( HWND window, &gDocument );
  74.         #endif
  75.         
  76.         MainEventLoop();
  77.         
  78.         QD3DSupport_DisposeDoc3DData( &gDocument ) ;
  79.     
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. /************************************************************
  88. *                                                           *
  89. *    FUNCTION:  HiWrd                                       *
  90. *                                                           *
  91. *    PURPOSE:   Given a long value, return the high word    *
  92. *                                                           *
  93. *************************************************************/
  94.  
  95. static short HiWrd(long aLong)
  96. {
  97.     return    (short)(((aLong) >> 16) & 0xFFFF) ;
  98. }
  99.  
  100. /************************************************************
  101. *                                                           *
  102. *    FUNCTION:  LoWrd                                       *
  103. *                                                           *
  104. *    PURPOSE:   Given a long value, return the low word     *
  105. *                                                           *
  106. *************************************************************/
  107.  
  108. static short LoWrd(long aLong)
  109. {
  110.     return    (short)((aLong) & 0xFFFF) ;
  111.  
  112. }
  113.  
  114. /************************************************************
  115. *                                                           *
  116. *    FUNCTION:  InitToolbox                                 *
  117. *                                                           *
  118. *    PURPOSE:   Initialize the various Macintosh Toolbox    *
  119. *               Managers                                    *
  120. *                                                           *
  121. *************************************************************/
  122.  
  123. static void InitToolbox()
  124. {
  125.     Handle        menuBar = nil;
  126.  
  127.     MaxApplZone() ;
  128.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  129.     
  130.     InitGraf( &qd.thePort );
  131.     InitFonts();
  132.     InitWindows();
  133.     InitCursor();
  134.  
  135.     FlushEvents( everyEvent, 0 ) ;
  136.     
  137. }
  138.  
  139.  
  140. /************************************************************
  141. *                                                           *
  142. *    FUNCTION:  MainEventLoop                               *
  143. *                                                           *
  144. *    PURPOSE:   Standard Macintosh event loop               *
  145. *                                                           *
  146. *************************************************************/
  147. static void MainEventLoop()
  148. {
  149.     EventRecord     event;
  150.     WindowPtr       window;
  151.     short           thePart;
  152.     Rect            screenRect;
  153.     Point            aPoint = {100, 100};
  154.     Boolean         quitFlag = false;
  155.     GrafPtr            oldPort;
  156.  
  157.     while( !quitFlag )
  158.     {
  159.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  160.         {
  161.  
  162.             switch (event.what)
  163.             {
  164.                 case mouseDown:
  165.                 
  166.                     thePart = FindWindow( event.where, &window );
  167.                     
  168.                     switch( thePart )
  169.                     {
  170.                         case inMenuBar: 
  171.                             break;
  172.                         
  173.                         case inDrag:
  174.                     
  175.                             screenRect = (**GetGrayRgn()).rgnBBox;
  176.                             DragWindow( window, event.where, &screenRect );
  177.                             break ;
  178.                     
  179.                         case inContent:
  180.                     
  181.                             if (window != FrontWindow())
  182.                                 SelectWindow( window );
  183.                             break ;
  184.                     
  185.                         case inGoAway:
  186.                             if (TrackGoAway( window, event.where ))
  187.                             {
  188.                                 DisposeWindow ( window );
  189.                                 quitFlag = true;
  190.  
  191.                             }
  192.                             break ;
  193.                             
  194.                         default:
  195.                             break ;
  196.                     }
  197.                     break ;
  198.                             
  199.                         
  200.                 case updateEvt:
  201.                 
  202.                     window = (WindowPtr)event.message;
  203.                     
  204.                     GetPort(&oldPort);
  205.                     SetPort( window ) ;
  206.                     
  207.                     BeginUpdate( window );
  208.                     QD3DSupport_DocDraw3DData( &gDocument ) ;
  209.                     EndUpdate( window );
  210.                     
  211.                     SetPort(oldPort);
  212.                     break ;
  213.                     
  214.                 case keyDown:
  215.                 case autoKey:
  216.                     HandleKeyPress(&event);
  217.                     break;
  218.                     
  219.                 case diskEvt:
  220.                     if ( HiWrd(event.message) != noErr ) 
  221.                         (void) DIBadMount(aPoint, event.message);
  222.                     break;
  223.                     
  224.                 case osEvt:
  225.                 case activateEvt:
  226.                     break;
  227.             }
  228.         }
  229.         else
  230.         {
  231.                 /* we received a null event */
  232.             Rect theRect = ((GrafPtr)gDocument.fMainWindow)->portRect ;
  233.             
  234.                 SetPort((GrafPtr)gDocument.fMainWindow) ;
  235.                 InvalRect( &theRect ) ;
  236.         }
  237.     }
  238. }
  239.  
  240.  
  241. /************************************************************
  242. *                                                           *
  243. *    FUNCTION:  HandleKeyPress                              *
  244. *                                                           *
  245. *    PURPOSE:   Handle any keypress events                  *
  246. *                                                           *
  247. *************************************************************/
  248. static void HandleKeyPress(EventRecord *event)
  249. {
  250. #pragma unused (event)
  251. }
  252.  
  253.