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

Author Topic: deltatime is 0 on my faster computer  (Read 2947 times)

0 Members and 1 Guest are viewing this topic.

digimikeh

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
deltatime is 0 on my faster computer
« 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

« Last Edit: April 04, 2021, 09:09:43 pm by digimikeh »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: deltatime is 0 on my faster computer
« Reply #1 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: deltatime is 0 on my faster computer
« Reply #2 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

digimikeh

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: deltatime is 0 on my faster computer
« Reply #3 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: deltatime is 0 on my faster computer
« Reply #4 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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

digimikeh

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: deltatime is 0 on my faster computer
« Reply #5 on: April 11, 2021, 01:53:18 am »
Thanks!