Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Public Member Functions | Related Functions | List of all members
gf::Time Class Reference

Represents a time value. More...

#include <gf/Time.h>

Public Member Functions

 Time ()
 Default constructor. More...
 
 Time (std::chrono::steady_clock::duration duration)
 Constructor with a duration. More...
 
float asSeconds () const
 Return the time value as a number of seconds. More...
 
int32_t asMilliseconds () const
 Return the time value as a number of milliseconds. More...
 
int64_t asMicroseconds () const
 Return the time value as a number of microseconds. More...
 
std::chrono::steady_clock::duration asDuration () const
 Return the time value as a duration. More...
 

Related Functions

(Note that these are not member functions.)

Time seconds (float amount)
 Construct a time value from a number of seconds. More...
 
Time milliseconds (int32_t amount)
 Construct a time value from a number of milliseconds. More...
 
Time microseconds (int64_t amount)
 Construct a time value from a number of microseconds. More...
 
bool operator== (const Time &rhs, const Time &lhs)
 Equality operator. More...
 
bool operator!= (const Time &rhs, const Time &lhs)
 Inequality operator. More...
 
bool operator< (const Time &rhs, const Time &lhs)
 Lesser than operator. More...
 
bool operator> (const Time &rhs, const Time &lhs)
 Greater than operator. More...
 
bool operator<= (const Time &rhs, const Time &lhs)
 Lesser or equal operator. More...
 
bool operator>= (const Time &rhs, const Time &lhs)
 Greater or equal than operator. More...
 

Detailed Description

Represents a time value.

gf::Time encapsulates a time value in a flexible way. It allows to define a time value either as a number of seconds, milliseconds or microseconds. It also works the other way round: you can read a time value as either a number of seconds, milliseconds or microseconds.

By using such a flexible interface, the API doesn't impose any fixed type or resolution for time values, and let the user choose its own favorite representation.

Since they represent a time span and not an absolute time value, times can also be negative.

gf::Time is a thin wrapper around C++11 std::chrono::steady_clock.

Usage example:

gf::Time t1 = gf::seconds(0.1f);
int32_t milli = t1.asMilliseconds(); // 100
gf::Time t2 = gf::milliseconds(30);
int64_t micro = t2.asMicroseconds(); // 30000
gf::Time t3 = gf::microseconds(-800000);
float sec = t3.asSeconds(); // -0.8
See also
gf::Clock

Constructor & Destructor Documentation

gf::Time::Time ( )

Default constructor.

Sets the time value to zero.

gf::Time::Time ( std::chrono::steady_clock::duration  duration)
explicit

Constructor with a duration.

Parameters
durationA duration expressed with a std::chrono type

Member Function Documentation

std::chrono::steady_clock::duration gf::Time::asDuration ( ) const
inline

Return the time value as a duration.

Returns
Time as a duration (std::chrono type)
int64_t gf::Time::asMicroseconds ( ) const

Return the time value as a number of microseconds.

Returns
Time in microseconds
See also
asSeconds(), asMilliseconds()
int32_t gf::Time::asMilliseconds ( ) const

Return the time value as a number of milliseconds.

Returns
Time in milliseconds
See also
asSeconds(), asMicroseconds()
float gf::Time::asSeconds ( ) const

Return the time value as a number of seconds.

Returns
Time in seconds
See also
asMilliseconds(), asMicroseconds()

Friends And Related Function Documentation

Time microseconds ( int64_t  amount)
related

Construct a time value from a number of microseconds.

Parameters
amountNumber of microseconds
Returns
Time value constructed from the amount of microseconds
See also
seconds(), milliseconds()
Time milliseconds ( int32_t  amount)
related

Construct a time value from a number of milliseconds.

Parameters
amountNumber of milliseconds
Returns
Time value constructed from the amount of milliseconds
See also
seconds(), microseconds()
bool operator!= ( const Time rhs,
const Time lhs 
)
related

Inequality operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time and the second time are different
bool operator< ( const Time rhs,
const Time lhs 
)
related

Lesser than operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is lesser than the second time
bool operator<= ( const Time rhs,
const Time lhs 
)
related

Lesser or equal operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is lesser or equal than the second time
bool operator== ( const Time rhs,
const Time lhs 
)
related

Equality operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time and the second time are the same
bool operator> ( const Time rhs,
const Time lhs 
)
related

Greater than operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is greater than the second time
bool operator>= ( const Time rhs,
const Time lhs 
)
related

Greater or equal than operator.

Parameters
rhsFirst time
lhsSecond time
Returns
True if the first time is greater or equal than the second time
Time seconds ( float  amount)
related

Construct a time value from a number of seconds.

Parameters
amountNumber of seconds
Returns
Time value constructed from the amount of seconds
See also
milliseconds(), microseconds()