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

Author Topic: No more default constructor for sf::Sprite?  (Read 3863 times)

0 Members and 2 Guests are viewing this topic.

queqomar

  • Newbie
  • *
  • Posts: 1
    • View Profile
No more default constructor for sf::Sprite?
« on: February 05, 2025, 07:12:27 pm »
Hello everyone,

I’ve recently started working with SFML 3.0 and I’m facing an issue with the removal of the default constructor for sf::Sprite. In SFML 3.0, sf::Sprite now requires a texture to be passed during construction, which is a change from previous versions where the texture could be set later.

The main reason for my concern is that I prefer to initialize sprites without a texture initially and then assign a texture in a separate function, especially when the texture might not be loaded yet. This approach worked fine in SFML 2.x but is no longer possible in SFML 3.0.

While I can work around this by using lazy initialization (e.g., through std::unique_ptr), this feels cumbersome and inefficient for every sprite. The overhead of managing a unique_ptr just to allow lazy texture assignment doesn't seem ideal. Alternatively, using a placeholder texture works, but it feels like a less clean solution, as it can result in incorrect rendering if forgotten.

I’m curious if there are any other potential workarounds, and I’d appreciate suggestions from anyone who’s dealt with similar challenges.

Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11154
    • View Profile
    • development blog
    • Email
Re: No more default constructor for sf::Sprite?
« Reply #1 on: February 06, 2025, 08:24:32 am »
Instead of using std::unique_ptr you could use std::optional which purpose is to allow such optional states.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3410
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: No more default constructor for sf::Sprite?
« Reply #2 on: February 06, 2025, 09:51:45 am »
The placeholder resource seems to be the simplest workaround for projects designed around SFML 2 and ported to SFML 3. It does feel like a bit of a hack, to be fair.

Going forward, I expect that while designing projects for SFML 3, these things can be taken into consideration and prepared for (e.g. loading textures first and passing them to the sprites when creating them).

This might be the single weirdest in choice of design in SFML 3 (for me) for such lightweight classes requiring a link to a heavy resource to even be able to be declared but it does ensure that that lightweight class (this includes sprite, sounds, texts etc.) is always in a useable state so I guess this is a just another challenge that we obviously enjoy to endure! 8)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

wrathnut

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: No more default constructor for sf::Sprite?
« Reply #3 on: February 08, 2025, 02:36:07 pm »
I am actually trying to port portions of my old game engine written with SFML 2.5 and am having the same issue. I am really having a hard time finding an elegant solution to this.

zTn

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: No more default constructor for sf::Sprite?
« Reply #4 on: February 26, 2025, 10:42:25 pm »
Yeah...I'm generally a fan of the 3.0 changes but this is more than just problematic.  Wrapping all your sprites in a unique_ptr or optional is ridiculous.  I'm seriously considering creating a singleton just to have a consistent default texture that sprites can use.  If somebody comes up with a better solution please post it.  Not a good look sfml, not a good look. >:(

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11154
    • View Profile
    • development blog
    • Email
Re: No more default constructor for sf::Sprite?
« Reply #5 on: February 26, 2025, 11:11:36 pm »
You can also swap out all the sprites with a rectangle shape, which can optionally be textures.

Personally, I still believe that this is the right approach, as a sprite without a texture doesn't make sense. And I'm convinced that in most situations where you create a sprite without having access to the texture, your design could be improved in some way.
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3410
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: No more default constructor for sf::Sprite?
« Reply #6 on: March 01, 2025, 03:37:52 pm »
The logic of having sf::Sprite without a texture doesn't make sense so this is probably the right approach. It's just the ability to pre-create sprites before attaching textures to them (often in large scales vectors). As there are alternatives (custom vertex arrays or just simply a rectangle), this could be just worked around.

However, things like sf::Sound are a bit harder to work around. Possible, but harder.

It's just a new workflow that we should get used to; again, I think it's the right approach.

My earlier suggestion is to help with porting code from stuff designed for SFML 2 (create a temporary, empty texture and create the sprites with that) but should not be the design going forward.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything