📜 ⬆️ ⬇️

2.1 Processing time



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.

Table of contents:
0.1 Introduction

1. Getting Started
')
  1. SFML and Visual Studio
  2. SFML and Code :: Blocks (MinGW)
  3. SFML and Linux
  4. SFML and Xcode (Mac OS X)
  5. Compiling SFML with CMake

2. System Module

  1. Processing time
  2. Streams
  3. Work with custom data streams

3. Window Module

  1. Opening and managing windows
  2. Event handling
  3. Work with the keyboard, mouse and joysticks
  4. Using OpenGL

4. Graphics Module

  1. Drawing 2D objects
  2. Sprites and textures
  3. Text and Fonts
  4. Forms
  5. Designing your own objects using arrays of vertices
  6. Position, Rotation, Scale: Transform Objects
  7. Adding special effects with shaders
  8. 2D camera control and view

5. Audio module

  1. Playing sounds and music
  2. Audio recording
  3. Custom audio streams
  4. Spitalization: sounds in 3D

6. Network module

  1. Socket communication
  2. Use and Expansion Packages
  3. Web requests using HTTP
  4. File Transfer Using FTP


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 .

Source: https://habr.com/ru/post/279347/


All Articles