From d3a79e6282e9c35bf066d9c84ae6c61da1ffda97 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Sun, 12 Nov 2023 21:05:30 -0700 Subject: [PATCH] Add `sf::Rect::getCenter()` --- include/SFML/Graphics/Rect.hpp | 14 ++++++++++++-- include/SFML/Graphics/Rect.inl | 8 ++++++++ src/SFML/Graphics/Shape.cpp | 2 +- src/SFML/Graphics/View.cpp | 2 +- test/Graphics/Rect.test.cpp | 6 ++++++ 5 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/SFML/Graphics/Rect.hpp b/include/SFML/Graphics/Rect.hpp index cad47bd78..85abf4a75 100644 --- a/include/SFML/Graphics/Rect.hpp +++ b/include/SFML/Graphics/Rect.hpp @@ -109,7 +109,7 @@ public: /// /// \return Position of rectangle /// - /// \see getSize + /// \see getSize, getCenter /// //////////////////////////////////////////////////////////// constexpr Vector2 getPosition() const; @@ -119,11 +119,21 @@ public: /// /// \return Size of rectangle /// - /// \see getPosition + /// \see getPosition, getCenter /// //////////////////////////////////////////////////////////// constexpr Vector2 getSize() const; + //////////////////////////////////////////////////////////// + /// \brief Get the position of the center of the rectangle + /// + /// \return Center of rectangle + /// + /// \see getSize, getPosition + /// + //////////////////////////////////////////////////////////// + constexpr Vector2 getCenter() const; + //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// diff --git a/include/SFML/Graphics/Rect.inl b/include/SFML/Graphics/Rect.inl index 3c61263a5..7c0f58876 100644 --- a/include/SFML/Graphics/Rect.inl +++ b/include/SFML/Graphics/Rect.inl @@ -127,6 +127,14 @@ constexpr Vector2 Rect::getSize() const } +//////////////////////////////////////////////////////////// +template +constexpr Vector2 Rect::getCenter() const +{ + return getPosition() + getSize() / T{2}; +} + + //////////////////////////////////////////////////////////// template constexpr bool operator==(const Rect& left, const Rect& right) diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index 4412e5d93..6ba756da4 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -221,7 +221,7 @@ void Shape::update() m_insideBounds = m_vertices.getBounds(); // Compute the center and make it the first vertex - m_vertices[0].position = m_insideBounds.getPosition() + m_insideBounds.getSize() / 2.f; + m_vertices[0].position = m_insideBounds.getCenter(); // Color updateFillColors(); diff --git a/src/SFML/Graphics/View.cpp b/src/SFML/Graphics/View.cpp index 07eb24cc9..51726631a 100644 --- a/src/SFML/Graphics/View.cpp +++ b/src/SFML/Graphics/View.cpp @@ -90,7 +90,7 @@ void View::setViewport(const FloatRect& viewport) //////////////////////////////////////////////////////////// void View::reset(const FloatRect& rectangle) { - m_center = rectangle.getPosition() + rectangle.getSize() / 2.f; + m_center = rectangle.getCenter(); m_size = rectangle.getSize(); m_rotation = Angle::Zero; diff --git a/test/Graphics/Rect.test.cpp b/test/Graphics/Rect.test.cpp index 31280f2d0..78c34f1e8 100644 --- a/test/Graphics/Rect.test.cpp +++ b/test/Graphics/Rect.test.cpp @@ -103,6 +103,12 @@ TEST_CASE("[Graphics] sf::Rect") STATIC_CHECK(sf::IntRect({1, 2}, {3, 4}).getSize() == sf::Vector2i(3, 4)); } + SECTION("getCenter()") + { + STATIC_CHECK(sf::IntRect({}, {}).getCenter() == sf::Vector2i()); + STATIC_CHECK(sf::IntRect({1, 2}, {4, 6}).getCenter() == sf::Vector2i(3, 5)); + } + SECTION("Operators") { SECTION("operator==")