![]() |
The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
|
The step timer class. More...
#include <StepTimer.h>
Public Member Functions | |
CStepTimer () | |
Constructor. | |
template<typename t > | |
void | Tick (const t &) |
One tick per frame. More... | |
const float | GetElapsedSeconds () const |
Get time in seconds since last Tick. More... | |
const float | GetTotalSeconds () const |
Get current time in seconds. More... | |
const uint32_t | GetFrameCount () const |
Get total number of frames since start of the program. More... | |
const uint32_t | GetFramesPerSecond () const |
Get the current framerate. More... | |
void | SetFixedTimeStep (bool=true) |
Set fixed or variable timestep mode. More... | |
void | SetTargetElapsedSeconds (double) |
Set desired seconds per frame. More... | |
Protected Member Functions | |
void | ResetElapsedTime () |
Reset elapsed time in fixed timestep mode. More... | |
template<typename t > | |
void | FixedTimeStepTick (const t &) |
One time tick in fixed timestep mode. More... | |
template<typename t > | |
void | VariableTimeStepTick (const t &) |
One time tick in variable timestep mode. More... | |
Static Protected Member Functions | |
static double | TicksToSeconds (uint64_t) |
Convert ticks to seconds. More... | |
static uint64_t | SecondsToTicks (double) |
Convert seconds to ticks. More... | |
Protected Attributes | |
LARGE_INTEGER | m_qpcFrequency = {0} |
Performance counter frequency in QPC units. | |
LARGE_INTEGER | m_qpcLastTime = {0} |
Performance counter time in QPC units. | |
uint64_t | m_qpcMaxDelta = 0 |
Performance counter maximum time interval in QPC units. | |
uint64_t | m_qpcSecondCounter = 0 |
Performance counter second counter in QPC units. | |
uint64_t | m_elapsedTicks = 0 |
Elapsed ticks in current frame. | |
uint64_t | m_totalTicks = 0 |
Total ticks since program started. | |
uint64_t | m_leftOverTicks = 0 |
For fixed timestep calculations. | |
uint32_t | m_frameCount = 0 |
Total number of frames. | |
uint32_t | m_framesPerSecond = 0 |
Number of frames per second. | |
uint32_t | m_framesThisSecond = 0 |
Number of frames so far in current second. | |
bool | m_isFixedTimeStep = false |
In fixed timestep mode (versus variable timestep). | |
uint64_t | m_targetElapsedTicks = 0 |
Desired ticks per frame in fixed timestep mode. | |
uint64_t | m_nTimeDelta = 0 |
Local variable for time interval. | |
The step timer class allows you to measure frame time and total time. It has a Tick function that you should call once per frame with the code that you want executed once per frame as a parameter. The step timer can be set into fixed timestep mode in which the frame time reported is always fixed. This is important in physics games since locking to the monitor frame rate is bound to give you unequal frame times because monitor frequency is almost always not what you expect. For example, a monitor advertized at 60Hz (which Windows will report is 60Hz) may actually be 59.3Hz.
|
protected |
Process one time tick, fixed rate. If the app is running very close to the target elapsed time (within 1/4 of a millisecond) just clamp the clock to exactly match the target value. This prevents tiny and irrelevant errors from accumulating over time. Without this clamping, a game that requested a 60 fps fixed update, running with vsync enabled on a 59.94 NTSC display, would eventually accumulate enough tiny errors that it would drop a frame. It is better to just round small deviations down to zero to leave things running smoothly.
update | Function to be called once per tick. |
const float CStepTimer::GetElapsedSeconds | ( | ) | const |
const uint32_t CStepTimer::GetFrameCount | ( | ) | const |
const uint32_t CStepTimer::GetFramesPerSecond | ( | ) | const |
Get the current frame rate.
const float CStepTimer::GetTotalSeconds | ( | ) | const |
Get number of seconds since the game started.
|
protected |
After an intentional timing discontinuity (for instance a blocking IO operation), call this to avoid having the fixed timestep logic attempt a set of catch-up Updat()e calls.
|
staticprotected |
Convert seconds into ticks, the unit used by the Windows performance counter.
seconds | The number of seconds. |
void CStepTimer::SetFixedTimeStep | ( | bool | isFixedTimestep = true | ) |
Set whether to use fixed or variable timestep mode.
isFixedTimestep | true for fixed time step. |
void CStepTimer::SetTargetElapsedSeconds | ( | double | targetElapsed | ) |
Set target frame time for fixed time step mode.
targetElapsed | Target frame time in seconds. |
void CStepTimer::Tick | ( | const t & | update | ) |
Process one time tick, either fixed or variable rate.
update | Function to be called once per tick. |
|
staticprotected |
Convert ticks, the unit used by the performance counter, into seconds.
ticks | The number of ticks. |
|
protected |
Process one time tick, variable rate.
update | Function to be called once per tick. |