home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Snippets / PNL Libraries / Libraries / SpriteWorld / SpriteWorld files / Sources / SpriteFrame.c < prev    next >
Encoding:
Text File  |  1996-11-15  |  20.5 KB  |  886 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. //    SpriteFrame.c
  3. //
  4. //    Portions are copyright: © 1991-94 Tony Myles, All rights reserved worldwide.
  5. //
  6. //    Description:    implementation of the frame stuff
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef __SWCOMMON__
  11. #include "SWCommonHeaders.h"
  12. #endif
  13.  
  14. #ifndef __TOOLUTILS__
  15. #include <ToolUtils.h>
  16. #endif
  17.  
  18. #ifndef __MEMORY__
  19. #include <Memory.h>
  20. #endif
  21.  
  22. #ifndef __RESOURCES__
  23. #include <Resources.h>
  24. #endif
  25.  
  26. #ifndef __ERRORS__
  27. #include <Errors.h>
  28. #endif
  29.  
  30. #ifndef __SPRITEWORLDUTILS__
  31. #include "SpriteWorldUtils.h"
  32. #endif
  33.  
  34. #ifndef __SPRITECOMPILER__
  35. #include "SpriteCompiler.h"
  36. #endif
  37.  
  38. #ifndef __SPRITEFRAME__
  39. #include "SpriteFrame.h"
  40. #endif
  41.  
  42.  
  43. ///--------------------------------------------------------------------------------------
  44. //    SWCreateWindowFrame
  45. ///--------------------------------------------------------------------------------------
  46.  
  47. SW_FUNC OSErr SWCreateWindowFrame(
  48.     FramePtr* newFrameP,
  49.     Rect* frameRect)
  50. {
  51.     OSErr             err = noErr;
  52.     GDHandle        currentGDH;
  53.     GWorldPtr        windowGWorld;
  54.     FramePtr         tempFrameP = NULL;
  55.     short            depth;
  56.     long             numScanLines;
  57.     Point            globalOffset;
  58.     
  59.     SWAssert( newFrameP != NULL && frameRect != NULL );
  60.     
  61.     *newFrameP = NULL;
  62.     
  63.     GetGWorld( &windowGWorld, ¤tGDH );
  64.         // more scanlines than we need, but this saves us from 
  65.         // needing to calculate an offset in BlitPixie
  66.  
  67.     SWAssert( windowGWorld != NULL );
  68.  
  69.     numScanLines = (frameRect->bottom - frameRect->top) + 
  70.         (frameRect->top - windowGWorld->portRect.top);
  71.  
  72.     globalOffset = topLeft( *frameRect );
  73.     LocalToGlobal( &globalOffset );
  74.     
  75.     tempFrameP = (FramePtr)NewPtrClear((Size)sizeof(FrameRec) + 
  76.             (sizeof(unsigned long) * numScanLines));
  77.  
  78.     if (tempFrameP != NULL)
  79.     {
  80.         SWAssert( currentGDH != NULL && (**currentGDH).gdPMap != NULL );
  81.         
  82.         depth = (**(**currentGDH).gdPMap).pixelSize;
  83.         tempFrameP->framePort = windowGWorld;
  84.  
  85.         tempFrameP->frameRect = *frameRect;
  86.         
  87.         // cache frameRowBytes for use by our blitter routine
  88.  
  89.         tempFrameP->frameRowBytes = (**tempFrameP->framePort->portPixMap).rowBytes & 0x7FFF;
  90.  
  91.             // this calculation generates a mask value that we use to
  92.             // long word align the rectangle when we draw the frame.
  93.             // note that the expression "sizeof(long) * kBitsPerByte" gives us
  94.             // the number of bits in a long.
  95.     
  96.         tempFrameP->rightAlignFactor = ((sizeof(long) * kBitsPerByte) / depth) - 1;
  97.         tempFrameP->leftAlignFactor = ~(tempFrameP->rightAlignFactor);
  98.  
  99.             // useCount not used for window/work/back frames
  100.         tempFrameP->useCount = 0;
  101.  
  102.             // here we set up an array of offsets to the scan lines of
  103.             // this frame. this allows us to address a particular scan line
  104.             // without doing a costly multiply.
  105.         tempFrameP->numScanLines = numScanLines;
  106.         tempFrameP->scanLinePtrArray = (unsigned long*)(tempFrameP + 1);
  107.  
  108.         {    
  109.             tempFrameP->worldRectOffset = 0;
  110.                 
  111.             if (depth < 8 )
  112.             {
  113.                 short        roundOff;
  114.                 
  115.                 roundOff = 8/depth;
  116.                 tempFrameP->worldRectOffset = 
  117.                     (globalOffset.h - frameRect->left) - 
  118.                     (((globalOffset.h - frameRect->left)/roundOff)*roundOff);
  119.             }
  120.                 
  121.             for (numScanLines = 0; numScanLines < tempFrameP->numScanLines; numScanLines++)
  122.             {
  123.                 tempFrameP->scanLinePtrArray[numScanLines] = 
  124.                     ((numScanLines+(globalOffset.v - frameRect->top) ) * 
  125.                     tempFrameP->frameRowBytes) +
  126.                     (globalOffset.h - frameRect->left)*4/(32/depth);
  127.             }
  128.         }
  129.  
  130.         tempFrameP->sharesGWorld = false;
  131.  
  132.         *newFrameP = tempFrameP;
  133.     }
  134.     else
  135.     {
  136.         err = MemError();
  137.     }
  138.  
  139.     if (err != noErr)
  140.     {
  141.         if (tempFrameP != NULL)
  142.         {
  143.             DisposePtr((Ptr)tempFrameP);
  144.         }
  145.     }
  146.     
  147.     SWSetStickyIfError( err );
  148.     return err;
  149. }
  150.  
  151.  
  152. ///--------------------------------------------------------------------------------------
  153. //    SWCreateFrameFromCicnResource
  154. ///--------------------------------------------------------------------------------------
  155.  
  156. SW_FUNC OSErr SWCreateFrameFromCicnResource(
  157.     SpriteWorldPtr destSpriteWorld,
  158.     FramePtr* newFrameP,
  159.     short iconResID,
  160.     MaskType maskType)
  161. {
  162.     OSErr             err;  
  163.     GWorldPtr         saveGWorld;
  164.     GDHandle         saveGDH;
  165.     GDHandle        theGDH;
  166.     FramePtr         tempFrameP;
  167.     CIconHandle     cIconH;
  168.     RgnHandle         maskRgn;
  169.     Rect             frameRect;
  170.  
  171.     SWAssert( destSpriteWorld != NULL && newFrameP != NULL );
  172.     
  173.     *newFrameP = NULL;
  174.     tempFrameP = NULL;
  175.  
  176.     GetGWorld(&saveGWorld, &saveGDH);
  177.     
  178.     cIconH = GetCIcon( iconResID );
  179.  
  180.     if (cIconH != NULL)
  181.     {
  182.         HNoPurge((Handle)cIconH);
  183.         frameRect = (**cIconH).iconPMap.bounds;
  184.         
  185.         theGDH = destSpriteWorld->mainSWGDH;
  186.         
  187.         err = SWCreateFrame(theGDH, &tempFrameP, &frameRect);
  188.     }
  189.     else
  190.     {
  191.         err = MemError();
  192.         if ( err == noErr )
  193.             err = resNotFound;
  194.     }
  195.     
  196.     if (err == noErr)
  197.     {
  198.         (void)LockPixels( GetGWorldPixMap(tempFrameP->framePort) );
  199.         SetGWorld(tempFrameP->framePort, nil);
  200.         EraseRect( &frameRect );
  201.         PlotCIcon(&frameRect, cIconH);
  202.         UnlockPixels( GetGWorldPixMap(tempFrameP->framePort) );
  203.  
  204.             // make a region mask
  205.         if ((maskType & kRegionMask) != 0)
  206.         {
  207.             err = SWCreateRegionFromCIconMask(&maskRgn, cIconH);
  208.  
  209.             if (err == noErr)
  210.             {
  211.                 SWSetFrameMaskRgn(tempFrameP, maskRgn);
  212.             }
  213.         }
  214.     }
  215.  
  216.     if (err == noErr)
  217.     {
  218.             // make a pixel mask
  219.         if ((maskType & kPixelMask) != 0)
  220.         {
  221.             err = SWCreateGWorldFromCIconMask(destSpriteWorld, &tempFrameP->maskPort, cIconH);
  222.             
  223.             if (err == noErr && destSpriteWorld->pixelDepth <= 8)
  224.             {
  225.                 (void)LockPixels( GetGWorldPixMap(tempFrameP->maskPort) );
  226.                 SetGWorld(tempFrameP->maskPort, nil);
  227.                 InvertRect(&tempFrameP->maskPort->portRect);
  228.                 UnlockPixels( GetGWorldPixMap(tempFrameP->maskPort) );
  229.             }
  230.         }
  231.     }
  232.  
  233.     if (cIconH != NULL)
  234.     {
  235.         DisposeCIcon(cIconH);
  236.     }
  237.  
  238.     if (err == noErr)
  239.     {
  240.         *newFrameP = tempFrameP;
  241.     }
  242.  
  243.     if (err != noErr)
  244.     {
  245.             // an error occurred so dispose of anything we managed to create
  246.         if (tempFrameP != NULL)
  247.         {
  248.             SWDisposeFrame(tempFrameP);
  249.         }
  250.     }
  251.  
  252.     SetGWorld(saveGWorld, nil);
  253.  
  254.     SWSetStickyIfError( err );
  255.     return err;
  256. }
  257.  
  258.  
  259. ///--------------------------------------------------------------------------------------
  260. //    SWCreateFrameFromPictResource
  261. ///--------------------------------------------------------------------------------------
  262.  
  263. SW_FUNC OSErr SWCreateFrameFromPictResource(
  264.     SpriteWorldPtr destSpriteWorld,
  265.     FramePtr* newFrameP,
  266.     short pictResID,
  267.     short maskResID,
  268.     MaskType maskType)
  269. {
  270.     OSErr             err;
  271.     GWorldPtr         saveGWorld;
  272.     GDHandle         saveGDH;
  273.     GDHandle        theGDH;
  274.     PicHandle         spritePictH, 
  275.                     maskPictH;
  276.     FramePtr         tempFrameP;
  277.     RgnHandle         maskRgn;
  278.     Rect             frameRect;
  279.  
  280.     SWAssert( destSpriteWorld != NULL && newFrameP != NULL );
  281.     
  282.     tempFrameP = NULL;
  283.     *newFrameP = NULL;
  284.     
  285.     GetGWorld(&saveGWorld, &saveGDH);
  286.  
  287.     spritePictH = GetPicture(pictResID);
  288.  
  289.     if (spritePictH != NULL)
  290.     {
  291.         frameRect = (**spritePictH).picFrame;
  292.         OffsetRect( &frameRect, -frameRect.left, -frameRect.top );
  293.  
  294.         theGDH = destSpriteWorld->mainSWGDH;
  295.         
  296.         err = SWCreateFrame(theGDH, &tempFrameP, &frameRect);
  297.  
  298.         if (err == noErr)
  299.         {
  300.             (void)LockPixels( GetGWorldPixMap(tempFrameP->framePort) );
  301.             SetGWorld(tempFrameP->framePort, nil);
  302.             EraseRect(&frameRect);
  303.             DrawPicture(spritePictH, &frameRect);
  304.             UnlockPixels( GetGWorldPixMap(tempFrameP->framePort) );
  305.             ReleaseResource((Handle)spritePictH);
  306.         }
  307.         
  308.             // make a region mask
  309.         if (((maskType & kRegionMask) != 0) && (err == noErr))
  310.         {
  311.             maskPictH = GetPicture(maskResID);
  312.     
  313.             if (maskPictH != NULL)
  314.             {
  315.                 err = SWCreateRegionFromPict(&maskRgn, maskPictH );
  316.         
  317.                 ReleaseResource((Handle)maskPictH);
  318.             }
  319.             else
  320.             {
  321.                 err = MemError();
  322.                 if ( err == noErr )
  323.                     err = resNotFound;
  324.             }
  325.  
  326.             if (err == noErr)
  327.             {
  328.                 SWSetFrameMaskRgn(tempFrameP, maskRgn);
  329.             }
  330.         }
  331.  
  332.             // make a pixel mask
  333.         if (((maskType & kPixelMask) != 0) && (err == noErr))
  334.         {
  335.             err = SWCreateGWorldFromPictResource(destSpriteWorld, &tempFrameP->maskPort, maskResID);
  336.  
  337.             if (err == noErr)
  338.             {
  339.                 if ( pictResID == maskResID )
  340.                 {
  341.                     err = SWBlackenGWorld( tempFrameP->maskPort );
  342.                 }
  343.                 if (err == noErr && destSpriteWorld->pixelDepth <= 8)
  344.                 {
  345.                     (void)LockPixels( GetGWorldPixMap(tempFrameP->maskPort) );
  346.                     SetGWorld(tempFrameP->maskPort, nil);
  347.                     InvertRect(&tempFrameP->maskPort->portRect);
  348.                     UnlockPixels( GetGWorldPixMap(tempFrameP->maskPort) );
  349.                 }
  350.             }
  351.         }
  352.  
  353.         if (err == noErr)
  354.         {
  355.             *newFrameP = tempFrameP;
  356.         }
  357.         else
  358.         {
  359.                 // an error occurred so dispose of anything we managed to create
  360.             if (tempFrameP != NULL)
  361.             {
  362.                 SWDisposeFrame(tempFrameP);
  363.             }
  364.         }
  365.     }
  366.     else
  367.     {
  368.         err = MemError();
  369.         if ( err == noErr )
  370.             err = resNotFound;
  371.     }
  372.  
  373.     SetGWorld(saveGWorld, nil);
  374.  
  375.     SWSetStickyIfError( err );
  376.     return err;
  377. }
  378.  
  379.  
  380. SW_FUNC OSErr SWCreateFrameFromGWorldAndRectStart(
  381.     GWorldPtr *tempMaskGWorld,
  382.     int maxWidth,
  383.     int maxHeight)
  384. {
  385.     return SWCreateRegionFromGWorldAndRectStart( tempMaskGWorld, maxWidth, maxHeight );
  386. }
  387.  
  388. SW_FUNC void SWCreateFrameFromGWorldAndRectFinish(
  389.     GWorldPtr tempMaskGWorld)
  390. {
  391.     SWCreateRegionFromGWorldAndRectFinish( tempMaskGWorld );
  392. }
  393.  
  394. SW_FUNC OSErr SWCreateFrameFromGWorldAndRectPartial(
  395.     FramePtr* newFrameP,
  396.     GWorldPtr pictGWorld,
  397.     GWorldPtr maskGWorld,
  398.     GWorldPtr tempMaskGWorld,
  399.     Rect* frameRect,
  400.     MaskType maskType)
  401. {                        
  402.     OSErr             err = noErr;
  403.     short            depth;
  404.     long             numScanLines;
  405.     FramePtr         tempFrameP;
  406.     RgnHandle         maskRgn;
  407.  
  408.     SWAssert( newFrameP != NULL && pictGWorld != NULL && frameRect != NULL );
  409.  
  410.     tempFrameP = NULL;
  411.     *newFrameP = NULL;
  412.  
  413.     numScanLines = frameRect->bottom - frameRect->top;
  414.     depth = (**(GetGWorldPixMap( pictGWorld ))).pixelSize;
  415.     
  416.     tempFrameP = (FramePtr)NewPtrClear((Size)sizeof(FrameRec) + 
  417.         (sizeof(unsigned long) * (numScanLines+1)));
  418.     
  419.     if (tempFrameP != NULL)
  420.     {
  421.         tempFrameP->framePort = pictGWorld;
  422.         if ((maskType & kPixelMask) != 0) {
  423.             SWAssert( maskGWorld != NULL );
  424.             tempFrameP->maskPort = maskGWorld;
  425.         }
  426.         tempFrameP->frameRect = *frameRect;
  427.         tempFrameP->sharesGWorld = true;
  428.         tempFrameP->numScanLines = numScanLines;
  429.  
  430.         SWInitializeFrame( tempFrameP, depth );
  431.     }
  432.     else
  433.     {
  434.         err = MemError();
  435.     }
  436.  
  437.     if (err == noErr)
  438.     {
  439.             // make a region mask
  440.         if (((maskType & kRegionMask) != 0) && (err == noErr))
  441.         {
  442.             err = SWCreateRegionFromGWorldAndRectPartial(&maskRgn, maskGWorld, tempMaskGWorld, frameRect);
  443.                 
  444.             if (err == noErr)
  445.             {
  446.                 SWSetFrameMaskRgn(tempFrameP, maskRgn);
  447.             }
  448.         }
  449.     
  450.         if (err == noErr)
  451.         {
  452.             *newFrameP = tempFrameP;
  453.         }
  454.         else
  455.         {
  456.                 // an error occurred so dispose of anything we managed to create
  457.             if (tempFrameP != NULL)
  458.             {
  459.                 SWDisposeFrame(tempFrameP);
  460.             }
  461.         }
  462.     }
  463.     
  464.     SWSetStickyIfError( err );
  465.     return err;
  466. }
  467.  
  468. ///--------------------------------------------------------------------------------------
  469. //    SWCreateFrameFromGWorldAndRect
  470. ///--------------------------------------------------------------------------------------
  471.  
  472. SW_FUNC OSErr SWCreateFrameFromGWorldAndRect(
  473.     FramePtr* newFrameP,
  474.     GWorldPtr pictGWorld,
  475.     GWorldPtr maskGWorld,
  476.     Rect* frameRect,
  477.     MaskType maskType)
  478. {
  479.     OSErr err;
  480.     GWorldPtr tempMaskGWorld;
  481.     
  482.     err = SWCreateFrameFromGWorldAndRectStart( &tempMaskGWorld, frameRect->right - frameRect->left, frameRect->bottom - frameRect->top );
  483.     if ( err == noErr ) {
  484.         err = SWCreateFrameFromGWorldAndRectPartial( newFrameP, pictGWorld, maskGWorld, tempMaskGWorld, frameRect, maskType );
  485.         SWCreateFrameFromGWorldAndRectFinish( tempMaskGWorld );
  486.     }
  487.     return err;
  488. }
  489.  
  490. ///--------------------------------------------------------------------------------------
  491. //    SWCreateFrame
  492. ///--------------------------------------------------------------------------------------
  493.  
  494. SW_FUNC OSErr SWCreateFrame(
  495.     GDHandle theGDH,
  496.     FramePtr* newFrameP,
  497.     Rect* frameRect)
  498. {
  499.     OSErr             err = noErr;
  500.     short            depth;
  501.     GWorldPtr        frameGWorld;
  502.     FramePtr         tempFrameP;
  503.     long             numScanLines;    
  504.     
  505.     *newFrameP = NULL;
  506.     frameGWorld = NULL;
  507.     
  508.     SWAssert( theGDH != NULL && newFrameP != NULL && frameRect != NULL );
  509.     SWAssert( (**theGDH).gdPMap != NULL );
  510.     
  511.     depth = (**(**theGDH).gdPMap).pixelSize;
  512.     
  513.     numScanLines = frameRect->bottom - frameRect->top;
  514.  
  515.     tempFrameP = (FramePtr)NewPtrClear((Size)sizeof(FrameRec) + 
  516.         (sizeof(unsigned long) * (numScanLines+1)));
  517.     
  518.     if (tempFrameP != NULL)
  519.     {
  520.         err = NewGWorld( &frameGWorld, depth, frameRect, nil, theGDH, noNewDevice );
  521.         
  522.         if (err == noErr)
  523.         {
  524.             tempFrameP->framePort = frameGWorld;
  525.             tempFrameP->frameRect = *frameRect;
  526.             tempFrameP->tileMaskIsSolid = false;
  527.             tempFrameP->isFrameLocked = false;
  528.             tempFrameP->numScanLines = numScanLines;
  529.             tempFrameP->sharesGWorld = false;
  530.     
  531.             SWInitializeFrame( tempFrameP, depth );
  532.             
  533.             *newFrameP = tempFrameP;
  534.         }
  535.     }
  536.     else
  537.     {
  538.         err = MemError();
  539.     }
  540.  
  541.     if (err != noErr)
  542.     {
  543.         if (tempFrameP != NULL)
  544.         {
  545.             if (tempFrameP->framePort != NULL)
  546.                 DisposeGWorld(tempFrameP->framePort);
  547.             DisposePtr((Ptr)tempFrameP);
  548.         }
  549.     }
  550.     
  551.     SWSetStickyIfError( err );
  552.     return err;
  553. }
  554.  
  555.  
  556. ///--------------------------------------------------------------------------------------
  557. //    SWCreateFrameFromDepth
  558. ///--------------------------------------------------------------------------------------
  559.  
  560. SW_FUNC OSErr SWCreateFrameFromDepth(
  561.     FramePtr* newFrameP,
  562.     short depth,
  563.     Rect* frameRect)
  564. {
  565.     OSErr             err = noErr;
  566.     GWorldPtr        frameGWorld;
  567.     FramePtr         tempFrameP;
  568.     long             numScanLines;    
  569.     
  570.     *newFrameP = NULL;
  571.     frameGWorld = NULL;
  572.     
  573.     SWAssert( depth > 0 && newFrameP != NULL && frameRect != NULL );
  574.     
  575.     numScanLines = frameRect->bottom - frameRect->top;
  576.  
  577.     tempFrameP = (FramePtr)NewPtrClear((Size)sizeof(FrameRec) + 
  578.         (sizeof(unsigned long) * (numScanLines+1)));
  579.     
  580.     if (tempFrameP != NULL)
  581.     {
  582.         err = NewGWorld( &frameGWorld, depth, frameRect, nil, nil, 0 );
  583.         
  584.         if (err == noErr)
  585.         {
  586.             tempFrameP->framePort = frameGWorld;
  587.             tempFrameP->frameRect = *frameRect;
  588.             tempFrameP->tileMaskIsSolid = false;
  589.             tempFrameP->isFrameLocked = false;
  590.             tempFrameP->numScanLines = numScanLines;
  591.             tempFrameP->sharesGWorld = false;
  592.     
  593.             SWInitializeFrame( tempFrameP, depth );
  594.             
  595.             *newFrameP = tempFrameP;
  596.         }
  597.     }
  598.     else
  599.     {
  600.         err = MemError();
  601.     }
  602.  
  603.     if (err != noErr)
  604.     {
  605.         if (tempFrameP != NULL)
  606.         {
  607.             if (tempFrameP->framePort != NULL)
  608.                 DisposeGWorld(tempFrameP->framePort);
  609.             DisposePtr((Ptr)tempFrameP);
  610.         }
  611.     }
  612.     
  613.     SWSetStickyIfError( err );
  614.     return err;
  615. }
  616.  
  617.  
  618. SW_FUNC OSErr SWCloneFrame(
  619.     FramePtr cloneFrameP,
  620.     FramePtr* newFrameP)
  621. {
  622.     OSErr err;
  623.     FramePtr         tempFrameP;
  624.  
  625.     SWAssert( cloneFrameP != NULL );
  626.     
  627.     tempFrameP = (FramePtr)NewPtrClear(GetPtrSize((Ptr)cloneFrameP));
  628.     
  629.     if (tempFrameP != NULL)
  630.     {
  631.         *tempFrameP = *cloneFrameP;
  632.         
  633.         tempFrameP->scanLinePtrArray = (unsigned long*)(tempFrameP + 1);
  634.         tempFrameP->maskRgn = NULL;
  635.         tempFrameP->sharesGWorld = true;
  636.         tempFrameP->useCount = 0;
  637.         tempFrameP->pixCodeH = NULL;
  638.         tempFrameP->frameBlitterP = NULL;
  639.         
  640.         if ( cloneFrameP->maskRgn != NULL )
  641.         {
  642.             tempFrameP->maskRgn = cloneFrameP->maskRgn;
  643.             err = HandToHand( &(Handle)tempFrameP->maskRgn );
  644.             if ( err != noErr )
  645.             {
  646.                 tempFrameP->maskRgn = NULL;
  647.             }
  648.         }
  649.         
  650.         if ( err == noErr && cloneFrameP->pixCodeH != NULL )
  651.         {
  652.             tempFrameP->pixCodeH = cloneFrameP->pixCodeH;
  653.             err = HandToHand( &(Handle)tempFrameP->pixCodeH );
  654.             if ( err != noErr )
  655.             {
  656.                 tempFrameP->pixCodeH = NULL;
  657.             }
  658.         }
  659.         
  660.     }
  661.     else
  662.     {
  663.         err = MemError();
  664.     }
  665.  
  666.     if (err == noErr)
  667.     {
  668.         if (tempFrameP->isFrameLocked) // Fill in some extra fields like frameBlitterP
  669.         {
  670.             SWUnlockFrame( tempFrameP );
  671.             SWLockFrame( tempFrameP );
  672.         }
  673.         *newFrameP = tempFrameP;
  674.     }
  675.     else
  676.     {
  677.         if (tempFrameP != NULL)
  678.         {
  679.             SWDisposeFrame( tempFrameP );
  680.         }
  681.         *newFrameP = NULL;
  682.     }
  683.     
  684.     SWSetStickyIfError( err );
  685.     return err;
  686. }
  687.     
  688.  
  689. ///--------------------------------------------------------------------------------------
  690. //    SWInitializeFrame
  691. ///--------------------------------------------------------------------------------------
  692.  
  693. SW_FUNC void SWInitializeFrame(
  694.     FramePtr tempFrameP,
  695.     short depth )
  696. {
  697.         long             numScanLines;
  698.         
  699.     SWAssert( tempFrameP != NULL && depth > 0 );
  700.     SWAssert( tempFrameP->framePort != NULL && tempFrameP->framePort->portPixMap != NULL );
  701.         
  702.         // cache frameRowBytes for use by our blitter routine
  703.  
  704.     tempFrameP->frameRowBytes = (**tempFrameP->framePort->portPixMap).rowBytes & 0x7FFF;
  705.  
  706.         // this calculation generates a mask value that we use to
  707.         // long word align the rectangle when we draw the frame.
  708.         // note that the expression "sizeof(long) * kBitsPerByte" gives us
  709.         // the number of bits in a long.
  710.  
  711.     tempFrameP->rightAlignFactor = ((sizeof(long) * kBitsPerByte) / depth) - 1;
  712.     tempFrameP->leftAlignFactor = ~(tempFrameP->rightAlignFactor);
  713.  
  714.         // the useCount keeps track of the number of sprites that
  715.         // are using this frame. we need to know this so we don't
  716.         // dispose this frame twice when we are disposing the 
  717.         // sprites that use it.
  718.     tempFrameP->useCount = 0;
  719.     
  720.     tempFrameP->worldRectOffset = 0;
  721.     
  722.         // here we set up an array of offsets to the scan lines of
  723.         // this frame. this allows us to address a particular scan line
  724.         // without doing a costly multiply.
  725.     tempFrameP->scanLinePtrArray = (unsigned long*)(tempFrameP + 1);
  726.     
  727.     for (numScanLines = 0; numScanLines < tempFrameP->numScanLines; numScanLines++)
  728.     {
  729.         tempFrameP->scanLinePtrArray[numScanLines] = 
  730.             ((tempFrameP->frameRect.top+numScanLines) * tempFrameP->frameRowBytes);
  731.     }
  732. }
  733.  
  734.  
  735. ///--------------------------------------------------------------------------------------
  736. //    SWDisposeFrame
  737. ///--------------------------------------------------------------------------------------
  738. SW_FUNC Boolean SWDisposeFrame(
  739.     FramePtr oldFrameP)
  740. {
  741.     Boolean        frameDisposed;
  742.     
  743.     SWAssert( oldFrameP != NULL );
  744.     
  745.     frameDisposed = false;
  746.     if (oldFrameP != NULL)
  747.     {
  748.             // is this frame still in use by another sprite?
  749.         if (oldFrameP->useCount > 1)
  750.         {
  751.                 // one less sprite is using it now!
  752.             oldFrameP->useCount--;
  753.         }
  754.         else    // no more sprites are using this frame
  755.         {    
  756.             frameDisposed = true;
  757.             if (oldFrameP->framePort != NULL)
  758.             {
  759.                 if ( !oldFrameP->sharesGWorld )
  760.                     DisposeGWorld(oldFrameP->framePort);
  761.                 oldFrameP->framePort = NULL;
  762.             }
  763.             
  764.             if (oldFrameP->maskRgn != NULL)
  765.             {
  766.                 DisposeRgn(oldFrameP->maskRgn);
  767.                 oldFrameP->maskRgn = NULL;
  768.             }
  769.             
  770.             if (oldFrameP->maskPort != NULL)
  771.             {
  772.                 if ( !oldFrameP->sharesGWorld )
  773.                     DisposeGWorld(oldFrameP->maskPort);
  774.                 oldFrameP->maskPort = NULL;
  775.             }
  776.  
  777.             if (oldFrameP->pixCodeH != NULL)
  778.             {
  779.                 DisposeHandle((Handle)oldFrameP->pixCodeH);
  780.                 oldFrameP->pixCodeH = NULL;
  781.                 oldFrameP->frameBlitterP = NULL;
  782.             }
  783.  
  784.             DisposePtr((Ptr)oldFrameP);
  785.             oldFrameP = NULL;
  786.         }
  787.     }
  788.     return frameDisposed;
  789. }
  790.  
  791.  
  792. ///--------------------------------------------------------------------------------------
  793. //    SWSetFrameMaskRgn
  794. ///--------------------------------------------------------------------------------------
  795.  
  796. SW_FUNC void SWSetFrameMaskRgn(
  797.     FramePtr srcFrameP,
  798.     RgnHandle maskRgn)
  799. {
  800.     SWAssert( srcFrameP != NULL && maskRgn != NULL );
  801.  
  802.     srcFrameP->maskRgn = maskRgn;
  803.  
  804.     srcFrameP->offsetPoint.h = (**maskRgn).rgnBBox.left;
  805.     srcFrameP->offsetPoint.v = (**maskRgn).rgnBBox.top;
  806. }
  807.  
  808.  
  809. ///--------------------------------------------------------------------------------------
  810. //    SWLockFrame
  811. ///--------------------------------------------------------------------------------------
  812.  
  813. SW_FUNC void SWLockFrame(
  814.     FramePtr srcFrameP)
  815. {
  816.     PixMapHandle         pixMapH;
  817.  
  818.     SWAssert( srcFrameP != NULL );
  819.  
  820.     if ( !srcFrameP->isFrameLocked ) {
  821.         srcFrameP->isFrameLocked = true;
  822.         
  823.         pixMapH = GetGWorldPixMap( srcFrameP->framePort );
  824.         (void)LockPixels( pixMapH );
  825.         HLockHi( (Handle)pixMapH );
  826.         srcFrameP->framePixHndl = pixMapH;
  827.         srcFrameP->framePix = (PixMapPtr)StripAddress( *(Handle)pixMapH );
  828.         srcFrameP->frameBaseAddr = GetPixBaseAddr( pixMapH );
  829.         srcFrameP->frameRowBytes = (**pixMapH).rowBytes & 0x7FFF;
  830.         
  831.         if (srcFrameP->pixCodeH != NULL)
  832.         {
  833.             HLockHi((Handle)srcFrameP->pixCodeH);
  834.             srcFrameP->frameBlitterP = (BlitFuncPtr)StripAddress( *srcFrameP->pixCodeH );
  835.         }
  836.  
  837.         if (srcFrameP->maskPort != NULL)
  838.         {
  839.             pixMapH = GetGWorldPixMap( srcFrameP->maskPort );
  840.             (void)LockPixels( pixMapH );
  841.             HLockHi( (Handle)pixMapH );
  842.             srcFrameP->maskPixHndl = pixMapH;
  843.             srcFrameP->maskPix = (PixMapPtr)StripAddress( *(Handle)pixMapH );
  844.             srcFrameP->maskBaseAddr = GetPixBaseAddr( pixMapH );
  845.         }
  846.     }
  847. }
  848.  
  849.  
  850. ///--------------------------------------------------------------------------------------
  851. //    SWUnlockFrame
  852. ///--------------------------------------------------------------------------------------
  853.  
  854. SW_FUNC void SWUnlockFrame(
  855.     FramePtr srcFrameP)
  856. {
  857.     SWAssert( srcFrameP != NULL );
  858.     
  859.     if ( srcFrameP->isFrameLocked ) {
  860.         srcFrameP->isFrameLocked = false;
  861.         
  862.         if (srcFrameP->framePort != NULL)
  863.         {
  864.             HUnlock( (Handle)srcFrameP->framePixHndl );
  865.             UnlockPixels(srcFrameP->framePixHndl);
  866.         }
  867.  
  868.         if (srcFrameP->maskPort != NULL)
  869.         {
  870.             HUnlock( (Handle)srcFrameP->maskPixHndl );
  871.             UnlockPixels(srcFrameP->maskPixHndl);
  872.         }
  873.  
  874.         srcFrameP->framePix = NULL;
  875.         srcFrameP->frameBaseAddr = NULL;
  876.         srcFrameP->maskPix = NULL;
  877.         srcFrameP->maskBaseAddr = NULL;
  878.         
  879.         if (srcFrameP->pixCodeH != NULL)
  880.         {
  881.             HUnlock((Handle)srcFrameP->pixCodeH);
  882.             srcFrameP->frameBlitterP = NULL;
  883.         }
  884.     }
  885. }
  886.