Page 1 of 1
Timed Sprite Animation
Posted: Fri Oct 03, 2008 2:39 am
by Fluid Byte
I would like to know how you would animate a sprite within a specific time range. E.g. I want to move the sprite in this demo from the left to the right side of the screen in 1000 millsecs. How would you do this?
Code: Select all
InitSprite() : InitKeyboard()
#Fullscreen = 0
If #Fullscreen
OpenScreen(640,480,32,"void")
Else
OpenWindow(0,0,0,640,480,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
EndIf
CreateSprite(0,64,64)
StartDrawing(SpriteOutput(0))
Box(0,0,64,64,RGB(255,0,155))
StopDrawing()
Repeat
If #Fullscreen = 0
Repeat
EventID = WindowEvent()
Select EventID
Case #PB_Event_CloseWindow
End
EndSelect
Until EventID = 0
EndIf
ExamineKeyboard()
FlipBuffers()
ClearScreen($804020)
DisplaySprite(0,X,200)
X + 1
Delay(1)
Until KeyboardPushed(#PB_Key_Escape) Or EventID = #PB_Event_CloseWindow
Posted: Fri Oct 03, 2008 7:39 am
by blueznl
Posted: Fri Oct 03, 2008 10:24 am
by Kaeru Gaman
AnimProcs Include contains timing for animation, base could be used for movement, too.
http://www.purebasic.fr/german/viewtopi ... 3707#23707
(I thought I also posted/linked it here in the international forums, but I can't find it right now...)
Posted: Fri Oct 03, 2008 4:21 pm
by Psychophanta
Fluid Byte wrote:I would like to know how you would animate a sprite within a specific time range. E.g. I want to move the sprite in this demo from the left to the right side of the screen 1000ms. How would you do this?
Code: Select all
InitSprite() : InitKeyboard()
#Fullscreen = 0
If #Fullscreen
OpenScreen(640,480,32,"void")
Else
OpenWindow(0,0,0,640,480,"void",#PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
EndIf
CreateSprite(0,64,64)
StartDrawing(SpriteOutput(0))
Box(0,0,64,64,RGB(255,0,155))
StopDrawing()
FrameFreq.f=60.
TimePerFrame.f=1000./FrameFreq
LengthToWalk_InPixels.f=640.
TimeUsedToWalkThatLength.f=1000.
Repeat
If #Fullscreen = 0
Repeat
EventID = WindowEvent()
Select EventID
Case #PB_Event_CloseWindow
End
EndSelect
Until EventID = 0
EndIf
ExamineKeyboard()
FlipBuffers():Delay(16)
ClearScreen($804020)
DisplaySprite(0,X.f,200)
X+TimePerFrame*LengthToWalk_InPixels/TimeUsedToWalkThatLength
Until KeyboardPushed(#PB_Key_Escape) Or EventID = #PB_Event_CloseWindow
Posted: Fri Oct 03, 2008 4:56 pm
by Fluid Byte
Thanks for the links guys but I wasn't able to abstract the needed code yet.
@Psychophanta:
I see you put a point (".") after all the values. I can understand that would define the values as floats and is the same as writing, e.g. "1000.0". But does this have any effect on the calculation or is it just cosmetic?
Another point is the value of '16' for the Delay() command. Why exactly this value? I noticed it is similar to the 'TimePerFrame' value, are these two connected? If yes, would that mean that you have to adjust the delay when you change the 'FrameFreq' value?
One more thing, if you wanted to adapt this method for an application are you still tied to the framerate or is there another technique? I guess if that is true you would use DesktopFrequency(), wouldn't you?
Posted: Sat Oct 04, 2008 10:02 am
by Psychophanta
Fluid Byte wrote:I see you put a point (".") after all the values. I can understand that would define the values as floats and is the same as writing, e.g. "1000.0". But does this have any effect on the calculation or is it just cosmetic?
It is the same but in my way i just save to write a zero
Fluid Byte wrote:Another point is the value of '16' for the Delay() command. Why exactly this value? I noticed it is similar to the 'TimePerFrame' value, are these two connected? If yes, would that mean that you have to adjust the delay when you change the 'FrameFreq' value?
That is an old issue:
http://www.purebasic.fr/english/viewtopic.php?t=12825
I commonly use that trick in almost all my 60Hz fullscreen progs, it really works!
Fluid Byte wrote:One more thing, if you wanted to adapt this method for an application are you still tied to the framerate or is there another technique? I guess if that is true you would use DesktopFrequency(), wouldn't you?
For most apps I don't find any reason to use another freq than 60Hz (and that is a good freq for the desktop too if a LCD display is used). Anyway, if you wanna use other vertical frequency different, then the formula to get the Delay(x) is: x.l=1000./freq.f
There must be other methods to do smooth movements while saving CPU time in windows, for example using "Multimedia Timers" but there is at least a "perfect" way (which is the one used by a SEGA emulator for windows called KEGA FUSION) i still don't know how to do, since Steve Snake (the author of KEGA FUSION) does not give the sources. There are more threads about this in this forum.
Posted: Sun Oct 05, 2008 2:04 pm
by Fluid Byte
Thanks for the explanation Psychophanta. Got it working now.
