Controlling FPS in my game?

Started by
2 comments, last by Hodgman 16 years, 1 month ago
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. :(
Check out my open source code projects/libraries! My Homepage You may learn something.
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.
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?
Check out my open source code projects/libraries! My Homepage You may learn something.
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.

This topic is closed to new replies.

Advertisement