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

Author Topic: [Newbie] How to use sf::Clock correctly?  (Read 8368 times)

0 Members and 2 Guests are viewing this topic.

Gauzr

  • Newbie
  • *
  • Posts: 20
    • View Profile
[Newbie] How to use sf::Clock correctly?
« on: September 09, 2015, 06:29:20 pm »
Hi.
Before I request for help, please bear in mind I'm pretty new to SFML so don't have a lot of knowledge on it.
I have a function in a game I am making where a clock is needed, and I want to use sf::Clock but I don't really know how. I tried but the values I get appear to be only numbers 59-64 and I don't know why. Here's my code:
sf::Clock startClock;
...
float clocktime = startClock.getElapsedTime().asSeconds();
        if (clocktime >= 5)
        {
                windowName.close();
        }

        std::cout << clocktime;
 

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: [Newbie] How to use sf::Clock correctly?
« Reply #1 on: September 09, 2015, 08:24:08 pm »
The official tutorial explains enough about sf::Clock and sf::Time usage.

You should use sf::Clock only once (initialize at a start of project) and you use sf::Time to manipulate the values you receive from sf::Clock.

As said in the tutorial, the main usage of sf::Clock and sf::Time is to update the game logic:


sf::Clock clock;
while (window.isOpen())
{
    sf::Time elapsed = clock.restart();
    updateGame(elapsed);
    ...
}
 

Please note that there are other, better ways to update game logic, like using fixed time steps. But if you are just starting out, the code above should be enough.
Also in this case, it would be smart to use vsync or to set the framerate limit.

You should also read (and bookmark) this: http://gafferongames.com/game-physics/fix-your-timestep/ - it is really helpful, especially when you want to start with more complex projects. :)

Gauzr

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: [Newbie] How to use sf::Clock correctly?
« Reply #2 on: September 10, 2015, 07:53:37 pm »
@Brax Thanks for being the only one that bothered to answer my newbie question ;) I'm pretty sure I have clasped the concept now, cheers.

Satus

  • Guest
Re: [Newbie] How to use sf::Clock correctly?
« Reply #3 on: September 10, 2015, 10:01:08 pm »
@Brax Thanks for being the only one that bothered to answer my newbie question ;) I'm pretty sure I have clasped the concept now, cheers.

All you had to do is to read documentation.

Gauzr

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: [Newbie] How to use sf::Clock correctly?
« Reply #4 on: September 11, 2015, 11:20:57 pm »
@Brax Thanks for being the only one that bothered to answer my newbie question ;) I'm pretty sure I have clasped the concept now, cheers.

All you had to do is to read documentation.

Bit late to tell me that isn't it. Good job.

 

anything