diff --git a/examples/shader/Shader.cpp b/examples/shader/Shader.cpp index eb8142349..df3f24287 100644 --- a/examples/shader/Shader.cpp +++ b/examples/shader/Shader.cpp @@ -151,7 +151,7 @@ public: const auto r = static_cast(colorDistribution(rng)); const auto g = static_cast(colorDistribution(rng)); const auto b = static_cast(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 diff --git a/include/SFML/Graphics/Vertex.hpp b/include/SFML/Graphics/Vertex.hpp index 43879750b..62ad4b9d7 100644 --- a/include/SFML/Graphics/Vertex.hpp +++ b/include/SFML/Graphics/Vertex.hpp @@ -37,73 +37,24 @@ namespace sf //////////////////////////////////////////////////////////// /// \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 //////////////////////////////////////////////////////////// - Vector2f position; //!< 2D position of the vertex + Vector2f position{}; //!< 2D position 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 -#include - //////////////////////////////////////////////////////////// -/// \class sf::Vertex +/// \struct sf::Vertex /// \ingroup graphics /// /// A vertex is an improved point. It has a position and other @@ -124,20 +75,44 @@ public: /// Example: /// \code /// // 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)), -/// sf::Vertex(sf::Vector2f( 0, 100), sf::Color::Red, sf::Vector2f( 0, 10)), -/// sf::Vertex(sf::Vector2f(100, 100), sf::Color::Red, sf::Vector2f(10, 10)), -/// sf::Vertex(sf::Vector2f( 0, 0), sf::Color::Red, sf::Vector2f( 0, 0)), -/// sf::Vertex(sf::Vector2f(100, 100), sf::Color::Red, sf::Vector2f(10, 10)), -/// sf::Vertex(sf::Vector2f(100, 0), sf::Color::Red, sf::Vector2f(10, 0)) +/// {{ 0.0f, 0.0f}, sf::Color::Red, { 0.0f, 0.0f}}, +/// {{ 0.0f, 100.0f}, sf::Color::Red, { 0.0f, 10.0f}}, +/// {{100.0f, 100.0f}, sf::Color::Red, {10.0f, 10.0f}}, +/// {{ 0.0f, 0.0f}, sf::Color::Red, { 0.0f, 0.0f}}, +/// {{100.0f, 100.0f}, sf::Color::Red, {10.0f, 10.0f}}, +/// {{100.0f, 0.0f}, sf::Color::Red, {10.0f, 0.0f}} /// }; /// /// // draw it /// window.draw(vertices, 6, sf::PrimitiveType::Triangles); /// \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 /// amount of pixels, their type is float because of some buggy graphics /// drivers that are not able to process integer coordinates correctly. diff --git a/include/SFML/Graphics/Vertex.inl b/include/SFML/Graphics/Vertex.inl deleted file mode 100644 index baac4beca..000000000 --- a/include/SFML/Graphics/Vertex.inl +++ /dev/null @@ -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 // 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 diff --git a/include/SFML/Graphics/VertexBuffer.hpp b/include/SFML/Graphics/VertexBuffer.hpp index 784d0772b..6f343d902 100644 --- a/include/SFML/Graphics/VertexBuffer.hpp +++ b/include/SFML/Graphics/VertexBuffer.hpp @@ -41,7 +41,7 @@ namespace sf { class RenderTarget; -class Vertex; +struct Vertex; //////////////////////////////////////////////////////////// /// \brief Vertex buffer storage for one or more 2D primitives diff --git a/src/SFML/Graphics/CMakeLists.txt b/src/SFML/Graphics/CMakeLists.txt index e436b05b1..d7adf0fca 100644 --- a/src/SFML/Graphics/CMakeLists.txt +++ b/src/SFML/Graphics/CMakeLists.txt @@ -48,7 +48,6 @@ set(SRC ${SRCROOT}/View.cpp ${INCROOT}/View.hpp ${INCROOT}/Vertex.hpp - ${INCROOT}/Vertex.inl ) source_group("" FILES ${SRC}) diff --git a/src/SFML/Graphics/Text.cpp b/src/SFML/Graphics/Text.cpp index c1b264e5c..c188ccc0c 100644 --- a/src/SFML/Graphics/Text.cpp +++ b/src/SFML/Graphics/Text.cpp @@ -52,15 +52,12 @@ void addLine(sf::VertexArray& vertices, const float top = std::floor(lineTop + offset - (thickness / 2) + 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( - sf::Vertex(sf::Vector2f(lineLength + outlineThickness, top - outlineThickness), color, sf::Vector2f(1, 1))); - vertices.append(sf::Vertex(sf::Vector2f(-outlineThickness, bottom + outlineThickness), color, sf::Vector2f(1, 1))); - vertices.append(sf::Vertex(sf::Vector2f(-outlineThickness, bottom + outlineThickness), color, sf::Vector2f(1, 1))); - vertices.append( - 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))); + vertices.append({{-outlineThickness, top - outlineThickness}, color, {1.0f, 1.0f}}); + vertices.append({{lineLength + outlineThickness, top - outlineThickness}, color, {1.0f, 1.0f}}); + vertices.append({{-outlineThickness, bottom + outlineThickness}, color, {1.0f, 1.0f}}); + vertices.append({{-outlineThickness, bottom + outlineThickness}, color, {1.0f, 1.0f}}); + vertices.append({{lineLength + outlineThickness, top - outlineThickness}, color, {1.0f, 1.0f}}); + vertices.append({{lineLength + outlineThickness, bottom + outlineThickness}, color, {1.0f, 1.0f}}); } // 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(glyph.textureRect.left + glyph.textureRect.width) + padding; const float v2 = static_cast(glyph.textureRect.top + glyph.textureRect.height) + padding; - vertices.append( - sf::Vertex(sf::Vector2f(position.x + left - italicShear * top, position.y + top), color, sf::Vector2f(u1, v1))); - 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 + left - italicShear * bottom, position.y + bottom), color, sf::Vector2f(u1, 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))); + vertices.append({{position.x + left - italicShear * top, position.y + top}, color, {u1, v1}}); + vertices.append({{position.x + right - italicShear * top, position.y + top}, color, {u2, v1}}); + vertices.append({{position.x + left - italicShear * bottom, position.y + bottom}, color, {u1, v2}}); + vertices.append({{position.x + left - italicShear * bottom, position.y + bottom}, color, {u1, v2}}); + vertices.append({{position.x + right - italicShear * top, position.y + top}, color, {u2, v1}}); + vertices.append({{position.x + right - italicShear * bottom, position.y + bottom}, color, {u2, v2}}); } } // namespace diff --git a/test/Graphics/Vertex.test.cpp b/test/Graphics/Vertex.test.cpp index ff9fe1838..0fca658f5 100644 --- a/test/Graphics/Vertex.test.cpp +++ b/test/Graphics/Vertex.test.cpp @@ -13,11 +13,12 @@ TEST_CASE("[Graphics] sf::Vertex") STATIC_CHECK(std::is_copy_assignable_v); STATIC_CHECK(std::is_nothrow_move_constructible_v); STATIC_CHECK(std::is_nothrow_move_assignable_v); + STATIC_CHECK(std::is_aggregate_v); } SECTION("Construction") { - SECTION("Default constructor") + SECTION("Aggregate initialization -- Nothing") { constexpr sf::Vertex vertex; 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)); } - 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.color == sf::Color(255, 255, 255)); 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.color == sf::Color(3, 4, 5, 6)); 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}); - 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}); + 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(3, 4, 5, 6)); STATIC_CHECK(vertex.texCoords == sf::Vector2f(7.0f, 8.0f)); diff --git a/test/Graphics/VertexArray.test.cpp b/test/Graphics/VertexArray.test.cpp index 1ada790e0..1916c42a9 100644 --- a/test/Graphics/VertexArray.test.cpp +++ b/test/Graphics/VertexArray.test.cpp @@ -41,9 +41,9 @@ TEST_CASE("[Graphics] sf::VertexArray") CHECK(vertexArray.getBounds() == sf::FloatRect({0, 0}, {0, 0})); for (std::size_t i = 0; i < vertexArray.getVertexCount(); ++i) { - CHECK(vertexArray[i].position == sf::Vertex().position); - CHECK(vertexArray[i].color == sf::Vertex().color); - CHECK(vertexArray[i].texCoords == sf::Vertex().texCoords); + CHECK(vertexArray[i].position == sf::Vertex{}.position); + CHECK(vertexArray[i].color == sf::Vertex{}.color); + CHECK(vertexArray[i].texCoords == sf::Vertex{}.texCoords); } } } @@ -55,9 +55,9 @@ TEST_CASE("[Graphics] sf::VertexArray") CHECK(vertexArray.getVertexCount() == 42); for (std::size_t i = 0; i < vertexArray.getVertexCount(); ++i) { - CHECK(vertexArray[i].position == sf::Vertex().position); - CHECK(vertexArray[i].color == sf::Vertex().color); - CHECK(vertexArray[i].texCoords == sf::Vertex().texCoords); + CHECK(vertexArray[i].position == sf::Vertex{}.position); + CHECK(vertexArray[i].color == sf::Vertex{}.color); + CHECK(vertexArray[i].texCoords == sf::Vertex{}.texCoords); } } @@ -72,7 +72,7 @@ TEST_CASE("[Graphics] sf::VertexArray") SECTION("Append to array") { 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); CHECK(vertexArray.getVertexCount() == 1); CHECK(vertexArray[0].position == vertex.position); @@ -84,7 +84,7 @@ TEST_CASE("[Graphics] sf::VertexArray") { sf::VertexArray vertexArray; 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; CHECK(vertexArray[9].position == otherVertex.position); CHECK(vertexArray[9].color == otherVertex.color); @@ -101,14 +101,14 @@ TEST_CASE("[Graphics] sf::VertexArray") SECTION("Get bounds") { sf::VertexArray vertexArray; - vertexArray.append(sf::Vertex({1, 1})); - vertexArray.append(sf::Vertex({2, 2})); + vertexArray.append({{1, 1}}); + vertexArray.append({{2, 2}}); 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})); - vertexArray[0] = sf::Vertex({5, 5}); + vertexArray[0] = {{5, 5}}; 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})); } }