From 8a993dca06d8206391cb2e963c28d481751993aa Mon Sep 17 00:00:00 2001 From: laurentgom Date: Mon, 4 May 2009 07:05:13 +0000 Subject: [PATCH] FS#106 - Fixed undefined behaviour when rendering a sprite bound to an empty image git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/trunk@1080 4e206d99-4929-0410-ac5d-dfc041789085 --- src/SFML/Graphics/Sprite.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index c1f65369d..e7ff04f25 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -49,12 +49,11 @@ myIsFlippedY(false) //////////////////////////////////////////////////////////// Sprite::Sprite(const Image& Img, const Vector2f& Position, const Vector2f& Scale, float Rotation, const Color& Col) : Drawable (Position, Scale, Rotation, Col), -myImage (&Img), -mySubRect (0, 0, Img.GetWidth(), Img.GetHeight()), +mySubRect (0, 0, 1, 1), myIsFlippedX(false), myIsFlippedY(false) { - + SetImage(Img); } @@ -63,9 +62,11 @@ myIsFlippedY(false) //////////////////////////////////////////////////////////// void Sprite::SetImage(const Image& Img) { - // If there was no source image before, adjust the rectangle - if (!myImage) + // If there was no source image before and the new image is valid, adjust the source rectangle + if (!myImage && (Img.GetWidth() > 0) && (Img.GetHeight() > 0)) + { SetSubRect(IntRect(0, 0, Img.GetWidth(), Img.GetHeight())); + } // Assign the new image myImage = &Img; @@ -87,10 +88,11 @@ void Sprite::SetSubRect(const IntRect& SubRect) //////////////////////////////////////////////////////////// void Sprite::Resize(float Width, float Height) { - if ((mySubRect.GetWidth() > 0) && (mySubRect.GetHeight() > 0)) - { - SetScale(Width / mySubRect.GetWidth(), Height / mySubRect.GetHeight()); - } + int LocalWidth = mySubRect.GetWidth(); + int LocalHeight = mySubRect.GetHeight(); + + if ((LocalWidth > 0) && (LocalHeight > 0)) + SetScale(Width / LocalWidth, Height / LocalHeight); } @@ -182,9 +184,9 @@ void Sprite::Render(RenderTarget&) const float Height = static_cast(mySubRect.GetHeight()); // Check if the image is valid - if (myImage) + if (myImage && (myImage->GetWidth() > 0) && (myImage->GetHeight() > 0)) { - // Set the texture + // Bind the texture myImage->Bind(); // Calculate the texture coordinates