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 | Static Private Attributes | List of all members
LTimer Class Reference

The timer class. More...

#include <Timer.h>

Inheritance diagram for LTimer:
LWindowDesc

Public Member Functions

 LTimer ()
 Constructor.
 
template<typename t >
void Tick (const t &)
 One tick per frame. More...
 
const float GetFrameTime () const
 Get time in seconds since last Tick. More...
 
const float GetTime () 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 GetFPS () const
 Get the current framerate. More...
 
void SetFixedTimeStep (bool=true)
 Set fixed or variable timestep mode. More...
 
void SetFrameTime (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_nElapsedTicks = 0
 Elapsed ticks in current frame.
 
uint64_t m_nTotalTicks = 0
 Total ticks since program started.
 
uint64_t m_nLeftOverTicks = 0
 For fixed timestep calculations.
 
uint32_t m_nFrameCount = 0
 Total number of frames.
 
uint32_t m_nFramesPerSec = 0
 Number of frames per second.
 
uint32_t m_nFramesThisSec = 0
 Number of frames so far in current second.
 
bool m_bFixedTimeStep = false
 Whether in fixed timestep mode.
 
uint64_t m_nTargetTicks = 0
 Desired ticks per frame (fixed timestep mode).
 
uint64_t m_nTimeDelta = 0
 Local variable for time interval.
 

Static Private Attributes

static const uint64_t m_nTicksPerSec = 10000000
 Integer format represents time using 10,000,000 ticks per second.
 
- Static Private Attributes inherited from LWindowDesc
static HWND m_Hwnd = 0
 Window handle.
 
static HINSTANCE m_hInst = 0
 Instance handle.
 
static bool m_bExitSizeMove
 User just finished moving/resizing window.
 

Detailed Description

The 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 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 LTimer::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.

◆ GetFPS()

const uint32_t LTimer::GetFPS ( ) const

Get the current frame rate.

Returns
Frame rate in frames per second.

◆ GetFrameCount()

const uint32_t LTimer::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.

◆ GetFrameTime()

const float LTimer::GetFrameTime ( ) 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.

◆ GetTime()

const float LTimer::GetTime ( ) const

Get number of seconds since the game started.

Returns
Total seconds since game started.

◆ ResetElapsedTime()

void LTimer::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 LTimer::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 LTimer::SetFixedTimeStep ( bool  isFixedTimestep = true)

Set whether to use fixed or variable timestep mode.

Parameters
isFixedTimesteptrue for fixed time step.

◆ SetFrameTime()

void LTimer::SetFrameTime ( double  targetElapsed)

Set target frame time for fixed time step mode.

Parameters
targetElapsedTarget frame time in seconds.

◆ Tick()

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

Process one time tick, either fixed or variable rate.

Parameters
updateFunction to be called once per tick.

◆ TicksToSeconds()

double LTimer::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 LTimer::VariableTimeStepTick ( const t &  update)
protected

Process one time tick, variable rate.

Parameters
updateFunction to be called once per tick.