Skip to main content
GameDev.net gamedev.net
🔒 Locked

Frame rate limiter

Started by Jernej.L Apr 23, 2007 at 12:34 PM 9 replies 5.8k views
Original Post
Jernej.L
Jernej.L
What is a reliable, most precise way to create a frame rate limiter in a game? basically, i need game to be stable to achieve stable 30 fps, right now i am calling SLEEP to do idle cycles, but that is a terribly unreliable, unstable and unprecise way of doing it, how is a frame rate limiter made in a professional game?
Projects: Top Down City: http://mathpudding.com/
xycsoscyx
xycsoscyx
I assume you mean you don't want it to go over 30 fps? The best way is to keep looping, and check the time each loop. Once your frame time is greater than what you want, draw the frame and continue looping, something like this:

float Time = 0;while(true){    if((GetTime() - Time) > (1.0f / 30.0f))    {        Time = GetTime();        DrawStuff();    }}

This means it will wait until (1/30) seconds have passed until it does the next frame. The only proble here is if you can't maintain at least 30 frames per second, then things start to bog down (obviously).
Jernej.L
Jernej.L
Quote:
The only proble here is if you can't maintain at least 30 frames per second, then things start to bog down (obviously).


Thanks for that advice, i was thinking, i only have to maintain 30 game updates / second, i could skip rendering if it ever falls below 30.
Projects: Top Down City: http://mathpudding.com/
Aressera
Aressera
This is a bad idea. It is much better to use a timer to find the delta time and then use that to advance any animation/physics. This is much more robust and makes full use of the CPU, where as in that method you waste clock cycles looping.

There's really no sane reason why you would want to limit the framerate at 30fps of all things. The refresh rate of the moniter is the only frequency that would make sense, and that is done through other means (VSync). Animation can be set up as a function of time, where as you normally have your dt be 1/30.0f, instead use the time interval that it took to draw the last frame.
ajax75
ajax75
hi there. thank google, ive found a site...
iam not a coder and didnt know anything how to code. i was looking for a tool to limit the fps in dx games. is there any tool to cap the fps?

would be nice if someone can give me a hint.

thank you
GroZZleR
GroZZleR
Quote:
Original post by Aressera
This is a bad idea. It is much better to use a timer to find the delta time and then use that to advance any animation/physics (emphasis mine). This is much more robust and makes full use of the CPU, where as in that method you waste clock cycles looping.


I disagree with this. Using a fixed time-step for physics simulations is definitely the way to go to prevent all sorts of issues. However, you should be rendering (and updating animations) as fast as you possibly can.
CadetUmfer
CadetUmfer
Here's a stripped down version of what I'm currently doing.

#define TIMESTEP 10#define MAX_FPS 85#define MIN_DT (1000 / MAX_FPS)unsigned lastTime, accumulator, t;unsigned time, dt;do {    if (/* app minimized/etc */) {        Sleep(5);    } else {        Sleep(0);    }    do {        time = /* add input events to queue, return last event time */        dt = time - lastTime;    } while (dt < MIN_DT);    lastTime = time;    accumulator += dt;    while (accumulator >= TIMESTEP) {        RunFrame(t);        accumulator -= TIMESTEP;        ++t;    }    DrawFrame(accumulator / (float)TIMESTEP);} while (1);
Anthony Umfer
Klarre
Klarre
You might find this site interesting http://www.gaffer.org/game-physics/fix-your-timestep/
http://www.klarre.se
thre3dee
thre3dee
I once had an idea for motion blurring which has most likely been used somewhere but it goes like this..

You render to your *screen* 30fps (video) or 24fps (film) or whatever you want.. But you render and update as many times as possible between the frames (though you might have it cap at five to keep memory usage for each intermediate
thre3dee
thre3dee
I once had an idea for motion blurring which has most likely been used somewhere but it goes like this..

You render to your *screen* 30fps (video) or 24fps (film) or whatever you want.. But you render and update as many times as possible to off-screen textures between the screen frames (though you might have it cap at five to keep memory usage for each intermediate framebuffer down) so then u accumulate the time between the capped framerate just like the eye does.

So say ur computer is capable of 90 fps at anyone time during the game, but the framerate is capped to a film rate (24fps) to make it seem more like a film, in the 41ms of frame time, you'll have rendered and updated 3.75 off-screen frames (which would end up as 4 full frames in real world application), thus giving you some basic motionblur. I'm sure this method of motionblur is terrible haha but hey

Anyone know what this kind of motion blur is called?
blackcloak
blackcloak
I some thing similar to motion blur in my engine. I blend the sleeps over time so the end user does not notice pause. By doing a bunch of small sleeps over time the cap keeps a smooth frame rate.
feel free to look at how I am doing it in the UpdateScreen and fpsDelay functions of my engine
Black CloakEpee Engine.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.