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

Author Topic: How sfClock_getElapsedTime() works ?  (Read 1198 times)

0 Members and 1 Guest are viewing this topic.

Saw

  • Newbie
  • *
  • Posts: 11
    • View Profile
How sfClock_getElapsedTime() works ?
« on: March 08, 2023, 08:21:44 am »
I don't understand how the function works. It feels like too little time has passed since the clock was updated, and therefore the counter value is always 0.

       
        float time = sfTime_asSeconds( sfClock_restart(clock) ) ;
        delta_time = sfClock_getElapsedTime(clock) ;
        timer += sfTime_asMicroseconds(delta_time);
 

But timer always 0.

Also:

CSFML:
        sfTime PreviousTime = sfTime_Zero ;
        sfTime  CurrentTime = sfClock_getElapsedTime(clock) ;
        sfTime  FrameTime = CurrentTime - PreviousTime ;
        PreviousTime = CurrentTime ;
        float DeltaTime = sfTime_asMicroseconds(FrameTime) ;
 

Error: [bcc32 Error] main.cpp(76): E2093 'operator-' not implemented in type 'sfTime' for arguments of the same type

SFML:
        sf::Clock clock;
        sf::Time previousTime{ sf::Time::Zero };
        const sf::Time currentTime{ clock.getElapsedTime() };
        const sf::Time frameTime{ currentTime - previousTime };
        previousTime = currentTime;
        const float dt{ frameTime.asSeconds() };
 

Compiles without errors.

What am I doing wrong?

Upd.

Works:
        sfClock *clock = sfClock_create();
        sfTime PreviousTime = sfTime_Zero ;
        sfTime  CurrentTime = sfClock_getElapsedTime(clock) ;
        float DeltaTime = sfTime_asMicroseconds(CurrentTime) -  sfTime_asMicroseconds(PreviousTime) ;
        PreviousTime = CurrentTime ;
 

The problem with the timer occurs if 2 clock sources are declared.

Not works:
        sfClock *clock = sfClock_create();
        sfClock *clock2 = sfClock_create();
        sfTime PreviousTime = sfTime_Zero ;
        sfTime  CurrentTime = sfClock_getElapsedTime(clock) ;
        float DeltaTime = sfTime_asMicroseconds(CurrentTime) -  sfTime_asMicroseconds(PreviousTime) ;
        PreviousTime = CurrentTime ;
 

This is a quest. It's a pity there is no description for CSFML.
« Last Edit: March 08, 2023, 09:32:25 am by Saw »

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: How sfClock_getElapsedTime() works ?
« Reply #1 on: March 08, 2023, 10:16:42 am »
The SFML and CSFML clocks works on integer microsecond precision (I don't know where that might be documented, I'm just digging through the source code). So for your first bit of code reading the elapsed time is too close to resetting the clock, it would have been quicker than 1 microsecond.

In the next part, the C language doesn't have operator overloading, so it's not possible to subtract sfTimes from each other, you need to get the numeric value out of the sfTime (using sfTime_asMicroseconds as you did further down).

The last one looks like the same as the first one, reading elapsed time less than a microsecond after making the clock.

Saw

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: How sfClock_getElapsedTime() works ?
« Reply #2 on: March 08, 2023, 10:40:13 am »
The problem is not even what value is given, but also that it can be different each time if you rearrange the places of the operators or add some code with a time variable. I see this for the first time. Feeling that some garbage from memory gets into these variables.
The same example, marked as working, already shows exorbitant values after 15 minutes.
If I run the same code on SFML, it always works correctly. Therefore, move no further. But for now, this is mostly a problem with variables or functions that work with time.
The second day I try to make these 5 lines of code work correctly.

« Last Edit: March 08, 2023, 10:48:33 am by Saw »

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: How sfClock_getElapsedTime() works ?
« Reply #3 on: March 09, 2023, 02:48:17 am »
I built CSFML myself (VS2022) and tried several of the code samples you gave. Everything worked. I left the not working two clock one run for an hour and it was consistently giving around 200 microseconds (mostly from printing to the console).

I see from the error message that you are using the Borland or Embarcadero 32 bit compiler. I wonder if that is part of the issue? SFML on windows uses QueryPerformanceCounter and does some 64 bit maths with it, maybe there's a problem there with bcc32?

Saw

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: How sfClock_getElapsedTime() works ?
« Reply #4 on: March 09, 2023, 12:34:40 pm »
I'm starting to lean towards this too. But, there are other projects where external .dll are used. There are no problems with them. In VS2019, everything also works fine for me (there are only nuances with international fonts). I decided to start with time counters, because. not much can be done without them. And while the results are constantly different. The source of the problem was not found. But this unpredictability does not allow to move on.
The C++ Builder is convenient for me only for the sake of not having to jump between different IDEs.
I will try to figure it out.

 

anything