![]() |
The LARC Game Engine
A Simple Game Engine from the Laboratory for Recreational Computing
|
The timer class. More...
#include <Timer.h>
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... | |
Static Private Attributes | |
static const uint64_t | m_nTicksPerSec = 10000000 |
Integer format represents time using 10,000,000 ticks per second. | |
![]() | |
static HWND | m_Hwnd = 0 |
Window handle. | |
static HINSTANCE | m_hInst = 0 |
Instance handle. | |
static bool | m_bExitSizeMove |
User just finished moving/resizing window. | |
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.
|
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 uint32_t LTimer::GetFPS | ( | ) | const |
Get the current frame rate.
const uint32_t LTimer::GetFrameCount | ( | ) | const |
const float LTimer::GetFrameTime | ( | ) | const |
const float LTimer::GetTime | ( | ) | 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 LTimer::SetFixedTimeStep | ( | bool | isFixedTimestep = true | ) |
Set whether to use fixed or variable timestep mode.
isFixedTimestep | true for fixed time step. |
void LTimer::SetFrameTime | ( | double | targetElapsed | ) |
Set target frame time for fixed time step mode.
targetElapsed | Target frame time in seconds. |
void LTimer::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. |