Turn 'sf::Vertex' into an aggregate 'struct'

This commit is contained in:
vittorioromeo 2024-02-09 13:21:44 +01:00 committed by Chris Thrasher
parent 691292b23a
commit 23c26f9b70
8 changed files with 72 additions and 180 deletions

View File

@ -151,7 +151,7 @@ public:
const auto r = static_cast<std::uint8_t>(colorDistribution(rng)); const auto r = static_cast<std::uint8_t>(colorDistribution(rng));
const auto g = static_cast<std::uint8_t>(colorDistribution(rng)); const auto g = static_cast<std::uint8_t>(colorDistribution(rng));
const auto b = static_cast<std::uint8_t>(colorDistribution(rng)); const auto b = static_cast<std::uint8_t>(colorDistribution(rng));
m_points.append(sf::Vertex(sf::Vector2f(x, y), sf::Color(r, g, b))); m_points.append({{x, y}, {r, g, b}});
} }
// Load the shader // Load the shader

View File

@ -37,73 +37,24 @@ namespace sf
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Define a point with color and texture coordinates /// \brief Define a point with color and texture coordinates
/// ///
/// By default, the vertex color is white and texture coordinates are (0, 0).
///
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
class Vertex struct Vertex
{ {
public:
////////////////////////////////////////////////////////////
/// \brief Default constructor
///
////////////////////////////////////////////////////////////
constexpr Vertex();
////////////////////////////////////////////////////////////
/// \brief Construct the vertex from its position
///
/// The vertex color is white and texture coordinates are (0, 0).
///
/// \param thePosition Vertex position
///
////////////////////////////////////////////////////////////
constexpr Vertex(const Vector2f& thePosition);
////////////////////////////////////////////////////////////
/// \brief Construct the vertex from its position and color
///
/// The texture coordinates are (0, 0).
///
/// \param thePosition Vertex position
/// \param theColor Vertex color
///
////////////////////////////////////////////////////////////
constexpr Vertex(const Vector2f& thePosition, const Color& theColor);
////////////////////////////////////////////////////////////
/// \brief Construct the vertex from its position and texture coordinates
///
/// The vertex color is white.
///
/// \param thePosition Vertex position
/// \param theTexCoords Vertex texture coordinates
///
////////////////////////////////////////////////////////////
constexpr Vertex(const Vector2f& thePosition, const Vector2f& theTexCoords);
////////////////////////////////////////////////////////////
/// \brief Construct the vertex from its position, color and texture coordinates
///
/// \param thePosition Vertex position
/// \param theColor Vertex color
/// \param theTexCoords Vertex texture coordinates
///
////////////////////////////////////////////////////////////
constexpr Vertex(const Vector2f& thePosition, const Color& theColor, const Vector2f& theTexCoords);
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Vector2f position; //!< 2D position of the vertex Vector2f position{}; //!< 2D position of the vertex
Color color{Color::White}; //!< Color of the vertex Color color{Color::White}; //!< Color of the vertex
Vector2f texCoords; //!< Coordinates of the texture's pixel to map to the vertex Vector2f texCoords{}; //!< Coordinates of the texture's pixel to map to the vertex
}; };
} // namespace sf } // namespace sf
#include <SFML/Graphics/Vertex.inl>
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \class sf::Vertex /// \struct sf::Vertex
/// \ingroup graphics /// \ingroup graphics
/// ///
/// A vertex is an improved point. It has a position and other /// A vertex is an improved point. It has a position and other
@ -124,20 +75,44 @@ public:
/// Example: /// Example:
/// \code /// \code
/// // define a 100x100 square, red, with a 10x10 texture mapped on it /// // define a 100x100 square, red, with a 10x10 texture mapped on it
/// sf::Vertex vertices[] = /// sf::Vertex vertices[]
/// { /// {
/// sf::Vertex(sf::Vector2f( 0, 0), sf::Color::Red, sf::Vector2f( 0, 0)), /// {{ 0.0f, 0.0f}, sf::Color::Red, { 0.0f, 0.0f}},
/// sf::Vertex(sf::Vector2f( 0, 100), sf::Color::Red, sf::Vector2f( 0, 10)), /// {{ 0.0f, 100.0f}, sf::Color::Red, { 0.0f, 10.0f}},
/// sf::Vertex(sf::Vector2f(100, 100), sf::Color::Red, sf::Vector2f(10, 10)), /// {{100.0f, 100.0f}, sf::Color::Red, {10.0f, 10.0f}},
/// sf::Vertex(sf::Vector2f( 0, 0), sf::Color::Red, sf::Vector2f( 0, 0)), /// {{ 0.0f, 0.0f}, sf::Color::Red, { 0.0f, 0.0f}},
/// sf::Vertex(sf::Vector2f(100, 100), sf::Color::Red, sf::Vector2f(10, 10)), /// {{100.0f, 100.0f}, sf::Color::Red, {10.0f, 10.0f}},
/// sf::Vertex(sf::Vector2f(100, 0), sf::Color::Red, sf::Vector2f(10, 0)) /// {{100.0f, 0.0f}, sf::Color::Red, {10.0f, 0.0f}}
/// }; /// };
/// ///
/// // draw it /// // draw it
/// window.draw(vertices, 6, sf::PrimitiveType::Triangles); /// window.draw(vertices, 6, sf::PrimitiveType::Triangles);
/// \endcode /// \endcode
/// ///
///
/// It is recommended to use aggregate initialization to create vertex
/// objects, which initializes the members in order.
///
/// On a C++20-compliant compiler (or where supported as an extension)
/// it is possible to use "designated initializers" to only initialize
/// a subset of members, with the restriction of having to follow the
/// same order in which they are defined.
///
/// Example:
/// \code
/// // C++17 and above
/// sf::Vertex v0{{5.0f, 5.0f}}; // explicit 'position', implicit 'color' and 'texCoords'
/// sf::Vertex v1{{5.0f, 5.0f}, sf::Color::Red}; // explicit 'position' and 'color', implicit 'texCoords'
/// sf::Vertex v2{{5.0f, 5.0f}, sf::Color::Red, {1.0f, 1.0f}}; // everything is explicitly specified
///
/// // C++20 and above (or compilers supporting "designated initializers" as an extension)
/// sf::Vertex v3{
/// .position{5.0f, 5.0f},
/// .texCoords{1.0f, 1.0f}
/// };
/// \endcode
///
///
/// Note: although texture coordinates are supposed to be an integer /// Note: although texture coordinates are supposed to be an integer
/// amount of pixels, their type is float because of some buggy graphics /// amount of pixels, their type is float because of some buggy graphics
/// drivers that are not able to process integer coordinates correctly. /// drivers that are not able to process integer coordinates correctly.

