'Speed' of loop

BlitzMax Forums/BlitzMax Beginners Area/'Speed' of loop

Matt Vinyl(Posted 2009) [#1]
' Reel Test

reelblur=LoadAnimImage("reelblur.png",97,195,0,9)

Graphics 800,600

Repeat
	Cls
	If KeyDown(KEY_SPACE) Then
		If a<8 Then
			a:+1
		Else
			a=0
		End If
		DrawImage reelblur,0,0,a
	End If
	Flip
Until KeyHit(KEY_ESCAPE)


My head is hurtning today gentlemen and am now struggling with a new problem. How do I get this to work 'slower'? It obviously flies at the refresh rate at the moment, but I'd like it a good few frames slower than that. Sorry it's such a noob-ish request. :)


johnnyfreak(Posted 2009) [#2]
mmm try to change keydown with keyhit


Warpy(Posted 2009) [#3]
A couple of options:

1: use the Delay command to halt the program for small increments of time, so for example
Delay 50

would get you 1000/50 = 20 frames per second


2: Keep track of the last time you incremented the frame counter, and only increment it again if a certain amount of time has elapsed. This way, your loop keeps going, but the speed of the animation is limited.

Local lasttick
Repeat
	Cls
	If KeyDown(KEY_SPACE) and Millisecs()>lasttick+50 Then
		If a<8 Then
			a:+1
		Else
			a=0
		End If
		lasttick = Millisecs()
		DrawImage reelblur,0,0,a
	End If
	Flip
Until KeyHit(KEY_ESCAPE)



GfK(Posted 2009) [#4]
Another option: Make 'A' a float and increment it by a fractional value. It'll be automatically floored to an Int when you use it in DrawImage.


MGE(Posted 2009) [#5]
flip(1)


johnnyfreak(Posted 2009) [#6]
sorry, i didn't catch your question :P


xlsior(Posted 2009) [#7]
One option:

Use an incremental coutner variable, and do something like this:

while not keydown(key_escape)
   if variable mod 10 = 0 then
       variable=1
       ' Do your stuff
   end if
   variable = variable +1
   flip
wend


That way the 'do stuff' portion only gets invoked every 10th loop.

You could also create a timer, and if the timer gets triggered invoke your function.


xlsior(Posted 2009) [#8]
Oh, and finally: if nothing else needs to change/run/whatever in the mean time, you could always just insert a delay <time> after your flip to halt the program altogether until the time is up.


Matt Vinyl(Posted 2009) [#9]
Thank-you for the swift replies people. I'll give it a go tomorrow. Other stuff does need to be going on at the same time, so I'll probably have to go with the additional 'counter' variable, which when it reaches a certain value increments the other value by 1. Thanks once again.


BladeRunner(Posted 2009) [#10]
Use a Timer and increase a by the times it has fired (see timerticks() for more info)

a = timerticks(timer) mod animationsteps


delay would be bad as it pauses the whole program and it would get very hard to synchronise different animations that way.


therevills(Posted 2009) [#11]
I normally do the following:

' Reel Test
SuperStrict
local reelblur:Timage=LoadAnimImage("reelblur.png",97,195,0,9)

Graphics 800,600

local a% = 0
local frameDelay%=0

Repeat
	Cls
	If KeyDown(KEY_SPACE) Then
		frameDelay:+1
		if frameDelay > 10
				frameDelay = 0
				If a<8 Then
					a:+1
				Else
					a=0
				End If
		endif
		DrawImage reelblur,0,0,a
	End If
	Flip
Until KeyHit(KEY_ESCAPE)


BTW use SuperStrict... ;-)


xlsior(Posted 2009) [#12]
That particular example doesn't work: You increase the variable until 9, and then zero it out. It never reaches 10, so your if clause will never get invoked.

the principle is OK, but the numbers need a tweak.


Arowx(Posted 2009) [#13]
Have a search for delta timing, this is in it simplest form is where you time your framerate and measure the millisecs() elapsed each frame.

Now just use an interval in milliseconds for the animation to update and check when that has elapsed each frame.

I'm sure there are a couple of handy DeltaTimer types floating around the forums somewhere as well as guides and examples of use!


therevills(Posted 2009) [#14]
Hi xlsior, are you talking about my example?

I didnt test it when I added the extra code, but I have just tried now and it seems to work alright...

Heres a copy and paste runnable example:
SuperStrict

Graphics 800,600

Local reelblur:Timage = CreateImage(100,100,10, DYNAMICIMAGE)

For Local x% = 0 To 8 Step 2
	Cls
	SetColor Rand(255),Rand(255),Rand(255)
	DrawOval 0,0,100,100
	SetColor 255,255,255
	DrawText x,50,50
	GrabImage(reelblur,0,0,x)
	Cls
	SetColor Rand(255),Rand(255),Rand(255)	
	DrawRect 0,0,100,100
	SetColor 255,255,255	
	DrawText x+1,50,50
	GrabImage(reelblur,0,0,x+1)
Next

Local a% = 0
Local frameDelay%=0

Repeat
	Cls
	If KeyDown(KEY_SPACE) Then
		frameDelay:+ 1
		If frameDelay > 10
				frameDelay = 0				
				If a<8 Then
					a:+ 1					
				Else
					a=0
				End If
		EndIf		
		DrawImage reelblur,0,0,a
	End If
	Flip
Until KeyHit(KEY_ESCAPE)