some functions I wrote to use in a 2D platform game I am making..
This is the base system, I posted it here a while ago
the problem in this one that it doesn't support multiple instances.
Init_AnimSys:
Rem Call this Sub to initialize the system..
Rem Max Slots for IDs since you cant delete one at time
Rem Set this to a suitable number for you
Rem You can use AnimSys_Clear() to Clear the whole thing
#constant AnimSys_MaxAnimationSlots = 100
REM ImageRange sets the where the Image ID-ing starts
#Constant AnimSys_ImageRange = 500
Global AnimSys_CurrentImage
AnimSys_CurrentImage = AnimSys_ImageRange
Global AnimSys_LastLoadedImage
AnimSys_LastLoadedImage = AnimSys_ImageRange
Global AnimSys_CurrentID
AnimSys_CurrentID = -1
type AnimationSys_type
MaxFrames as integer
Frame as integer
ImageRange as integer
speed as float
FrameTimer as float
Play as boolean
Active As boolean
endType
Dim AnimSys(AnimSys_MaxAnimationSlots) as AnimationSys_type
For i = 0 to AnimSys_MaxAnimationSlots
AnimSys(i).MaxFrames = 0
AnimSys(i).Frame = 0
AnimSys(i).ImageRange = 0
AnimSys(i).speed = 0
AnimSys(i).FrameTimer = 0
AnimSys(i).play = 0
AnimSys(i).Active = 0
Next i
Return
Function AnimSys_Clear()
REM Clears the system (delete images, restarts variable ... new level?
For imageZZ = AnimSys_ImageRange to AnimSys_LastLoadedImage
IF IMAGE EXIST(imageZZ) then Delete Image imageZZ
Next imageZZ
AnimSys_CurrentImage = AnimSys_ImageRange
AnimSys_LastLoadedImage = AnimSys_ImageRange
AnimSys_CurrentID = -1
For i = 0 to AnimSys_MaxAnimationSlots
AnimSys(i).MaxFrames = 0
AnimSys(i).Frame = 0
AnimSys(i).ImageRange = 0
AnimSys(i).speed = 0
AnimSys(i).FrameTimer = 0
AnimSys(i).play = 0
AnimSys(i).Active = 0
Next i
EndFunction
Function AnimSys_LoadImageSeq(Dir$,name$,Ext$,first,last,Zeros,TextureFlag)
REM Load Images Sequence in a floder
REM Dir$: Directory where the Images are stored [Example: "imageSequenceswater"]
REM name$: the name of the files, if your files are something like water001.png, then this would be water
REM Ext$: Extension of you image sequence
REM First: Starting number of you image files, 1?
REM last: The last number of your image files
REM Zeros: if your Images are named like water0001.jpg,...,water0108.jpg then this would be 4
REM TextureFlag: check DBP help in Load Image, Set this to 1 if you will use transparency
REM The Function will return the ID of the animation so you can control it.
inc AnimSys_CurrentID
if AnimSys_CurrentID > AnimSys_MaxAnimationSlots
ExitFunction -1
endif
AnimSys(AnimSys_CurrentID).ImageRange = AnimSys_CurrentImage
For i = first to last
fill$ = ""
if Zeros > 0
lenn = len(str$(i))
for fill = 1 to (Zeros - lenn)
fill$ = fill$ + "0"
next fill
endif
id$ = fill$ + str$(i)
FullPath$ = Dir$ + "" + name$ + id$ + "." + Ext$
Load Image FullPath$,AnimSys_CurrentImage,TextureFlag
AnimSys_LastLoadedImage = AnimSys_CurrentImage
inc AnimSys_CurrentImage
Next i
AnimSys(AnimSys_CurrentID).MaxFrames = last - first
AnimSys(AnimSys_CurrentID).Frame = 0
AnimSys(AnimSys_CurrentID).speed = 0.5
AnimSys(AnimSys_CurrentID).FrameTimer = 0.0
AnimSys(AnimSys_CurrentID).play = 0
AnimSys(AnimSys_CurrentID).Active = 1
EndFunction AnimSys_CurrentID
Function AnimSys_LoadILF(ILF_File_Path$,ImageDir$,TextureFlag)
inc AnimSys_CurrentID
if AnimSys_CurrentID > AnimSys_MaxAnimationSlots or File Exist(ILF_File_Path$) = 0
ExitFunction -1
endif
AnimSys(AnimSys_CurrentID).ImageRange = AnimSys_CurrentImage
Open To Read 5,ILF_File_Path$
FrameCounter = 0
While File End(5) = 0
Read String 5,fileName$
if File Exist(ImageDir$ + "" + fileName$)
Load Image ImageDir$ + "" + fileName$,AnimSys_CurrentImage,TextureFlag
AnimSys_LastLoadedImage = AnimSys_CurrentImage
inc FrameCounter
inc AnimSys_CurrentImage
endif
EndWhile
close File 5
AnimSys(AnimSys_CurrentID).MaxFrames = FrameCounter - 1
AnimSys(AnimSys_CurrentID).Frame = 0
AnimSys(AnimSys_CurrentID).speed = 0.5
AnimSys(AnimSys_CurrentID).FrameTimer = 0.0
AnimSys(AnimSys_CurrentID).play = 0
AnimSys(AnimSys_CurrentID).Active = 1
EndFunction AnimSys_CurrentID
Function AnimSys_SetSpeed(ID,speed#)
REM Sets the playback speed of the animation
REM ID: Animation ID returned by AnimSys_LoadImageSeq()
REM Speed# 0 to 1 : 0.5 would be half the speed.
if Speed# < 0 then Speed# = 0
if Speed# > 1 then Speed# = 1
AnimSys(ID).speed = speed#
EndFunction
Function AnimSys_SetFrame(ID,Frame)
REM Sets the current frame of the animation.
REM ID: Animation ID returned by AnimSys_LoadImageSeq()
REM Frame: The frame you want!
if Frame >= 0 and Frame <= AnimSys(ID).ImageRange + AnimSys(ID).MaxFrames - 1
AnimSys(ID).Frame = Frame
endif
EndFunction
Function AnimSys_Play(ID,flag)
AnimSys(ID).Play = flag
EndFunction
Function AnimSys_Update()
REM Call this at the start of your loops
For i = 0 to AnimSys_MaxAnimationSlots
if AnimSys(i).Active and AnimSys(i).Play
inc AnimSys(i).FrameTimer,AnimSys(i).speed
if AnimSys(i).FrameTimer > 1.0
AnimSys(i).FrameTimer = AnimSys(i).FrameTimer - 1.0
inc AnimSys(i).Frame
if AnimSys(i).Frame > AnimSys(i).MaxFrames then AnimSys(i).Frame = 0
endif
Endif
Next i
EndFunction
Function AnimSys_GetImage(ID)
REM This will give you the current Image of your animation sequence
REM ID: Animation ID returned by AnimSys_LoadImageSeq()
REM Example: Paste Image AnimSys_GetImage(water),10,10
REM Another: Texture Object WaterPlainObject,AnimSys_GetImage(water)
if AnimSys(ID).Active
image = AnimSys(ID).ImageRange + AnimSys(ID).Frame
endif
EndFunction image
You can clear all the memory used by:
AnimSys_Clear()
Sprite system:
This is the real deal..
I called it the SS (sprite system) I dont know if this fits
but anyway ... this SS uses the animations you load with the
Animation system so you can use the same animation on many objects
with independent speed,frame,etc..
Init_SpriteSys:
type Sprite_Type
Frame as integer
speed as float
FrameTimer as float
Play as boolean
Active As boolean
Playing as boolean
Looping as boolean
Anim as integer
LoopTo as integer
LoopFrom as integer
SuperLoop as boolean
EndType
Global DIM SpriteSys(1000) as Sprite_Type
Global SpriteSys_CurrentID as integer
SpriteSys_CurrentID = 0
Return
Function SS_MakeSprite(Anim)
retID = SpriteSys_CurrentID
SpriteSys(retID).Anim = Anim
SpriteSys(retID).Speed = AnimSys(Anim).Speed
SpriteSys(retID).FrameTimer = 0.0
SpriteSys(retID).Play = 0
SpriteSys(retID).Playing = 0
SpriteSys(retID).Looping = 0
SpriteSys(retID).Frame =0
SpriteSys(retID).LoopTo = 0
SpriteSys(retID).LoopFrom = 0
SpriteSys(retID).SuperLoop = 0
SpriteSys(retID).Active = 1
inc SpriteSys_CurrentID
EndFunction retID
Function SpriteSys_Update()
REM Call this at the start of your loops
For i = 0 to 1000
if SpriteSys(i).Active and SpriteSys(i).Play
inc SpriteSys(i).FrameTimer,SpriteSys(i).speed
if SpriteSys(i).FrameTimer > 1.0
SpriteSys(i).FrameTimer = SpriteSys(i).FrameTimer - 1.0
inc SpriteSys(i).Frame
if SpriteSys(i).SuperLoop = 0
if SpriteSys(i).Frame > AnimSys(SpriteSys(i).Anim).MaxFrames
if SpriteSys(i).Looping
SpriteSys(i).Frame = 0
else
SpriteSys(i).Frame = AnimSys(SpriteSys(i).Anim).MaxFrames
SpriteSys(i).play = 0
endif
endif
else
if SpriteSys(i).Frame > SpriteSys(i).LoopTo
SpriteSys(i).Frame = SpriteSys(i).LoopFrom
endif
endif
endif
Endif
Next i
EndFunction
Function SS_Clear()
SpriteSys_CurrentID = 0
For i = 0 to 1000
SpriteSys(i).Frame = 0
SpriteSys(i).speed = 0
SpriteSys(i).FrameTimer = 0
SpriteSys(i).play = 0
SpriteSys(i).Active = 0
SpriteSys(i).Looping = 0
SpriteSys(i).Anim = 0
Next i
EndFunction
Function SS_Loop(ID)
`SpriteSys(ID).Frame = 0
SpriteSys(ID).Play = 1
SpriteSys(ID).Looping = 1
SpriteSys(ID).SuperLoop = 0
EndFunction
Function SS_Loop2(ID,lfrom,lto)
`SpriteSys(ID).Frame = 0
SpriteSys(ID).Play = 1
SpriteSys(ID).Looping = 1
SpriteSys(ID).SuperLoop = 1
SpriteSys(ID).LoopFrom = lfrom
SpriteSys(ID).LoopTo = lto
EndFunction
Function SS_Play(ID,flag)
SpriteSys(ID).Play = flag
SpriteSys(ID).Frame = 0
SpriteSys(ID).Looping = 0
SpriteSys(ID).SuperLoop = 0
EndFunction
Function SS_IsPlaying(ID)
ret = SpriteSys(ID).Play
EndFunction ret
Function SS_SetFrame(ID,Frame)
if Frame >= 0 and Frame <= AnimSys(SpriteSys(ID).Anim).ImageRange + AnimSys(SpriteSys(ID).Anim).MaxFrames - 1
SpriteSys(ID).Frame = Frame
endif
EndFunction
Function SS_GetFrame(ID)
ret = SpriteSys(ID).Frame
EndFunction ret
Function SS_GetImage(ID)
image = AnimSys(SpriteSys(ID).Anim).ImageRange + SpriteSys(ID).Frame
EndFunction image
Function SS_SetSpeed(ID,Speed#)
SpriteSys(ID).Speed = Speed#
EndFunction
Check the attachment for a full sample.
I know my english is bad.
Snake_x86