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

Author Topic: How do I pause an sf::Clock? (SFML 2.0)  (Read 17154 times)

0 Members and 2 Guests are viewing this topic.

someguyk

  • Newbie
  • *
  • Posts: 4
    • View Profile
How do I pause an sf::Clock? (SFML 2.0)
« on: June 25, 2013, 06:24:44 pm »
Hi,

I would like to know how to pause a clock.

At the start of my program I have:
sf::Clock timer_clock;
sf::Time timer_elapsedTime = timer_clock.restart();
float timer_currentTime = 0.0f;

This is how I update the timer:
timer_elapsedTime = timer_clock.getElapsedTime();
timer_currentTime = timer_elapsedTime.asSeconds();
// (...) here I set timer_currentTime as a string but it's not relevant.

Everything is working fine.

But now I want to pause the clock when my game is in a "paused game state", but it keeps running when my game is paused. Since I haven't found out how to pause the clock, do I have to use arithmetics or something?

Please guys, I need your help. My knowledge is depleted :(
Thank you so much!
« Last Edit: June 25, 2013, 06:32:06 pm by someguyk »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #1 on: June 25, 2013, 06:26:43 pm »
sf::Clock just measures time, you can't pause it, you have to write code to do that or use Thor.
Back to C++ gamedev with SFML in May 2023

someguyk

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #2 on: June 25, 2013, 06:31:42 pm »
Thank you!

Do you know by any chance if there is such code already available? I would rather use that code instead of using a new C++ library just for the timer.

I have Googled like crazy (after trying for myself and failing) but couldn't find anything.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #3 on: June 25, 2013, 06:45:26 pm »
FRex mentioned the Thor library, alternatively you can use my impl from here : http://mantognini.github.io/sftools/ You can take only what you need as it's a header-only library.
SFML / OS X developer

someguyk

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #4 on: June 25, 2013, 07:08:05 pm »
Thanks, I'll take a look at that.

someguyk

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #5 on: June 25, 2013, 07:18:43 pm »
I'm using your Chronometer header now and everything is working perfectly.

Thank you so much :)

magneonx

  • Full Member
  • ***
  • Posts: 141
    • MSN Messenger - magnumneon04@hotmail.com
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #6 on: June 27, 2013, 07:12:39 am »
Great tools!!!

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #7 on: August 05, 2016, 05:37:00 pm »
I know this topic is super old, but I wanted to thank you, Hiura, for the sfTools!  I had the exact same problem as OP and your code fixed everything!

I just copied your code from GitHub, I hope that's alright.  This is at the top of my Chronometer.h:
// This code written by Hiura from the SFML development commnunity.
// Many thanks to them!

I hope that's cool!  Lemme know if not ;)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #8 on: August 05, 2016, 05:59:30 pm »
You're welcome!  :D
SFML / OS X developer

OmaManfred

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #9 on: February 25, 2017, 09:07:30 pm »
If someone is interested in a pretty lightweight solution:
namespace sf {
        struct Timer {
                sf::Clock mC;
                float runTime;
                bool bPaused;

                Timer() {
                        bPaused = false;
                        runTime = 0;
                        mC.restart();
                }

                void Reset() {
                        mC.restart();
                        runTime = 0;
                        bPaused = false;
                }

                void Start() {
                        if (bPaused) {
                                mC.restart();
                        }
                        bPaused = false;
                }

                void Pause() {
                        if (!bPaused) {
                                runTime += mC.getElapsedTime().asSeconds();
                        }
                        bPaused = true;
                }

                float GetElapsedSeconds() {
                        if (!bPaused) {
                                return runTime + mC.getElapsedTime().asSeconds();
                        }
                        return runTime;
                }
        };
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #10 on: February 25, 2017, 09:26:11 pm »
It is not recommended to use the sf namespace. As it will make people wrongfully assume that it's part of SFML.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OmaManfred

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #11 on: February 25, 2017, 09:59:49 pm »
Yeah no clue why I put it in sf namespace. But you can just remove the namespace.

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #12 on: January 16, 2019, 07:35:46 pm »
Hi folks.  Yes, I'm replying to a very old thread.  If I should make a new one instead, let me know and I'll be happy to.

I found great success using sftools in the past but noticed on its repo: https://github.com/mantognini/sftools that it hasn't been updated in several years.  I suppose I'm just curious as to its current status.  I can understand why these tools aren't a part of SFML itself, but I did notice that it's specifically mentioned (and even documented somewhat) in SFML's documentation on github, and that the last edit to such documentation was done by eXpl0it3r: https://github.com/SFML/SFML/wiki/Source:-Chronometer

Is it "officially" part of the SFML project, if not the library itself?  If so, are there any plans to update it for SFML 2.5?  I'm not knowledgeable enough to look at the source code and determine whether anything would need to be updated.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #13 on: January 16, 2019, 09:12:20 pm »
There are two misunderstandings here.

Just because something has aged doesn't mean it won't work totally fine. sf::Clock and sf::Time have not changed in the past years and SFML 2.x is backwards compatible, so if it worked then, it will work now. ;)

The Wiki on GitHub is a community wiki, anyone can edit and contribute, as such it's by definition not an official code repository. My name is listed on a bunch of pages as the author, but that's just because I once brought a structure into the wiki, moving pages around and GitHub didn't track the history fully.

If there's something you have noticed not to work woth sftools, I suggest to open an GitHub issue on sftools repository. If there's nothing broken, then feel free to ignore the SFML 2.0 label. ;)

It's generally best to create a new thread unless it's 100% on topic. You can always just mention the thread you found and seen some parallels.
« Last Edit: January 17, 2019, 07:10:59 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gws923

  • Jr. Member
  • **
  • Posts: 60
    • View Profile
Re: How do I pause an sf::Clock? (SFML 2.0)
« Reply #14 on: January 17, 2019, 05:53:23 pm »
Thanks for the explanation, that all makes sense.  FWIW I just tried sftools::Chronometer in a new 2.5 project and it works fine!  (And next time I'll make a new thread)

 

anything