Skip to main content
gamedev.net
🔒 Locked

Limiting FPS

Started by gellin Oct 16, 2005 at 2:02 PM 6 replies 700+ views
Original Post
gellin
gellin
I'm making a simple 2D game and want to limit the FPS to 30 fps. I'm using C++, OpenGL, and GLUT. I was just wondering what the best way is to go about doing this.
Kambiz
Kambiz
1.save the current time
2.flip(show the next backbuffer)
3.do not wait here! render the next frame(s)
4.read the time until dt=33ms (you have to use a high resolution timer)
5.go to 1
gellin
gellin
could you explain what a backbuffer is? thanks
JohnBolton
JohnBolton
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 ) );        }    } 
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Kambiz
Kambiz
A secondary surface where the current frame's graphics are stored before they are transferred to the primary display surface.
by: Kevin Hawkins
dimebolt
dimebolt
Quote:
Original post by gellin
could you explain what a backbuffer is? thanks


Just to clarify a bit more:

What he means with "2.flip(show the next backbuffer)" is the glutSwapBuffers() function-call.

Tom
skullbonez
skullbonez
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.