Sign in to follow this  
ajm113

Controlling FPS in my game?

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this