From 4c2899876efad8bb0928ed881f68d798c2ad5f6a Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Sat, 10 Apr 2010 12:05:53 +0000 Subject: [PATCH] Fixed bug with texture coordinates in RenderImage after the sf::Rect modification git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1508 4e206d99-4929-0410-ac5d-dfc041789085 --- src/SFML/Graphics/Image.cpp | 8 ++++---- src/SFML/Graphics/Sprite.cpp | 22 ++++++++++++---------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 7d961b75..5be0c779 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -572,10 +572,10 @@ FloatRect Image::GetTexCoords(const IntRect& rect) const if (myPixelsFlipped) { - return FloatRect(rect.Left / width, - rect.Height / height, - rect.Width / width, - rect.Top / height); + return FloatRect( rect.Left / width, + (rect.Top + rect.Height) / height, + rect.Width / width, + -rect.Height / height); } else { diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index 93d7a215..9b8b713e 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -188,23 +188,25 @@ void Sprite::Render(RenderTarget&, Renderer& renderer) const // Check if the image is valid, and calculate the texture coordinates FloatRect coords; if (myImage) - { coords = myImage->GetTexCoords(mySubRect); - if (myIsFlippedX) coords.Width = -coords.Width; - if (myIsFlippedY) coords.Height = -coords.Height; - } + + // Compute the texture coordinates + float left = coords.Left; + float top = coords.Top; + float right = coords.Left + coords.Width; + float bottom = coords.Top + coords.Height; + if (myIsFlippedX) std::swap(left, right); + if (myIsFlippedY) std::swap(top, bottom); // Bind the texture renderer.SetTexture(myImage); // Draw the sprite's geometry - float right = coords.Left + coords.Width; - float bottom = coords.Top + coords.Height; renderer.Begin(Renderer::TriangleStrip); - renderer.AddVertex(0, 0, coords.Left, coords.Top); - renderer.AddVertex(width, 0, right, coords.Top); - renderer.AddVertex(0, height, coords.Left, bottom); - renderer.AddVertex(width, height, right, bottom); + renderer.AddVertex(0, 0, left, top); + renderer.AddVertex(width, 0, right, top); + renderer.AddVertex(0, height, left, bottom); + renderer.AddVertex(width, height, right, bottom); renderer.End(); }