# 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.

## 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 on other sites
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)?
 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 on other sites
Quote:
 Original post by HodgmanIs 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!

1. 1
2. 2
3. 3
4. 4
Rutin
11
5. 5

• 12
• 19
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632665
• Total Posts
3007724
• ### Who's Online (See full list)

There are no registered users currently online

×