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

Author Topic: [Help] I don't understand sf::Time, sf::Clock  (Read 2143 times)

0 Members and 1 Guest are viewing this topic.

S0urce

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
[Help] I don't understand sf::Time, sf::Clock
« on: November 03, 2019, 08:47:23 pm »
Hello everyone !
 
I'm new on this community-platform and this is also my first post, so please be gentle.

I'm using a SFML blueprint for my project, but I don't understand everything in it. By that, I mean specifically sf::Clock and sf::Time. I have spent so much sf::time (haha, get it?) reading and trying to understand it, that I in the end gave up and came here for help.

This is the blueprint I'm talking about: read attachment.

Now, there're two while-loops in it. The red is just for keeping the window open/program running, but the yellow one has something to do with time. Mind explaining?

Also, I want to create a timer. That show the time in seconds and gets reset if time is X.
« Last Edit: November 03, 2019, 08:52:16 pm by S0urce »

Stauricus

  • Full Member
  • ***
  • Posts: 177
    • View Profile
    • Email
Re: [Help] I don't understand sf::Time, sf::Clock
« Reply #1 on: November 04, 2019, 10:30:12 am »
I don't think there is much to explain. sf::Time is a specific time, and sf::Clock is a time that change automatically as time passes.

You can make something like:
sf::Clock clock;
sf::String string;
if (clock.getElapsedTime().asSeconds >= sf::seconds(10){
    clock.reset();
}
string.setText(clock.getElapsedTime().asSeconds());
 

I typed from my phone, so I didnt tested it ?

Also, did you check the tutorials?
https://www.sfml-dev.org/tutorials/2.5/system-time.php
« Last Edit: November 04, 2019, 10:35:35 am by Laurent »

S0urce

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: [Help] I don't understand sf::Time, sf::Clock
« Reply #2 on: November 04, 2019, 12:25:45 pm »
Hi !

I tried, in different ways, but the code did not work.
I've read the material/tutorials and they are aweful.

Stauricus

  • Full Member
  • ***
  • Posts: 177
    • View Profile
    • Email
Re: [Help] I don't understand sf::Time, sf::Clock
« Reply #3 on: November 04, 2019, 03:14:33 pm »
the tutorials are not awful, but you need to have the slightest idea of what you are doing.
what exactly didnt work?

Hapax

  • Hero Member
  • *****
  • Posts: 2999
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: [Help] I don't understand sf::Time, sf::Clock
« Reply #4 on: November 09, 2019, 07:31:06 pm »
The 'yellow' loop is a way of processing fixed timesteps when frame times even though frame times are not actually this length of time.
For more information, this article could help. Be aware that how this works can be rather complicated or confusing at first.

For a more simple "blueprint" that is easier to understand, try:

// ...
sf::Clock clock
sf::Time frameTime{ sf::Time::Zero };

while (window.isOpen())
{
    // ...

    frameTime = clock.restart();

    window.clear();

    window.display();
}

// ...

You can see that there is only one line instead of that yellow loop and it basically stores how long it took since last time it was here (each time the clock is restarted, it returns how long it was running).

Then, you can simply multiply any motions or other values that change over time by that frame time - commonly by it in seconds:
position += velocity * frameTime.asSeconds();
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

S0urce

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: [Help] I don't understand sf::Time, sf::Clock
« Reply #5 on: November 28, 2019, 01:18:33 pm »
The 'yellow' loop is a way of processing fixed timesteps when frame times even though frame times are not actually this length of time.
For more information, this article could help. Be aware that how this works can be rather complicated or confusing at first.

For a more simple "blueprint" that is easier to understand, try:

// ...
sf::Clock clock
sf::Time frameTime{ sf::Time::Zero };

while (window.isOpen())
{
    // ...

    frameTime = clock.restart();

    window.clear();

    window.display();
}

// ...

You can see that there is only one line instead of that yellow loop and it basically stores how long it took since last time it was here (each time the clock is restarted, it returns how long it was running).

Then, you can simply multiply any motions or other values that change over time by that frame time - commonly by it in seconds:
position += velocity * frameTime.asSeconds();


Thanks for your reply. I'm getting better;

sf::Time timer = sf::Time::Zero;
timer += clock.getElapsedTime();   // This happen while bool gameOver is false.
text.setString("Time: " + std::to_string(this->timer.asSeconds())); // This is what I have in my update-loop.

But how do I cut decimals off timer? I would like to decide if 1s, or 1.5s, or 1.52s (you get the picture) should be presented instead of 1.52123124s

Hapax

  • Hero Member
  • *****
  • Posts: 2999
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: [Help] I don't understand sf::Time, sf::Clock
« Reply #6 on: December 01, 2019, 10:01:52 pm »
This would be more standard C stuff but I'll point you in the direction to figure it out  ;)

If whole number of seconds only, you can just convert to integer (probably unsigned integer).

If tenths or hundredths, you could multiple by ten or a hundred, convert to integer and then place a decimal place before the last one or two digits of that value. This seems very convoluted, of course. Especially since...

Change the format in a stream. You can use a string stream to work with strings as streams (similar to std::cout, for example) and you can specify its "decimal precision".
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter