home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks96 / FinderFlocks.sit / FinderFlocks / FlockDrawing.cp < prev    next >
Text File  |  1996-06-22  |  8KB  |  305 lines

  1. #include "DrawSprocket.h"
  2. #include "FlockDrawing.h"
  3. #include "FinderRegistry.h"
  4.  
  5. /*
  6. ********************************************************************************
  7. ** globals
  8. ********************************************************************************
  9. */
  10. GrafPtr                    gOldPort;
  11. DSpContextReference        gDrawContext;
  12. DSpAltBufferReference    gUnderlay;
  13. extern long                gNumItems;
  14. extern Handle            gIcons[];
  15. extern Handle            gMasks[];
  16. extern Point            gPositions[];
  17. extern Rect                gWindowRect;
  18. extern long                gWindowView;
  19.     
  20. // Local prototype
  21. void MyInitAttributes( DSpContextAttributes *inAttributes);
  22.  
  23. /*
  24. ********************************************************************************
  25. ** SetUpDrawing
  26. ********************************************************************************
  27. */
  28. OSErr SetUpDrawing(RgnHandle worldRgn)
  29. {
  30.     DSpContextAttributes theDesiredAttributes;
  31.     OSStatus theError;
  32.     short    desiredWidth = 1024;
  33.     short    desiredHeight = 768;
  34.     short    cnt, rectsize = (gWindowView == pIconBitmap) ? 32 : 16;
  35.     GDHandle    mainDevice;
  36.     Rect        mainRect, rect;
  37.         
  38.  
  39.     CGrafPtr theAltBufferPort;
  40.     GDHandle theAltBufferGDevice, theOldGDevice;
  41.     GrafPtr theOldPort;
  42.     short    rectSize = 16;
  43.     
  44.     if(worldRgn == nil)
  45.         return memFullErr;
  46.     
  47.     // Get the info about the main device
  48.     mainDevice = GetMainDevice();
  49.     mainRect = (**mainDevice).gdRect;
  50.     desiredWidth = mainRect.right - mainRect.left;
  51.     desiredHeight = mainRect.bottom - mainRect.top;
  52.     
  53.     /* set the context */
  54.     MyInitAttributes( &theDesiredAttributes );
  55.     theDesiredAttributes.displayWidth            = desiredWidth;
  56.     theDesiredAttributes.displayHeight            = desiredHeight;
  57.     theDesiredAttributes.colorNeeds                = kDSpColorNeeds_Require;
  58.     theDesiredAttributes.backBufferDepthMask    = kDSpDepthMask_8;
  59.     theDesiredAttributes.displayDepthMask        = kDSpDepthMask_8;
  60.     theDesiredAttributes.backBufferBestDepth    = 8;
  61.     theDesiredAttributes.displayBestDepth        = 8;
  62.     theDesiredAttributes.pageCount                = 2;
  63.     theError = DSpFindBestContext( &theDesiredAttributes, &gDrawContext );
  64.     if( theError && kDSpContextNotFoundErr != theError )
  65.     {
  66.         return theError;
  67.     }
  68.     if( kDSpContextNotFoundErr == theError )
  69.     {
  70.         return theError;
  71.     }
  72.     
  73.     // save the world rgn
  74.     SetRectRgn(worldRgn, 0, 0, theDesiredAttributes.displayWidth, theDesiredAttributes.displayHeight);
  75.     
  76.     /*
  77.     ** Here is where I need to OR in the value to use page flipping.  If
  78.     ** I had used the value when calling DSpFindBestContext() then it would
  79.     ** have only considered displays that have page flipping hardware, but
  80.     ** I want to run with software buffering too.
  81.     */
  82.     theDesiredAttributes.contextOptions |= kDSpContextOption_PageFlip;
  83.  
  84.     /* reserve the context */    
  85.     theError = DSpContext_Reserve( gDrawContext, &theDesiredAttributes );
  86.     if( theError )
  87.     {
  88.         return theError;
  89.     }
  90.  
  91.     /*
  92.     ** allocate an alt buffer that will be used for the underlay.  An
  93.     ** underlay is useful in games that have a need to restore from a
  94.     ** static background.
  95.     */
  96.     theError = DSpAltBuffer_New( gDrawContext, false, 0, &gUnderlay );
  97.     if( theError )
  98.     {
  99.         DSpContext_Release( gDrawContext );
  100.         return theError;
  101.     }
  102.     
  103.     theError = DSpContext_SetUnderlayAltBuffer( gDrawContext, gUnderlay );
  104.     if( theError )
  105.     {
  106.         DSpContext_Release( gDrawContext );
  107.         return theError;
  108.     }
  109.     
  110.     // Capture the desktop into the alt buffer
  111.     GetPort( &theOldPort );
  112.     theOldGDevice = GetGDevice();
  113.     
  114.     /* set the alt buffer to be the current port */
  115.     theError = DSpAltBuffer_GetCGrafPtr( gUnderlay,
  116.         kDSpBufferKind_Normal, &theAltBufferPort, &theAltBufferGDevice );
  117.     if( theError )
  118.     {
  119.         DSpContext_Release( gDrawContext );
  120.         return theError;
  121.     }
  122.     
  123.     /*
  124.     ** if you want to use QuickDraw or any other toolbox rendering
  125.     ** code, you must setup the GDevice as well as the port when
  126.     ** working with AltBuffers!
  127.     */
  128.     SetPort( (GrafPtr)theAltBufferPort );
  129.     SetGDevice( theAltBufferGDevice );
  130.  
  131.     /*
  132.     ** fill the underlay buffer with the screen image
  133.     */
  134.     CopyBits(&qd.screenBits, &((GrafPtr)theAltBufferPort)->portBits,
  135.                 &theAltBufferPort->portRect, &theAltBufferPort->portRect, srcCopy, nil);    
  136.     
  137.     // Erase all the icons that will be flocking, clipping to the window rect
  138.     for(cnt = 0; cnt < gNumItems; cnt++)
  139.     {
  140.         SetRect(&rect, gPositions[cnt].h, gPositions[cnt].v,
  141.                     gPositions[cnt].h + rectsize, gPositions[cnt].v + rectsize);
  142.         SectRect(&rect, &gWindowRect, &rect);
  143.         EraseRect(&rect);
  144.     }
  145.     
  146.     // Restore port
  147.     SetPort( theOldPort );
  148.     SetGDevice( theOldGDevice );
  149.  
  150.     
  151.     // Set debug mode
  152.     //DSpSetDebugMode(true);
  153.     
  154.     /* put the context into the active state */
  155.     theError = DSpContext_SetState( gDrawContext, kDSpContextState_Active );
  156.     if( theError )
  157.     {
  158.         DSpContext_Release( gDrawContext );
  159.         return theError;
  160.     }
  161.     
  162.     return noErr;
  163. }
  164.  
  165. /*
  166. ********************************************************************************
  167. ** CloseDownDrawing
  168. ********************************************************************************
  169. */
  170. OSErr CloseDownDrawing(void)
  171. {
  172.     OSStatus theError = 0;
  173.  
  174.     /* fade to black */
  175.     //theError = DSpContext_FadeGammaOut( NULL, NULL );
  176.     //if( theError )
  177.     {
  178.         DSpContext_Release( gDrawContext );
  179.         return theError;
  180.     }
  181.     
  182.     // remove and dispose underlay
  183.     DSpContext_SetUnderlayAltBuffer( gDrawContext, NULL );
  184.     DSpAltBuffer_Dispose( gUnderlay );
  185.     gUnderlay = nil;
  186.     
  187.     /* put the context into the inactive state */
  188.     theError = DSpContext_SetState( gDrawContext, kDSpContextState_Inactive );
  189.     if( theError )
  190.     {
  191.         DSpContext_FadeGammaIn( NULL, NULL );
  192.         DSpContext_Release( gDrawContext );
  193.         return theError;
  194.     }
  195.  
  196.     /* fade back in */
  197.     //theError = DSpContext_FadeGammaIn( NULL, NULL );
  198.     //if( theError )
  199.     {
  200.         DSpContext_Release( gDrawContext );
  201.         return theError;
  202.     }
  203.     
  204.     /* release the context */
  205.     theError = DSpContext_Release( gDrawContext );
  206.     if( theError )
  207.     {
  208.         return theError;
  209.     }
  210.     return noErr;
  211. }
  212.  
  213. /*
  214. ********************************************************************************
  215. ** StartDrawing
  216. ********************************************************************************
  217. */
  218. OSErr StartDrawing()
  219. {
  220.     Rect        theRect;
  221.     OSErr        theError;
  222.     CGrafPtr    theAltBufferPort;
  223.     
  224.     GetPort( &gOldPort );
  225.     
  226.     /* set the alt buffer to be the current port */
  227.     
  228.     theError = DSpContext_GetBackBuffer( gDrawContext,
  229.         kDSpBufferKind_Normal, &theAltBufferPort);
  230.     if( theError )
  231.     {
  232.         DSpContext_Release( gDrawContext );
  233.         return theError;
  234.     }
  235.     
  236.     /*
  237.     ** if you want to use QuickDraw or any other toolbox rendering
  238.     ** code, you must setup the GDevice as well as the port when
  239.     ** working with AltBuffers!
  240.     */
  241.     SetPort( (GrafPtr)theAltBufferPort );
  242.     return noErr;
  243. }
  244.  
  245. /*
  246. ********************************************************************************
  247. ** EndDrawing
  248. ********************************************************************************
  249. */
  250. OSErr EndDrawing()
  251. {
  252.     OSErr    theError;
  253.     
  254.     // Swap buffers and restore old port
  255.     theError = DSpContext_SwapBuffers( gDrawContext, NULL, 0 );
  256.     SetPort( gOldPort );
  257.     return theError;
  258. }
  259.  
  260. void MarkRect(Rect *rect)
  261. {
  262.     // we ignore the error, if any.
  263.     DSpContext_InvalBackBufferRect(gDrawContext, rect);
  264. }
  265.  
  266. /*
  267. ********************************************************************************
  268. **
  269. ** Name: MyInitAttributes
  270. **
  271. ** Description:
  272. **
  273. **    Initialize a context attributes structure so that there is no garbage
  274. **    data in it.  This is important to do because DS will return an error
  275. **    if some fields are set incorrectly (including the "reserved" fields not
  276. **    being set to zero), and some things can actually cause a crash (such
  277. **    as a bogus color table handle).
  278. **
  279. ********************************************************************************
  280. */
  281. void MyInitAttributes( DSpContextAttributes *inAttributes)
  282. {
  283.     if( NULL == inAttributes )
  284.         DebugStr("\pStimpy! You Idiot!");
  285.         
  286.     inAttributes->frequency                    = 0;
  287.     inAttributes->displayWidth                = 0;
  288.     inAttributes->displayHeight                = 0;
  289.     inAttributes->reserved1                    = 0;
  290.     inAttributes->reserved2                    = 0;
  291.     inAttributes->colorNeeds                = 0;
  292.     inAttributes->colorTable                = NULL;
  293.     inAttributes->contextOptions            = 0;
  294.     inAttributes->backBufferDepthMask        = 0;
  295.     inAttributes->displayDepthMask            = 0;
  296.     inAttributes->backBufferBestDepth        = 0;
  297.     inAttributes->displayBestDepth            = 0;
  298.     inAttributes->pageCount                    = 0;
  299.     inAttributes->gameMustConfirmSwitch        = false;
  300.     inAttributes->reserved3[0]                = 0;
  301.     inAttributes->reserved3[1]                = 0;
  302.     inAttributes->reserved3[2]                = 0;
  303.     inAttributes->reserved3[3]                = 0;
  304. }
  305.