From 7c8d1b533243b04928f0ef56de9fa713d69ec136 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 2 May 2023 13:04:33 -0600 Subject: [PATCH] Use more `sf::Vector` operations When dealing with calculations that are fundamentally two dimensional it's helpful to use a 2D data type as much as possible rather than decomposing the calculations into x and y components. The more we use vector operations the better chance we have of easiliy reasoning about what the code is doing. --- examples/shader/Shader.cpp | 3 +-- src/SFML/Graphics/Font.cpp | 3 +-- src/SFML/Graphics/Image.cpp | 6 ++---- src/SFML/Graphics/RenderTarget.cpp | 14 +++++--------- src/SFML/Graphics/Shape.cpp | 19 +++++++++---------- src/SFML/Graphics/View.cpp | 6 ++---- src/SFML/Window/Android/WindowImplAndroid.cpp | 4 ++-- src/SFML/Window/WindowBase.cpp | 3 +-- 8 files changed, 23 insertions(+), 35 deletions(-) diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index 01c8d4d98..e1e4026c8 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -279,8 +279,7 @@ public: // Spread the coordinates from -480 to +480 // So they'll always fill the viewport at 800x600 std::uniform_real_distribution positionDistribution(-480, 480); - m_pointCloud[i].position.x = positionDistribution(rng); - m_pointCloud[i].position.y = positionDistribution(rng); + m_pointCloud[i].position = {positionDistribution(rng), positionDistribution(rng)}; } // Load the texture diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index d1819257a..604b96cd6 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -396,8 +396,7 @@ float Font::getKerning(std::uint32_t first, std::uint32_t second, unsigned int c const auto secondLsbDelta = static_cast(getGlyph(second, characterSize, bold).lsbDelta); // Get the kerning vector if present - FT_Vector kerning; - kerning.x = kerning.y = 0; + FT_Vector kerning{0, 0}; if (FT_HAS_KERNING(face)) FT_Get_Kerning(face, index1, index2, FT_KERNING_UNFITTED, &kerning); diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index a22380855..2fd93059a 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -122,8 +122,7 @@ void Image::create(const Vector2u& size, const Color& color) std::vector().swap(m_pixels); // Assign the new size - m_size.x = 0; - m_size.y = 0; + m_size = {}; } } @@ -148,8 +147,7 @@ void Image::create(const Vector2u& size, const std::uint8_t* pixels) std::vector().swap(m_pixels); // Assign the new size - m_size.x = 0; - m_size.y = 0; + m_size = {}; } } diff --git a/src/SFML/Graphics/RenderTarget.cpp b/src/SFML/Graphics/RenderTarget.cpp index 36c1f765c..f0e95505d 100644 --- a/src/SFML/Graphics/RenderTarget.cpp +++ b/src/SFML/Graphics/RenderTarget.cpp @@ -211,10 +211,9 @@ Vector2f RenderTarget::mapPixelToCoords(const Vector2i& point) const Vector2f RenderTarget::mapPixelToCoords(const Vector2i& point, const View& view) const { // First, convert from viewport coordinates to homogeneous coordinates - Vector2f normalized; - const FloatRect viewport = FloatRect(getViewport(view)); - normalized.x = -1.f + 2.f * (static_cast(point.x) - viewport.left) / viewport.width; - normalized.y = 1.f - 2.f * (static_cast(point.y) - viewport.top) / viewport.height; + const FloatRect viewport = FloatRect(getViewport(view)); + const Vector2f normalized = Vector2f(-1, 1) + + Vector2f(2, -2).cwiseMul(Vector2f(point) - viewport.getPosition()).cwiseDiv(viewport.getSize()); // Then transform by the inverse of the view matrix return view.getInverseTransform().transformPoint(normalized); @@ -235,12 +234,9 @@ Vector2i RenderTarget::mapCoordsToPixel(const Vector2f& point, const View& view) const Vector2f normalized = view.getTransform().transformPoint(point); // Then convert to viewport coordinates - Vector2i pixel; const FloatRect viewport = FloatRect(getViewport(view)); - pixel.x = static_cast((normalized.x + 1.f) / 2.f * viewport.width + viewport.left); - pixel.y = static_cast((-normalized.y + 1.f) / 2.f * viewport.height + viewport.top); - - return pixel; + return Vector2i((normalized.cwiseMul({1, -1}) + sf::Vector2f(1, 1)).cwiseDiv({2, 2}).cwiseMul(viewport.getSize()) + + viewport.getPosition()); } diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index b68ae39d8..4412e5d93 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -221,8 +221,7 @@ void Shape::update() m_insideBounds = m_vertices.getBounds(); // Compute the center and make it the first vertex - m_vertices[0].position.x = m_insideBounds.left + m_insideBounds.width / 2; - m_vertices[0].position.y = m_insideBounds.top + m_insideBounds.height / 2; + m_vertices[0].position = m_insideBounds.getPosition() + m_insideBounds.getSize() / 2.f; // Color updateFillColors(); @@ -270,14 +269,14 @@ void Shape::updateTexCoords() for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) { - const float xratio = m_insideBounds.width > 0 - ? (m_vertices[i].position.x - m_insideBounds.left) / m_insideBounds.width - : 0; - const float yratio = m_insideBounds.height > 0 - ? (m_vertices[i].position.y - m_insideBounds.top) / m_insideBounds.height - : 0; - m_vertices[i].texCoords.x = convertedTextureRect.left + convertedTextureRect.width * xratio; - m_vertices[i].texCoords.y = convertedTextureRect.top + convertedTextureRect.height * yratio; + const float xratio = m_insideBounds.width > 0 + ? (m_vertices[i].position.x - m_insideBounds.left) / m_insideBounds.width + : 0; + const float yratio = m_insideBounds.height > 0 + ? (m_vertices[i].position.y - m_insideBounds.top) / m_insideBounds.height + : 0; + m_vertices[i].texCoords = convertedTextureRect.getPosition() + + convertedTextureRect.getSize().cwiseMul({xratio, yratio}); } } diff --git a/src/SFML/Graphics/View.cpp b/src/SFML/Graphics/View.cpp index a3a8ae17c..07eb24cc9 100644 --- a/src/SFML/Graphics/View.cpp +++ b/src/SFML/Graphics/View.cpp @@ -90,10 +90,8 @@ void View::setViewport(const FloatRect& viewport) //////////////////////////////////////////////////////////// void View::reset(const FloatRect& rectangle) { - m_center.x = rectangle.left + rectangle.width / 2.f; - m_center.y = rectangle.top + rectangle.height / 2.f; - m_size.x = rectangle.width; - m_size.y = rectangle.height; + m_center = rectangle.getPosition() + rectangle.getSize() / 2.f; + m_size = rectangle.getSize(); m_rotation = Angle::Zero; m_transformUpdated = false; diff --git a/src/SFML/Window/Android/WindowImplAndroid.cpp b/src/SFML/Window/Android/WindowImplAndroid.cpp index 23cd4c3d3..f6c8b8023 100644 --- a/src/SFML/Window/Android/WindowImplAndroid.cpp +++ b/src/SFML/Window/Android/WindowImplAndroid.cpp @@ -235,8 +235,8 @@ void WindowImplAndroid::forwardEvent(const Event& event) if (event.type == Event::GainedFocus) { - WindowImplAndroid::singleInstance->m_size.x = static_cast(ANativeWindow_getWidth(states.window)); - WindowImplAndroid::singleInstance->m_size.y = static_cast(ANativeWindow_getHeight(states.window)); + WindowImplAndroid::singleInstance->m_size = Vector2u( + Vector2i(ANativeWindow_getWidth(states.window), ANativeWindow_getHeight(states.window))); WindowImplAndroid::singleInstance->m_windowBeingCreated = true; WindowImplAndroid::singleInstance->m_hasFocus = true; } diff --git a/src/SFML/Window/WindowBase.cpp b/src/SFML/Window/WindowBase.cpp index 453927a65..9be065b28 100644 --- a/src/SFML/Window/WindowBase.cpp +++ b/src/SFML/Window/WindowBase.cpp @@ -386,8 +386,7 @@ bool WindowBase::filterEvent(const Event& event) if (event.type == Event::Resized) { // Cache the new size - m_size.x = event.size.width; - m_size.y = event.size.height; + m_size = {event.size.width, event.size.height}; // Notify the derived class onResize();