Frame Rate Limiter
Hey guys, my first time posting, sorry if there is a post like this I looked and coudn't find one
What I need help with is this :
I am creating a 2D OpenGL strategy game under Windows, and I need help with code that will limit the frame rate of the program, as it is refreshing it well I guess it would be everytime around the game loop (however long that is). I am not sure what code would be helpful to you guys in helping me, but my drawing all happens in the one function which gets called by the game loop.
Thanks in advance for any help anyone can give me
You need an accurate timer and the Sleep function.
Oh ok thanks
I do have a performance counter active in the program
Here all of us are trying to eek out more frames and we continually get these posts about limiting frame rate!
Why does everyone insist on limiting the frame rate?
That's because we (I'm included in the list) are still on the initial phase of game development, making 2d games that runs at the max speed that cpu can give, and different speed in each pc. I don't think that's a wrong practice; if I can determine that 27fps are a good rate for a game, I set a timer-counter for my first game to limit and unify it's speed, but and I still use it on my (I'm dreaming here) mega virtual 3d game :) using it as an alert of low framerate.
The game speed should stay the same through delta calculation (time elapsed per frame), not through frame rate limiters.
Ok, but if you let rendering backbuffer-frames in higer speed than swapping backbuffer into screen will cause cutted movements if you want to slow down the game speed.
You mean like waiting for vsync? Shouldn't there be a flag for that somewhere?
As long as the gameplay is not frame-based, you don't really need to limit the frame rate. But if, for example, you move something a given distance each frame, then either stop doing that or limit the frame rate.
The trick to limiting the frame rate is to not just delay a fixed period of time after displaying each frame. What you need to do is record the time you start painting the frame, and then after the frame, delay as long as you want, but incorporate the time spent painting the frame.
Here's the basic idea (SDL code, but you get the idea):
SDL_GetTicks() is much like clock(): it returns the number of milliseconds the program has been executing for. But you can use any timing function, such as time().
Uint32 before, delay;
before = SDL_GetTicks();
delay = before + 33 - SDL_GetTicks();
if(delay > 0) SDL_Delay(delay);
33 is of course the frame rate. Make it whatever you like.
SDL_Delay() delays for the given number of milliseconds. If you can't find a function like that you could use something like this, though it would kill your CPU:
while(SDL_GetTicks() < before + 33);
Before you insist on wasting precious CPU and GPU horsepower, perhaps a bit of research would be in order. There is no reason to clamp framerates for rendering. There is a reason to clamp physics framerate but I won't go into that here.
Plain and simple: You should not be wasting cycles.
Hey Bubba, I'll keep in mind your recomanations. And "...I won't go into that here.", you have reason, that isn't my thread.
I make my first post on that thread because I was remembering that when I started using Allegro (yes, I have already seen that the post is about OGL) there was a timer function and the game speed was controlled via that timer.
And forget my posts if I have induced to confusion.
I found this site that talks about the differences in using FPS vs Frame Time. It is based in DirectX but the ideas could be used for any graphics library.
The FAQ also has an artical about how to perform timing.
He's making a strategy game, so if he has AI in there, frame limiting isn't so bad a thought since you don't want the AI to be working so fast that you'll never be able to keep up with it. Though I would suggest that you can clamp the AI loop instead of the entire game loop.
SDL_gfx has a nice frame-speed limiter. It's quite useful, and I use it all the time. That is, if you're using the SDL.