home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-06-19 | 15.6 KB | 666 lines | [TEXT/KAHL] |
- ///--------------------------------------------------------------------------------------
- // SpriteTest.c
- //
- // Created: 8/14/91 at 1:53 AM
- // By: Tony Myles
- //
- // Copyright © 1992-93 Tony Myles, All rights reserved worldwide.
- ///--------------------------------------------------------------------------------------
-
-
- #if THINK_C
- #ifndef __BDC__
- #include <BDC.h>
- #endif
- #else
- #ifndef __PACKAGES__
- #include <Packages.h>
- #endif
- #endif
-
- #ifndef __DIALOGS__
- #include <Dialogs.h>
- #endif
-
- #ifndef __RESOURCES__
- #include <Resources.h>
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __EVENTS__
- #include <Events.h>
- #endif
-
- #ifndef __OSEVENTS__
- #include <OSEvents.h>
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifndef __SEGLOAD__
- #include <SegLoad.h>
- #endif
-
- #ifndef __SPRITEWORLD__
- #include <SpriteWorld.h>
- #endif
-
- #ifndef __SPRITELAYER__
- #include <SpriteLayer.h>
- #endif
-
- #ifndef __SPRITE__
- #include <Sprite.h>
- #endif
-
- #ifndef __FRAME__
- #include <Frame.h>
- #endif
-
- #ifndef __SPRITEWORLDUTILS__
- #include <SpriteWorldUtils.h>
- #endif
-
- #include <GameUtils.h>
- #include <DebugUtils.h>
-
- #include <BlitPixie.h>
- #include "SpriteTest.h"
-
-
- RgnHandle gWorkRgn = NULL;
-
-
- ///--------------------------------------------------------------------------------------
- // CreateSpriteTest
- ///--------------------------------------------------------------------------------------
-
- OSErr CreateSpriteTest(
- SpriteTestPtr* spriteTestP,
- CWindowPtr srcWindowP)
- {
- OSErr err = noErr;
- SpriteTestPtr tempSpriteTestP;
- SpriteWorldPtr spriteWorldP;
- SpriteLayerPtr spriteLayerP;
- SpritePtr testSpriteArray[kNumberOfTestSprites];
- SpritePtr titleSpriteP;
- FramePtr titleFrameP;
- PixPatHandle pixPatH;
- long spriteNum;
- short oldResRefNum, curResRefNum;
- Rect worldRect;
-
- *spriteTestP = NULL;
-
- gWorkRgn = NewRgn();
-
- SetPort((GrafPtr)srcWindowP);
- worldRect = srcWindowP->portRect;
-
- tempSpriteTestP = (SpriteTestPtr)NewPtrClear((Size)sizeof(SpriteTestRec));
-
- if (tempSpriteTestP == NULL)
- {
- err = MemError();
- }
-
- if (err == noErr)
- {
- err = SWEnterSpriteWorld();
-
- }
-
- if (err == noErr)
- {
- // create the sprite world
- err = SWCreateSpriteWorldFromWindow(&spriteWorldP, srcWindowP, NULL);
- }
-
- if (err == noErr)
- {
- tempSpriteTestP->spriteWorldP = spriteWorldP;
-
- // create the sprite layer
- err = SWCreateSpriteLayer(&spriteLayerP);
- }
-
- if (err == noErr)
- {
- oldResRefNum = CurResFile();
- curResRefNum = OpenResFile("\pSpriteTest Frames");
- err = ResError();
- }
-
- if (err == noErr)
- {
- UseResFile(curResRefNum);
-
- tempSpriteTestP->spriteLayerP = spriteLayerP;
-
- err = SWCreateSpriteFromCIconResource(testSpriteArray, NULL, kTestCIconID, kNumberOfTestFrames, kFatMask);
-
- UseResFile(oldResRefNum);
- CloseResFile(curResRefNum);
- }
-
- if (err == noErr)
- {
- tempSpriteTestP->testSpriteArray[0] = testSpriteArray[0];
-
- // create our sprites
- for (spriteNum = 1; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- err = SWCloneSprite(testSpriteArray[0], testSpriteArray + spriteNum, NULL);
-
- if (err == noErr)
- {
- tempSpriteTestP->testSpriteArray[spriteNum] = testSpriteArray[spriteNum];
- }
- else
- {
- break;
- }
- }
- }
-
- if (err == noErr)
- {
- err = SWCreateSprite(&titleSpriteP, NULL, 1);
- }
-
- if (err == noErr)
- {
- tempSpriteTestP->titleSpriteP = titleSpriteP;
-
- // create a frame for our sprite
- err = SWCreateFrameFromPictResource(&titleFrameP, 128, 129, kFatMask);
- }
-
- if (err == noErr)
- {
- tempSpriteTestP->titleFrameP = titleFrameP;
- *spriteTestP = tempSpriteTestP;
-
- SWAddFrame(titleSpriteP, titleFrameP);
-
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- if (spriteNum == (kNumberOfTestSprites / 2))
- {
- SWAddSprite(spriteLayerP, titleSpriteP);
- }
-
- // add the sprite in the layer
- SWAddSprite(spriteLayerP, testSpriteArray[spriteNum]);
- }
-
- // add the layer to the world
- SWAddSpriteLayer(spriteWorldP, spriteLayerP);
-
- SWLockSpriteWorld(spriteWorldP);
-
- SetupSpriteTest(tempSpriteTestP);
-
- SWSetPortToBackGround(spriteWorldP);
-
- if (SWHasColorQuickDraw() && ((**srcWindowP->portPixMap).pixelSize > 1))
- {
- // fill the sprite world with a pretty pattern
- pixPatH = GetPixPat(kBackDropPixPatID);
-
- if (pixPatH != NULL)
- {
- FillCRect(&worldRect, pixPatH);
- DisposePixPat(pixPatH);
- }
- else
- {
- FillRect(&worldRect, qd.ltGray);
- }
- }
- else
- {
- FillRect(&worldRect, qd.ltGray);
- }
-
- SetPort((GrafPtr)srcWindowP);
- }
-
- if (err != noErr)
- {
- DisposeSpriteTest(tempSpriteTestP);
- }
-
- return err;
- }
-
-
- void DisposeSpriteTest(
- SpriteTestPtr spriteTestP)
- {
- long spriteNum;
-
- if (gWorkRgn != NULL)
- DisposeRgn(gWorkRgn);
-
- if (spriteTestP != NULL)
- {
- if (spriteTestP->spriteWorldP != NULL)
- {
- SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
- }
-
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- if (spriteTestP->testSpriteArray[spriteNum] != NULL)
- {
- SWDisposeSprite(spriteTestP->testSpriteArray[spriteNum], spriteNum == 0);
- }
- }
-
- if (spriteTestP->titleFrameP != NULL)
- {
- SWDisposeFrame(spriteTestP->titleFrameP);
- }
-
- if (spriteTestP->titleSpriteP != NULL)
- {
- SWDisposeSprite(spriteTestP->titleSpriteP, false);
- }
-
- if (spriteTestP->spriteLayerP != NULL)
- {
- SWDisposeSpriteLayer(spriteTestP->spriteLayerP);
- }
-
- if (spriteTestP->spriteWorldP != NULL)
- {
- SWDisposeSpriteWorld(spriteTestP->spriteWorldP);
- }
-
- DisposePtr((Ptr)spriteTestP);
- }
-
- SWExitSpriteWorld();
- }
-
-
- void SetupSpriteTest(
- SpriteTestPtr spriteTestP)
- {
- register long spriteNum;
- register SpritePtr testSpriteP;
- short horizMoveDelta;
- short vertMoveDelta;
- Rect moveBoundsRect;
-
- // set up the sprites
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- testSpriteP = spriteTestP->testSpriteArray[spriteNum];
-
- horizMoveDelta = GetRandom(2, 6);
- vertMoveDelta = GetRandom(2, 6);
-
- // calculate the movement boundary rectangle
- moveBoundsRect = spriteTestP->spriteWorldP->windowFrameP->frameRect;
- //InsetRect(&moveBoundsRect, horizMoveDelta / 2, vertMoveDelta / 2);
-
- if (GetRandom(0, 1) == 1)
- {
- horizMoveDelta = -horizMoveDelta;
- }
-
- if (GetRandom(0, 1) == 1)
- {
- vertMoveDelta = -vertMoveDelta;
- }
-
- // set the sprite’s movement characteristics
- SWSetSpriteMoveBounds(testSpriteP, &moveBoundsRect);
- SWSetSpriteMoveDelta(testSpriteP, horizMoveDelta, vertMoveDelta);
- SWSetSpriteMoveProc(testSpriteP, SWBounceSpriteMoveProc);
- SWSetSpriteMoveTime(testSpriteP, kTestSpriteMoveTime);
-
- SWSetSpriteFrameTime(testSpriteP, kTestSpriteFrameTime);
- SWSetSpriteFrameRange(testSpriteP, 0, kNumberOfTestFrames - 1);
- SWSetSpriteFrameAdvance(testSpriteP, GetRandom(0, 1) ? -1 : 1);
-
- SWSetSpriteCollideProc(testSpriteP, SWBounceSpriteCollideProc);
-
- // set the sprite’s initial location
- SWSetSpriteLocation(testSpriteP,
- GetRandom(0, moveBoundsRect.right),
- GetRandom(0, moveBoundsRect.bottom));
- }
-
- moveBoundsRect = spriteTestP->spriteWorldP->windowFrameP->frameRect;
- SWSetSpriteLocation(spriteTestP->titleSpriteP,
- (moveBoundsRect.right / 2) - (spriteTestP->titleFrameP->frameRect.right / 2),
- (moveBoundsRect.bottom / 2) - (spriteTestP->titleFrameP->frameRect.bottom / 2));
- SWSetSpriteMoveTime(spriteTestP->titleSpriteP, -1);
-
- spriteTestP->isCommandActive[kSpriteTestTitleCommand] = true;
- spriteTestP->isCommandActive[kBouncingBallsCommand] = true;
- spriteTestP->isCommandActive[kCollisionDetectionCommand] = false;
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SWBounceSpriteCollideProc
- ///--------------------------------------------------------------------------------------
-
- void SWBounceSpriteCollideProc(
- SpritePtr srcSpriteP,
- SpritePtr dstSpriteP,
- Rect* sectRect)
- {
- register short tempDelta;
- Rect rgnRect;
-
- // If both sprites use the same collision routine (this one),ignore the second collision.
- if ((!srcSpriteP->isVisible || !dstSpriteP->isVisible) ||
- ((srcSpriteP->spriteCollideProc == dstSpriteP->spriteCollideProc) &&
- (srcSpriteP > dstSpriteP)))
- return;
-
- rgnRect = (**srcSpriteP->curFrameP->maskRgn).rgnBBox;
-
- // move the mask region to the new sprite location
- OffsetRgn(srcSpriteP->curFrameP->maskRgn,
- (srcSpriteP->destFrameRect.left - rgnRect.left) +
- srcSpriteP->curFrameP->offsetPoint.h,
- (srcSpriteP->destFrameRect.top - rgnRect.top) +
- srcSpriteP->curFrameP->offsetPoint.v);
-
- rgnRect = (**dstSpriteP->curFrameP->maskRgn).rgnBBox;
-
- // move the mask region to the new sprite location
- OffsetRgn(dstSpriteP->curFrameP->maskRgn,
- (dstSpriteP->destFrameRect.left - rgnRect.left) +
- dstSpriteP->curFrameP->offsetPoint.h,
- (dstSpriteP->destFrameRect.top - rgnRect.top) +
- dstSpriteP->curFrameP->offsetPoint.v);
-
- SectRgn(srcSpriteP->curFrameP->maskRgn, dstSpriteP->curFrameP->maskRgn, gWorkRgn);
-
- if (!EmptyRgn(gWorkRgn))
- {
- if ((dstSpriteP->horizMoveDelta == 0) && (dstSpriteP->vertMoveDelta == 0))
- {
- if (!((srcSpriteP->destFrameRect.left == sectRect->left) &&
- (srcSpriteP->destFrameRect.right == sectRect->right)))
- {
- srcSpriteP->horizMoveDelta = -srcSpriteP->horizMoveDelta;
-
- SWOffsetSprite(srcSpriteP, srcSpriteP->horizMoveDelta, 0);
- }
-
- if (!((srcSpriteP->destFrameRect.top == sectRect->top) &&
- (srcSpriteP->destFrameRect.bottom == sectRect->bottom)))
- {
- srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
-
- SWOffsetSprite(srcSpriteP, 0, srcSpriteP->vertMoveDelta);
- }
-
- return;
- }
-
- // swap movement delta's
- if (srcSpriteP < dstSpriteP) // Only one time!
- {
- // reverse spins.
- srcSpriteP->frameAdvance = -srcSpriteP->frameAdvance;
- dstSpriteP->frameAdvance = -dstSpriteP->frameAdvance;
-
- // swap h/v delta's
- tempDelta = srcSpriteP->horizMoveDelta;
- srcSpriteP->horizMoveDelta = dstSpriteP->horizMoveDelta;
- dstSpriteP->horizMoveDelta = tempDelta;
-
- tempDelta = srcSpriteP->vertMoveDelta;
- srcSpriteP->vertMoveDelta = dstSpriteP->vertMoveDelta;
- dstSpriteP->vertMoveDelta = tempDelta;
- }
- }
- }
-
-
- void RunSpriteTest(
- SpriteTestPtr spriteTestP)
- {
- SWLockSpriteWorld(spriteTestP->spriteWorldP);
-
- if (spriteTestP->isCommandActive[kCollisionDetectionCommand])
- {
- SWCollideSpriteLayer(spriteTestP->spriteLayerP, spriteTestP->spriteLayerP);
- }
-
- SWProcessSpriteWorld(spriteTestP->spriteWorldP);
- SWAnimateSpriteWorld(spriteTestP->spriteWorldP);
-
- SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
- }
-
-
- void UpdateSpriteTest(
- SpriteTestPtr spriteTestP)
- {
- SWLockSpriteWorld(spriteTestP->spriteWorldP);
- SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
- SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
- }
-
-
- void HandleSpriteTestTitleCommand(
- SpriteTestPtr spriteTestP)
- {
- spriteTestP->isCommandActive[kSpriteTestTitleCommand] =
- !spriteTestP->isCommandActive[kSpriteTestTitleCommand];
-
- if (spriteTestP->isCommandActive[kSpriteTestTitleCommand])
- {
- //SWAddSprite(spriteTestP->spriteLayerP, spriteTestP->titleSpriteP);
- SWSetSpriteVisible(spriteTestP->titleSpriteP, true);
- }
- else
- {
- // erase the sprite (theres got to be an easier way!)
- SWSetSpriteVisible(spriteTestP->titleSpriteP, false);
- //RunSpriteTest(spriteTestP);
-
- // remove the sprite
- //SWRemoveSprite(spriteTestP->spriteLayerP, spriteTestP->titleSpriteP);
- }
- }
-
-
- void HandleBouncingBallsCommand(
- SpriteTestPtr spriteTestP)
- {
- SpritePtr ballSpriteP;
- Boolean isCommandActive;
- long ballNum;
-
- isCommandActive = spriteTestP->isCommandActive[kBouncingBallsCommand] =
- !spriteTestP->isCommandActive[kBouncingBallsCommand];
-
- for (ballNum = 0; ballNum < kNumberOfTestSprites; ballNum++)
- {
- ballSpriteP = spriteTestP->testSpriteArray[ballNum];
-
- SWSetSpriteVisible(ballSpriteP, isCommandActive);
- }
- }
-
-
- void CopyBitsSpeedTestCommand(
- SpriteTestPtr spriteTestP)
- {
- WindowPtr testWindowP = FrontWindow();
- unsigned long frames, seconds, spriteNum;
- long ticks;
-
- HideCursor();
- HideMenuBar(testWindowP);
-
- SWLockSpriteWorld(spriteTestP->spriteWorldP);
- SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
-
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- SWSetSpriteMoveTime(spriteTestP->testSpriteArray[spriteNum], 0);
- SWSetSpriteFrameTime(spriteTestP->testSpriteArray[spriteNum], 0);
- }
-
- ticks = TickCount();
-
- for (frames = 0; ((TickCount() - ticks) < kTestTime) && (!Button()); frames++)
- {
- SWProcessSpriteWorld(spriteTestP->spriteWorldP);
- SWAnimateSpriteWorld(spriteTestP->spriteWorldP);
- }
-
- seconds = ((TickCount() - ticks) / 60);
-
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- SWSetSpriteMoveTime(spriteTestP->testSpriteArray[spriteNum], kTestSpriteMoveTime);
- SWSetSpriteFrameTime(spriteTestP->testSpriteArray[spriteNum], kTestSpriteFrameTime);
- }
-
- SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
-
- ShowCursor();
- ShowMenuBar(testWindowP);
-
- DisplayPerformance(frames, seconds);
- }
-
-
- void BlitPixieSpeedTestCommand(
- SpriteTestPtr spriteTestP)
- {
- WindowPtr testWindowP = FrontWindow();
- unsigned long frames, seconds, spriteNum;
- long ticks;
-
- HideCursor();
- HideMenuBar(testWindowP);
-
- SWLockSpriteWorld(spriteTestP->spriteWorldP);
- SWUpdateSpriteWorld(spriteTestP->spriteWorldP);
-
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- SWSetSpriteMoveTime(spriteTestP->testSpriteArray[spriteNum], 0);
- SWSetSpriteFrameTime(spriteTestP->testSpriteArray[spriteNum], 0);
- SWSetSpriteDrawProc(spriteTestP->testSpriteArray[spriteNum], BlitPixieMaskDrawProc);
- }
-
- SWSetSpriteWorldEraseProc(spriteTestP->spriteWorldP, BlitPixieEraseProc);
- SWSetSpriteWorldDrawProc(spriteTestP->spriteWorldP, BlitPixieDrawProc);
-
- ticks = TickCount();
-
- for (frames = 0; ((TickCount() - ticks) < kTestTime) && (!Button()); frames++)
- {
- SWProcessSpriteWorld(spriteTestP->spriteWorldP);
- SWAnimateSpriteWorld(spriteTestP->spriteWorldP);
- }
-
- seconds = ((TickCount() - ticks) / 60);
-
- for (spriteNum = 0; spriteNum < kNumberOfTestSprites; spriteNum++)
- {
- SWSetSpriteMoveTime(spriteTestP->testSpriteArray[spriteNum], kTestSpriteMoveTime);
- SWSetSpriteFrameTime(spriteTestP->testSpriteArray[spriteNum], kTestSpriteFrameTime);
- SWSetSpriteDrawProc(spriteTestP->testSpriteArray[spriteNum], SWStdDrawProc);
- }
-
- SWSetSpriteWorldEraseProc(spriteTestP->spriteWorldP, SWStdDrawProc);
- SWSetSpriteWorldDrawProc(spriteTestP->spriteWorldP, SWStdMaskDrawProc);
-
- SWUnlockSpriteWorld(spriteTestP->spriteWorldP);
-
- ShowCursor();
- ShowMenuBar(testWindowP);
-
- DisplayPerformance(frames, seconds);
- }
-
-
- void SpriteTestCommand(
- SpriteTestPtr spriteTestP,
- CWindowPtr srcWindowP)
- {
- short x, i;
-
- SetPort((GrafPtr)srcWindowP);
- HideMenuBar((WindowPtr)srcWindowP);
-
- for (x = 0; (x < 1000) && !Button(); x++)
- {
- FillRect(&srcWindowP->portRect, qd.black);
- DisposeSpriteTest(spriteTestP);
- CreateSpriteTest(&spriteTestP, srcWindowP);
- SetupSpriteTest(spriteTestP);
- UpdateSpriteTest(spriteTestP);
-
- for (i = 0; (i < 100) && !Button(); i++)
- {
- SystemTask();
- RunSpriteTest(spriteTestP);
- }
- }
-
- ShowMenuBar((WindowPtr)srcWindowP);
- }
-
-
- void AdjustSpriteTestMenu(
- SpriteTestPtr spriteTestP,
- MenuHandle spriteTestMenuH)
- {
- short commandNumber;
-
- for (commandNumber = 1; commandNumber <= kNumberOfCommands; commandNumber++)
- {
- CheckItem(spriteTestMenuH, commandNumber, spriteTestP->isCommandActive[commandNumber - 1]);
- }
-
- // disable the BlitPixie test if we, are not in 8 bit
- if ((!SWHasColorQuickDraw()) ||
- ((**spriteTestP->spriteWorldP->backFrameP->framePort.colorGrafP->portPixMap).pixelSize != 8))
- {
- DisableItem(spriteTestMenuH, kBlitPixieTestCommand + 1);
- }
- }
-
-
- void DisplayPerformance(
- long frames,
- long seconds)
- {
- Str255 framesString, secondsString, fpsString;
- long fps;
-
- NumToString(frames, framesString);
- NumToString(seconds, secondsString);
-
- fps = (seconds > 0) ? frames / seconds : frames;
- NumToString(fps, fpsString);
-
- ParamText(framesString, secondsString, fpsString, "\p");
- NoteAlert(kPerformanceAlertID, NULL);
- }
-
-
-