control speed of execution

BlitzPlus Forums/BlitzPlus Beginners Area/control speed of execution

amu lojes(Posted 2006) [#1]
graphics 640,480
setbuffer backbuffer()

frametimer = createtimer(60)

while not keyhit(1)

;waittimer(frametimer);why this function is deprecated?

; what code here i should write instead of waittimer()

flip
cls
wend


Adam Novagen(Posted 2006) [#2]
One problem with WaitTimer() is that it works much the same way as Delay. It's a good way to ensure that faster PCs don't boost your game's speed to unwanted levels, but on a slower PC where your game is already slowed down, WaitTimer will simply add an extra delay.

Lemme explain that another way. (This is all just an example.) Let's say you make a game that runs at 30 frames per second (FPS.) You would use frametimer = CreateTimer(30) to make sure the game updates no more than thirty times a second. But when you use WaitTimer(frametimer) the game actually pauses for 1/30 of a second. So if someone plays your game, and their computer is slower so that the game can only make 24 FPS, WaitTimer(frametimer) will just cause an extra 1/30 second delay, which is not a good thing.

What you need is a function that only pauses the game long enough to keep the framerate even, and not at all if it's already behind schedule. Fortunately, I recently made some code that does just that. Here, you (and anyone else viewing this message) can have it.
Global prevtime;make ABSOLUTELY SURE you include this global somewhere, or this won't work!


Function Wait(pauselength)


Repeat
Until MilliSecs() => prevtime + pauselength
prevtime = MilliSecs()


End Function
You can use this in your program just like the Delay command, like this:
Global prevtime;always essential


Function Wait(pauselength)


Repeat
Until MilliSecs() => prevtime + pauselength
prevtime = MilliSecs()


End Function


While Not KeyDown(1);a basic main loop


Cls


Wait(1000);will pause the program for up to one second, if necessary


Flip


Wend
Take note that using the Wait() function more than once in any loop wil cause problems. I plan to rectify that problem at a later date. Meanwhile, I hope this helps!