From 55f0918c629991888409134aa52ba9893d04538c Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 13 Nov 2023 13:07:48 -0700 Subject: [PATCH] Remove `sf::View::reset` in favor of assignment operations It's rare that a type truly needs a .reset function. Copy/move assignment typically accomplishes the same thing with less code and is easier to maintain since it doesn't require updating your .reset() function as new data members are added. To reset a type is conceptually the same thing as simply assigning from a newly constructed instance of the same type. --- include/SFML/Graphics/View.hpp | 15 +-------------- src/SFML/Graphics/RenderTarget.cpp | 4 ++-- src/SFML/Graphics/View.cpp | 16 ++-------------- test/Graphics/View.test.cpp | 18 ------------------ 4 files changed, 5 insertions(+), 48 deletions(-) diff --git a/include/SFML/Graphics/View.hpp b/include/SFML/Graphics/View.hpp index f895bc4d6..77f056271 100644 --- a/include/SFML/Graphics/View.hpp +++ b/include/SFML/Graphics/View.hpp @@ -143,18 +143,6 @@ public: //////////////////////////////////////////////////////////// void setScissor(const FloatRect& scissor); - //////////////////////////////////////////////////////////// - /// \brief Reset the view to the given rectangle - /// - /// Note that this function resets the rotation angle to 0. - /// - /// \param rectangle Rectangle defining the zone to display - /// - /// \see setCenter, setSize, setRotation - /// - //////////////////////////////////////////////////////////// - void reset(const FloatRect& rectangle); - //////////////////////////////////////////////////////////// /// \brief Get the center of the view /// @@ -339,10 +327,9 @@ private: /// Usage example: /// \code /// sf::RenderWindow window; -/// sf::View view; /// /// // Initialize the view to a rectangle located at (100, 100) and with a size of 400x200 -/// view.reset(sf::FloatRect({100, 100}, {400, 200})); +/// sf::View view(sf::FloatRect({100, 100}, {400, 200})); /// /// // Rotate it by 45 degrees /// view.rotate(sf::degrees(45)); diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index 654199711..b3c183e07 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -641,8 +641,8 @@ void RenderTarget::resetGLStates() void RenderTarget::initialize() { // Setup the default and current views - m_defaultView.reset(FloatRect({0, 0}, Vector2f(getSize()))); - m_view = m_defaultView; + m_defaultView = View(FloatRect({0, 0}, Vector2f(getSize()))); + m_view = m_defaultView; // Set GL states only on first draw, so that we don't pollute user's states m_cache.glStatesSet = false; diff --git a/src/SFML/Graphics/View.cpp b/src/SFML/Graphics/View.cpp index d35c5d67e..b5e77ab01 100644 --- a/src/SFML/Graphics/View.cpp +++ b/src/SFML/Graphics/View.cpp @@ -34,9 +34,8 @@ namespace sf { //////////////////////////////////////////////////////////// -View::View(const FloatRect& rectangle) +View::View(const FloatRect& rectangle) : m_center(rectangle.getCenter()), m_size(rectangle.getSize()) { - reset(rectangle); } @@ -54,6 +53,7 @@ void View::setCenter(const Vector2f& center) m_invTransformUpdated = false; } + //////////////////////////////////////////////////////////// void View::setSize(const Vector2f& size) { @@ -95,18 +95,6 @@ void View::setScissor(const FloatRect& scissor) } -//////////////////////////////////////////////////////////// -void View::reset(const FloatRect& rectangle) -{ - m_center = rectangle.getCenter(); - m_size = rectangle.getSize(); - m_rotation = Angle::Zero; - - m_transformUpdated = false; - m_invTransformUpdated = false; -} - - //////////////////////////////////////////////////////////// const Vector2f& View::getCenter() const { diff --git a/test/Graphics/View.test.cpp b/test/Graphics/View.test.cpp index 3cb829311..b81935b5a 100644 --- a/test/Graphics/View.test.cpp +++ b/test/Graphics/View.test.cpp @@ -107,24 +107,6 @@ TEST_CASE("[Graphics] sf::View") CHECK(view.getViewport() == sf::FloatRect({0, 0}, {1, 1})); } - SECTION("reset()") - { - sf::View view; - view.setCenter({3.14f, 4.2f}); - view.setSize({600, 900}); - view.setRotation(sf::degrees(15)); - view.setViewport({{150, 250}, {500, 750}}); - view.setScissor({{0.2f, 0.3f}, {0.4f, 0.5f}}); - view.reset({{1, 2}, {3, 4}}); - CHECK(view.getCenter() == sf::Vector2f(2.5f, 4)); - CHECK(view.getSize() == sf::Vector2f(3, 4)); - CHECK(view.getRotation() == sf::Angle::Zero); - CHECK(view.getViewport() == sf::FloatRect({150, 250}, {500, 750})); - CHECK(view.getScissor() == sf::FloatRect({0.2f, 0.3f}, {0.4f, 0.5f})); - CHECK(view.getTransform() == Approx(sf::Transform(0.666667f, 0, -1.66667f, 0, -0.5f, 2, 0, 0, 1))); - CHECK(view.getInverseTransform() == Approx(sf::Transform(1.5f, 0, 2.5f, 0, -2, 4, 0, 0, 1))); - } - SECTION("move()") { sf::View view;