From ac0881f6a0d1d6c932d08508d09f03a4150f22c0 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Fri, 14 Aug 2009 09:27:00 +0000 Subject: [PATCH] FS#126 - Add an optional parameter to Sprite::SetImage for automatic adjustment of the subrect git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1202 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/Graphics/Sprite.hpp | 5 +++-- src/SFML/Graphics/Sprite.cpp | 10 +++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/include/SFML/Graphics/Sprite.hpp b/include/SFML/Graphics/Sprite.hpp index 4967d5640..ee771e941 100644 --- a/include/SFML/Graphics/Sprite.hpp +++ b/include/SFML/Graphics/Sprite.hpp @@ -66,10 +66,11 @@ public : //////////////////////////////////////////////////////////// /// Change the image of the sprite /// - /// \param image : New image + /// \param image : New image + /// \param adjustToNewSize : If true, the SubRect of the sprite will be adjusted to the size of the new image (false by default) /// //////////////////////////////////////////////////////////// - void SetImage(const Image& image); + void SetImage(const Image& image, bool adjustToNewSize = false); //////////////////////////////////////////////////////////// /// Set the sub-rectangle of the sprite inside the source image. diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index d6fd86141..52fb60b57 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -60,10 +60,14 @@ myIsFlippedY(false) //////////////////////////////////////////////////////////// /// Set the image of the sprite //////////////////////////////////////////////////////////// -void Sprite::SetImage(const Image& image) +void Sprite::SetImage(const Image& image, bool adjustToNewSize) { - // If there was no source image before and the new image is valid, adjust the source rectangle - if (!myImage && (image.GetWidth() > 0) && (image.GetHeight() > 0)) + // If there was no valid image before, force adjusting to the new image size + if (!myImage) + adjustToNewSize = true; + + // If we want to adjust the size and the new image is valid, we adjust the source rectangle + if (adjustToNewSize && (image.GetWidth() > 0) && (image.GetHeight() > 0)) { SetSubRect(IntRect(0, 0, image.GetWidth(), image.GetHeight())); }