Этот файл содержит неоднозначные символы Юникода, которые могут быть перепутаны с другими в текущей локали. Если это намеренно, можете спокойно проигнорировать это предупреждение. Используйте кнопку Экранировать, чтобы подсветить эти символы.
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.
Usage
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
Variable-step mode
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.
Fixed-step mode
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.
StepTimer
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.
Platform notes
Universal Windows Platform (UWP) apps and XBox One
The CoreApplication::Resuming
handler should call ResetElapsedTime.
Notes
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
Source
Further reading
For Use
- Universal Windows Platform apps
- Windows desktop apps
- Windows 11
- Windows 10
- Windows 8.1
- Windows 7 Service Pack 1
- Xbox One
Architecture
- x86
- x64
- ARM64
For Development
- Visual Studio 2022
- Visual Studio 2019 (16.11)
- clang/LLVM v12 - v18
- MinGW 12.2, 13.2
- CMake 3.20
Related Projects
DirectX Tool Kit for DirectX 12
Tools
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 opencode@microsoft.com with any additional questions or comments.