Game Development Reference
In-Depth Information
sf::Time and sf::Clock
As always, SFML is our friend, as it has two classes which work very well with time.
Time
is a class which holds a duration. This means that it doesn't tell us anything about the
current time of day or the time elapsed since the program started, it just has a variable
which holds a time amount. It could be five microseconds or it can be 10
months—anything that represents a period of time.
We can use the functions
sf::seconds()
,
sf::milliseconds()
, and
sf::microseconds()
to construct a time object from seconds, milliseconds, and mi-
croseconds, respectively. Once we have that object, we can use arithmetic operations (add,
subtract, and compare) on it. We can later convert the
Time
object into seconds, milli-
seconds, and microseconds by calling the functions
Time::asSeconds()
,
Time::asMilliseconds()
, and
Time::asMicroseconds()
. Here is an ex-
ample of how the
Time
class works:
The
Time
class is handy for storing time, but it doesn't provide us with a way of capturing
it. The
Clock
class provides an interface to measure elapsed time by using the OS clock.
It is simple to use as well.
Apart from
Clock::getElapsedTime()
, the
Clock
class has another function in-
side it and that is
Clock::restart()
, which returns the elapsed time and restarts the
clock at the same time.