Jump to content
  • Advertisement
Sign in to follow this  
Toji

Fixed Time-step logic check

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

If you don't mind, I'd like a second pair of eyes (or third, or tenth...) to look over my code and double check my logic.
double elapsed = 0.0;
double target = 0.0;
OnUpdate(0.0);
gameTime.Start();

while(!exitFlag) {
   // Handle all queued windows messages

   OnDraw(elapsed);
   elapsed = gameTime.Elapsed();
   //Allow the update to catch up if we've missed our interval target
   for(; target < elapsed; target += timeStepInterval) {
      OnUpdate(target);
   }
   OnEndFrame();
}
This is my (slightly simplified) code that should keep my update logic running at a fixed step. In the way of explanation on a couple of bits: gameTime: A high resolution timer that returns elapsed times as a double timeStepInterval: The fixed step interval we want (defaults to 1/60) OnDraw(): Processes all rendering code except for presenting the scene OnUpdate(): Processes all game logic, input handling, etc. OnEndFrame(): Presents the scene rendered in the previous OnDraw() As you might see, I've tried to design the loop with GPU parallelism in mind. All updates for the upcoming frame are done while the previous frame finishes drawing, which is only presented when the updates for the next frame are finished. This should (in theory) give the GPU and CPU ample time to finish what they need to do in a somewhat parallel fashion. Also, obviously this loop does not take into account the idea of the update consistently taking longer than timeStepInterval, at which point the game would grind to a slow and painful halt. This is a known "bug", and one I don't intend to address at this time. My main concern is that for most standard scenarios that this loop will perform well. Any seasoned game devs out there seeing any read flags I should be aware of?

Share this post


Link to post
Share on other sites
Advertisement
Is elapsed the time since last frame, or the time since the start of the program?
Is target an absolute time too (i.e. time since the start of the program)?
[edit] Reading it again it looks ok (assuming you are doing everything in absolute time) ;)
You might want some kind of frame-limiter though, so that you don't end up calling OnDraw 100 times per second.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Is elapsed the time since last frame, or the time since the start of the program?

Elapsed is the time since gameTime.Start(), so essentially the start of the program :)

Quote:
Is target an absolute time too (i.e. time since the start of the program)?

Yes. Sorry, I should have explained these things!

Quote:
You might want some kind of frame-limiter though, so that you don't end up calling OnDraw 100 times per second.

What's the harm, really? I would hesitate to put a cap simply for the sake of measuring FPS. Besides, in my actual code this loop contains a switch for several time step modes: Fixed step/Variable Draw (what you see here), Fixed Step/Fixed Draw (Identical but Draw is limited to timeStepInterval as well), and Variable Step (which just calls the functions with the elapsed time no matter how much time has passed). I only showed the one loop to keep things simple.

Thanks for the quick feedback!

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!