home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-07 | 6.4 KB | 207 lines | [TEXT/ZBAS] |
- 'SWSimpleTest.main by Robert Hommel
- '© Copyright 1994
- 'All rights granted for any use whatsoever
-
- 'SWSimpleTest is precisely that: a simple test of some basic
- 'FBSpriteWorld routines. SWSimpleTest opens a window and creates a
- 'SpriteWorld based on that window's grafPort. The SpriteWorld
- 'contains a single Layer, and that Layer a single Sprite. The Sprite
- 'contains one Frame, which is created from a PICT resource.
-
- 'We use the standard "SWBounceMoveProc" to define the Sprite's move-
- 'ment. Once we have everything set up, the main Animation Loop takes
- 'care of everything until we quit by clicking the mouse button.
-
- 'Though 'simple,' this program shows all the basic steps needed to
- 'create your own animations. Even the most complex animations will
- 'merely be elaborations on these basic steps.
-
- 'Disclaimer: I've tested these routines quite thoroughly on my Mac
- 'LC running System 7.01 and FB 1.02c. I make no promises or warranties
- 'of any kind.
- '*********************************************************************
-
- COMPILE 0, _MacsBugLabels _caseInsensitive
- RESOURCES "FBSpriteWorld.RSRC"
-
- '---------------------------- GLOBALS --------------------------------
-
- GLOBALS "GraphicUtils.glbl"
- GLOBALS "FBSpriteWorld.glbl"
-
- _backPatRSRC=128
- _spritePictRSRC=129
-
- END GLOBALS
-
- GOTO "Main"
-
- '--------------------------- INCLUDES --------------------------------
-
- INCLUDE "FBSpriteWorld.incl"
-
- '------------------------- ERROR HANDLER -----------------------------
-
- CLEAR LOCAL
- LOCAL FN FatalError(errCode)
- 'Simple error handler. You'll want to improve on this in your
- 'program...
-
- LONG IF errCode<>_noErr
- SELECT errCode
- CASE _swTooManyLayers:errStr$="Out of Memory."
- CASE _swTooManySprites:errStr$="Too many Sprites."
- CASE _swTooManyFrames:errStr$="Too many Frames."
- CASE _swNotSystemSeven:errStr$="SpriteWorld requires System 7!"
- CASE _swTimeMgrNotPresent:errStr$="SpriteWorld requires Time Manager."
- CASE _swOutOfMemory:errStr$="Out of Memory."
- CASE ELSE
- errStr$="Unknown error."
- END SELECT
-
- tmp$="Error Code:"+STR$(errCode)
- CALL PARAMTEXT(errStr$,tmp$,"","")
- x=FN STOPALERT(1,0)
- END
- END IF
- END FN
-
- '------------------------ SPRITEWORLD PROCS --------------------------
-
- "SWBounceMoveProc"
- ENTERPROC(SWPtr&,spritePtr&,curRectPtr&)
- 'standard bounce movement proc. Keeps sprite inside sprite boundsRect
-
- LONG IF curRectPtr&.left%+spritePtr&.xDelta%<=spritePtr&.sBoundsRect.left%
- spritePtr&.xDelta%=spritePtr&.xDelta%*-1
- XELSE
- LONG IF curRectPtr&.right%+spritePtr&.xDelta%>=spritePtr&.sBoundsRect.right%
- spritePtr&.xDelta%=spritePtr&.xDelta%*-1
- END IF
- END IF
- LONG IF curRectPtr&.top%+spritePtr&.yDelta%<=spritePtr&.sBoundsRect.top%
- spritePtr&.yDelta%=spritePtr&.yDelta%*-1
- XELSE
- LONG IF curRectPtr&.bottom%+spritePtr&.yDelta%>=spritePtr&.sBoundsRect.bottom%
- spritePtr&.yDelta%=spritePtr&.yDelta%*-1
- END IF
- END IF
- EXITPROC
- RETURN
-
- "SWTimeTask"
- 'Sets the frameTTHasFired or moveTTHasFired field of the sprite record
- 'to _zTrue (-1). Called by the Time Manager if frameTimeInterval or
- 'moveTimeInterval field of sprite record > 0.
-
- ` move.w #-1,tmXQSize(a1) ;[move|frame]TTHasFired=_zTrue
- ` rts ;return
-
- '-------------------------- MAIN LOOP --------------------------------
-
- "Main"
- DIM wRect.8
- DIM mySW.SpriteWorldRec
- DIM myLayer.SWLayerRec
- DIM mySprite.SWSpriteRec
- DIM myFrame.SWFrameRec
- DIM wndPort&
-
- CURSOR _watchCursor 'takes a few seconds to set up
-
- '--------------------------------------------------------------------
- 'Initialization and Set Up
- '--------------------------------------------------------------------
-
- 'Can we run in this environment?
- err=FN SWEnterSpriteWorld
- FN FatalError(err)
-
- 'Open a window and draw pretty background
- pat&=FN GETPIXPAT(_backPatRSRC) 'get pattern RSRC
- wWidth=SYSTEM(_scrnWidth) 'get width of screen
- wHeight=SYSTEM(_scrnHeight) 'get height of screen
- CALL SETRECT(wRect,0,40,wWidth,wHeight) 'set our window rect
- WINDOW #1,"SimpleTest",@wRect,5 'open a window the same size as screen
- wndPort&=FN GetCurrPort 'get grafPtr
- CALL FILLCRECT(#wndPort&+_portRect,pat&) 'fill with nice pattern
-
- 'Create SpriteWorld based on window port
- err=FN SWCreateSWFromWindow(@mySW,wndPort&)
- FN FatalError(err)
-
- 'Get time task ptr (same for all sprites)
- ttPtr&=LINE "SWTimeTask"
-
- '--------------------------------------------------------------------
- 'Create Sprite
- '--------------------------------------------------------------------
-
- 'Get moveProc ptr
- movePtr&=LINE "SWBounceMoveProc"
-
- 'Create ball sprite
- err=FN SWSpriteFromPict(@mySprite,0,0,0,wndPort&+_portRect,_zTrue,2,2,-1,ttPtr&,movePtr&,_spritePictRSRC)
- FN FatalError(err)
-
- 'Frame time <0 means we don't change frames
- FN SWSetFrameTime(@mySprite,-1)
-
- 'Set move time
- FN SWSetMoveTime(@mySprite,35)
-
- '--------------------------------------------------------------------
- 'Create Frame
- '--------------------------------------------------------------------
-
- 'Create frame from PICT resource
- err=FN SWFrameFromPict(@myFrame,_spritePictRSRC)
- FN FatalError(err)
-
- '--------------------------------------------------------------------
- 'Assemble the Pieces
- '--------------------------------------------------------------------
-
- 'Add Frame to Sprite
- err=FN SWAddFrameToSprite(@mySprite,@myFrame)
- FN FatalError(err)
-
- 'Add Sprite to Layer
- err=FN SWAddSpriteToLayer(@myLayer,@mySprite)
- FN FatalError(err)
-
- 'Add Layer to World
- err=FN SWAddLayerToWorld(@mySW,@myLayer)
- FN FatalError(err)
-
- '--------------------------------------------------------------------
- 'Final Set Up
- '--------------------------------------------------------------------
-
- 'Prepare loadframe for animation
- FN SWRefreshBackground(@mySW)
-
- CURSOR _arrowCursor 'we're ready to go...
-
- 'Render first frame of animation
- FN SWAnimateSpriteWorld(@mySW)
-
- '--------------------------------------------------------------------
- 'Animation Loop
- '--------------------------------------------------------------------
-
- DO
- FN SWProcessSpriteWorld(@mySW)
- FN SWAnimateSpriteWorld(@mySW)
- CALL SYSTEMTASK
- UNTIL FN BUTTON
-
- '--------------------------------------------------------------------
- 'Dispose SpriteWorld and Exit
- '--------------------------------------------------------------------
-
- err=FN SWDisposSpriteWorld(@mySW)
-
- END
-
-