View File

@ -1,65 +0,0 @@
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
// you must not claim that you wrote the original software.
// If you use this software in a product, an acknowledgment
// in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
// and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics/Vertex.hpp> // NOLINT(misc-header-include-cycle)
namespace sf
{
////////////////////////////////////////////////////////////
constexpr Vertex::Vertex() = default;
////////////////////////////////////////////////////////////
constexpr Vertex::Vertex(const Vector2f& thePosition) : position(thePosition)
{
}
////////////////////////////////////////////////////////////
constexpr Vertex::Vertex(const Vector2f& thePosition, const Color& theColor) : position(thePosition), color(theColor)
{
}
////////////////////////////////////////////////////////////
constexpr Vertex::Vertex(const Vector2f& thePosition, const Vector2f& theTexCoords) :
position(thePosition),
texCoords(theTexCoords)
{
}
////////////////////////////////////////////////////////////
constexpr Vertex::Vertex(const Vector2f& thePosition, const Color& theColor, const Vector2f& theTexCoords) :
position(thePosition),
color(theColor),
texCoords(theTexCoords)
{
}
} // namespace sf

View File

@ -41,7 +41,7 @@
namespace sf namespace sf
{ {
class RenderTarget; class RenderTarget;
class Vertex; struct Vertex;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Vertex buffer storage for one or more 2D primitives /// \brief Vertex buffer storage for one or more 2D primitives

View File

@ -48,7 +48,6 @@ set(SRC
${SRCROOT}/View.cpp ${SRCROOT}/View.cpp
${INCROOT}/View.hpp ${INCROOT}/View.hpp
${INCROOT}/Vertex.hpp ${INCROOT}/Vertex.hpp
${INCROOT}/Vertex.inl
) )
source_group("" FILES ${SRC}) source_group("" FILES ${SRC})

View File

@ -52,15 +52,12 @@ void addLine(sf::VertexArray& vertices,
const float top = std::floor(lineTop + offset - (thickness / 2) + 0.5f); const float top = std::floor(lineTop + offset - (thickness / 2) + 0.5f);
const float bottom = top + std::floor(thickness + 0.5f); const float bottom = top + std::floor(thickness + 0.5f);
vertices.append(sf::Vertex(sf::Vector2f(-outlineThickness, top - outlineThickness), color, sf::Vector2f(1, 1))); vertices.append({{-outlineThickness, top - outlineThickness}, color, {1.0f, 1.0f}});
vertices.append( vertices.append({{lineLength + outlineThickness, top - outlineThickness}, color, {1.0f, 1.0f}});
sf::Vertex(sf::Vector2f(lineLength + outlineThickness, top - outlineThickness), color, sf::Vector2f(1, 1))); vertices.append({{-outlineThickness, bottom + outlineThickness}, color, {1.0f, 1.0f}});
vertices.append(sf::Vertex(sf::Vector2f(-outlineThickness, bottom + outlineThickness), color, sf::Vector2f(1, 1))); vertices.append({{-outlineThickness, bottom + outlineThickness}, color, {1.0f, 1.0f}});
vertices.append(sf::Vertex(sf::Vector2f(-outlineThickness, bottom + outlineThickness), color, sf::Vector2f(1, 1))); vertices.append({{lineLength + outlineThickness, top - outlineThickness}, color, {1.0f, 1.0f}});
vertices.append( vertices.append({{lineLength + outlineThickness, bottom + outlineThickness}, color, {1.0f, 1.0f}});
sf::Vertex(sf::Vector2f(lineLength + outlineThickness, top - outlineThickness), color, sf::Vector2f(1, 1)));
vertices.append(
sf::Vertex(sf::Vector2f(lineLength + outlineThickness, bottom + outlineThickness), color, sf::Vector2f(1, 1)));
} }
// Add a glyph quad to the vertex array // Add a glyph quad to the vertex array
@ -78,19 +75,12 @@ void addGlyphQuad(sf::VertexArray& vertices, sf::Vector2f position, const sf::Co
const float u2 = static_cast<float>(glyph.textureRect.left + glyph.textureRect.width) + padding; const float u2 = static_cast<float>(glyph.textureRect.left + glyph.textureRect.width) + padding;
const float v2 = static_cast<float>(glyph.textureRect.top + glyph.textureRect.height) + padding; const float v2 = static_cast<float>(glyph.textureRect.top + glyph.textureRect.height) + padding;
vertices.append( vertices.append({{position.x + left - italicShear * top, position.y + top}, color, {u1, v1}});
sf::Vertex(sf::Vector2f(position.x + left - italicShear * top, position.y + top), color, sf::Vector2f(u1, v1))); vertices.append({{position.x + right - italicShear * top, position.y + top}, color, {u2, v1}});
vertices.append( vertices.append({{position.x + left - italicShear * bottom, position.y + bottom}, color, {u1, v2}});
sf::Vertex(sf::Vector2f(position.x + right - italicShear * top, position.y + top), color, sf::Vector2f(u2, v1))); vertices.append({{position.x + left - italicShear * bottom, position.y + bottom}, color, {u1, v2}});
vertices.append( vertices.append({{position.x + right - italicShear * top, position.y + top}, color, {u2, v1}});
sf::Vertex(sf::Vector2f(position.x + left - italicShear * bottom, position.y + bottom), color, sf::Vector2f(u1, v2))); vertices.append({{position.x + right - italicShear * bottom, position.y + bottom}, color, {u2, v2}});
vertices.append(
sf::Vertex(sf::Vector2f(position.x + left - italicShear * bottom, position.y + bottom), color, sf::Vector2f(u1, v2)));
vertices.append(
sf::Vertex(sf::Vector2f(position.x + right - italicShear * top, position.y + top), color, sf::Vector2f(u2, v1)));
vertices.append(sf::Vertex(sf::Vector2f(position.x + right - italicShear * bottom, position.y + bottom),
color,
sf::Vector2f(u2, v2)));
} }
} // namespace } // namespace

View File

@ -13,11 +13,12 @@ TEST_CASE("[Graphics] sf::Vertex")
STATIC_CHECK(std::is_copy_assignable_v<sf::Vertex>); STATIC_CHECK(std::is_copy_assignable_v<sf::Vertex>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Vertex>); STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Vertex>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Vertex>); STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Vertex>);
STATIC_CHECK(std::is_aggregate_v<sf::Vertex>);
} }
SECTION("Construction") SECTION("Construction")
{ {
SECTION("Default constructor") SECTION("Aggregate initialization -- Nothing")
{ {
constexpr sf::Vertex vertex; constexpr sf::Vertex vertex;
STATIC_CHECK(vertex.position == sf::Vector2f(0.0f, 0.0f)); STATIC_CHECK(vertex.position == sf::Vector2f(0.0f, 0.0f));
@ -25,33 +26,25 @@ TEST_CASE("[Graphics] sf::Vertex")
STATIC_CHECK(vertex.texCoords == sf::Vector2f(0.0f, 0.0f)); STATIC_CHECK(vertex.texCoords == sf::Vector2f(0.0f, 0.0f));
} }
SECTION("Position constructor") SECTION("Aggregate initialization -- Position")
{ {
constexpr sf::Vertex vertex({1, 2}); constexpr sf::Vertex vertex{{1.0f, 2.0f}};
STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f)); STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f));
STATIC_CHECK(vertex.color == sf::Color(255, 255, 255)); STATIC_CHECK(vertex.color == sf::Color(255, 255, 255));
STATIC_CHECK(vertex.texCoords == sf::Vector2f(0.0f, 0.0f)); STATIC_CHECK(vertex.texCoords == sf::Vector2f(0.0f, 0.0f));
} }
SECTION("Position and color constructor") SECTION("Aggregate initialization -- Position and color")
{ {
constexpr sf::Vertex vertex({1, 2}, {3, 4, 5, 6}); constexpr sf::Vertex vertex{{1.0f, 2.0f}, {3, 4, 5, 6}};
STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f)); STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f));
STATIC_CHECK(vertex.color == sf::Color(3, 4, 5, 6)); STATIC_CHECK(vertex.color == sf::Color(3, 4, 5, 6));
STATIC_CHECK(vertex.texCoords == sf::Vector2f(0.0f, 0.0f)); STATIC_CHECK(vertex.texCoords == sf::Vector2f(0.0f, 0.0f));
} }
SECTION("Position and coords constructor") SECTION("Aggregate initialization -- Position, color, and coords")
{ {
constexpr sf::Vertex vertex({1, 2}, {3, 4}); constexpr sf::Vertex vertex{{1.0f, 2.0f}, {3, 4, 5, 6}, {7.0f, 8.0f}};
STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f));
STATIC_CHECK(vertex.color == sf::Color(255, 255, 255));
STATIC_CHECK(vertex.texCoords == sf::Vector2f(3.0f, 4.0f));
}
SECTION("Position, color, and coords constructor")
{
constexpr sf::Vertex vertex({1, 2}, {3, 4, 5, 6}, {7, 8});
STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f)); STATIC_CHECK(vertex.position == sf::Vector2f(1.0f, 2.0f));
STATIC_CHECK(vertex.color == sf::Color(3, 4, 5, 6)); STATIC_CHECK(vertex.color == sf::Color(3, 4, 5, 6));
STATIC_CHECK(vertex.texCoords == sf::Vector2f(7.0f, 8.0f)); STATIC_CHECK(vertex.texCoords == sf::Vector2f(7.0f, 8.0f));

