TurtleBrains  0.3.1
High quality, portable, C++ framework for rapid 2D game development.
TurtleBrains::Game::GameTimer Class Reference

#include <tb_game_timer.h>

Public Types

typedef tbCore::uint32 Milliseconds
 

Public Member Functions

 GameTimer (void)
 
 GameTimer (const Milliseconds &valueMilliseconds)
 
 GameTimer (const GameTimer &other)
 
 ~GameTimer (void)
 
GameTimeroperator= (const GameTimer &other)
 
bool DecrementStep (void)
 
bool IncrementStep (const GameTimer &maximumValue=Infinity())
 
bool IsZero (void) const
 
const MillisecondsGetElapsedTime (void) const
 
const MillisecondsGetRemainingTime (void) const
 
float GetPercentageOf (const GameTimer &timeValue) const
 
TurtleBrains::Game::GameTimer operator+ (const TurtleBrains::Game::GameTimer &right) const
 
TurtleBrains::Game::GameTimeroperator+= (const TurtleBrains::Game::GameTimer &right)
 
TurtleBrains::Game::GameTimer operator- (const TurtleBrains::Game::GameTimer &right) const
 
TurtleBrains::Game::GameTimeroperator-= (const TurtleBrains::Game::GameTimer &right)
 

Static Public Member Functions

static GameTimer Zero (void)
 
static GameTimer Infinity (void)
 
static float ToSeconds (const Milliseconds &valueMilliseconds)
 
static Milliseconds ToMilliseconds (const float &valueSeconds)
 
static void SetMillisecondsPerStep (const Milliseconds &millisecondsPerStep)
 
static Milliseconds GetMillisecondsPerStep (void)
 
static float GetSecondsPerStep (void)
 

Detailed Description

The GameTimer provides a simple interface for creating a timer to manually increment and decrement each Simulate() step for game logic. Internally milliseconds are stored and can be greater than or equal to zero allowing use cases to count the elapsed time since an event (by setting the timer to zero and incrementing each step), or to count down to some event.

Member Typedef Documentation

The storage type of the milliseconds used internally in the GameTimer.

Constructor & Destructor Documentation

TurtleBrains::Game::GameTimer::GameTimer ( void  )

Constructs a GameTimer object with an internal value of 0 milliseconds elapsed/remaining.

TurtleBrains::Game::GameTimer::GameTimer ( const Milliseconds valueMilliseconds)

Constructs a GameTimer object with the desired value in milliseconds.

Parameters
valueMillisecondsThe initial value of the timer object, in milliseconds, to track time from.
TurtleBrains::Game::GameTimer::GameTimer ( const GameTimer other)

Copy constructor to create a GameTimer object from another object, copying all internal values.

TurtleBrains::Game::GameTimer::~GameTimer ( void  )

Destructs a GameTimer object, which is minimal since the timer does not need to release any resources.

Member Function Documentation

bool TurtleBrains::Game::GameTimer::DecrementStep ( void  )

Decrement the value of the timer by the milliseconds in a single Simulate step and returns true if the internal value is now at 0, including if it was already at 0.

Note
Will not allow the timer object to underflow, and will remain at 0 even if step size is greater than value.
const Milliseconds& TurtleBrains::Game::GameTimer::GetElapsedTime ( void  ) const

Returns the value of the timer in milliseconds, typically used with a timer object that is counting upwards using IncrementStep() for clarity that some amount of time has passed.

static Milliseconds TurtleBrains::Game::GameTimer::GetMillisecondsPerStep ( void  )
static

Returns the number of milliseconds per Simulate step in TurtleBrains as set by SetMillisecondsPerStep().

float TurtleBrains::Game::GameTimer::GetPercentageOf ( const GameTimer timeValue) const

Computes a percentage value from 0.0f to 1.0f where the timer object is between 0 and the timeValue passed in. If the internal milliseconds value is larger than that of the timeValue parameter, the returned percentage may be greater than 1.0f.

Parameters
timeValueThe range of milliseconds representing "100 percent" or a return value of 1.0f.
Note
The returned result may be greater than 1.0f if the timers value is greater than the provided value.
const Milliseconds& TurtleBrains::Game::GameTimer::GetRemainingTime ( void  ) const

Returns the value of the timer in milliseconds, typically used with a timer object that is counting down towards zero using DecrementStep() for clarity that some amount of time may be remaining.

static float TurtleBrains::Game::GameTimer::GetSecondsPerStep ( void  )
static

Returns the partial seconds per Simulate step in TurtleBrains as set by SetMillisecondsPerStep().

Note
The returned value will be converted from milliseconds and in the process precision may be lost, the returned value may be slightly smaller than the actual value. Ex. 0.0018 seconds may become 1 millisecond.
bool TurtleBrains::Game::GameTimer::IncrementStep ( const GameTimer maximumValue = Infinity())

Increments the value of the timer by the milliseconds in a single Simulate step so that GetElapsedTime() can be used to track how long the timer has been running for. This will also prevent the timer from overflowing should it ever get large enough, or approach a value greater than maximumValue supplied. Returns true if the increment reached or stopped at the maximumValue during the call, false otherwise.

Parameters
maximumValueThe highest value the timer can increment to, defaults to kInfinity, the maximum value possible.
Note
It is possible that the timer overflows, in this version of the GameTimer the millisecond value is stored as a 32-bit unsigned integer value which will hold approximately 49 days. This may be changed to 64-bit in the future.
static GameTimer TurtleBrains::Game::GameTimer::Infinity ( void  )
static

A constant GameTimer object representing the maximum value for the timer, infinity.

bool TurtleBrains::Game::GameTimer::IsZero ( void  ) const

Checks if the timer is at zero returning true if it is or false it if the value is greater than 0.

GameTimer& TurtleBrains::Game::GameTimer::operator= ( const GameTimer other)

Set the value of the GameTimer to the time value of another timer object.

static void TurtleBrains::Game::GameTimer::SetMillisecondsPerStep ( const Milliseconds millisecondsPerStep)
static

Changes the number of milliseconds per Simulate step in TurtleBrains.

Parameters
millisecondsPerStepThe number of milliseconds that pass for each simulate step. 10 milliseconds is the default per step and results in Simulate being called at roughly 100hz.
static Milliseconds TurtleBrains::Game::GameTimer::ToMilliseconds ( const float &  valueSeconds)
static

Converts a time value from partial seconds to milliseconds, being returned as an integer type.

Note
The returned value will be the input parameter converted to milliseconds and in the process precision may be lost, the returned value may be slightly smaller than the input. 0.0018 seconds may become 1 millisecond.
static float TurtleBrains::Game::GameTimer::ToSeconds ( const Milliseconds valueMilliseconds)
static

Converts a value in milliseconds to a value in partial seconds, being returned as a floating point.

Note
The actual assigned value will be converted to seconds and in the process some precision may be lost.
static GameTimer TurtleBrains::Game::GameTimer::Zero ( void  )
static

A constant GameTimer object representing a time value of zero.