From 082a928555125e37cc52a80c11cf286f0b03dee5 Mon Sep 17 00:00:00 2001 From: LaurentGom Date: Fri, 9 Apr 2010 13:04:49 +0000 Subject: [PATCH] *important* sf::Rect now uses Width/Height instead of Right/Bottom Removed Offset, GetSize and GetCenter functions from sf::Rect Added a sf::Rect constructor taking two Vector2 parameters Updated the API documentation of the sf::Rect class git-svn-id: https://sfml.svn.sourceforge.net/svnroot/sfml/branches/sfml2@1503 4e206d99-4929-0410-ac5d-dfc041789085 --- include/SFML/Graphics/Rect.hpp | 154 +++++++++++++++++++---------- include/SFML/Graphics/Rect.inl | 94 +++++------------- include/SFML/Graphics/View.hpp | 4 +- include/SFML/Network/Ftp.hpp | 2 +- src/SFML/Graphics/Font.cpp | 10 +- src/SFML/Graphics/Image.cpp | 38 +++---- src/SFML/Graphics/RenderTarget.cpp | 8 +- src/SFML/Graphics/Renderer.cpp | 11 ++- src/SFML/Graphics/Sprite.cpp | 31 +++--- src/SFML/Graphics/Text.cpp | 43 ++++---- src/SFML/Graphics/View.cpp | 6 +- 11 files changed, 205 insertions(+), 196 deletions(-) diff --git a/include/SFML/Graphics/Rect.hpp b/include/SFML/Graphics/Rect.hpp index 8985fa1e1..f152b03cd 100644 --- a/include/SFML/Graphics/Rect.hpp +++ b/include/SFML/Graphics/Rect.hpp @@ -35,8 +35,8 @@ namespace sf { //////////////////////////////////////////////////////////// -/// Rect is an utility class for manipulating rectangles. -/// Template parameter defines the type of coordinates (integer, float, ...) +/// \brief Utility class for manipulating 2D axis aligned rectangles +/// //////////////////////////////////////////////////////////// template class Rect @@ -44,94 +44,89 @@ class Rect public : //////////////////////////////////////////////////////////// - /// Default constructor + /// \brief Default constructor + /// + /// Creates an empty rectangle (it is equivalent to calling + /// Rect(0, 0, 0, 0)). /// //////////////////////////////////////////////////////////// Rect(); //////////////////////////////////////////////////////////// - /// Construct the rectangle from its coordinates + /// \brief Construct the rectangle from its coordinates /// - /// \param left : Left coordinate of the rectangle - /// \param top : Top coordinate of the rectangle - /// \param right : Right coordinate of the rectangle - /// \param bottom : Bottom coordinate of the rectangle + /// Be careful, the last two parameters are the width + /// and height, not the right and bottom coordinates! + /// + /// \param left Left coordinate of the rectangle + /// \param top Top coordinate of the rectangle + /// \param width Width of the rectangle + /// \param height Height of the rectangle /// //////////////////////////////////////////////////////////// - Rect(T left, T top, T right, T bottom); + Rect(T left, T top, T width, T height); //////////////////////////////////////////////////////////// - /// Get the size of the rectangle + /// \brief Construct the rectangle from position and size /// - /// \return Size of rectangle + /// Be careful, the last parameter is the size, + /// not the bottom-right corner! + /// + /// \param position Position of the top-left corner of the rectangle + /// \param size Size of the rectangle /// //////////////////////////////////////////////////////////// - Vector2 GetSize() const; + Rect(const Vector2& position, const Vector2& size); //////////////////////////////////////////////////////////// - /// Get the center of the rectangle + /// \brief Check if a point is inside the rectangle's area /// - /// \return Center of rectangle + /// \param x X coordinate of the point to test + /// \param y Y coordinate of the point to test /// - //////////////////////////////////////////////////////////// - Vector2 GetCenter() const; - - //////////////////////////////////////////////////////////// - /// Move the whole rectangle by the given offset + /// \return True if the point is inside, false otherwise /// - /// \param offsetX : Horizontal offset - /// \param offsetY : Vertical offset - /// - //////////////////////////////////////////////////////////// - void Offset(T offsetX, T offsetY); - - //////////////////////////////////////////////////////////// - /// Move the whole rectangle by the given offset - /// - /// \param offset : Offset to apply to the current position - /// - //////////////////////////////////////////////////////////// - void Offset(const Vector2& offset); - - //////////////////////////////////////////////////////////// - /// Check if a point is inside the rectangle's area - /// - /// \param x : X coordinate of the point to test - /// \param y : Y coordinate of the point to test - /// - /// \return True if the point is inside + /// \see Intersects /// //////////////////////////////////////////////////////////// bool Contains(T x, T y) const; //////////////////////////////////////////////////////////// - /// Check if a point is inside the rectangle's area + /// \brief Check if a point is inside the rectangle's area /// - /// \param point : Point to test + /// \param point Point to test /// - /// \return True if the point is inside + /// \return True if the point is inside, false otherwise + /// + /// \see Intersects /// //////////////////////////////////////////////////////////// bool Contains(const Vector2& point) const; //////////////////////////////////////////////////////////// - /// Check intersection between two rectangles + /// \brief Check the intersection between two rectangles /// - /// \param rectangle : Rectangle to test + /// \param rectangle Rectangle to test /// - /// \return True if rectangles overlap + /// \return True if rectangles overlap, false otherwise + /// + /// \see Contains /// //////////////////////////////////////////////////////////// bool Intersects(const Rect& rectangle) const; //////////////////////////////////////////////////////////// - /// Check intersection between two rectangles and return the - /// resulting rectangle + /// \brief Check the intersection between two rectangles /// - /// \param rectangle : Rectangle to test - /// \param intersection : Rectangle to be filled with the intersection of both rectangles + /// This overload returns the overlapped rectangle in the + /// \a intersection parameter. /// - /// \return True if rectangles overlap + /// \param rectangle Rectangle to test + /// \param intersection Rectangle to be filled with the intersection + /// + /// \return True if rectangles overlap, false otherwise + /// + /// \see Contains /// //////////////////////////////////////////////////////////// bool Intersects(const Rect& rectangle, Rect& intersection) const; @@ -141,13 +136,13 @@ public : //////////////////////////////////////////////////////////// T Left; ///< Left coordinate of the rectangle T Top; ///< Top coordinate of the rectangle - T Right; ///< Right coordinate of the rectangle - T Bottom; ///< Bottom coordinate of the rectangle + T Width; ///< Width of the rectangle + T Height; ///< Height of the rectangle }; #include -// Define the most common types +// Create typedefs for the most common types typedef Rect IntRect; typedef Rect FloatRect; @@ -155,3 +150,54 @@ typedef Rect FloatRect; #endif // SFML_RECT_HPP + + +//////////////////////////////////////////////////////////// +/// \class sf::Rect +/// +/// A rectangle is defined by its top-left corner and its size. +/// It is a very simple class defined for convenience, so +/// its member variables (Left, Top, Width and Height) are public +/// and can be accessed directly, just like the vector classes +/// (Vector2 and Vector3). +/// +/// To keep things simple, sf::Rect doesn't define +/// functions to emulate the properties that are not directly +/// members (such as Right, Bottom, Center, etc.), it rather +/// only provides intersection functions. +/// +/// sf::Rect uses the usual rules for its boundaries: +/// \li The Left and Top edges are included in the rectangle's area +/// \li The right (Left + Width) and bottom (Top + Height) edges are excluded from the rectangle's area +/// +/// This means that sf::IntRect(0, 0, 1, 1) and sf::IntRect(1, 1, 1, 1) +/// don't intersect. +/// +/// sf::Rect is a template and may be used with any numeric type, but +/// for simplicity the instanciations used by SFML are typedefed: +/// \li sf::Rect is sf::IntRect +/// \li sf::Rect is sf::FloatRect +/// +/// So that you don't have to care about the template syntax. +/// +/// Usage example: +/// \code +/// // Define a rectangle, located at (0, 0) with a size of 20x5 +/// sf::IntRect r1(0, 0, 20, 5); +/// +/// // Define another rectangle, located at (4, 2) with a size of 18x10 +/// sf::Vector2i position(4, 2); +/// sf::Vector2i size(18, 10); +/// sf::IntRect r2(position, size); +/// +/// // Test intersections with the point (3, 1) +/// bool b1 = r1.Contains(3, 1); // true +/// bool b2 = r2.Contains(3, 1); // false +/// +/// // Test the intersection between r1 and r2 +/// sf::IntRect result; +/// bool b3 = r1.Intersects(r2, result); // true +/// // result == (4, 2, 16, 3) +/// \endcode +/// +//////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/Rect.inl b/include/SFML/Graphics/Rect.inl index 78c2f2606..f2d92453d 100644 --- a/include/SFML/Graphics/Rect.inl +++ b/include/SFML/Graphics/Rect.inl @@ -23,89 +23,50 @@ //////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////// -/// Default constructor //////////////////////////////////////////////////////////// template Rect::Rect() : Left (0), Top (0), -Right (0), -Bottom(0) +Width (0), +Height(0) { } -//////////////////////////////////////////////////////////// -/// Construct the color from its coordinates //////////////////////////////////////////////////////////// template -Rect::Rect(T left, T top, T right, T bottom) : +Rect::Rect(T left, T top, T width, T height) : Left (left), Top (top), -Right (right), -Bottom(bottom) +Width (width), +Height(height) { } -//////////////////////////////////////////////////////////// -/// Get the size of the rectangle //////////////////////////////////////////////////////////// template -Vector2 Rect::GetSize() const +Rect::Rect(const Vector2& position, const Vector2& size) : +Left (position.x), +Top (position.y), +Width (size.x), +Height(size.y) { - return Vector2(Right - Left, Bottom - Top); + } -//////////////////////////////////////////////////////////// -/// Get the center of the rectangle -//////////////////////////////////////////////////////////// -template -Vector2 Rect::GetCenter() const -{ - return Vector2((Left + Right) / 2, (Top + Bottom) / 2); -} - - -//////////////////////////////////////////////////////////// -/// Move the whole rectangle by the given offset -//////////////////////////////////////////////////////////// -template -void Rect::Offset(T offsetX, T offsetY) -{ - Left += offsetX; - Right += offsetX; - Top += offsetY; - Bottom += offsetY; -} - - -//////////////////////////////////////////////////////////// -/// Move the whole rectangle by the given offset -//////////////////////////////////////////////////////////// -template -void Rect::Offset(const Vector2& offset) -{ - Offset(offset.x, offset.y); -} - - -//////////////////////////////////////////////////////////// -/// Check if a point is inside the rectangle's area //////////////////////////////////////////////////////////// template bool Rect::Contains(T x, T y) const { - return (x >= Left) && (x <= Right) && (y >= Top) && (y <= Bottom); + return (x >= Left) && (x < Left + Width) && (y >= Top) && (y < Top + Height); } -//////////////////////////////////////////////////////////// -/// Check if a point is inside the rectangle's area //////////////////////////////////////////////////////////// template bool Rect::Contains(const Vector2& point) const @@ -114,40 +75,29 @@ bool Rect::Contains(const Vector2& point) const } -//////////////////////////////////////////////////////////// -/// Check intersection between two rectangles //////////////////////////////////////////////////////////// template bool Rect::Intersects(const Rect& rectangle) const { - // Compute overlapping rect - Rect overlapping(std::max(Left, rectangle.Left), - std::max(Top, rectangle.Top), - std::min(Right, rectangle.Right), - std::min(Bottom, rectangle.Bottom)); - - // If overlapping rect is valid, then there is intersection - return (overlapping.Left < overlapping.Right) && (overlapping.Top < overlapping.Bottom); + Rect intersection; + return Intersects(rectangle, intersection); } -//////////////////////////////////////////////////////////// -/// Check intersection between two rectangles and return the -/// resulting rectangle //////////////////////////////////////////////////////////// template bool Rect::Intersects(const Rect& rectangle, Rect& intersection) const { - // Compute overlapping rect - Rect overlapping(std::max(Left, rectangle.Left), - std::max(Top, rectangle.Top), - std::min(Right, rectangle.Right), - std::min(Bottom, rectangle.Bottom)); + // Compute the intersection boundaries + T left = std::max(Left, rectangle.Left); + T top = std::max(Top, rectangle.Top); + T right = std::min(Left + Width, rectangle.Left + rectangle.Width); + T bottom = std::min(Top + Height, rectangle.Top + rectangle.Height); - // If overlapping rect is valid, then there is intersection - if ((overlapping.Left < overlapping.Right) && (overlapping.Top < overlapping.Bottom)) + // If the intersection is valid (positive non zero area), then there is an intersection + if ((left < right) && (top < bottom)) { - intersection = overlapping; + intersection = Rect(left, top, right - left, bottom - top); return true; } else diff --git a/include/SFML/Graphics/View.hpp b/include/SFML/Graphics/View.hpp index dd9c5130d..89e22b056 100644 --- a/include/SFML/Graphics/View.hpp +++ b/include/SFML/Graphics/View.hpp @@ -320,8 +320,8 @@ private : /// sf::RenderWindow window; /// sf::View view; /// -/// // Initialize the view to a rectangle going from (100, 100) to (500, 300) -/// view.Reset(sf::FloatRect(100, 100, 500, 300)); +/// // Initialize the view to a rectangle located at (100, 100) and with a size of 400x200 +/// view.Reset(sf::FloatRect(100, 100, 400, 200)); /// /// // Rotate it by 45 degrees /// view.Rotate(45); diff --git a/include/SFML/Network/Ftp.hpp b/include/SFML/Network/Ftp.hpp index c30a16bbe..c806317ae 100644 --- a/include/SFML/Network/Ftp.hpp +++ b/include/SFML/Network/Ftp.hpp @@ -39,7 +39,7 @@ namespace sf class IpAddress; //////////////////////////////////////////////////////////// -/// \li A FTP client +/// \brief A FTP client /// //////////////////////////////////////////////////////////// class SFML_API Ftp : NonCopyable diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index 124c29cbd..1ed9441d2 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -369,8 +369,8 @@ Glyph Font::LoadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c // Compute the glyph's bounding box glyph.Bounds.Left = bitmapGlyph->left - padding; glyph.Bounds.Top = -bitmapGlyph->top - padding; - glyph.Bounds.Right = bitmapGlyph->left + width + padding; - glyph.Bounds.Bottom = -bitmapGlyph->top + height + padding; + glyph.Bounds.Width = width + 2 * padding; + glyph.Bounds.Height = height + 2 * padding; // Extract the glyph's pixels from the bitmap myPixelBuffer.resize(width * height * 4, 255); @@ -408,8 +408,8 @@ Glyph Font::LoadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c IntRect subrect = glyph.SubRect; subrect.Left += padding; subrect.Top += padding; - subrect.Right -= padding; - subrect.Bottom -= padding; + subrect.Width -= 2 * padding; + subrect.Height -= 2 * padding; page.Texture.UpdatePixels(&myPixelBuffer[0], subrect); } @@ -481,7 +481,7 @@ IntRect Font::FindGlyphRect(Page& page, unsigned int width, unsigned int height) } // Find the glyph's rectangle on the selected row - IntRect rect(row->Width, row->Top, row->Width + width, row->Top + height); + IntRect rect(row->Width, row->Top, width, height); // Update the row informations row->Width += width; diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index 94f8686fb..7d961b755 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -272,24 +272,24 @@ void Image::Copy(const Image& source, unsigned int destX, unsigned int destY, co // Adjust the source rectangle IntRect srcRect = sourceRect; - if (srcRect.GetSize().x == 0 || (srcRect.GetSize().y == 0)) + if (srcRect.Width == 0 || (srcRect.Height == 0)) { srcRect.Left = 0; srcRect.Top = 0; - srcRect.Right = source.myWidth; - srcRect.Bottom = source.myHeight; + srcRect.Width = source.myWidth; + srcRect.Height = source.myHeight; } else { if (srcRect.Left < 0) srcRect.Left = 0; if (srcRect.Top < 0) srcRect.Top = 0; - if (srcRect.Right > static_cast(source.myWidth)) srcRect.Right = source.myWidth; - if (srcRect.Bottom > static_cast(source.myHeight)) srcRect.Bottom = source.myHeight; + if (srcRect.Width > static_cast(source.myWidth)) srcRect.Width = source.myWidth; + if (srcRect.Height > static_cast(source.myHeight)) srcRect.Height = source.myHeight; } // Then find the valid bounds of the destination rectangle - int width = srcRect.GetSize().x; - int height = srcRect.GetSize().y; + int width = srcRect.Width; + int height = srcRect.Height; if (destX + width > myWidth) width = myWidth - destX; if (destY + height > myHeight) height = myHeight - destY; @@ -353,24 +353,24 @@ bool Image::CopyScreen(RenderWindow& window, const IntRect& sourceRect) { // Adjust the source rectangle IntRect srcRect = sourceRect; - if (srcRect.GetSize().x == 0 || (srcRect.GetSize().y == 0)) + if (srcRect.Width == 0 || (srcRect.Height == 0)) { srcRect.Left = 0; srcRect.Top = 0; - srcRect.Right = window.GetWidth(); - srcRect.Bottom = window.GetHeight(); + srcRect.Width = window.GetWidth(); + srcRect.Height = window.GetHeight(); } else { if (srcRect.Left < 0) srcRect.Left = 0; if (srcRect.Top < 0) srcRect.Top = 0; - if (srcRect.Right > static_cast(window.GetWidth())) srcRect.Right = window.GetWidth(); - if (srcRect.Bottom > static_cast(window.GetHeight())) srcRect.Bottom = window.GetHeight(); + if (srcRect.Width > static_cast(window.GetWidth())) srcRect.Width = window.GetWidth(); + if (srcRect.Height > static_cast(window.GetHeight())) srcRect.Height = window.GetHeight(); } // Store the texture dimensions - myWidth = srcRect.GetSize().x; - myHeight = srcRect.GetSize().y; + myWidth = srcRect.Width; + myHeight = srcRect.Height; // We can then create the texture if (window.SetActive() && CreateTexture()) @@ -486,7 +486,7 @@ void Image::UpdatePixels(const Uint8* pixels, const IntRect& rectangle) // Update the texture from the array of pixels GLCheck(glBindTexture(GL_TEXTURE_2D, myTexture)); - GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, rectangle.Left, rectangle.Top, rectangle.GetSize().x, rectangle.GetSize().y, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); + GLCheck(glTexSubImage2D(GL_TEXTURE_2D, 0, rectangle.Left, rectangle.Top, rectangle.Width, rectangle.Height, GL_RGBA, GL_UNSIGNED_BYTE, pixels)); GLCheck(glBindTexture(GL_TEXTURE_2D, previous)); @@ -573,16 +573,16 @@ FloatRect Image::GetTexCoords(const IntRect& rect) const if (myPixelsFlipped) { return FloatRect(rect.Left / width, - rect.Bottom / height, - rect.Right / width, + rect.Height / height, + rect.Width / width, rect.Top / height); } else { return FloatRect(rect.Left / width, rect.Top / height, - rect.Right / width, - rect.Bottom / height); + rect.Width / width, + rect.Height / height); } } else diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index 4f3ee418a..60d3b58e5 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -153,8 +153,8 @@ IntRect RenderTarget::GetViewport(const View& view) const return IntRect(static_cast(0.5f + width * viewport.Left), static_cast(0.5f + height * viewport.Top), - static_cast(0.5f + width * viewport.Right), - static_cast(0.5f + height * viewport.Bottom)); + static_cast(width * viewport.Width), + static_cast(height * viewport.Height)); } @@ -171,8 +171,8 @@ Vector2f RenderTarget::ConvertCoords(unsigned int x, unsigned int y, const View& // First, convert from viewport coordinates to homogeneous coordinates Vector2f coords; IntRect viewport = GetViewport(view); - coords.x = -1.f + 2.f * (static_cast(x) - viewport.Left) / viewport.GetSize().x; - coords.y = 1.f - 2.f * (static_cast(y) - viewport.Top) / viewport.GetSize().y; + coords.x = -1.f + 2.f * (static_cast(x) - viewport.Left) / viewport.Width; + coords.y = 1.f - 2.f * (static_cast(y) - viewport.Top) / viewport.Height; // Then transform by the inverse of the view matrix return view.GetInverseMatrix().Transform(coords); diff --git a/src/SFML/Graphics/Renderer.cpp b/src/SFML/Graphics/Renderer.cpp index b4889a2df..bddf9e031 100644 --- a/src/SFML/Graphics/Renderer.cpp +++ b/src/SFML/Graphics/Renderer.cpp @@ -173,12 +173,15 @@ void Renderer::ApplyColor(const Color& color) //////////////////////////////////////////////////////////// void Renderer::SetViewport(const IntRect& viewport) { - if ((viewport.Left != myViewport.Left) || (viewport.Right != myViewport.Right) || - (viewport.Top != myViewport.Top) || (viewport.Bottom != myViewport.Bottom) || + if ((viewport.Left != myViewport.Left) || (viewport.Width != myViewport.Width) || + (viewport.Top != myViewport.Top) || (viewport.Height != myViewport.Height) || !myViewportIsValid) { - // Apply the new viewport -- revert Y axis to match the OpenGL convention - GLCheck(glViewport(viewport.Left, myTarget.GetHeight() - viewport.Bottom, viewport.GetSize().x, viewport.GetSize().y)); + // Revert the Y axis to match the OpenGL convention + int top = myTarget.GetHeight() - (viewport.Top + viewport.Height); + + // Apply the new viewport + GLCheck(glViewport(viewport.Left, top, viewport.Width, viewport.Height)); // Store it myViewport = viewport; diff --git a/src/SFML/Graphics/Sprite.cpp b/src/SFML/Graphics/Sprite.cpp index eab5a29ca..93d7a215c 100644 --- a/src/SFML/Graphics/Sprite.cpp +++ b/src/SFML/Graphics/Sprite.cpp @@ -93,11 +93,8 @@ void Sprite::SetSubRect(const IntRect& rectangle) //////////////////////////////////////////////////////////// void Sprite::Resize(float width, float height) { - int localWidth = mySubRect.GetSize().x; - int localHeight = mySubRect.GetSize().y; - - if ((localWidth > 0) && (localHeight > 0)) - SetScale(width / localWidth, height / localHeight); + if ((mySubRect.Width > 0) && (mySubRect.Height > 0)) + SetScale(width / mySubRect.Width, height / mySubRect.Height); } @@ -152,7 +149,7 @@ const IntRect& Sprite::GetSubRect() const //////////////////////////////////////////////////////////// Vector2f Sprite::GetSize() const { - return Vector2f(mySubRect.GetSize().x * GetScale().x, mySubRect.GetSize().y * GetScale().y); + return Vector2f(mySubRect.Width * GetScale().x, mySubRect.Height * GetScale().y); } @@ -167,8 +164,8 @@ Color Sprite::GetPixel(unsigned int x, unsigned int y) const unsigned int imageX = mySubRect.Left + x; unsigned int imageY = mySubRect.Top + y; - if (myIsFlippedX) imageX = mySubRect.GetSize().x - imageX - 1; - if (myIsFlippedY) imageY = mySubRect.GetSize().y - imageY - 1; + if (myIsFlippedX) imageX = mySubRect.Width - imageX - 1; + if (myIsFlippedY) imageY = mySubRect.Height - imageY - 1; return myImage->GetPixel(imageX, imageY) * GetColor(); } @@ -185,27 +182,29 @@ Color Sprite::GetPixel(unsigned int x, unsigned int y) const void Sprite::Render(RenderTarget&, Renderer& renderer) const { // Get the sprite size - float width = static_cast(mySubRect.GetSize().x); - float height = static_cast(mySubRect.GetSize().y); + float width = static_cast(mySubRect.Width); + float height = static_cast(mySubRect.Height); // Check if the image is valid, and calculate the texture coordinates FloatRect coords; if (myImage) { coords = myImage->GetTexCoords(mySubRect); - if (myIsFlippedX) std::swap(coords.Left, coords.Right); - if (myIsFlippedY) std::swap(coords.Top, coords.Bottom); + if (myIsFlippedX) coords.Width = -coords.Width; + if (myIsFlippedY) coords.Height = -coords.Height; } // 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, coords.Right, coords.Top); - renderer.AddVertex(0, height, coords.Left, coords.Bottom); - renderer.AddVertex(width, height, coords.Right, coords.Bottom); + 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.End(); } diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 1fc7687db..8e82d289b 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -202,8 +202,8 @@ FloatRect Text::GetRect() const FloatRect rect; rect.Left = (myBaseRect.Left - GetOrigin().x) * GetScale().x + GetPosition().x; rect.Top = (myBaseRect.Top - GetOrigin().y) * GetScale().y + GetPosition().y; - rect.Right = (myBaseRect.Right - GetOrigin().x) * GetScale().x + GetPosition().x; - rect.Bottom = (myBaseRect.Bottom - GetOrigin().y) * GetScale().y + GetPosition().y; + rect.Width = myBaseRect.Width * GetScale().x; + rect.Height = myBaseRect.Height * GetScale().y; return rect; } @@ -229,6 +229,10 @@ void Text::Render(RenderTarget&, Renderer& renderer) const float underlineOffset = myCharacterSize * 0.1f; float underlineThickness = myCharacterSize * (bold ? 0.1f : 0.07f); FloatRect underlineCoords = texture.GetTexCoords(IntRect(1, 1, 1, 1)); + float underlineLeft = underlineCoords.Left; + float underlineTop = underlineCoords.Top; + float underlineRight = underlineCoords.Left + underlineCoords.Width; + float underlineBottom = underlineCoords.Top + underlineCoords.Height; // Initialize the rendering coordinates float space = static_cast(myFont->GetGlyph(L' ', myCharacterSize, bold).Advance); @@ -257,10 +261,10 @@ void Text::Render(RenderTarget&, Renderer& renderer) const float bottom = top + underlineThickness; renderer.Begin(Renderer::QuadList); - renderer.AddVertex(0, top, underlineCoords.Left, underlineCoords.Top); - renderer.AddVertex(x, top, underlineCoords.Right, underlineCoords.Top); - renderer.AddVertex(x, bottom, underlineCoords.Right, underlineCoords.Bottom); - renderer.AddVertex(0, bottom, underlineCoords.Left, underlineCoords.Bottom); + renderer.AddVertex(0, top, underlineLeft, underlineTop); + renderer.AddVertex(x, top, underlineRight, underlineTop); + renderer.AddVertex(x, bottom, underlineRight, underlineBottom); + renderer.AddVertex(0, bottom, underlineLeft, underlineBottom); renderer.End(); } @@ -279,12 +283,17 @@ void Text::Render(RenderTarget&, Renderer& renderer) const const IntRect& bounds = glyph.Bounds; const FloatRect& coords = texture.GetTexCoords(glyph.SubRect); + int boundsRight = bounds.Left + bounds.Width; + int boundsBottom = bounds.Top + bounds.Height; + float coordsRight = coords.Left + coords.Width; + float coordsBottom = coords.Top + coords.Height; + // Draw a textured quad for the current character renderer.Begin(Renderer::QuadList); - renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Top, y + bounds.Top, coords.Left, coords.Top); - renderer.AddVertex(x + bounds.Right - italicCoeff * bounds.Top, y + bounds.Top, coords.Right, coords.Top); - renderer.AddVertex(x + bounds.Right - italicCoeff * bounds.Bottom, y + bounds.Bottom, coords.Right, coords.Bottom); - renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Bottom, y + bounds.Bottom, coords.Left, coords.Bottom); + renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Top, y + bounds.Top, coords.Left, coords.Top); + renderer.AddVertex(x + boundsRight - italicCoeff * bounds.Top, y + bounds.Top, coordsRight, coords.Top); + renderer.AddVertex(x + boundsRight - italicCoeff * boundsBottom, y + boundsBottom, coordsRight, coordsBottom); + renderer.AddVertex(x + bounds.Left - italicCoeff * boundsBottom, y + boundsBottom, coords.Left, coordsBottom); renderer.End(); // Advance to the next character @@ -298,10 +307,10 @@ void Text::Render(RenderTarget&, Renderer& renderer) const float bottom = top + underlineThickness; renderer.Begin(Renderer::QuadList); - renderer.AddVertex(0, top, underlineCoords.Left, underlineCoords.Top); - renderer.AddVertex(x, top, underlineCoords.Right, underlineCoords.Top); - renderer.AddVertex(x, bottom, underlineCoords.Right, underlineCoords.Bottom); - renderer.AddVertex(0, bottom, underlineCoords.Left, underlineCoords.Bottom); + renderer.AddVertex(0, top, underlineLeft, underlineTop); + renderer.AddVertex(x, top, underlineRight, underlineTop); + renderer.AddVertex(x, bottom, underlineRight, underlineBottom); + renderer.AddVertex(0, bottom, underlineLeft, underlineBottom); renderer.End(); } } @@ -375,7 +384,7 @@ void Text::UpdateRect() const curWidth += static_cast(curGlyph.Advance); // Update the maximum height - float charHeight = charSize + curGlyph.Bounds.Bottom; + float charHeight = charSize + curGlyph.Bounds.Top + curGlyph.Bounds.Height; if (charHeight > curHeight) curHeight = charHeight; } @@ -404,8 +413,8 @@ void Text::UpdateRect() const // Finally update the rectangle myBaseRect.Left = 0; myBaseRect.Top = 0; - myBaseRect.Right = width; - myBaseRect.Bottom = height; + myBaseRect.Width = width; + myBaseRect.Height = height; } } // namespace sf diff --git a/src/SFML/Graphics/View.cpp b/src/SFML/Graphics/View.cpp index fcfd4fdc2..bd3e416c2 100644 --- a/src/SFML/Graphics/View.cpp +++ b/src/SFML/Graphics/View.cpp @@ -126,8 +126,10 @@ void View::SetViewport(const FloatRect& viewport) //////////////////////////////////////////////////////////// void View::Reset(const FloatRect& rectangle) { - myCenter = rectangle.GetCenter(); - mySize = rectangle.GetSize(); + myCenter.x = (rectangle.Left + rectangle.Width) / 2.f; + myCenter.y = (rectangle.Top + rectangle.Height) / 2.f; + mySize.x = rectangle.Width; + mySize.y = rectangle.Height; myRotation = 0; myMatrixUpdated = false;