SFML community forums

Help => System => Topic started by: ahmads1990 on April 08, 2020, 07:43:17 pm

Title: making time counte using clock
Post by: ahmads1990 on April 08, 2020, 07:43:17 pm
i have  wrote a time counter function to display time on window but i i want help to right it in cleaner form
Text Menu::returnTime(Clock clock)
{
        //to set time variable to the elapsed time  
        time = clock.getElapsedTime();

        //set the int seconds to elapsed time - 60 if minutes have passed
        //bec elapsed after 1 minute doesnt reset will be 61 seconds
        seconds = time.asSeconds()- 60 * minutes;

        //if seconds less than 10 draw extra 0 so it look like time: 0:00 not time : 0: 0
        if (seconds < 10)
                str = "Time: " + to_string(minutes) + ":" + "0" + to_string(seconds);
        else
                str = "Time: " + to_string(minutes) + ":" +  to_string(seconds);  //else set it normaly cast variables to strings

       
        if (seconds == 60)
                minutes++;


        //set the text string to next time string
        timeText.setString(str);

        return timeText;
}

and also how i can do reset clock if seconds = 60
Title: Re: making time counte using clock
Post by: eXpl0it3r on April 09, 2020, 09:08:41 pm
Can you rephrase the question, as I'm unsure what you're trying to achieve exactly?
Title: Re: making time counte using clock
Post by: ahmads1990 on April 10, 2020, 05:51:54 pm
Can you rephrase the question, as I'm unsure what you're trying to achieve exactly?

i want to know how to reset the clock after it hit 60 seconds so  i can get rid off this line

//set the int seconds to elapsed time - 60 if minutes have passed
        //bec elapsed after 1 minute doesnt reset will be 61 seconds
        seconds = time.asSeconds()- 60 * minutes;
Title: Re: making time counte using clock
Post by: Hapax on April 10, 2020, 07:18:12 pm
clock.restart()

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Clock.php#a123e2627f2943e5ecaa1db0c7df3231b
Title: Re: making time counte using clock
Post by: ahmads1990 on April 10, 2020, 10:44:28 pm
clock.restart()

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Clock.php#a123e2627f2943e5ecaa1db0c7df3231b
why i cant restart the clock from the method inside the class directly
but it worked when i set the parameter to (Clock & clock)
Title: Re: making time counte using clock
Post by: Hapax on April 11, 2020, 06:14:29 pm
With the ampersand (&), the clock is passed to the function "by reference" and therefore everything done to it inside the function happens to the original.
Without, the clock is passed "by value"; this makes a copy of the clock to worth with inside the function but is destroyed when the function ends. None of the changes to this copy affect the original.