home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 / SpriteWorld Examples / SpriteTest / About.c next >
Encoding:
Text File  |  1998-12-19  |  8.0 KB  |  334 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. //    About.c
  3. //
  4. //    By:        Tony Myles
  5. //
  6. //    Copyright: © 1993-94 Tony Myles, All rights reserved worldwide.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <SWIncludes.h>            // Automatically include all SpriteWorld.h files
  11. #include <SWApplication.h>    
  12.  
  13. #ifndef __DIALOGUTILS__
  14. #include <SWDialogUtils.h>
  15. #endif
  16.  
  17. #ifndef __SPRITETEST__
  18. #include "SpriteTest.h"
  19. #endif
  20.  
  21. #ifndef __APPLICATION__
  22. #include "Application.h"
  23. #endif
  24.  
  25. #ifndef __ABOUT__
  26. #include "About.h"
  27. #endif
  28.  
  29.  
  30. extern SpriteTestPtr gSpriteTestP;
  31.  
  32.  
  33. ///--------------------------------------------------------------------------------------
  34. // DisplayAboutBox
  35. ///--------------------------------------------------------------------------------------
  36.  
  37. void DisplayAboutBox(void)
  38. {
  39.     OSErr                 err;
  40.     GrafPtr             savePort;
  41.     DialogPtr             aboutDialogP;
  42.     SpriteWorldPtr         spriteWorldP = NULL;
  43.     SpriteLayerPtr         spriteLayerP = NULL;
  44.     SpritePtr             earthSpriteP = NULL; 
  45.     SpritePtr            shadowSpriteP = NULL;
  46.     Rect                 itemRect, moveBoundsRect;
  47.     short                 itemHit, itemType;
  48.     Handle                itemHandle;
  49.     ModalFilterUPP        spriteWorldDialogFilter;
  50.     UserItemUPP            procForVersUserItem;
  51.  
  52.     aboutDialogP = GetNewDialog(kAboutDialogID, NULL, (WindowPtr)-1L);
  53.     
  54.     procForVersUserItem = NewUserItemProc(DrawVersionProc);
  55.     GetDialogItem(aboutDialogP, 6, &itemType, &itemHandle, &itemRect);
  56.     SetDialogItem(aboutDialogP, 6, itemType, (Handle)procForVersUserItem, &itemRect);
  57.     
  58.     if (aboutDialogP != NULL)
  59.     {
  60.         GetPort(&savePort);
  61.         SetPort(aboutDialogP);
  62.  
  63.             // create the sprite world
  64.         err = SWCreateSpriteWorldFromWindow(&spriteWorldP, (CWindowPtr)aboutDialogP, NULL, NULL, 0);
  65.     
  66.         if (err == noErr)
  67.         {    
  68.                 // create the sprite layer
  69.             err = SWCreateSpriteLayer(&spriteLayerP);
  70.         }
  71.  
  72.         if (err == noErr)
  73.         {
  74.             err = SWCreateSpriteFromCicnResource(
  75.                 spriteWorldP,
  76.                 &earthSpriteP, 
  77.                 NULL, 
  78.                 kEarthCIconID, 
  79.                 1, 
  80.                 kRegionMask);
  81.         }
  82.  
  83.         if (err == noErr)
  84.         {
  85.             err = SWCreateSpriteFromCicnResource(
  86.                 spriteWorldP, 
  87.                 &shadowSpriteP, 
  88.                 NULL, 
  89.                 kShadowCIconID,
  90.                 kNumberOfShadowFrames, 
  91.                 kRegionMask);
  92.         }
  93.  
  94.         if (err == noErr)
  95.         {
  96.             shadowSpriteP->userData = (long)earthSpriteP;
  97.  
  98.             SWAddSprite(spriteLayerP, shadowSpriteP);
  99.             SWAddSprite(spriteLayerP, earthSpriteP);
  100.             SWAddSpriteLayer(spriteWorldP, spriteLayerP);
  101.  
  102.             GetDItemRect(aboutDialogP, 4, &itemRect);
  103.             moveBoundsRect = aboutDialogP->portRect;
  104.             moveBoundsRect.bottom = itemRect.top + ((itemRect.bottom-itemRect.top)/2);
  105.  
  106.                 // set the sprite’s movement characteristics
  107.             SWSetSpriteMoveBounds(earthSpriteP, &moveBoundsRect);
  108.             SWSetSpriteMoveDelta(earthSpriteP, 0, kInitialSpeed);
  109.             SWSetSpriteMoveProc(earthSpriteP, EarthMoveProc);
  110.             SWSetSpriteMoveTime(earthSpriteP, 55);
  111.             SWSetSpriteMoveTime(shadowSpriteP, -1);        // never move
  112.             SWSetSpriteFrameTime(shadowSpriteP, 0);
  113.             SWSetSpriteFrameRange(shadowSpriteP, 0, 4);
  114.             SWSetSpriteFrameProc(shadowSpriteP, ShadowFrameProc);
  115.             SWSetCurrentFrameIndex(shadowSpriteP, 2);
  116.  
  117.                 // set the sprite’s initial location
  118.             SWSetSpriteLocation(shadowSpriteP, itemRect.left, itemRect.top);
  119.             GetDItemRect(aboutDialogP, 5, &itemRect);
  120.             SWSetSpriteLocation(earthSpriteP, itemRect.left, itemRect.top);
  121.  
  122.             ((WindowPeek)aboutDialogP)->refCon = (long)spriteWorldP;
  123.  
  124.             ShowWindow(aboutDialogP);
  125.             SetPort(aboutDialogP);
  126.             DrawDialog(aboutDialogP);
  127.             EraseRect(&itemRect);
  128.             OutlineDefaultButton(aboutDialogP, ok);
  129.  
  130.             SWLockSpriteWorld(spriteWorldP);
  131.  
  132.                 // Copy contents of dialog to SpriteWorld's backFrame
  133.             CopyBits(&aboutDialogP->portBits,
  134.                         (BitMap*)spriteWorldP->backFrameP->framePix,
  135.                         &aboutDialogP->portRect,
  136.                         &aboutDialogP->portRect,
  137.                         srcCopy, NULL);
  138.  
  139.  
  140.             spriteWorldDialogFilter = NewModalFilterProc(AboutDialogFilter);
  141.  
  142.             do
  143.             {
  144.                 ModalDialog(spriteWorldDialogFilter, &itemHit);
  145.             } while (itemHit != ok);
  146.  
  147.             DisposeRoutineDescriptor(spriteWorldDialogFilter);
  148.             DisposeRoutineDescriptor(procForVersUserItem);
  149.             SWUnlockSpriteWorld(spriteWorldP);
  150.         }
  151.  
  152.         DisposeDialog(aboutDialogP);
  153.         SWDisposeSpriteWorld(&spriteWorldP);
  154.  
  155.         SetPort(savePort);
  156.  
  157.         if (err != noErr)
  158.         {
  159.             FatalError(err);
  160.         }
  161.     }
  162.     else
  163.     {
  164.         CantFindResource();
  165.     }
  166. }
  167.  
  168.  
  169. ///--------------------------------------------------------------------------------------
  170. // EarthMoveProc
  171. ///--------------------------------------------------------------------------------------
  172.  
  173. SW_FUNC void EarthMoveProc(
  174.     SpritePtr srcSpriteP)
  175. {
  176.         // gravity
  177.     if (srcSpriteP->vertMoveDelta < (short)kMaxSpeed)
  178.         srcSpriteP->vertMoveDelta++;
  179.  
  180.     SWOffsetSprite( srcSpriteP, 0, srcSpriteP->vertMoveDelta );
  181.         // bounce
  182.     if (srcSpriteP->destFrameRect.bottom > srcSpriteP->moveBoundsRect.bottom)
  183.     {
  184.         srcSpriteP->vertMoveDelta = -srcSpriteP->vertMoveDelta;
  185.         
  186.         SWMoveSprite( srcSpriteP, srcSpriteP->destFrameRect.left, 
  187.             srcSpriteP->moveBoundsRect.bottom -
  188.             (srcSpriteP->curFrameP->frameRect.bottom - 
  189.             srcSpriteP->curFrameP->frameRect.top));
  190.     }
  191. }
  192.  
  193.  
  194. ///--------------------------------------------------------------------------------------
  195. // ShadowFrameProc
  196. ///--------------------------------------------------------------------------------------
  197.  
  198. SW_FUNC void ShadowFrameProc(
  199.     SpritePtr srcSpriteP,
  200.     FramePtr curFrameP,
  201.     long* curFrameIndex)
  202. {
  203.     #pragma unused(curFrameP)
  204.     SpritePtr earthSpriteP = (SpritePtr)srcSpriteP->userData;
  205.     short distanceFromTop = earthSpriteP->destFrameRect.top - srcSpriteP->moveBoundsRect.top;
  206.     short height = earthSpriteP->moveBoundsRect.bottom - earthSpriteP->moveBoundsRect.top;
  207.  
  208.     *curFrameIndex = (long)((distanceFromTop * 5) / height);
  209.  
  210.         // lets not index past the last frame!
  211.         // (even though SpriteWorld will not allow it to happen)
  212.     if (*curFrameIndex > 4L) *curFrameIndex = 4L;
  213. }
  214.  
  215.  
  216. ///--------------------------------------------------------------------------------------
  217. // AboutDialogFilter
  218. ///--------------------------------------------------------------------------------------
  219.  
  220. pascal Boolean AboutDialogFilter(
  221.     DialogPtr aboutDialogP,
  222.     EventRecord *event,
  223.     short *itemHit)
  224. {
  225.     Boolean eventHandled = false;
  226.     SpriteWorldPtr spriteWorldP = (SpriteWorldPtr)((WindowPeek)aboutDialogP)->refCon;
  227.  
  228.     switch (event->what)
  229.     {
  230.         case nullEvent:
  231.         {
  232.                 // run the about box animation
  233.             SWProcessSpriteWorld(spriteWorldP);
  234.             SWAnimateSpriteWorld(spriteWorldP);
  235.  
  236.                 // keep the title animation going!
  237.             SpriteTestIdle(gSpriteTestP);
  238.             break;
  239.         }
  240.  
  241.         case keyDown:
  242.         case autoKey:
  243.         {
  244.             char key = (char)(event->message & charCodeMask);
  245.  
  246.             if (key == kReturnChar || (key == kEnterChar))
  247.             {
  248.                 *itemHit = ok;
  249.                 eventHandled = true;
  250.                 ClickDialogButton(aboutDialogP, ok);
  251.             }
  252.  
  253.             break;
  254.         }
  255.  
  256.         case updateEvt:
  257.         {
  258.             if ((DialogPtr)event->message == aboutDialogP)
  259.             {
  260.                 SetPort(aboutDialogP);
  261.                 BeginUpdate(aboutDialogP);
  262.  
  263.                 SWUpdateSpriteWorld(spriteWorldP, true);
  264.                 
  265.                 EndUpdate(aboutDialogP);
  266.                 eventHandled = true;
  267.             }
  268.             else
  269.             {
  270.                 HandleUpdateEvent((WindowPtr)event->message);
  271.             }
  272.  
  273.             break;
  274.         }
  275.     }
  276.  
  277.     return eventHandled;
  278. }
  279.  
  280. /**********************************  DrawVersionProc  *******/
  281. pascal void DrawVersionProc (DialogPtr theDialog, short itemNo)
  282.  
  283. {
  284.     Rect            itemRect;
  285.     short            itemType;
  286.     Handle            itemHandle;
  287.     unsigned long    versNum;
  288.     short            majorNum;
  289.     short            betaNum;
  290.     char            versDigit1, versDigit2, versDigit3;
  291.     
  292.     
  293.     
  294.     GetDialogItem ( theDialog, itemNo, &itemType, &itemHandle, &itemRect );
  295.     
  296.     versNum = SWGetSpriteWorldVersion();
  297.     
  298.     majorNum = HiWord( versNum );
  299.     majorNum>>=8;
  300.     versDigit1 = majorNum | '0';
  301.     majorNum = (HiWord( versNum ))&0x00FF;
  302.     majorNum >>=4;
  303.     versDigit2 = majorNum | '0';
  304.     majorNum = (HiWord( versNum ))&0x000F;
  305.     versDigit3 = majorNum | '0';
  306.     
  307.     MoveTo( itemRect.left, itemRect.top + 9 );
  308.     TextFont(applFont);
  309.     TextSize(9);
  310.     DrawChar( versDigit1 );
  311.     DrawChar( '.' );
  312.     DrawChar( versDigit2 );
  313.     if ( versDigit3 > '0' )
  314.     {
  315.         DrawChar( '.' );
  316.         DrawChar( versDigit3 );
  317.     }
  318.  
  319.     betaNum = LoWord( versNum );
  320.     if ( betaNum )
  321.     {
  322.         ForeColor( blueColor );
  323.         DrawString( "\p beta" );
  324.         DrawChar( '0'+betaNum );
  325.         ForeColor( blackColor );
  326.     }
  327.     DrawString( "\p by" );
  328.     TextFace( 0 );
  329.     TextFont(systemFont);
  330.     TextSize(12);
  331. }
  332.  
  333.  
  334.