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

Author Topic: sf::Time and sf::Clock  (Read 1364 times)

0 Members and 1 Guest are viewing this topic.

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
sf::Time and sf::Clock
« on: March 28, 2016, 08:10:27 am »
Hello everyone, i am trying to display a window for 3 seconds,after that it closes. yet why does the window displays on random amount of time. it does not display the way i wanted to. Can anyone please enlighten me of my rather stup!D error.  it does display the window but not exactly 3 seconds. sometimes it jumps to 3.xx to greater than 3 seconds. can somebody enlighten me with this please?

sf::Clock clock;
            sf::Time limit = sf::milliseconds(3000);
            sf:: Time elapsed;

...loop...
elapsed=clock.getElapsedTime();
             while(window5.pollEvent(event))
                {
                    //std::cout<<elapsed.asMilliseconds()<<std::endl;
                    if(elapsed.asMilliseconds()>limit.asMilliseconds())
                            {
                                window5.close();
                            }
                }
window5.display();
...end loop...
-dove96-

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10827
    • View Profile
    • development blog
    • Email
Re: sf::Time and sf::Clock
« Reply #1 on: March 28, 2016, 10:10:42 am »
You get the elapsed time once, so when the loop starts both the limit and elapsed time have been set and all you do is compare two fixed values. On top of that you call getElapsedTime before the loop, so whatever time has past since the creation of the clock object and that call, will be returned.
What you want to do is call restart() before the loop and then simply check against getElapsedTime() during the iteration, the elapsed sf::Time object is not necessarily needed.

Also please make use of the [code=cpp][/code] tags.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: sf::Time and sf::Clock
« Reply #2 on: March 28, 2016, 03:25:54 pm »
hi exploiter, i get a little bit confused when you say:

"you get the elapsed time once","all you do is compare two fixed values" and " you call getElapsedTime before the loop"

are you telling me what i should do, or what my mistake are??
-dove96-

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: sf::Time and sf::Clock
« Reply #3 on: March 28, 2016, 05:09:27 pm »
Don't put your code inside the event loop if it don't want it to be triggered only when there are events.

dove96

  • Newbie
  • *
  • Posts: 20
    • View Profile
    • Email
Re: sf::Time and sf::Clock
« Reply #4 on: March 28, 2016, 05:29:35 pm »
oww. there. got it. thanks both.
-dove96-