home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / Libraries / SpriteWorld / SpriteWorld files / Utils C / SWGameUtils.c < prev    next >
Encoding:
Text File  |  1996-10-24  |  7.1 KB  |  244 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. //    SWGameUtils.c
  3. //
  4. //    Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
  5. //
  6. //    Description:    some utility functions for games
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <QDOffscreen.h>
  11. #include <SWCommonHeaders.h>
  12. #include <SWGameUtils.h>
  13.  
  14.  
  15. ///--------------------------------------------------------------------------------------
  16. //    Randomize - initialize random number seed
  17. ///--------------------------------------------------------------------------------------
  18.  
  19. pascal void Randomize( void )
  20. {
  21.     GetDateTime( (unsigned long *)(&qd.randSeed) );
  22. }
  23.  
  24.  
  25. ///--------------------------------------------------------------------------------------
  26. //    GetRandom - generate a random number between min and max inclusive
  27. ///--------------------------------------------------------------------------------------
  28.  
  29. pascal short GetRandom(short min, short max)
  30. {
  31.     unsigned short        random;
  32.     long                range, temp;
  33.  
  34.     random = Random();
  35.     range = (max - min) + 1;
  36.     temp = (random * range) / 65536;
  37.     random = temp + min;
  38.  
  39.     return random;
  40. }
  41.  
  42.  
  43. ///--------------------------------------------------------------------------------------
  44. //    CenterRect - centers rectA in rectB
  45. ///--------------------------------------------------------------------------------------
  46.  
  47. pascal void CenterRect(Rect* rectA, Rect* rectB)
  48. {
  49.     short width = (rectA->right - rectA->left);
  50.     short height = (rectA->bottom - rectA->top);
  51.  
  52.     rectA->left = rectB->left + (((rectB->right - rectB->left) / 2) - (width / 2));
  53.     rectA->top = rectB->top + (((rectB->bottom - rectB->top) / 2) - (height / 2));
  54.     rectA->right = rectA->left + width;
  55.     rectA->bottom = rectA->top + height;
  56. }
  57.  
  58.  
  59. short        gOldEventMask;    // Used by AllowKeyUpEvents and RestoreEventMask
  60. Boolean        eventMaskIsGood = false;
  61.  
  62. ///--------------------------------------------------------------------------------------
  63. //    AllowKeyUpEvents - allows keyUpEvents. Make sure to call RestoreEventMask
  64. //    before your program quits if you call AllowKeyUpEvents.
  65. ///--------------------------------------------------------------------------------------
  66.  
  67. pascal void AllowKeyUpEvents( void )
  68. {
  69.     gOldEventMask = LMGetSysEvtMask();
  70.     SetEventMask(everyEvent);
  71.     
  72.         // Let RestoreEventMask know that the old mask has been saved
  73.     eventMaskIsGood = true;
  74. }
  75.  
  76.  
  77. ///--------------------------------------------------------------------------------------
  78. //    RestoreEventMask - call this before your program quits if you previously
  79. //    called AllowKeyUpEvents. This will beep if you never called AllowKeyUpEvents first.
  80. ///--------------------------------------------------------------------------------------
  81.  
  82. pascal void RestoreEventMask( void )
  83. {
  84.     if (eventMaskIsGood)
  85.         SetEventMask(gOldEventMask);
  86.     else
  87.         SysBeep(1);
  88. }
  89.  
  90.  
  91.  
  92. ///--------------------------------------------------------------------------------------
  93. //    Globals for HideMenuBar and ShowMenuBar
  94. ///--------------------------------------------------------------------------------------
  95.         
  96. RgnHandle    gOldVisRgn = NULL;    // visRgn of window before hiding menu bar
  97. RgnHandle    gUpdateRgn = NULL;    // region returned to user
  98. short        gOldMBarHeight;
  99.  
  100.  
  101. ///--------------------------------------------------------------------------------------
  102. //    HideMenuBar - expands the vis region of grafPort to cover the entire window, which
  103. // will allow you to draw in the top of that window to erase the menu bar. This is a
  104. // simple routine designed for programs with only one window that covers the menu bar.
  105. // If you need to expand the region of more than one window, you need a different routine.
  106. // Be sure to make the window visible before calling this. HideMenuBar returns the
  107. // region of the menu bar and corners of the screen, in case you want to erase or
  108. // draw in that area.
  109. ///--------------------------------------------------------------------------------------
  110.  
  111. pascal RgnHandle    HideMenuBar(GrafPtr grafPort)
  112. {
  113.     RgnHandle newVisRgn;
  114.     GrafPtr savePort;
  115.     
  116.     if (gOldVisRgn != NULL)
  117.         return NULL;
  118.  
  119.     GetPort(&savePort);
  120.     SetPort(grafPort);
  121.     
  122.     gOldMBarHeight = LMGetMBarHeight();
  123.     LMSetMBarHeight( 0 );        // Keeps things like SuperClock from coming on.
  124.  
  125.         // save off vis region
  126.     gOldVisRgn = NewRgn();
  127.     CopyRgn(grafPort->visRgn, gOldVisRgn);
  128.  
  129.         // expand the vis region of the port rect to be completely rectangular
  130.     newVisRgn = NewRgn();
  131.     RectRgn(newVisRgn, &grafPort->portRect);
  132.     CopyRgn(newVisRgn, grafPort->visRgn);
  133.     DisposeRgn(newVisRgn);
  134.     
  135.         // set gUpdateRgn to region of rounder corners and menu bar
  136.     gUpdateRgn = NewRgn();
  137.     CopyRgn(gOldVisRgn, gUpdateRgn);
  138.     DiffRgn(grafPort->visRgn, gUpdateRgn, gUpdateRgn);
  139.  
  140.     SetPort(savePort);
  141.     return gUpdateRgn;
  142. }
  143.  
  144.  
  145. ///--------------------------------------------------------------------------------------
  146. //    ShowMenuBar - restores the grafPort to the way it was before the call to HideMenuBar.
  147. //    Make sure to call this after every call to HideMenuBar to dispose of gOldVisRgn.
  148. ///--------------------------------------------------------------------------------------
  149.  
  150. pascal void ShowMenuBar(GrafPtr grafPort)
  151. {
  152.     GrafPtr savePort;
  153.     RgnHandle junkRgn;
  154.     
  155.     if (gOldVisRgn == NULL)
  156.         return;
  157.  
  158.     GetPort(&savePort);
  159.     SetPort(grafPort);
  160.     
  161.     LMSetMBarHeight( gOldMBarHeight );
  162.  
  163.         // fill the rounded corners of the screen with black again
  164.     junkRgn = NewRgn();
  165.     CopyRgn(gOldVisRgn, junkRgn);
  166.     DiffRgn(grafPort->visRgn, junkRgn, junkRgn);
  167.  
  168.     #ifdef dangerousPattern
  169.     FillRgn(junkRgn, qd.black);
  170.     #else
  171.     FillRgn(junkRgn, &qd.black);
  172.     #endif
  173.  
  174.     DisposeRgn(junkRgn);
  175.  
  176.         // restore the old vis region
  177.     CopyRgn(gOldVisRgn, grafPort->visRgn);
  178.     DisposeRgn(gOldVisRgn);
  179.     gOldVisRgn = NULL;
  180.     
  181.     DisposeRgn(gUpdateRgn);
  182.     gUpdateRgn = NULL;
  183.  
  184.     DrawMenuBar();
  185.     
  186.     SetPort(savePort);
  187. }
  188.  
  189.  
  190. ///--------------------------------------------------------------------------------------
  191. //    GetDepthFromWindow
  192. ///--------------------------------------------------------------------------------------
  193.  
  194. pascal short GetDepthFromWindow( WindowPtr theWindow )
  195. {
  196.     GWorldPtr        oldGWorld, windowGWld;
  197.     GDHandle        oldGDH, windowGDH;
  198.     short            depth;
  199.     
  200.     GetGWorld( &oldGWorld, &oldGDH );
  201.     SetPort( theWindow );
  202.     GetGWorld( &windowGWld, &windowGDH );
  203.     
  204.     depth = GetScreenDepth( windowGDH );
  205.     
  206.     SetGWorld( oldGWorld, oldGDH );
  207.     return depth;
  208. }
  209.  
  210.  
  211. ///--------------------------------------------------------------------------------------
  212. //    GetScreenDepth
  213. ///--------------------------------------------------------------------------------------
  214.  
  215. pascal short GetScreenDepth( GDHandle theGDH )
  216. {
  217.     SysEnvRec        theSERec;
  218.     PixMapHandle    thePixMap;
  219.     
  220.     
  221.     SysEnvirons( 2, &theSERec );
  222.     if ( !theSERec.hasColorQD )                // no colorQD?
  223.         return 1;
  224.     else
  225.     {
  226.         thePixMap = (**theGDH).gdPMap;
  227.         return (**thePixMap).pixelSize;     // Depth in bits (1,2,4...)
  228.     }
  229. }
  230.  
  231.  
  232. ///--------------------------------------------------------------------------------------
  233. //    HasSystem7
  234. ///--------------------------------------------------------------------------------------
  235.  
  236. pascal Boolean HasSystem7( void )
  237. {
  238.     long    gestaltResponse;
  239.     OSErr    err;
  240.  
  241.     err = Gestalt( gestaltSystemVersion, &gestaltResponse );
  242.     return ( err == noErr && (gestaltResponse >= 0x0700));
  243. }
  244.