Limiting the FPS?

Blitz3D Forums/Blitz3D Programming/Limiting the FPS?

nazca(Posted 2003) [#1]
I have a code that should prevent the game from going too fast, but it's not quite working.. here it is:

SyncRate = 30
while not keydown(1)
 RenderTime = MilliSecs() - RenderStart
 renderStart = MilliSecs()
 If Rendertime<(1000/syncrate) Then temp = (1000/syncrate) - Rendertime: Delay temp
 UpdateWorld
 RenderWorld
 Flip
wend
end


[To sum up the above, it finds out how long it's taking to render one loop, then comparing it to how long it should take, then delaying for the right amount of time]

what's wrong with the above? It DOES limit the fps, but it still allows it to go about 10 fps over the SyncRate value!

how can I make it more percise?


jhocking(Posted 2003) [#2]
I don't limit the framerate. Instead I scale changes in the game based on how quickly frames are being calculated. This method is usually referred to as delta time. Basically you figure out how much time has passed since last frame (the delta time) and then multiply stuff by that value. This way there are larger changes between frames when the framerate is lower, resulting in the overall visual appearance being identical at all framerates.


Peer(Posted 2003) [#3]
Hey why not use the Timer Functions?

Timer = Createtimer(Max_FPS)
Repeat
WaitTimer(Timer)
.
.
.

Updateworld()
Renderworld()
.
.
.
Forever

This is much easier to use :)


jhocking(Posted 2003) [#4]
That is easiest and for something simple that is fine. But for any major project, such as a game you intend to distribute, that has a number of drawbacks. The main drawback is that using WaitTimer is extremely wasteful of processing power. The hardware renders the scene as fast as it can and then just sits there waiting, doing nothing, until it is time to render the next frame. Doing something like delta time or tweening or whatever allows the game to take advantage of better hardware (ie. hardware which can process the game faster than the target framerate you have in mind) by keeping the computer busy doing something useful (drawing more frames for smoother animation, calculating AI further for smarter enemies, calculating physics more accurately, etc.) Personally I recommend the delta time approach over tweening because it is much simpler (seriously, pretty much all you have to do is setup a framerate counter) but has many of the same benefits.


Ice9(Posted 2003) [#5]
Using a delay is not optimal. You should run it as fast it will go and adjust all time based variables with a modifier based on the last or average time of the iteration of your game loop.


nazca(Posted 2003) [#6]
but delta time is way too choppy sometimes


Anthony Flack(Posted 2003) [#7]
Follow the link and read more...

[faq 48]


Hotcakes(Posted 2003) [#8]
More specifically, read the thread that FAQ refers to.

Good call, Anthony.


ozak(Posted 2005) [#9]
See the castle demo example that comes with Blitz3D :)