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

Author Topic: The new time API  (Read 22444 times)

0 Members and 2 Guests are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The new time API
« on: January 13, 2012, 02:33:10 pm »
Hi

Many users complained after I switched to milliseconds for time
http://www.sfml-dev.org/forum/viewtopic.php?t=4864
... this thread clearly shows that people need different resolutions and different types for representing time values.

I could of course provide the most precise (Uint64 nanoseconds) but that would not be simple --  user code would be pretty ugly.

So here is a proposal for a new time API, that is a little more verbose than before but should make everyone happy.

Code: [Select]
class Time
{
public:

    Time();
    float ToSeconds() const;
    Int32 ToMilliseconds() const;
    Int64 ToMicroseconds() const;

private:

    Int64 myMicroseconds;
};

Time Seconds(float amount);
Time Milliseconds(Int32 amount);
Time Microseconds(Int64 amount);

class Clock
{
public:

    Time GetElapsedTime() const;
    ...
}

void Sleep(Time duration);
// same in all functions that require/return a time value

User code:
Code: [Select]
sf::Clock clock;

sf::Time elapsed = clock.GetElapsedTime();
game.Update(elapsed.ToSeconds());

sf::Sleep(sf::Milliseconds(100));

sf::Time would be a time span, not a time point, so negative values would be allowed (hence the signed integers). It would have comparison and mathematical operators.
It could also support implicit conversions to/from std::chrono classes in the future.
Laurent Gomila - SFML developer

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
The new time API
« Reply #1 on: January 13, 2012, 03:36:28 pm »
Sounds good, although I'd tend to return a const reference to a Time member inside Clock (not sure about Time's structure).

What are the function calls after the class definition for? I'm a bit confused there.

Klaim

  • Full Member
  • ***
  • Posts: 137
    • View Profile
The new time API
« Reply #2 on: January 13, 2012, 03:38:48 pm »
It would be fine, as far as the internal data is integer type to keep the precision.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The new time API
« Reply #3 on: January 13, 2012, 04:16:22 pm »
Quote
Sounds good, although I'd tend to return a const reference to a Time member inside Clock (not sure about Time's structure).

A time is an integer value, nothing more. But let's focus on the API please, not the details ;)

Quote
What are the function calls after the class definition for? I'm a bit confused there.

They construct time values. sf::Time has no constructor other than the default one, one must use one of these three functions.

Quote
It would be fine, as far as the internal data is integer type to keep the precision.

Internally it is of course a Int64 number of microseconds.
Laurent Gomila - SFML developer

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
The new time API
« Reply #4 on: January 13, 2012, 04:24:35 pm »
Looks fine to me :)
Want to play movies in your SFML application? Check out sfeMovie!

Silvah

  • Guest
Re: The new time API
« Reply #5 on: January 13, 2012, 06:29:15 pm »
Quote from: "Laurent"
So here is a proposal for a new time API, that is a little more verbose than before but should make everyone happy.
It's almost certain that some people will not like it. You know, "omfg what u done its not simple anymore" ;)

As my opinion goes, it's okay.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
The new time API
« Reply #6 on: January 13, 2012, 07:57:06 pm »
Yeah, I knew this would come. Totally endorsed.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
The new time API
« Reply #7 on: January 14, 2012, 12:43:04 am »
This is very similar to how I implemented time in rimfrost engine. But we also have hierarchy timers. Though think that is over kill here.

Anyway you have my thumbs upp.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

c0ffee

  • Newbie
  • *
  • Posts: 13
    • View Profile
The new time API
« Reply #8 on: January 14, 2012, 01:08:06 am »
Quote
"omfg what u done its not simple anymore"

julen26

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
    • http://julen26.blogspot.com
The new time API
« Reply #9 on: January 14, 2012, 02:24:30 am »
It seems nice for me.
This way SFML still keeps satisfaying all of its users  :D

hayer

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
The new time API
« Reply #10 on: January 14, 2012, 02:32:33 am »
I don't see any pause? :- (

And a interval with a callback like C# timers would be nice, but I guess I could just convert my old class.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
The new time API
« Reply #11 on: January 14, 2012, 02:42:47 am »
Quote
Internally it is of course a Int64 number of microseconds.


Okay does that really remove the issue?

I mean you handle time still as Int64 which has the problem that you get 0 for all the function calls in between 0 and 1ms.
Which means that if I had a game loop that executes faster than 1ms I still would have to deal with the problem of 0.
Example: If I add every iteration the float of the passed time I'll constantly add 0+0+0+0+0, maybe occasionally get an 0.000001 in there but looking at my variable it would seem as if time has stopped.

Sure it can be fixed with always forcing to add a least 0.000001 and then sync it from time to time with the exact int and who ever needs performance below 1ms?

Just saying it doesn't fix all the issues...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
The new time API
« Reply #12 on: January 14, 2012, 03:18:34 am »
Looks good to me.

Quote from: "eXpl0it3r"
Quote
Internally it is of course a Int64 number of microseconds.


Okay does that really remove the issue?

I mean you handle time still as Int64 which has the problem that you get 0 for all the function calls in between 0 and 1ms.
Which means that if I had a game loop that executes faster than 1ms I still would have to deal with the problem of 0.
Example: If I add every iteration the float of the passed time I'll constantly add 0+0+0+0+0, maybe occasionally get an 0.000001 in there but looking at my variable it would seem as if time has stopped.

Sure it can be fixed with always forcing to add a least 0.000001 and then sync it from time to time with the exact int and who ever needs performance below 1ms?

Just saying it doesn't fix all the issues...


Looks like he would be storing microseconds in that int64, not milliseconds.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
The new time API
« Reply #13 on: January 14, 2012, 09:53:02 am »
Seems to be a good solution. And whoever thinks it's not simple anymore... wtf? ;-)

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
The new time API
« Reply #14 on: January 14, 2012, 11:21:40 am »
Quote from: "hayer"
I don't see any pause? :- (
Pause() in the Time class ? I wouldn't make any sense. As Laurent said :

Quote
sf::Time would be a time span


BTW : looks good!
SFML / OS X developer