SAGE
A Simple Academic Game Engine
Loading...
Searching...
No Matches
Sage::CTimer Class Reference

The timer class. More...

#include <SageTimer.h>

Inheritance diagram for Sage::CTimer:
Collaboration diagram for Sage::CTimer:

Public Member Functions

 CTimer ()
 Constructor.
 
template<typename t >
void Tick (const t &)
 One tick per frame.
 
const float GetFrameTime () const
 Get time in seconds since last Tick.
 
const float GetTime () const
 Get current time in seconds.
 
const uint32_t GetFrameCount () const
 Get total number of frames since start of the program.
 
const uint32_t GetFPS () const
 Get the current framerate.
 
void SetFixedTimeStep (bool=true)
 Set fixed or variable timestep mode.
 
void SetFrameTime (double)
 Set desired seconds per frame.
 

Protected Member Functions

void ResetElapsedTime ()
 Reset elapsed time in fixed timestep mode.
 
template<typename t >
void FixedTimeStepTick (const t &)
 One time tick in fixed timestep mode.
 
template<typename t >
void VariableTimeStepTick (const t &)
 One time tick in variable timestep mode.
 

Static Protected Member Functions

static double TicksToSeconds (uint64_t)
 Convert ticks to seconds.
 
static uint64_t SecondsToTicks (double)
 Convert seconds to ticks.
 

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.
 

Additional Inherited Members

- Static Protected Attributes inherited from Sage::CWindowDesc
static HWND m_Hwnd = 0
 Window handle.
 
static HINSTANCE m_hInst = 0
 Instance handle.
 
static bool m_bExitSizeMove = true
 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 Sage::CTimer::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.
Here is the caller graph for this function:

◆ GetFPS()

const uint32_t CTimer::GetFPS ( ) const

Get the current frame rate.

Returns
Frame rate in frames per second.

◆ GetFrameCount()

const uint32_t CTimer::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 CTimer::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.
Here is the call graph for this function:

◆ GetTime()

const float CTimer::GetTime ( ) const

Get number of seconds since the game started.

Returns
Total seconds since game started.
Here is the call graph for this function:

◆ ResetElapsedTime()

void CTimer::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.

Here is the caller graph for this function:

◆ SecondsToTicks()

uint64_t CTimer::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.
Here is the caller graph for this function:

◆ SetFixedTimeStep()

void CTimer::SetFixedTimeStep ( bool isFixedTimestep = true)

Set whether to use fixed or variable timestep mode.

Parameters
isFixedTimesteptrue for fixed time step.

◆ SetFrameTime()

void CTimer::SetFrameTime ( double targetElapsed)

Set target frame time for fixed time step mode.

Parameters
targetElapsedTarget frame time in seconds.
Here is the call graph for this function:

◆ Tick()

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

Process one time tick, either fixed or variable rate.

Parameters
updateCode to be called once per tick.
Here is the call graph for this function:

◆ TicksToSeconds()

double CTimer::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.
Here is the caller graph for this function:

◆ VariableTimeStepTick()

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

Process one time tick, variable rate.

Parameters
updateFunction to be called once per tick.
Here is the caller graph for this function: