Jump to content
  • Advertisement
Sign in to follow this  
DarkCybo1

How would I set a "maximum FPS"?

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
In Eternal Lands we've set up the following in the renderer:


if(!limit_fps || ((cur_time-last_time) && (1000/(cur_time-last_time) < limit_fps)))
{
//draw everything
draw_scene();
last_time=cur_time;
}
else
SDL_Delay(1);//give up timeslice for anyone else

cur_time is an integer measuring the time in ms since the program was started. last_time denotes the last time we rendered the scene. You can naturally use anything for sleeping 1 ms (and even change how long you wish the application to sleep), we just use SDL in EL, which is cross-platform.

You can calculate the FPS based on the cur_time and last_time, but it will be quite inaccurate hence you won't see the real FPS (and you'll experience that the FPS can jump a lot). In EL we just count the # of frames drawn per second, then set the FPS to this in order to reduce the fluctation of the FPS - This means that it's only updated once every second, so if you want to do it faster, you can split it up (say 4 times per second would mean that the FPS is 4*counted_fps).

Share this post


Link to post
Share on other sites
call the windows message pump until the time elapsed since last frame = your frame tick rate.

Share this post


Link to post
Share on other sites
The method I use in the main loop of my engine is something like this:

this_ticks = GetTicks();
if (this_ticks - last_ticks > (1000 / 60))
{
//Run the main engine stuff here
last_ticks = this_ticks;
}
else
Sleep(0);



GetTicks() is, in this case, a function that returns some (unimportant) value in milliseconds. So if 1000/60 milliseconds go by, the game gets updated.

If you're not programming with the Win32 API, replace Sleep(0) with whatever function yields the remainder of the timeslice to your OS. It's always nice to do this, in case the user happens to be running other important programs in the background. (The OS would still give the other programs time without the Sleep() call, but not all that it could be.)

EDIT:
Wytter's solution, posted while I was still writing mine, is nearly identical. Kewl :D

Share this post


Link to post
Share on other sites
what is it with people wanting to limit the FPS? I'd go crazy if I was playing something which forced my refresh rate to 60fps (not least because I get eye strain below 75hz), infact thats a sureway to cause an instant delete...

The better method is to limit the world update rate and let the gfx card render as often as needed.

my standard link to the Canoical Game Loop which more people should be using.

Share this post


Link to post
Share on other sites
60 FPS is the US standard for zillions of things, games included. In every book and article I've read on the subject, 60 is the sweet spot to aim for right now. That being the case, I wouldn't be surprised if half the games on the shelves of [insert your local superstore, such as Wal-Mart, here] are running at 60. That being said, the thing to do (the method I actually use, for now at least) is to v-sync it -- that is, render at most once for every vertical retrace. There's no point in doing it any more often than that.

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
60 FPS is the US standard for zillions of things, games included. In every book and article I've read on the subject, 60 is the sweet spot to aim for right now. That being the case, I wouldn't be surprised if half the games on the shelves of [insert your local superstore, such as Wal-Mart, here] are running at 60. That being said, the thing to do (the method I actually use, for now at least) is to v-sync it -- that is, render at most once for every vertical retrace. There's no point in doing it any more often than that.

Everything is going 60 because that is as fast as they can run. They are not arbitrarly limiting ther games to 60, they are making games go as quickly as they can and being able to go 60.

What is going to happen to your game when the framerate dips to 30? That is still in the playable range, but if you are updating everything based on a "change per frame" method everything is going half as fast!

Share this post


Link to post
Share on other sites
@intrest:
I don't limit my games to a special framerate but the game movement is game independet. Doing that is really no big deal. You just have to have a global frametime variable which stores the time the game needed for the last frame. btw i haven't played a game that limits it's framerate so far...

regards,
m4gnus

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When I got used to 72HZ in my game there's really no going back to 60 now. But I doubt many casual gamers notice the difference.

Share this post


Link to post
Share on other sites
while developing my last game i ran across the same problem intrest86 mentiones. there was far too much code to start over so i got around the problem by adding additional timers, for example:

if (key_pressed && key_time<=0)
{
key_time=.01;
move_sprite();
}

key_time-=frame_time;

that works fine but i had to add many timers (every key, mouse buttons, accelerations, bacground scrolling, sounds, etc) and i ended up with so many timers i now consider setting a fixed frame rate for my next game.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!