-
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?
-
Console output is typically very slow, so you can't expect the code to execute at the same speed.
-
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?
-
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;
-
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!
-
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 ;)
-
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
-
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
-
Thank you, eXpl0it3r and Nexus
-
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;
}