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

Author Topic: Why console messages slow time "sf::Clock" ?  (Read 5245 times)

0 Members and 1 Guest are viewing this topic.

WaiHak

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Why console messages slow time "sf::Clock" ?
« 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Why console messages slow time "sf::Clock" ?
« Reply #1 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

WaiHak

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #2 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #3 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;
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

WaiHak

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #4 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!

« Last Edit: November 25, 2013, 05:34:13 pm by WaiHak »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Why console messages slow time "sf::Clock" ?
« Reply #5 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 ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

WaiHak

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #6 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
« Last Edit: November 25, 2013, 06:22:49 pm by WaiHak »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #7 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

WaiHak

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #8 on: November 25, 2013, 06:43:59 pm »

Thank you, eXpl0it3r and Nexus
« Last Edit: November 25, 2013, 06:49:09 pm by WaiHak »

WaiHak

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Why console messages slow time "sf::Clock" ?
« Reply #9 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;
}
 

 

anything