Locking the frame rate

Started by
5 comments, last by hahaha 16 years, 9 months ago
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
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();			}		}	}
Thanks for that!:)
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?
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
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.
Thanks for the all the help. That link was great!

This topic is closed to new replies.

Advertisement