SFML community forums

Help => Graphics => Topic started by: xanderxylona on April 18, 2019, 04:13:40 pm

Title: Inheritance of sf::Sprite
Post by: xanderxylona 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?
Title: Re: Inheritance of sf::Sprite
Post by: Haze 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.