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

Author Topic: Inheritance of sf::Sprite  (Read 1341 times)

0 Members and 1 Guest are viewing this topic.

xanderxylona

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Inheritance of sf::Sprite
« on: April 18, 2019, 04:13:40 pm »
I'm trying to make an AnimatedSprite class that inherits sf::Sprite and provides some auxiliary functions to make animation easier. In the docs it doesn't look like sf::Sprite has any destructor whatever with exception of those inherited from sf::Drawable and sf::Transformable. Should I call
delete getTexture()
or leave it alone and let it automatically call ~Drawable?

Haze

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Github Profile
Re: Inheritance of sf::Sprite
« Reply #1 on: April 18, 2019, 06:27:59 pm »
Sprite objects do not own a copy of a texture object (and do not allocate textures objects), they only point to an already existing texture.

From sprite setTexture documentation:

Quote
The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite
doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function.

Multiple sprite objects can point to the same texture. You need to manage life cycle of your texture objects separately, and you should not attempt to deallocate textures when a sprite is deleted.

 

anything