The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
CStepTimer Class Reference

The step timer class. More...

#include <StepTimer.h>

Inheritance diagram for CStepTimer:
CWindowDesc

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.
 

Detailed Description

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.

Member Function Documentation

◆ FixedTimeStepTick()

template<typename t >
void CStepTimer::FixedTimeStepTick ( const t &  update)
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.

Parameters
updateFunction to be called once per tick.

◆ GetElapsedSeconds()

const float CStepTimer::GetElapsedSeconds ( ) const

Get number of seconds that have elapsed since the last time Tick() was called. Ideally this is the last frame time, assuming that Tick() is called one per frame.

Returns
Elapsed seconds since last frame.

◆ GetFrameCount()

const uint32_t CStepTimer::GetFrameCount ( ) const

Get total number of times Tick() has been called since the game started. Ideally this is the number of frames rendered, assuming that Tick() is called one per frame.

Returns
Frame count.

◆ GetFramesPerSecond()

const uint32_t CStepTimer::GetFramesPerSecond ( ) const

Get the current frame rate.

Returns
Frame rate in frames per second.

◆ GetTotalSeconds()

const float CStepTimer::GetTotalSeconds ( ) const

Get number of seconds since the game started.

Returns
Total seconds since game started.

◆ ResetElapsedTime()

void CStepTimer::ResetElapsedTime ( )
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.

◆ SecondsToTicks()

uint64_t CStepTimer::SecondsToTicks ( double  seconds)
staticprotected

Convert seconds into ticks, the unit used by the Windows performance counter.

Parameters
secondsThe number of seconds.
Returns
The corresponding number of ticks.

◆ SetFixedTimeStep()

void CStepTimer::SetFixedTimeStep ( bool  isFixedTimestep = true)

Set whether to use fixed or variable timestep mode.

Parameters
isFixedTimesteptrue for fixed time step.

◆ SetTargetElapsedSeconds()

void CStepTimer::SetTargetElapsedSeconds ( double  targetElapsed)

Set target frame time for fixed time step mode.

Parameters
targetElapsedTarget frame time in seconds.

◆ Tick()

template<typename t >
void CStepTimer::Tick ( const t &  update)

Process one time tick, either fixed or variable rate.

Parameters
updateFunction to be called once per tick.

◆ TicksToSeconds()

double CStepTimer::TicksToSeconds ( uint64_t  ticks)
staticprotected

Convert ticks, the unit used by the performance counter, into seconds.

Parameters
ticksThe number of ticks.
Returns
The corresponding number of seconds.

◆ VariableTimeStepTick()

template<typename t >
void CStepTimer::VariableTimeStepTick ( const t &  update)
protected

Process one time tick, variable rate.

Parameters
updateFunction to be called once per tick.