SFML community forums

Help => General => Topic started by: digimikeh on April 04, 2021, 09:06:05 pm

Title: deltatime is 0 on my faster computer
Post by: digimikeh on April 04, 2021, 09:06:05 pm
Hi!

I have a Windows (12 cores) machine and a Linux (1st gen i5 dual core) machine...
on windows my tick's deltatime tooks 0.0s, on Linux it tooks 0.045s

when i want to move a sprite i calculate the current position * speed * deltatime,... but deltatime on Windows is 0 (i don't know may be is really so fast in make inbetween operations...) the result is on Windows there is no movement at all..

This is my code:


void juego::tick(){
        while (rw.isOpen()){
                if (escena_actual){
                        std::clock_t clk = std::clock();    //clock starts (time is 0.0f)
                        rw.clear(sf::Color::White);

                        if (!escena_actual->hasFinished){
                                sf::Event e;
                                escena_actual->gameplay(e, dt(clk));  //n1

                                if (cortina->current_state == fader::eEstado::cerrada)
                                        cortina->abrir(rw, dt(clk));   
                               
                        } else {
                                cortina->cerrar(rw, dt(clk));
                                if (cortina->current_state == fader::eEstado::cerrada)
                                        cargar_escena();
                        }      

                        for (basic_actor*& a : escena_actual->get_vbasic_actors())
                                rw.draw(a->get_spr());

                        rw.draw(cortina->get_spr());
                        rw.display();
                }
        }      
}
 

n1 : the dt(clk) is a function that returns a const float which it is deltatime that is located on juego.h

inline const float dt(std::clock_t& _clk) {
        //current second - second passed by argument
        //on Windows this is 0, on Linux it is 0.045
        return (std::clock() - _clk) / (float) CLOCKS_PER_SEC;
}
 

Then to move my sprites:
//_dt is a reference for returned dt(clk), as on windows is 0 then there is no movement.
clouds->get_spr().move(C_CLOUD_SPEED * _dt, 0);
 

May i'm calculating deltatime incorrectly ?
Windows compiler is msvc 2017 32bits
Linux compiler is GCC 64bits

Title: Re: deltatime is 0 on my faster computer
Post by: kojack on April 05, 2021, 10:48:41 am
On windows, CLOCKS_PER_SEC is 1000. On Linux (and other Posix systems) CLOCKS_PER_SEC is 1000000.
So on Windows std::clock works with milliseconds while on Linux it's microseconds.

You'll need to find a higher precision clock on windows if you are calling it too fast.
Title: Re: deltatime is 0 on my faster computer
Post by: eXpl0it3r on April 06, 2021, 05:51:32 pm
You're better off using sf::Clock or the <chronos> header instead of depending on old C/POSIX functions with implementation defined precisions
Title: Re: deltatime is 0 on my faster computer
Post by: digimikeh on April 08, 2021, 02:38:16 am
Thanks both, nice point about 1000000 and 1000 difference!
i just wanted to use standard library just as a practical thing... but i agreed about using sf::Clock  instead..

thanks again
Title: Re: deltatime is 0 on my faster computer
Post by: eXpl0it3r on April 08, 2021, 08:17:28 am
i just wanted to use standard library just as a practical thing...
Nothing against that, but make sure to use the <chronos> header, instead of old, old, old POSIX functions. ;)
Title: Re: deltatime is 0 on my faster computer
Post by: digimikeh on April 11, 2021, 01:53:18 am
Thanks!