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

Author Topic: Which draws nothing faster? Drawing a default sprite or skip with if() check?  (Read 1907 times)

0 Members and 1 Guest are viewing this topic.

zTn

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
I noticed early on that drawing a default constructed sprite doesn't end up drawing anything.  I've found myself depending on this often.  I make sf::Drawable classes that always { target.draw(m_sprite); }, even when there is nothing to draw. In these cases I simply leave m_sprite in it's default constructed state.  I assumed that this was faster than wrapping all of these draw calls in an if, such as { if (m_willDraw) target.draw(m_sprite); }, because I assumed that somewhere inside SFML there is already such an if that skips drawing the default sprite.  My instinct was to reduce branch instructions in my draw code.  Am I wrong?

Given what you experts know about SFML's inner workings and assuming typical desktop graphics cards and processors (branch prediction especially) -is there a definitive answer, or is it too close to call and I will need to test?

Thanks for any help!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Drawing a default sprite does nothing because it's texture is null: https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/Sprite.cpp#L137

That said you should just write however you feel fits best and looks clearest, not worry about performance of minor things like these.

Doing an if in your own code is actually more efficient because it does the same job much sooner and avoids jumping around memory (both data and code), those few extra function calls that need to go on the stack (and one of them is even virtual) and constructing an unneeded render states object (more functions, more jumping around memory).

But you shouldn't worry about such micro optimizations until you have a real performance problem.

People write entire games in whatever style they feel is clear in interpreted languages like Lua and Python. You'll be fine with C++.
Back to C++ gamedev with SFML in May 2023