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

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

0 Members and 3 Guests are viewing this topic.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
The new time API
« Reply #45 on: January 19, 2012, 09:15:04 am »
Well, am I not dumb?  :lol:

I've read each line of the code separately.

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
The new time API
« Reply #46 on: January 19, 2012, 01:08:17 pm »
Also functioning code wasn't my point but the fact that you can read the code as normal English. Ruby is based on this concept. Example:
Code: [Select]
10.times do
  print "Hello World!"
end
Reading that becomes "10 times print hello world!" or even "10 times do action print hello world!" (If you take to note that the do-notation in Ruby is a lambda function)

As soon as you add more text to a name to "reduce ambiguity", reading it becomes harder. Now of course not everyone might approve of how I do it to solve ambiguity and increase readability. But since SFML is supposed to be simple I think having it close to English speech makes it that by default. As having the code written as this forces the user to maintain it simple, readable and the context in the code can be derived from the surrounding code just like in a book.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Silvah

  • Guest
The new time API
« Reply #47 on: January 19, 2012, 07:50:44 pm »
Quote from: "Groogy"
From my understanding this is the proposed convention from Silvah:
Code: [Select]
time = sf::Time::FromSeconds(N);
I wasn't the one who proposed this. Mario was.

Quote from: "Ceylo"
So much discussion... for a little innocent name.. :(
No wonder, naming is the hardest thing in programming :D

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
The new time API
« Reply #48 on: January 19, 2012, 08:48:57 pm »
While I agree that function names should be intuitive, I wouldn't exaggerate. Long names are more difficult to read, write and remember; in my opinion they are only worth the drawbacks when they really increase the expressivity.

And I think that
Code: [Select]
sf::Time time = sf::Seconds(4);is perfectly expressive, as well as
Code: [Select]
sf::Sleep(sf::Seconds(4));
On the other side, things like
Code: [Select]
sf::Time t = sf::Time::FromSeconds(4);or
Code: [Select]
sf::Sleep(sf::Time::FromSeconds(4))are heavy and contain duplicate information, but don't give us more information about the semantics. At least no useful one -- we see that a sf::Time object is constructed, but since sf::Time is the only SFML time class, this is  obvious. Additionally, the exactly returned type isn't that important.

Also concerning sf::TimeSpan: While it is useful to differ between time points and time spans (like duration in Boost/Std), SFML doesn't have this concept, so I consider sf::Time general enough.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The new time API
« Reply #49 on: January 19, 2012, 11:53:56 pm »
I've pushed the new API. I didn't change anything, except the prefix: "To" is now "As".
Laurent Gomila - SFML developer

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
The new time API
« Reply #50 on: January 20, 2012, 01:16:58 am »
Quote from: "Laurent"
I've pushed the new API. I didn't change anything, except the prefix: "To" is now "As".

My work here is done  8)
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
The new time API
« Reply #51 on: January 20, 2012, 01:14:35 pm »
As it's no longer possible to just use "sf::Sleep(0);" to allow context switches (or I missed something new other than calling "sf::Sleep(sf::Seconds(0));"?), how about adding something like "sf::Idle();" or "sf::Sleep(sf::Idle);" just doing that? Just a bit lazy here.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The new time API
« Reply #52 on: January 20, 2012, 01:21:03 pm »
There's a special Time::Zero value
Code: [Select]
sf::Sleep(sf::Time::Zero);
Is that enough?

If I implemented an "Idle" thing it would have to be specialized according to the OS, Sleep(0) is not strictly equivalent. And by the way, it would probably be named sf::Yield().
Laurent Gomila - SFML developer

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
The new time API
« Reply #53 on: January 20, 2012, 04:11:47 pm »
Sounds reasonable enough.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
The new time API
« Reply #54 on: January 20, 2012, 07:41:33 pm »
Quote from: "Laurent"
I've pushed the new API. I didn't change anything, except the prefix: "To" is now "As".
Sounds great, I'm looking forward to update and adapt everything in Thor :)
(really)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
The new time API
« Reply #55 on: January 21, 2012, 01:19:54 pm »
What do you think about an operator that returns the ratio between two times?
Code: [Select]
float operator/ (sf::Time lhs, sf::Time rhs)
{
    return lhs.AsSeconds() / rhs.AsSeconds();
}

By the way, why don't you pass sf::Time objects by value? You do it also for sf::Uint64. 8 bytes to copy is negligible, considering that references usually consume also 4 or 8 bytes. However, pass by value doesn't imply the dereferencing cost, and code becomes more beautiful without const&. In the end, you can only win :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The new time API
« Reply #56 on: January 21, 2012, 01:42:38 pm »
Quote
What do you think about an operator that returns the ratio between two times?

What would it mean?

Quote
By the way, why don't you pass sf::Time objects by value?

I have no idea, I already do it for all other functions :lol:
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
The new time API
« Reply #57 on: January 21, 2012, 01:48:19 pm »
Quote from: "Laurent"
What would it mean?
Generally, the operator/ can be used to find out what fraction of a time span has already passed. A concrete use case looks like this:
Code: [Select]
// Returns particle lifetime progress in [0, 1]
float GetPassedRatio(const Particle& particle)
{
return GetPassedLifetime(particle) / GetTotalLifetime(particle);
}


Quote from: "Laurent"
I have no idea, I already do it for all other functions :lol:
A search&replace is performed fast :D
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The new time API
« Reply #58 on: January 21, 2012, 03:43:34 pm »
Quote
Generally, the operator/ can be used to find out what fraction of a time span has already passed.

Ok. But what if one wants an integer division? Isn't it better to let users choose what type they want to manipulate for this specific use case?

Quote
A search&replace is performed fast

Yep, I'll do it.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
The new time API
« Reply #59 on: January 21, 2012, 07:59:02 pm »
I thought this operation will be natural from a mathematical point of view, if we are able to multiply times with factors.
Code: [Select]
time1         == factor * time2
time1 / time2 == factor

However I see that there is a problem because there are operator* overloads for float and sf::Uint64...

I'm not sure whether two overloads are a good idea anyway, since it makes multiplications ambiguous for other factor types. But I also understand that providing only sf::Uint64 is too unflexible, and only float might lead to loss of precision...

In Boost.Chrono, the division of two duration<T> objects yields T as return type. Hmm, tricky :?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything