Jump to content
  • Advertisement
Sign in to follow this  
hahaha

Locking the frame rate

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

For my game i want to be able to lock the framerate. Im using Visual C++ 2005 and opengl. Please could someone tell me what the best way to achieve this would be? I also want a function to calculate my framerate(to make sure the locked framerate works)but i dont know where to start in writing a fps counter

Share this post


Link to post
Share on other sites
Advertisement
here you go:


DWORD old_time=timeGetTime();
DWORD new_time;

while (msg.message!=WM_QUIT && game->next_game)
{
if(game->next_game!=game)
{
//switch games here
Log<<"time to switch games"<<endl;
}

if (PeekMessage( &msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
new_time=timeGetTime();
if( (new_time-old_time)>20 )
{
old_time=new_time;
Log<<"tick"<<endl;

Input::Update();
Log<<"about to call game->Tick"<<endl;
game->Tick();
Log<<"about to call Graphics::Render"<<endl;

Graphics::Render();
}
}
}

Share this post


Link to post
Share on other sites
Is there some other way to do it with more accuracy?
eg if i change this line -> "if( (new_time-old_time)>20 )" from 20 to 15 i get a constant rate of 40fps. I if change it to 14 i get 60fps what would i have to do to get 50 which is the rate i want?

Share this post


Link to post
Share on other sites
well the measurement is in milliseconds, so 20 should be giving 1000/20 = 50 frames/second. Where did you get your numbers from, meaning how do you know that you are getting 40fps versus 60fps? Perhaps you are using a less accurate timing function. The one that I am using has 1 millisecond precision.

Check out this site: http://www.mvps.org/directx/articles/selecting_timer_functions.htm

Share this post


Link to post
Share on other sites
Assuming your timer is in miliseconds, then you want each frame to take 20 miliseconds (50 * 20) = 1000 aka 1 second. Now to do this you want 20 - (new - old) = sleepAmt; if (sleepAmt > 0){ Thread.sleep(sleepAmt)} The reason why this should produce a different result than what you are seeing is the removal of processing outside of the wait. Though in the loop shown it may cause a little IO funnyness.

In the loop posted before you may want to move the game tick outside the else so that the loop hits more often and more regularly.

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!