Jump to content
  • Advertisement
Sign in to follow this  

Controlling FPS in my game?

This topic is 3716 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

How can I control FPS in my game? I have a pong game going under OpenGL, C++ and the paddles move the way I want them on my PC, but on my school's pcs. They move very fast! So how can I keep that from happening? I did look for code samples and articles, but couldn't find any. :(

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ajm113
How can I control FPS in my game? I have a pong game going under OpenGL, C++ and the paddles move the way I want them on my PC, but on my school's pcs. They move very fast! So how can I keep that from happening? I did look for code samples and articles, but couldn't find any. :(


You need to add a timer to your program.

Share this post


Link to post
Share on other sites
Sorry I thought I clicked on the Game Dev forums, but oh well. Oh yeah I need to add a time of milliseconds that I render my game correct?

Share this post


Link to post
Share on other sites
Quote:
Original post by ajm113
How can I control FPS in my game? I have a pong game going under OpenGL, C++ and the paddles move the way I want them on my PC, but on my school's pcs. They move very fast! So how can I keep that from happening? I did look for code samples and articles, but couldn't find any. :(


The technique you're describing is generally called a "frame limiter", and is mostly no longer used (except maybe when the hardware configuration is fixed).

Instead, you should try to implement "frame-rate independent movement", so that no matter what the frame-rate is, the game will play at the same speed.
This is achieved by measuring the length of time that it takes your game to process a single frame, and multiplying all speed values by this elapsed-time value.

For example, this hypothetical game loop updates a position value by a fixed amount:

while( shouldQuit == false )
{
if( keys['A'] == true )
position += 1;
if( keys['Z'] == true )
position -= 1;

UpdateOtherStuff();
Render();
}



This one updates by a variable amount depending on the frame-rate:

float currentTime = getTimeInSeconds();
float lastTime = currentTime;
float deltaTime;
while( shouldQuit == false )
{
deltaTime = lastTime - currentTime;
lastTime = currentTime;
currentTime = getTimeInSeconds();

if( keys['A'] == true )
position += 1.0f * deltaTime;
if( keys['Z'] == true )
position -= 1.0f * deltaTime;

UpdateOtherStuff( deltaTime );
Render();
}




These examples are based off a hypothetical "getTimeInSeconds" function - the easiest way to make one of these is to use the timeGetTime on Windows.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!