Original Post
🔒 Locked
Limiting FPS
Advertisement
Here is a rough example of how you might do it:
while ( game_is_running ) { int start_time = GetTimeInMilliseconds(); ... // If this is Windows, process all messages ... // do a frame ... int end_time = GetTimeInMilliseconds(); if ( 33 - ( end_time - start_time ) > 0 ) { Sleep( 33 - ( end_time - start_time ) ); } }
Quote:
Why do you want to limit the fps ? I'd search for a solution where rendered fps is separated from the game engine fps.
I know how you feel being new to this - that above post definitely does not help you. Obviously you are writing a game with frame based physics where there is no distinction between framerate and physics - this is what you should be doing if you are new to it - what the above quote is suggesting is not for beginners.
I take it that you want 30fps all the time. Now, remember that slow machines may not even be able to handle this - ignore these computers for now.
You need some mechanism to time your frames - I recommend using the timing class from this project:
http://www.simoneschbach.com/Data/SkullbonezCore/SkullbonezCoreSource.zip
Copy this source code to your project - it is simple to use.
Open the SkullbonezRun::Run method (search the project for this using CTRL+SHIFT+F in Visual Studio) and you will see an example of the timer class - this class is located in SkullbonezTimer.cpp - All you need to do is:
// THIS IS YOUR GAME LOOPfor(;;) { // DECLARE TIMER CLASS Timer t; // START TIMING t.StartTimer(); // PERFORM PHYSICS/GAME LOGIC HERE // STOP TIMING t.StopTimer(); // FIND HOW LONG IT TOOK TO DO PHYSICS/GAME LOGIC double d = t.GetElapsedTime(); // NOTE: THIS IS 1/30th OF A SECOND (APPROX) if(d < 0.0333) { // THIS WILL KILL THE SPARE TIME Sleep((DWORD)(33.333 - d * 1000)); } // NOW SHOW THE NEW FRAME! glSwapBuffers();}Simple. The timer class will do the rest, just replace the "// PERFORM PHYSICS/GAME LOGIC HERE" line with the code that does your logic/rendering. Make the swap buffers call at the very end (all this does is display your rendered buffer to the screen replacing what you rendered last frame).
Hope this helps, just email me s.eschbach@gmail.com if you need more advice, I can walk you through it.
[Edited by - skullbonez on October 17, 2005 11:16:58 PM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement