Enable range based looping of sf::VertexArray

This commit is contained in:
Chris Thrasher 2024-10-01 09:38:40 -06:00
parent c8dbd65ff5
commit d706ca902d
No known key found for this signature in database
GPG Key ID: 56FB686C9DFC8E2C
5 changed files with 117 additions and 11 deletions

View File

@ -175,6 +175,54 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
[[nodiscard]] FloatRect getBounds() const; [[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<Vertex>::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<Vertex>::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<Vertex>::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<Vertex>::const_iterator end() const;
private: private:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Draw the vertex array to a render target /// \brief Draw the vertex array to a render target

View File

@ -255,8 +255,8 @@ void Shape::draw(RenderTarget& target, RenderStates states) const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Shape::updateFillColors() void Shape::updateFillColors()
{ {
for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) for (auto& vertex : m_vertices)
m_vertices[i].color = m_fillColor; 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, 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); 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); const Vector2f ratio = (vertex.position - m_insideBounds.position).componentWiseDiv(safeInsideSize);
m_vertices[i].texCoords = convertedTextureRect.position + convertedTextureRect.size.componentWiseMul(ratio); vertex.texCoords = convertedTextureRect.position + convertedTextureRect.size.componentWiseMul(ratio);
} }
} }
@ -335,8 +335,8 @@ void Shape::updateOutline()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void Shape::updateOutlineColors() void Shape::updateOutlineColors()
{ {
for (std::size_t i = 0; i < m_outlineVertices.getVertexCount(); ++i) for (auto& vertex : m_outlineVertices)
m_outlineVertices[i].color = m_outlineColor; vertex.color = m_outlineColor;
} }
} // namespace sf } // namespace sf

View File

@ -169,8 +169,8 @@ void Text::setFillColor(Color color)
// (if geometry is updated anyway, we can skip this step) // (if geometry is updated anyway, we can skip this step)
if (!m_geometryNeedUpdate) if (!m_geometryNeedUpdate)
{ {
for (std::size_t i = 0; i < m_vertices.getVertexCount(); ++i) for (auto& vertex : m_vertices)
m_vertices[i].color = m_fillColor; vertex.color = m_fillColor;
} }
} }
} }
@ -187,8 +187,8 @@ void Text::setOutlineColor(Color color)
// (if geometry is updated anyway, we can skip this step) // (if geometry is updated anyway, we can skip this step)
if (!m_geometryNeedUpdate) if (!m_geometryNeedUpdate)
{ {
for (std::size_t i = 0; i < m_outlineVertices.getVertexCount(); ++i) for (auto& vertex : m_outlineVertices)
m_outlineVertices[i].color = m_outlineColor; vertex.color = m_outlineColor;
} }
} }
} }

View File

@ -132,6 +132,34 @@ FloatRect VertexArray::getBounds() const
} }
////////////////////////////////////////////////////////////
std::vector<Vertex>::iterator VertexArray::begin()
{
return m_vertices.begin();
}
////////////////////////////////////////////////////////////
std::vector<Vertex>::const_iterator VertexArray::begin() const
{
return m_vertices.begin();
}
////////////////////////////////////////////////////////////
std::vector<Vertex>::iterator VertexArray::end()
{
return m_vertices.end();
}
////////////////////////////////////////////////////////////
std::vector<Vertex>::const_iterator VertexArray::end() const
{
return m_vertices.end();
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void VertexArray::draw(RenderTarget& target, RenderStates states) const void VertexArray::draw(RenderTarget& target, RenderStates states) const
{ {

View File

@ -111,4 +111,34 @@ TEST_CASE("[Graphics] sf::VertexArray")
vertexArray.append({{10, 10}}); vertexArray.append({{10, 10}});
CHECK(vertexArray.getBounds() == sf::FloatRect({2, 2}, {8, 8})); 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());
}
} }