diff --git a/include/SFML/Graphics/Glsl.inl b/include/SFML/Graphics/Glsl.inl index 282631aac..fafb16195 100644 --- a/include/SFML/Graphics/Glsl.inl +++ b/include/SFML/Graphics/Glsl.inl @@ -27,6 +27,7 @@ //////////////////////////////////////////////////////////// #include +#include #include // NOLINT(misc-header-include-cycle) #include @@ -34,7 +35,6 @@ namespace sf { -class Color; class Transform; } // namespace sf @@ -56,13 +56,6 @@ void SFML_GRAPHICS_API copyMatrix(const Transform& source, Matrix<4, 4>& dest); //////////////////////////////////////////////////////////// void SFML_GRAPHICS_API copyMatrix(const float* source, std::size_t elements, float* dest); -//////////////////////////////////////////////////////////// -/// \brief Helper functions to copy sf::Color to sf::Glsl::Vec4/Ivec4 -/// -//////////////////////////////////////////////////////////// -void SFML_GRAPHICS_API copyVector(const Color& source, Vector4& dest); -void SFML_GRAPHICS_API copyVector(const Color& source, Vector4& dest); - //////////////////////////////////////////////////////////// /// \brief Matrix type, used to set uniforms in GLSL @@ -112,7 +105,7 @@ struct Vector4 /// \brief Default constructor, creates a zero vector /// //////////////////////////////////////////////////////////// - Vector4() = default; + constexpr Vector4() = default; //////////////////////////////////////////////////////////// /// \brief Construct from 4 vector components @@ -127,7 +120,7 @@ struct Vector4 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" #endif - Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) + constexpr Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) { } #if defined(__GNUC__) @@ -141,7 +134,7 @@ struct Vector4 /// //////////////////////////////////////////////////////////// template - explicit Vector4(const Vector4& other) : + constexpr explicit Vector4(const Vector4& other) : x(static_cast(other.x)), y(static_cast(other.y)), z(static_cast(other.z)), @@ -150,16 +143,15 @@ struct Vector4 } //////////////////////////////////////////////////////////// - /// \brief Construct float vector implicitly from color + /// \brief Construct vector implicitly from color /// - /// \param color Color instance. Is normalized to [0, 1] - /// for floats, and left as-is for ints. + /// Vector is normalized to [0, 1] for floats, and left as-is + /// for ints. Not defined for other template arguments. + /// + /// \param color Color instance /// //////////////////////////////////////////////////////////// - Vector4(const Color& color) - { - copyVector(color, *this); - } + constexpr Vector4(const Color& color); T x{}; //!< 1st component (X) of the 4D vector T y{}; //!< 2nd component (Y) of the 4D vector @@ -167,4 +159,22 @@ struct Vector4 T w{}; //!< 4th component (W) of the 4D vector }; + +//////////////////////////////////////////////////////////// +template <> +constexpr Vector4::Vector4(const Color& color) : +x(color.r / 255.f), +y(color.g / 255.f), +z(color.b / 255.f), +w(color.a / 255.f) +{ +} + + +//////////////////////////////////////////////////////////// +template <> +constexpr Vector4::Vector4(const Color& color) : x(color.r), y(color.g), z(color.b), w(color.a) +{ +} + } // namespace sf::priv diff --git a/src/SFML/Graphics/Glsl.cpp b/src/SFML/Graphics/Glsl.cpp index 35669b5d0..88ab326be 100644 --- a/src/SFML/Graphics/Glsl.cpp +++ b/src/SFML/Graphics/Glsl.cpp @@ -69,24 +69,4 @@ void copyMatrix(const float* source, std::size_t elements, float* dest) std::copy(source, source + elements, dest); } - -//////////////////////////////////////////////////////////// -void copyVector(const Color& source, Vector4& dest) -{ - dest.x = source.r / 255.f; - dest.y = source.g / 255.f; - dest.z = source.b / 255.f; - dest.w = source.a / 255.f; -} - - -//////////////////////////////////////////////////////////// -void copyVector(const Color& source, Vector4& dest) -{ - dest.x = static_cast(source.r); - dest.y = static_cast(source.g); - dest.z = static_cast(source.b); - dest.w = static_cast(source.a); -} - } // namespace sf::priv diff --git a/test/Graphics/Glsl.test.cpp b/test/Graphics/Glsl.test.cpp index d09f3742a..b9d68a9a5 100644 --- a/test/Graphics/Glsl.test.cpp +++ b/test/Graphics/Glsl.test.cpp @@ -126,30 +126,30 @@ TEST_CASE("[Graphics] sf::Glsl") SECTION("Verbose constructor") { - const sf::Glsl::Vec4 vec(1, 2, 3, 4); - CHECK(vec.x == 1); - CHECK(vec.y == 2); - CHECK(vec.z == 3); - CHECK(vec.w == 4); + constexpr sf::Glsl::Vec4 vec(1, 2, 3, 4); + STATIC_CHECK(vec.x == 1); + STATIC_CHECK(vec.y == 2); + STATIC_CHECK(vec.z == 3); + STATIC_CHECK(vec.w == 4); } SECTION("Conversion constructor") { - const sf::Glsl::Ivec4 ivec(10, 12, 14, 16); - const sf::Glsl::Vec4 vec(ivec); - CHECK(vec.x == 10); - CHECK(vec.y == 12); - CHECK(vec.z == 14); - CHECK(vec.w == 16); + constexpr sf::Glsl::Ivec4 ivec(10, 12, 14, 16); + constexpr sf::Glsl::Vec4 vec(ivec); + STATIC_CHECK(vec.x == 10); + STATIC_CHECK(vec.y == 12); + STATIC_CHECK(vec.z == 14); + STATIC_CHECK(vec.w == 16); } SECTION("Color constructor") { - const sf::Glsl::Vec4 vec(sf::Color(0, 128, 192, 255)); - CHECK(vec.x == 0.f); - CHECK(vec.y == Approx(0.50196f)); - CHECK(vec.z == Approx(0.75294f)); - CHECK(vec.w == 1.f); + constexpr sf::Glsl::Vec4 vec = sf::Color(0, 128, 192, 255); + STATIC_CHECK(vec.x == 0.f); + STATIC_CHECK(vec.y == 128 / 255.f); + STATIC_CHECK(vec.z == 192 / 255.f); + STATIC_CHECK(vec.w == 1.f); } } @@ -174,30 +174,30 @@ TEST_CASE("[Graphics] sf::Glsl") SECTION("Verbose constructor") { - const sf::Glsl::Ivec4 vec(1, 2, 3, 4); - CHECK(vec.x == 1); - CHECK(vec.y == 2); - CHECK(vec.z == 3); - CHECK(vec.w == 4); + constexpr sf::Glsl::Ivec4 vec(1, 2, 3, 4); + STATIC_CHECK(vec.x == 1); + STATIC_CHECK(vec.y == 2); + STATIC_CHECK(vec.z == 3); + STATIC_CHECK(vec.w == 4); } SECTION("Conversion constructor") { - const sf::Glsl::Bvec4 bvec(true, false, true, false); - const sf::Glsl::Ivec4 vec(bvec); - CHECK(vec.x == 1); - CHECK(vec.y == 0); - CHECK(vec.z == 1); - CHECK(vec.w == 0); + constexpr sf::Glsl::Bvec4 bvec(true, false, true, false); + constexpr sf::Glsl::Ivec4 vec(bvec); + STATIC_CHECK(vec.x == 1); + STATIC_CHECK(vec.y == 0); + STATIC_CHECK(vec.z == 1); + STATIC_CHECK(vec.w == 0); } SECTION("Color constructor") { - const sf::Glsl::Ivec4 vec(sf::Color(0, 128, 192, 255)); - CHECK(vec.x == 0); - CHECK(vec.y == 128); - CHECK(vec.z == 192); - CHECK(vec.w == 255); + constexpr sf::Glsl::Ivec4 vec = sf::Color(0, 128, 192, 255); + STATIC_CHECK(vec.x == 0); + STATIC_CHECK(vec.y == 128); + STATIC_CHECK(vec.z == 192); + STATIC_CHECK(vec.w == 255); } } @@ -222,21 +222,21 @@ TEST_CASE("[Graphics] sf::Glsl") SECTION("Verbose constructor") { - const sf::Glsl::Bvec4 vec(false, true, true, false); - CHECK(vec.x == false); - CHECK(vec.y == true); - CHECK(vec.z == true); - CHECK(vec.w == false); + constexpr sf::Glsl::Bvec4 vec(false, true, true, false); + STATIC_CHECK(vec.x == false); + STATIC_CHECK(vec.y == true); + STATIC_CHECK(vec.z == true); + STATIC_CHECK(vec.w == false); } SECTION("Conversion constructor") { - const sf::Glsl::Ivec4 ivec(0, -7, 2, 10); - const sf::Glsl::Bvec4 vec(ivec); - CHECK(vec.x == false); - CHECK(vec.y == true); - CHECK(vec.z == true); - CHECK(vec.w == true); + constexpr sf::Glsl::Ivec4 ivec(0, -7, 2, 10); + constexpr sf::Glsl::Bvec4 vec(ivec); + STATIC_CHECK(vec.x == false); + STATIC_CHECK(vec.y == true); + STATIC_CHECK(vec.z == true); + STATIC_CHECK(vec.w == true); } }