SFML community forums

Help => General => Topic started by: dove96 on March 28, 2016, 08:10:27 am

Title: sf::Time and sf::Clock
Post by: dove96 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...
Title: Re: sf::Time and sf::Clock
Post by: eXpl0it3r 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.
Title: Re: sf::Time and sf::Clock
Post by: dove96 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??
Title: Re: sf::Time and sf::Clock
Post by: G. 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.
Title: Re: sf::Time and sf::Clock
Post by: dove96 on March 28, 2016, 05:29:35 pm
oww. there. got it. thanks both.