Skip to content

Commit

Permalink
Remove sf::View::reset in favor of assignment operations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChrisThrasher committed May 13, 2024
1 parent 3acc332 commit 86c6455
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 48 deletions.
15 changes: 1 addition & 14 deletions include/SFML/Graphics/View.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,6 @@ class SFML_GRAPHICS_API View
////////////////////////////////////////////////////////////
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
///
Expand Down Expand Up @@ -339,10 +327,9 @@ class SFML_GRAPHICS_API View
/// 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));
Expand Down
4 changes: 2 additions & 2 deletions src/SFML/Graphics/RenderTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 2 additions & 14 deletions src/SFML/Graphics/View.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand All @@ -54,6 +53,7 @@ void View::setCenter(const Vector2f& center)
m_invTransformUpdated = false;
}


////////////////////////////////////////////////////////////
void View::setSize(const Vector2f& size)
{
Expand Down Expand Up @@ -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
{
Expand Down
18 changes: 0 additions & 18 deletions test/Graphics/View.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 86c6455

Please sign in to comment.