Jump to content
  • Advertisement
Sign in to follow this  
mike74

locking frame rate

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

I'm writing a maze game right now, and, although it executes at a decent speed on my home computer, it runs way too fast at the library. Thus, I need a good way of locking the frame rate. Does anyone know the best way of doing this? Should I create some sort of timer thread that calls a draw function 60 times a second? If you do this, what happens if the computer can't pull 60 frames per second? Mike C. http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
Advertisement
What you can do is check each time through the loop if at least 1/60th of a second has passed, if so do what needs to be done and draw to the screen. Though there are some problems with this approach.

Read this animation tutorial for a decent explanation of some techniques you can use.

Share this post


Link to post
Share on other sites
Actually, I think I have a very simple way of doing this. Basically, my idea is to have a separate Timer thread that updates the physics 60 times a second no matter what. Then the drawing routine can run as fast as possible, which is what it's doing right now anyway. The only real caveat I see is that I probably have it pulling well over 100 fps on some machines, so drawing beyond the refresh rate of the PC is a little wasteful. But, for right now, this should be good enough.

Mike C.
http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
That'll work, but it might not look too good on older computers - but there's not much you can do about that.

Share this post


Link to post
Share on other sites
Here's my method Mike:


#include <ctime> //Include this header
clock_t time_now;//declare these two variables somewhere so main loop can see them
clock_t movement_timer;
while(Msg.message != WM_QUIT && isPlaying)
{
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else
{
time_now = clock();
if(time_now - movement_timer > CLK_TCK/15)//The denominator is your FPS lock, anything done in this block will be done only that many frames per second
{
movement_timer = time_now;
}
//Rest of code here executes as fast as the comp can do it
}
}

Share this post


Link to post
Share on other sites
I can't seem to find a Visual C++ Timer class that looks reasonable.
I'll probably do something like the following:


DWORD WINAPI ThreadProc( LPVOID lpParam )
{
while (1)
{
Sleep(20);
physics_processing();
}
return 0;
}

void main(void)
{
DWORD dwThreadId;
CreateThread(
NULL, // default security attributes
0, // use default stack size
ThreadProc, // thread function
NULL, // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier

}


Does this seem okay? Or is there something more elegant?

Thanks.

Mike C.
http://www.coolgroups.com/zoomer/

Share this post


Link to post
Share on other sites
No need for seperate threads in this situation, I think. I lock my framerate using the method at this page. Works great, you don't have to do the interpolation, but I do and it makes nice animation with a whole lot fewer jumps and jags.

A lot of his articles are pretty interesting. Good luck. Oh, and a timer class... Well, check this out. It gives a Query Peformance Counter based timer class... I made some mistakes when I wrote it, but it does work, and you can probably polish it up yourself.

Share this post


Link to post
Share on other sites
There are several ways to control game speed. You can have your game loop update values of variables every nth frame or you can adjust the movement according to frame rate (i.e. if the character takes 30 steps a second, have him move 1 step if it is 30 fps; 2 steps if 15 fps…) This second method depends if the movement is linear or not. If there is acceleration, it becomes more complicated.

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!