SFML community forums

Help => System => Topic started by: WaiHak on November 25, 2013, 04:49:51 pm

Title: Why console messages slow time "sf::Clock" ?
Post by: WaiHak on November 25, 2013, 04:49:51 pm
I have a problem i was testing next code with a timer.
CurrentTime = sf::milliseconds(CurrentTime.asMilliseconds() + Timer.getElapsedTime().asMilliseconds());
Timer.restart();
cout << CurrentTime.asMilliseconds() << endl;
 
With that code the timer was losing much seconds, but with following is not it.
CurrentTime = sf::milliseconds(CurrentTime.asMilliseconds() + Timer.getElapsedTime().asMilliseconds());
        Timer.restart();
 
It should elapse equal?
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: Nexus on November 25, 2013, 05:14:25 pm
Console output is typically very slow, so you can't expect the code to execute at the same speed.
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: WaiHak on November 25, 2013, 05:19:54 pm
Console output is typically very slow, so you can't expect the code to execute at the same speed.
i know it is that but timer is not real?
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: eXpl0it3r on November 25, 2013, 05:24:10 pm
Not sure if others understand it, but I'm kind of lost in translation.

You might want to write the code like this:
CurrentTime += Timer.restart();
std::cout << CurrentTime.asMilliseconds() << std::endl;
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: WaiHak on November 25, 2013, 05:32:32 pm
Not sure if others understand it, but I'm kind of lost in translation.

You might want to write the code like this:
CurrentTime += Timer.restart();
std::cout << CurrentTime.asMilliseconds() << std::endl;
1- Sorry for my translation i know it is bad.

2- Your code is good it works perfect.

3- Is it better restart () that getElapsed ()?  by what I see it works better

Thank You!

Title: Re: Why console messages slow time "sf::Clock" ?
Post by: Nexus on November 25, 2013, 05:34:55 pm
3- Is it better restart () that getElapsed ()?  by what I see it works better
They do different things. You should look at the API documentation before using functions ;)
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: WaiHak on November 25, 2013, 06:20:35 pm
3- Is it better restart () that getElapsed ()?  by what I see it works better
They do different things. You should look at the API documentation before using functions ;)

I do nothing but read this
This function returns the time elapsed since the last call to restart() (or the construction of the instance if restart() has not been called

Now you see this.
loop
        CurrentTime = sf::milliseconds(CurrentTime.asMilliseconds() + Timer.getElapsedTime().asMilliseconds()); // End counting
        Timer.restart();// Start counting
        cout << CurrentTime.asMilliseconds() << endl;
endloop
 
I think I'm understanding you correctly
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: eXpl0it3r on November 25, 2013, 06:33:11 pm
getElapsedTime() returns the elapsed time since the last call to restart()
restart() restarts the internal counter AND returns the elapsed time since the last call to restart()

If you only call getElapsedTime() the internal counter will never get reset, but it would count on. Since you're always adding the elapsed time to your CurrentTime object, you're essentially doing too much work and your code could even be further simplified by removing CurrentTime completely:

std::cout << Timer.getElapsedTime().asMilliseconds() << std::endl;


If you however want to reuse the elapsed time and want to reset the counter as soon as you got the elapsed time, you should use restart(), otherwise you'll get the elapsed time and once you reset the timer, it might already have propagated 1-2ms further.
CurrentTime += Timer.getElapsedTime();
// 1ms, 2ms, ...
Timer.restart() // We lost 2ms
//------------------------------
CurrentTime += Timer.restart() // We lost 0ms
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: WaiHak on November 25, 2013, 06:43:59 pm

Thank you, eXpl0it3r and Nexus
Title: Re: Why console messages slow time "sf::Clock" ?
Post by: WaiHak on November 25, 2013, 07:16:43 pm
Please you can said me if this is good code?
Clock.h
#ifndef CLOCK_H
#define CLOCK_H

#include <SFML/System.hpp>

class Clock {

public:
        Clock(int);//int mls end

        void Update();
        void Play();
        void Pause();
        void Stop();

        bool isFinish();

        void setTime(int);//int mls end

        sf::Time getTime();
private:
        int Milliseconds;
        sf::Clock Timer;
        sf::Time CurrentTime;
        bool stop;
        bool pause;
};

#endif //CLOCK_H
 
Clock.cpp

#include "Clock.h"

Clock::Clock(int mls)
{
        setTime(mls);
        stop = true;
        pause = true;
}
void Clock::Update()
{
        if (CurrentTime.asMilliseconds() >= Milliseconds)
        {
                stop = true;
        }
        else
        {
                if (pause == false)CurrentTime += Timer.restart();
                else Timer.restart();
        }
}
void Clock::Play()
{
        pause = false;
        stop = false;
}

void Clock::Pause()
{
        pause = true;
}

void Clock::Stop()
{
        stop = true;
        CurrentTime = sf::milliseconds(0);
}

void Clock::setTime(int mls)
{
        Milliseconds = mls;
}

bool Clock::isFinish()
{
        if (CurrentTime.asMilliseconds() >= Milliseconds)return true;
        else return false;
}
sf::Time Clock::getTime()
{
        return CurrentTime;
}