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

Author Topic: sf::Clock overhead  (Read 6239 times)

0 Members and 1 Guest are viewing this topic.

N1ghtly

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
sf::Clock overhead
« on: May 03, 2012, 06:00:25 pm »
Hello,

I'm designing an AnimatedSprite class for my sprite engine and I've run into a little issue.
I'm thinking of overriding the draw() function to draw the sprite AND automatically update the frame.

Now I'm thinking, is it madness to have an sf::Clock for every AnimatedSprite?
Because it would be very easy if you don't have to worry about updating the time for every AnimatedSprite.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Clock overhead
« Reply #1 on: May 03, 2012, 07:01:08 pm »
No it's not, sf::Clock is internally just a Int64 and a system call.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Clock overhead
« Reply #2 on: May 03, 2012, 07:04:11 pm »
I'm thinking of overriding the draw() function to draw the sprite AND automatically update the frame.
That's not a good idea, drawing shouldn't alter the object, for example one might draw it multiple times (maybe to different places). This is also expressed in code; sf::Drawable::draw() is const.

Now I'm thinking, is it madness to have an sf::Clock for every AnimatedSprite?
Not because of performances, but it strongly limits the use cases. With clocks, you cannot interrupt the animation, that is a game pause is not possible.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

N1ghtly

  • Jr. Member
  • **
  • Posts: 96
    • View Profile
Re: sf::Clock overhead
« Reply #3 on: May 03, 2012, 07:34:21 pm »
Ahh didn't think about that!
Code: [Select]
void update(sf::Time frameTime)it is then!

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: sf::Clock overhead
« Reply #4 on: May 03, 2012, 07:59:46 pm »
Ahh didn't think about that!
Code: [Select]
void update(sf::Time frameTime)it is then!

I don't know how big (in memory) is the sf::Time class, I think this is small but prefer a reference or a const reference since I'm sure this is smaller =)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Clock overhead
« Reply #5 on: May 03, 2012, 08:17:57 pm »
Nop: it's only a Int64, so even if it's bigger than a pointer on 32-bits systems, it's still faster to pass by value (no dereferencing, etc.).

N1ghtly just followed SFML, which passes all its sf::Time instances by value.
Laurent Gomila - SFML developer

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: sf::Clock overhead
« Reply #6 on: May 03, 2012, 08:23:20 pm »
Nop: it's only a Int64, so even if it's bigger than a pointer on 32-bits systems, it's still faster to pass by value (no dereferencing, etc.).

N1ghtly just followed SFML, which passes all its sf::Time instances by value.

Really ? To pass a 64bit value is faster than 32 reference ?
I need better C++ courses oO

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Clock overhead
« Reply #7 on: May 03, 2012, 08:40:35 pm »
Well, this conclusion is not obvious, in fact it depends on many parameters.

This article is very interesting and detailed:
http://www.macieira.org/blog/2012/02/the-value-of-passing-by-value/
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Clock overhead
« Reply #8 on: May 04, 2012, 06:01:48 pm »
Do not forget that -- although you don't see it in code -- references need to be dereferenced, which also has a cost.

In fact, sf::Vector2f should also be passed by value :P
(It also makes the syntax nicer and prevents API changes, for example if a returned const sf::Vector2f& that refers to a member is replaced by a copy to a local object).

Side note: Already with the old standard, yet more with C++11, some situations encourage to pass even big objects by value.
C++Next: Want speed? Pass by value
« Last Edit: May 04, 2012, 06:05:33 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything