From d706ca902d35b52cd29c7623d0b445b80aa74d75 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Tue, 1 Oct 2024 09:38:40 -0600 Subject: [PATCH] Enable range based looping of `sf::VertexArray` --- include/SFML/Graphics/VertexArray.hpp | 48 +++++++++++++++++++++++++++ src/SFML/Graphics/Shape.cpp | 14 ++++---- src/SFML/Graphics/Text.cpp | 8 ++--- src/SFML/Graphics/VertexArray.cpp | 28 ++++++++++++++++ test/Graphics/VertexArray.test.cpp | 30 +++++++++++++++++ 5 files changed, 117 insertions(+), 11 deletions(-) diff --git a/include/SFML/Graphics/VertexArray.hpp b/include/SFML/Graphics/VertexArray.hpp index 6dc472819..006f8c835 100644 --- a/include/SFML/Graphics/VertexArray.hpp +++ b/include/SFML/Graphics/VertexArray.hpp @@ -175,6 +175,54 @@ public: //////////////////////////////////////////////////////////// [[nodiscard]] FloatRect getBounds() const; + //////////////////////////////////////////////////////////// + /// \brief Return an iterator to the beginning of the array + /// + /// \return Read-write iterator to the beginning of the vertices + /// + /// \see `end` + /// + //////////////////////////////////////////////////////////// + [[nodiscard]] std::vector::iterator begin(); + + //////////////////////////////////////////////////////////// + /// \brief Return an iterator to the beginning of the array + /// + /// \return Read-only iterator to the beginning of the vertices + /// + /// \see `end` + /// + //////////////////////////////////////////////////////////// + [[nodiscard]] std::vector::const_iterator begin() const; + + //////////////////////////////////////////////////////////// + /// \brief Return an iterator to the end of the array + /// + /// The end iterator refers to 1 position past the last vertex; + /// thus it represents an invalid vertex and should never be + /// accessed. + /// + /// \return Read-write iterator to the end of the vertices + /// + /// \see `begin` + /// + //////////////////////////////////////////////////////////// + [[nodiscard]] std::vector::iterator end(); + + //////////////////////////////////////////////////////////// + /// \brief Return an iterator to the end of the array + /// + /// The end iterator refers to 1 position past the last vertex; + /// thus it represents an invalid vertex and should never be + /// accessed. + /// + /// \return Read-only iterator to the end of the vertices + /// + /// \see `begin` + /// + //////////////////////////////////////////////////////////// + [[nodiscard]] std::vector::const_iterator end() const; + private: //////////////////////////////////////////////////////////// /// \brief Draw the vertex array to a render target diff --git a/src/SFML/Graphics/Shape.cpp b/src/SFML/Graphics/Shape.cpp index e6d576af7..5a76145a7 100644 --- a/src/SFML/Graphics/Shape.cpp +++ b/src/SFML/Graphics/Shape.cpp @@ -255,8 +255,8 @@ void Shape::draw(RenderTarget& target, RenderStates states) const //////////////////////////////////////////////////////////// void Shape::updateFillColors() { - for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) - m_vertices[i].color = m_fillColor; + for (auto& vertex : m_vertices) + vertex.color = m_fillColor; } @@ -269,10 +269,10 @@ void Shape::updateTexCoords() const Vector2f safeInsideSize(m_insideBounds.size.x > 0 ? m_insideBounds.size.x : 1.f, m_insideBounds.size.y > 0 ? m_insideBounds.size.y : 1.f); - for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) + for (auto& vertex : m_vertices) { - const Vector2f ratio = (m_vertices[i].position - m_insideBounds.position).componentWiseDiv(safeInsideSize); - m_vertices[i].texCoords = convertedTextureRect.position + convertedTextureRect.size.componentWiseMul(ratio); + const Vector2f ratio = (vertex.position - m_insideBounds.position).componentWiseDiv(safeInsideSize); + vertex.texCoords = convertedTextureRect.position + convertedTextureRect.size.componentWiseMul(ratio); } } @@ -335,8 +335,8 @@ void Shape::updateOutline() //////////////////////////////////////////////////////////// void Shape::updateOutlineColors() { - for (std::size_t i = 0; i < m_outlineVertices.getVertexCount(); ++i) - m_outlineVertices[i].color = m_outlineColor; + for (auto& vertex : m_outlineVertices) + vertex.color = m_outlineColor; } } // namespace sf diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index 27f3912ec..7de0d4b9a 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -169,8 +169,8 @@ void Text::setFillColor(Color color) // (if geometry is updated anyway, we can skip this step) if (!m_geometryNeedUpdate) { - for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) - m_vertices[i].color = m_fillColor; + for (auto& vertex : m_vertices) + vertex.color = m_fillColor; } } } @@ -187,8 +187,8 @@ void Text::setOutlineColor(Color color) // (if geometry is updated anyway, we can skip this step) if (!m_geometryNeedUpdate) { - for (std::size_t i = 0; i < m_outlineVertices.getVertexCount(); ++i) - m_outlineVertices[i].color = m_outlineColor; + for (auto& vertex : m_outlineVertices) + vertex.color = m_outlineColor; } } } diff --git a/src/SFML/Graphics/VertexArray.cpp b/src/SFML/Graphics/VertexArray.cpp index 904c839e1..771ffda9d 100644 --- a/src/SFML/Graphics/VertexArray.cpp +++ b/src/SFML/Graphics/VertexArray.cpp @@ -132,6 +132,34 @@ FloatRect VertexArray::getBounds() const } +//////////////////////////////////////////////////////////// +std::vector::iterator VertexArray::begin() +{ + return m_vertices.begin(); +} + + +//////////////////////////////////////////////////////////// +std::vector::const_iterator VertexArray::begin() const +{ + return m_vertices.begin(); +} + + +//////////////////////////////////////////////////////////// +std::vector::iterator VertexArray::end() +{ + return m_vertices.end(); +} + + +//////////////////////////////////////////////////////////// +std::vector::const_iterator VertexArray::end() const +{ + return m_vertices.end(); +} + + //////////////////////////////////////////////////////////// void VertexArray::draw(RenderTarget& target, RenderStates states) const { diff --git a/test/Graphics/VertexArray.test.cpp b/test/Graphics/VertexArray.test.cpp index 1916c42a9..1157642dd 100644 --- a/test/Graphics/VertexArray.test.cpp +++ b/test/Graphics/VertexArray.test.cpp @@ -111,4 +111,34 @@ TEST_CASE("[Graphics] sf::VertexArray") vertexArray.append({{10, 10}}); CHECK(vertexArray.getBounds() == sf::FloatRect({2, 2}, {8, 8})); } + + SECTION("Ranged loop") + { + sf::VertexArray vertexArray; + vertexArray.append({{1, 1}}); + vertexArray.append({{1, 1}}); + vertexArray.append({{1, 1}}); + + std::size_t count = 0; + + SECTION("Const") + { + for (const auto& vertex : std::as_const(vertexArray)) + { + CHECK(vertex.position == sf::Vector2f(1, 1)); + ++count; + } + } + + SECTION("Non const") + { + for (const auto& vertex : vertexArray) + { + CHECK(vertex.position == sf::Vector2f(1, 1)); + ++count; + } + } + + CHECK(count == vertexArray.getVertexCount()); + } }