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

Author Topic: making time counte using clock  (Read 252 times)

0 Members and 1 Guest are viewing this topic.

ahmads1990

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
making time counte using clock
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 9573
    • View Profile
    • development blog
    • Email
Re: making time counte using clock
« Reply #1 on: April 09, 2020, 09:08:41 pm »
Can you rephrase the question, as I'm unsure what you're trying to achieve exactly?
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
——————————————————————
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/

ahmads1990

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: making time counte using clock
« Reply #2 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;

Hapax

  • Hero Member
  • *****
  • Posts: 2999
  • My number of posts is shown in hexadecimal.
    • View Profile
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

ahmads1990

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: making time counte using clock
« Reply #4 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)

Hapax

  • Hero Member
  • *****
  • Posts: 2999
  • My number of posts is shown in hexadecimal.
    • View Profile
Re: making time counte using clock
« Reply #5 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.
Selba Ward - SFML drawables
Kairos - Timing Library
Rectangular Boundary Collision - Rectangular SAT Collision

@Hapaxiation - Hapaxia on Twitter

 

anything