From 623d0f67ea0fcce3fa7b6c31b93a3f42a3f9cfdc Mon Sep 17 00:00:00 2001 From: kimci86 Date: Sat, 5 Aug 2023 20:17:02 +0200 Subject: [PATCH] Make Sprite::getTexture return a reference and do some simplification using the fact that a constructed sprite has a non-null texture pointer. --- include/SFML/Graphics/Sprite.hpp | 7 +++---- src/SFML/Graphics/Sprite.cpp | 10 ++++------ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/include/SFML/Graphics/Sprite.hpp b/include/SFML/Graphics/Sprite.hpp index 7c1aa260..7fd44b4f 100644 --- a/include/SFML/Graphics/Sprite.hpp +++ b/include/SFML/Graphics/Sprite.hpp @@ -139,16 +139,15 @@ public: //////////////////////////////////////////////////////////// /// \brief Get the source texture of the sprite /// - /// If the sprite has no source texture, a null pointer is returned. - /// The returned pointer is const, which means that you can't + /// The returned reference is const, which means that you can't /// 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 /// //////////////////////////////////////////////////////////// - const Texture* getTexture() const; + const Texture& getTexture() const; //////////////////////////////////////////////////////////// /// \brief Get the sub-rectangle of the texture displayed by the sprite diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index d7080bb6..3590636a 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -55,8 +55,8 @@ Sprite::Sprite(const Texture& texture, const IntRect& rectangle) //////////////////////////////////////////////////////////// void Sprite::setTexture(const Texture& texture, bool resetRect) { - // Recompute the texture area if requested, or if there was no valid texture & rect before - if (resetRect || (!m_texture && (m_textureRect == sf::IntRect()))) + // Recompute the texture area if requested + if (resetRect) { 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 { - assert(m_texture && "Cannot use null texture. Call Sprite::setTexture() to initialize it."); - RenderStates statesCopy(states); statesCopy.transform *= getTransform();