Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Questions about SF::Clock  (Read 1329 times)

0 Members and 1 Guest are viewing this topic.

PewPew

  • Newbie
  • *
  • Posts: 22
    • View Profile
Questions about SF::Clock
« on: July 11, 2016, 03:58:24 pm »
So I bought the SFML GameDev by example.

I was just curious if why is the clock keeps getting called without being called in the game loop

        sf::Clock clock;
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                window.clear();
                window.draw(shape);
                window.display();
               
                sf::Time a = clock.restart();
                std::cout << a.asSeconds() << std::endl;

        }

The restart function puts the time counter back to zero so it should be the the output I will get is 0 up until the end. Does that mean the the clock has its own game loop under the hood?

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Questions about SF::Clock
« Reply #1 on: July 11, 2016, 04:48:29 pm »
sf::Clock::restart() actually returns the time that had elapsed before resetting to zero so your example code restarts the clock each frame but "a" holds the length of time since the last restart.
sf::Time time = clock.restart();
is effectively just short-hand for:
sf::Time time = clock.getElapsedTime();
clock.restart();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

PewPew

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Questions about SF::Clock
« Reply #2 on: July 12, 2016, 04:12:22 pm »
I see. it makes sense now.. Thanks