Jump to content
  • Advertisement
Sign in to follow this  
jimbogd

is using a timing thread a good idea?

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

Hello all, My first post this. I'm after getting frame rate independent timing in my game. I've read and understand the whole 'delta' thing and updating my game objects based on the frame time, but this produces some unwanted effects (like not being able to record/playback games easily, problems on slow hardware (bullets flying through enemies), and problems on fast hardware (float rounding problems). Has anyone tried using another thread as a means of updating game objects at a fixed frequency, and just using the main thread for rendering as fast as possible? Anyone had any experience of this, and the best way to go about it (with respect to mutexes and protecting global data)? Is this how the "big commercial" games do it? Many thanks Jimbo p.s. I'm using C++ with SDL

Share this post


Link to post
Share on other sites
Advertisement
The easiest way to do this in a single thread looks something like:

t_Time lastUpdateTime = CurrentTime();
while (true)
{
t_Time currentTime = CurrentTime();
if (currentTime - lastUpdateTime >= UPDATE_DURATION)
{
do
{
UpdateGameState();
lastUpdateTime += UPDATE_DURATION;
} while (currentTime - lastUpdateTime >= UPDATE_DURATION);

Render();
}
}

Every UpdateGameState() call represents a single UPDATE_DURATION, so it runs at a fixed rate. If rendering takes more than UPDATE_DURATION to finish, your UpdateGameState() will get called the appropriate number of times next time through, to catch up.

You render at most once every time you update. If you don't support interpolation of visual objects, then rendering more often is useless. However, if you do support interpolation, and want to render faster than you update, then you can move the Render() call outside of the if () statement, and use (currentTime - lastUpdateTime) / UPDATE_DURATION to figure out the interpolation amount.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!