Translator: This article is the sixth in the translation cycle of the official SFML library guide. Past article can be found here. This series of articles aims to provide people who do not know the original language the opportunity to get acquainted with this library. SFML is a simple and cross-platform multimedia library. SFML provides a simple interface for developing games and other multimedia applications. The original article can be found here . Let's start.SFML time
Unlike many other libraries, in which time is represented by uint32 by the number of seconds, or by a fractional number of seconds, SFML does not impose any unit or type for time values. Instead, it leaves this choice to the user, providing the sf :: Time class. All classes and functions that manipulate time values use this class.
sf :: Time represents a time period (in other words, the time elapsed between two events). This is not a date and time class, which represents the current year / month / day / minute / second as a time stamp, it is simply a value that indicates the amount of time and provides a way to interpret this value depending on the context.
Time conversion
The sf :: Time value can be constructed from different source units: seconds, milliseconds, and microseconds. There are functions (they are not members of the class) that allow you to convert the value of each of these units into sf :: Time:
sf::Time t1 = sf::microseconds(10000); sf::Time t2 = sf::milliseconds(10); sf::Time t3 = sf::seconds(0.01f);
Notice that all three times are equal.
Similarly, sf: Time can be converted back to seconds, milliseconds and microseconds:
sf::Time time = ...; sf::Int64 usec = time.asMicroseconds(); sf::Int32 msec = time.asMilliseconds(); float sec = time.asSeconds();
We play with time
sf :: Time is just an amount of time, so this class supports arithmetic operations such as addition, subtraction, multiplication, and so on. Time can also be negative.
sf::Time t1 = ...; sf::Time t2 = t1 * 2; sf::Time t3 = t1 + t2; sf::Time t4 = -t3; bool b1 = (t1 == t2); bool b2 = (t3 > t4);
Time measurement
Now that we have seen how to manipulate the value of time in SFML, let's see how to do what any program needs: measure past time.
SFML has a very simple class for measuring time: sf :: Clock. There are only two methods in this class: getElapsedTime, designed to get the time since the last restart, and restart, to restart the clock.
sf::Clock clock; // ... sf::Time elapsed1 = clock.getElapsedTime(); std::cout << elapsed1.asSeconds() << std::endl; clock.restart(); ... sf::Time elapsed2 = clock.getElapsedTime(); std::cout << elapsed2.asSeconds() << std::endl;
Remember that the restart method also returns the elapsed time, so you can avoid a slight delay, which will be caused if getElapsedTim is called before restart.
Below is an example of using past tense to perform iterations of the game logic update cycle:
sf::Clock clock; while (window.isOpen()) { sf::Time elapsed = clock.restart(); updateGame(elapsed); ... }
Next article:
Streams .