Make Sprite::getTexture return a reference

and do some simplification using the fact that a constructed sprite has
a non-null texture pointer.
This commit is contained in:
kimci86 2023-08-05 20:17:02 +02:00 committed by Chris Thrasher
parent 751aceadf8
commit 623d0f67ea
2 changed files with 7 additions and 10 deletions

View File

@ -139,16 +139,15 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the source texture of the sprite /// \brief Get the source texture of the sprite
/// ///
/// If the sprite has no source texture, a null pointer is returned. /// The returned reference is const, which means that you can't
/// The returned pointer is const, which means that you can't
/// modify the texture when you retrieve it with this function. /// modify the texture when you retrieve it with this function.
/// ///
/// \return Pointer to the sprite's texture /// \return Reference to the sprite's texture
/// ///
/// \see setTexture /// \see setTexture
/// ///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const Texture* getTexture() const; const Texture& getTexture() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Get the sub-rectangle of the texture displayed by the sprite /// \brief Get the sub-rectangle of the texture displayed by the sprite

View File

@ -55,8 +55,8 @@ Sprite::Sprite(const Texture& texture, const IntRect& rectangle)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Sprite::setTexture(const Texture& texture, bool resetRect) void Sprite::setTexture(const Texture& texture, bool resetRect)
{ {
// Recompute the texture area if requested, or if there was no valid texture & rect before // Recompute the texture area if requested
if (resetRect || (!m_texture && (m_textureRect == sf::IntRect()))) if (resetRect)
{ {
setTextureRect(IntRect({0, 0}, Vector2i(texture.getSize()))); setTextureRect(IntRect({0, 0}, Vector2i(texture.getSize())));
} }
@ -90,9 +90,9 @@ void Sprite::setColor(const Color& color)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
const Texture* Sprite::getTexture() const const Texture& Sprite::getTexture() const
{ {
return m_texture; return *m_texture;
} }
@ -130,8 +130,6 @@ FloatRect Sprite::getGlobalBounds() const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Sprite::draw(RenderTarget& target, const RenderStates& states) const void Sprite::draw(RenderTarget& target, const RenderStates& states) const
{ {
assert(m_texture && "Cannot use null texture. Call Sprite::setTexture() to initialize it.");
RenderStates statesCopy(states); RenderStates statesCopy(states);
statesCopy.transform *= getTransform(); statesCopy.transform *= getTransform();