View File

@ -41,9 +41,9 @@ TEST_CASE("[Graphics] sf::VertexArray")
CHECK(vertexArray.getBounds() == sf::FloatRect({0, 0}, {0, 0})); CHECK(vertexArray.getBounds() == sf::FloatRect({0, 0}, {0, 0}));
for (std::size_t i = 0; i < vertexArray.getVertexCount(); ++i) for (std::size_t i = 0; i < vertexArray.getVertexCount(); ++i)
{ {
CHECK(vertexArray[i].position == sf::Vertex().position); CHECK(vertexArray[i].position == sf::Vertex{}.position);
CHECK(vertexArray[i].color == sf::Vertex().color); CHECK(vertexArray[i].color == sf::Vertex{}.color);
CHECK(vertexArray[i].texCoords == sf::Vertex().texCoords); CHECK(vertexArray[i].texCoords == sf::Vertex{}.texCoords);
} }
} }
} }
@ -55,9 +55,9 @@ TEST_CASE("[Graphics] sf::VertexArray")
CHECK(vertexArray.getVertexCount() == 42); CHECK(vertexArray.getVertexCount() == 42);
for (std::size_t i = 0; i < vertexArray.getVertexCount(); ++i) for (std::size_t i = 0; i < vertexArray.getVertexCount(); ++i)
{ {
CHECK(vertexArray[i].position == sf::Vertex().position); CHECK(vertexArray[i].position == sf::Vertex{}.position);
CHECK(vertexArray[i].color == sf::Vertex().color); CHECK(vertexArray[i].color == sf::Vertex{}.color);
CHECK(vertexArray[i].texCoords == sf::Vertex().texCoords); CHECK(vertexArray[i].texCoords == sf::Vertex{}.texCoords);
} }
} }
@ -72,7 +72,7 @@ TEST_CASE("[Graphics] sf::VertexArray")
SECTION("Append to array") SECTION("Append to array")
{ {
sf::VertexArray vertexArray; sf::VertexArray vertexArray;
const sf::Vertex vertex({1, 2}, {3, 4, 5, 6}, {7, 8}); const sf::Vertex vertex{{1.0f, 2.0f}, {3, 4, 5, 6}, {7.0f, 8.0f}};
vertexArray.append(vertex); vertexArray.append(vertex);
CHECK(vertexArray.getVertexCount() == 1); CHECK(vertexArray.getVertexCount() == 1);
CHECK(vertexArray[0].position == vertex.position); CHECK(vertexArray[0].position == vertex.position);
@ -84,7 +84,7 @@ TEST_CASE("[Graphics] sf::VertexArray")
{ {
sf::VertexArray vertexArray; sf::VertexArray vertexArray;
vertexArray.resize(10); vertexArray.resize(10);
const sf::Vertex otherVertex({2, 3}, {4, 5, 6, 7}, {8, 9}); const sf::Vertex otherVertex{{2.0f, 3.0f}, {4, 5, 6, 7}, {8.0f, 9.0f}};
vertexArray[9] = otherVertex; vertexArray[9] = otherVertex;
CHECK(vertexArray[9].position == otherVertex.position); CHECK(vertexArray[9].position == otherVertex.position);
CHECK(vertexArray[9].color == otherVertex.color); CHECK(vertexArray[9].color == otherVertex.color);
@ -101,14 +101,14 @@ TEST_CASE("[Graphics] sf::VertexArray")
SECTION("Get bounds") SECTION("Get bounds")
{ {
sf::VertexArray vertexArray; sf::VertexArray vertexArray;
vertexArray.append(sf::Vertex({1, 1})); vertexArray.append({{1, 1}});
vertexArray.append(sf::Vertex({2, 2})); vertexArray.append({{2, 2}});
CHECK(vertexArray.getBounds() == sf::FloatRect({1, 1}, {1, 1})); CHECK(vertexArray.getBounds() == sf::FloatRect({1, 1}, {1, 1}));
vertexArray[0] = sf::Vertex({0, 0}); vertexArray[0] = {{0, 0}};
CHECK(vertexArray.getBounds() == sf::FloatRect({0, 0}, {2, 2})); CHECK(vertexArray.getBounds() == sf::FloatRect({0, 0}, {2, 2}));
vertexArray[0] = sf::Vertex({5, 5}); vertexArray[0] = {{5, 5}};
CHECK(vertexArray.getBounds() == sf::FloatRect({2, 2}, {3, 3})); CHECK(vertexArray.getBounds() == sf::FloatRect({2, 2}, {3, 3}));
vertexArray.append(sf::Vertex({10, 10})); vertexArray.append({{10, 10}});
CHECK(vertexArray.getBounds() == sf::FloatRect({2, 2}, {8, 8})); CHECK(vertexArray.getBounds() == sf::FloatRect({2, 2}, {8, 8}));
} }
} }