-
Notifications
You must be signed in to change notification settings - Fork 513
StepTimer
The StepTimer class is a helper for managing a timed game loop.
The standard Universal Windows Platform app, Windows 8.1 Store DirectX, Windows phone 8.1, and Xbox One Visual Studio templates all make use of
StepTimer
as well.
The typical usage for the StepTimer helper class is to call Tick
once before you render each frame. The Tick
method takes a callback which is invoked to perform time-based updates in your game or application.
#include <Windows.h>
#include "StepTimer.h"
DX::StepTimer s_timer;
void Update(DX::StepTimer const& timer)
{
float delta = float(timer.GetElapsedSeconds());
// Do your game update here
}
void Render()
{
// Do your frame render here
}
void Tick()
{
s_timer.Tick([&]()
{
Update(s_timer);
});
Render();
}
// Your main loop then calls ``Tick`` in a loop
By default, the StepTimer class will invoke the callback given to Tick
once each frame, and the callback can query the StepTimer class for the duration of time since the last update.
StepTimer will take a number of precautions to ensure the delta time is never negative, nor is it so large (1/10th of a second is the default upper-bound) as to cause huge jumps that might otherwise disrupt debugging or suspending behavior.
Alternatively, the StepTimer class can be put into a fixed-step mode as follows:
s_timer.SetFixedTimeStep(true);
s_timer.SetTargetElapsedSeconds(1.f / 60.f);
In this mode, Tick
will invoke the callback as many times as needed to 'catch-up' with the expected fixed-update time. The callback can then rely on a steady (and fixed) duration which can increase robustness of physics and other simulation processing.
The fixed-step duration can be set by using SetTargetElapsedTicks or SetTargetElapsedSeconds. Note that StepTimer defines ticks as 10,000,000 per second (see TicksPerSecond
).
If a larger than usual delay is expected, the code should call ResetElapsedTime to avoid the fixed-step Tick
calling the update method a large number of times to 'catch-up'.
The overall loop time is limited by the frame rate of
Present
for standard 'render loop' usage. Otherwise, you can 'Tick' an instance ofStepTimer
on it's own thread at your own rate limited only by the system timer resolution.
The class provides the following accessors:
-
GetElapsedTicks: Returns the elapsed time in ticks.
-
GetElapsedSeconds: Returns the elapsed time in seconds.
-
GetTotalTicks: Returns the total time in ticks.
-
GetTotalSeconds: Returns the total time in seconds.
-
GetFrameCount: Returns the frame count (i.e. one frame per call to
Tick
). -
GetFramesPerSecond: Returns the frames per second. This value is updated once per second, so it's not instantaneous. It can return 0 if there's not been enough time since the start of the program or since the last call to ResetElapsedTime.
-
TicksToSeconds: Static method to convert ticks to seconds.
-
SecondsToTicks: Static method to covert seconds to ticks.
The CoreApplication::Resuming
handler should call ResetElapsedTime.
Ticks are stored in unsigned 64-bit integers and second values are stored in double
. Why it is important to avoid using 32-bit values or single-precision floats is addressed in this article: Don’t Store That in a Float
All content and source code for this package are subject to the terms of the MIT License.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Windows 8.1
- Xbox One
- x86
- x64
- ARM64
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v18
- MinGW 12.2, 13.2
- CMake 3.20