home *** CD-ROM | disk | FTP | other *** search
/ Mac Mania 2 / MacMania 2.toast / Demo's / Tools&Utilities / Programming / FBSpriteWorld 1.05b / Examples 1.01b / SWSimpleTest.main < prev    next >
Encoding:
Text File  |  1994-07-07  |  6.4 KB  |  207 lines  |  [TEXT/ZBAS]

  1. 'SWSimpleTest.main by Robert Hommel
  2. '© Copyright 1994
  3. 'All rights granted for any use whatsoever
  4.  
  5. 'SWSimpleTest is precisely that: a simple test of some basic
  6. 'FBSpriteWorld routines.  SWSimpleTest opens a window and creates a
  7. 'SpriteWorld based on that window's grafPort.  The SpriteWorld 
  8. 'contains a single Layer, and that Layer a single Sprite.  The Sprite
  9. 'contains one Frame, which is created from a PICT resource.
  10.  
  11. 'We use the standard "SWBounceMoveProc" to define the Sprite's move-
  12. 'ment.  Once we have everything set up, the main Animation Loop takes
  13. 'care of everything until we quit by clicking the mouse button.
  14.  
  15. 'Though 'simple,' this program shows all the basic steps needed to
  16. 'create your own animations.  Even the most complex animations will
  17. 'merely be elaborations on these basic steps.
  18.  
  19. 'Disclaimer:  I've tested these routines quite thoroughly on my Mac
  20. 'LC running System 7.01 and FB 1.02c.  I make no promises or warranties 
  21. 'of any kind.
  22. '*********************************************************************
  23.  
  24. COMPILE 0, _MacsBugLabels _caseInsensitive
  25. RESOURCES "FBSpriteWorld.RSRC"
  26.  
  27. '---------------------------- GLOBALS --------------------------------
  28.  
  29. GLOBALS "GraphicUtils.glbl"
  30. GLOBALS "FBSpriteWorld.glbl"
  31.  
  32. _backPatRSRC=128
  33. _spritePictRSRC=129
  34.  
  35. END GLOBALS
  36.  
  37. GOTO "Main"
  38.  
  39. '--------------------------- INCLUDES --------------------------------
  40.  
  41. INCLUDE "FBSpriteWorld.incl"
  42.  
  43. '------------------------- ERROR HANDLER -----------------------------
  44.  
  45. CLEAR LOCAL
  46. LOCAL FN FatalError(errCode)
  47.   'Simple error handler.  You'll want to improve on this in your
  48.   'program...
  49.   
  50.   LONG IF errCode<>_noErr
  51.     SELECT errCode
  52.       CASE _swTooManyLayers:errStr$="Out of Memory."
  53.       CASE _swTooManySprites:errStr$="Too many Sprites."
  54.       CASE _swTooManyFrames:errStr$="Too many Frames."
  55.       CASE _swNotSystemSeven:errStr$="SpriteWorld requires System 7!"
  56.       CASE _swTimeMgrNotPresent:errStr$="SpriteWorld requires Time Manager."
  57.       CASE _swOutOfMemory:errStr$="Out of Memory."
  58.       CASE ELSE
  59.         errStr$="Unknown error."
  60.     END SELECT
  61.     
  62.     tmp$="Error Code:"+STR$(errCode)
  63.     CALL PARAMTEXT(errStr$,tmp$,"","")
  64.     x=FN STOPALERT(1,0)
  65.     END
  66.   END IF
  67. END FN
  68.  
  69. '------------------------ SPRITEWORLD PROCS --------------------------
  70.  
  71. "SWBounceMoveProc"
  72. ENTERPROC(SWPtr&,spritePtr&,curRectPtr&)
  73.   'standard bounce movement proc.  Keeps sprite inside sprite boundsRect
  74.   
  75.   LONG IF curRectPtr&.left%+spritePtr&.xDelta%<=spritePtr&.sBoundsRect.left%
  76.     spritePtr&.xDelta%=spritePtr&.xDelta%*-1
  77.   XELSE
  78.     LONG IF curRectPtr&.right%+spritePtr&.xDelta%>=spritePtr&.sBoundsRect.right%
  79.       spritePtr&.xDelta%=spritePtr&.xDelta%*-1
  80.     END IF
  81.   END IF
  82.   LONG IF curRectPtr&.top%+spritePtr&.yDelta%<=spritePtr&.sBoundsRect.top%
  83.     spritePtr&.yDelta%=spritePtr&.yDelta%*-1
  84.   XELSE
  85.     LONG IF curRectPtr&.bottom%+spritePtr&.yDelta%>=spritePtr&.sBoundsRect.bottom%
  86.       spritePtr&.yDelta%=spritePtr&.yDelta%*-1
  87.     END IF
  88.   END IF
  89. EXITPROC
  90. RETURN
  91.  
  92. "SWTimeTask"
  93. 'Sets the frameTTHasFired or moveTTHasFired field of the sprite record
  94. 'to _zTrue (-1).  Called by the Time Manager if frameTimeInterval or
  95. 'moveTimeInterval field of sprite record > 0.
  96.  
  97. `                 move.w      #-1,tmXQSize(a1)  ;[move|frame]TTHasFired=_zTrue
  98. `                 rts                           ;return
  99.  
  100. '-------------------------- MAIN LOOP --------------------------------
  101.  
  102. "Main"
  103. DIM wRect.8
  104. DIM mySW.SpriteWorldRec
  105. DIM myLayer.SWLayerRec
  106. DIM mySprite.SWSpriteRec
  107. DIM myFrame.SWFrameRec
  108. DIM wndPort&
  109.  
  110. CURSOR _watchCursor                               'takes a few seconds to set up
  111.  
  112. '--------------------------------------------------------------------
  113. 'Initialization and Set Up
  114. '--------------------------------------------------------------------
  115.  
  116. 'Can we run in this environment?
  117. err=FN SWEnterSpriteWorld
  118. FN FatalError(err)                        
  119.  
  120. 'Open a window and draw pretty background
  121. pat&=FN GETPIXPAT(_backPatRSRC)                   'get pattern RSRC
  122. wWidth=SYSTEM(_scrnWidth)                         'get width of screen
  123. wHeight=SYSTEM(_scrnHeight)                       'get height of screen
  124. CALL SETRECT(wRect,0,40,wWidth,wHeight)           'set our window rect
  125. WINDOW #1,"SimpleTest",@wRect,5                   'open a window the same size as screen
  126. wndPort&=FN GetCurrPort                           'get grafPtr
  127. CALL FILLCRECT(#wndPort&+_portRect,pat&)          'fill with nice pattern
  128.  
  129. 'Create SpriteWorld based on window port
  130. err=FN SWCreateSWFromWindow(@mySW,wndPort&)
  131. FN FatalError(err)
  132.  
  133. 'Get time task ptr (same for all sprites)
  134. ttPtr&=LINE "SWTimeTask"
  135.  
  136. '--------------------------------------------------------------------
  137. 'Create Sprite
  138. '--------------------------------------------------------------------
  139.  
  140. 'Get moveProc ptr
  141. movePtr&=LINE "SWBounceMoveProc"
  142.  
  143. 'Create ball sprite
  144. err=FN SWSpriteFromPict(@mySprite,0,0,0,wndPort&+_portRect,_zTrue,2,2,-1,ttPtr&,movePtr&,_spritePictRSRC)
  145. FN FatalError(err)
  146.  
  147. 'Frame time <0 means we don't change frames
  148. FN SWSetFrameTime(@mySprite,-1)
  149.  
  150. 'Set move time
  151. FN SWSetMoveTime(@mySprite,35)
  152.  
  153. '--------------------------------------------------------------------
  154. 'Create Frame
  155. '--------------------------------------------------------------------
  156.  
  157. 'Create frame from PICT resource
  158. err=FN SWFrameFromPict(@myFrame,_spritePictRSRC)
  159. FN FatalError(err)
  160.  
  161. '--------------------------------------------------------------------
  162. 'Assemble the Pieces
  163. '--------------------------------------------------------------------
  164.  
  165. 'Add Frame to Sprite
  166. err=FN SWAddFrameToSprite(@mySprite,@myFrame)
  167. FN FatalError(err)
  168.  
  169. 'Add Sprite to Layer
  170. err=FN SWAddSpriteToLayer(@myLayer,@mySprite)
  171. FN FatalError(err)
  172.  
  173. 'Add Layer to World
  174. err=FN SWAddLayerToWorld(@mySW,@myLayer)
  175. FN FatalError(err)
  176.  
  177. '--------------------------------------------------------------------
  178. 'Final Set Up
  179. '--------------------------------------------------------------------
  180.  
  181. 'Prepare loadframe for animation
  182. FN SWRefreshBackground(@mySW)
  183.  
  184. CURSOR _arrowCursor                               'we're ready to go...
  185.  
  186. 'Render first frame of animation
  187. FN SWAnimateSpriteWorld(@mySW)
  188.  
  189. '--------------------------------------------------------------------
  190. 'Animation Loop
  191. '--------------------------------------------------------------------
  192.  
  193. DO
  194.   FN SWProcessSpriteWorld(@mySW)
  195.   FN SWAnimateSpriteWorld(@mySW)
  196.   CALL SYSTEMTASK
  197. UNTIL FN BUTTON
  198.  
  199. '--------------------------------------------------------------------
  200. 'Dispose SpriteWorld and Exit
  201. '--------------------------------------------------------------------
  202.  
  203. err=FN SWDisposSpriteWorld(@mySW)
  204.  
  205. END
  206.  
  207.