Timed Sprite Animation

Advanced game related topics
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Timed Sprite Animation

Post 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
Last edited by Fluid Byte on Fri Oct 03, 2008 4:35 pm, edited 1 time in total.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...)
oh... and have a nice day.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post 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?
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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 :wink:
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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Thanks for the explanation Psychophanta. Got it working now. :D
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
Post Reply