From 0e419543f2193da08858f8c46140b02d31936b3e Mon Sep 17 00:00:00 2001 From: Vittorio Romeo Date: Thu, 27 Jan 2022 02:28:19 +0000 Subject: [PATCH] Make 'Color' constants 'constexpr' and add tests --- include/SFML/Graphics/Color.hpp | 18 ++++++------- include/SFML/Graphics/Color.inl | 18 +++++++++++++ src/SFML/Graphics/CMakeLists.txt | 1 - src/SFML/Graphics/Color.cpp | 46 -------------------------------- test/Graphics/Color.cpp | 2 ++ 5 files changed, 29 insertions(+), 56 deletions(-) delete mode 100644 src/SFML/Graphics/Color.cpp diff --git a/include/SFML/Graphics/Color.hpp b/include/SFML/Graphics/Color.hpp index 24db35e1..ac9973da 100644 --- a/include/SFML/Graphics/Color.hpp +++ b/include/SFML/Graphics/Color.hpp @@ -80,15 +80,15 @@ public: //////////////////////////////////////////////////////////// // Static member data //////////////////////////////////////////////////////////// - SFML_GRAPHICS_API static const Color Black; //!< Black predefined color - SFML_GRAPHICS_API static const Color White; //!< White predefined color - SFML_GRAPHICS_API static const Color Red; //!< Red predefined color - SFML_GRAPHICS_API static const Color Green; //!< Green predefined color - SFML_GRAPHICS_API static const Color Blue; //!< Blue predefined color - SFML_GRAPHICS_API static const Color Yellow; //!< Yellow predefined color - SFML_GRAPHICS_API static const Color Magenta; //!< Magenta predefined color - SFML_GRAPHICS_API static const Color Cyan; //!< Cyan predefined color - SFML_GRAPHICS_API static const Color Transparent; //!< Transparent (black) predefined color + static const Color Black; //!< Black predefined color + static const Color White; //!< White predefined color + static const Color Red; //!< Red predefined color + static const Color Green; //!< Green predefined color + static const Color Blue; //!< Blue predefined color + static const Color Yellow; //!< Yellow predefined color + static const Color Magenta; //!< Magenta predefined color + static const Color Cyan; //!< Cyan predefined color + static const Color Transparent; //!< Transparent (black) predefined color //////////////////////////////////////////////////////////// // Member data diff --git a/include/SFML/Graphics/Color.inl b/include/SFML/Graphics/Color.inl index 42e6dd7b..e9c34889 100644 --- a/include/SFML/Graphics/Color.inl +++ b/include/SFML/Graphics/Color.inl @@ -147,3 +147,21 @@ constexpr Color& operator *=(Color& left, const Color& right) { return left = left * right; } + + +//////////////////////////////////////////////////////////// +// Static member data +//////////////////////////////////////////////////////////// + +// Note: the 'inline' keyword here is technically not required, but VS2019 fails +// to compile with a bogus "multiple definition" error if not explicitly used. + +inline constexpr Color Color::Black(0, 0, 0); +inline constexpr Color Color::White(255, 255, 255); +inline constexpr Color Color::Red(255, 0, 0); +inline constexpr Color Color::Green(0, 255, 0); +inline constexpr Color Color::Blue(0, 0, 255); +inline constexpr Color Color::Yellow(255, 255, 0); +inline constexpr Color Color::Magenta(255, 0, 255); +inline constexpr Color Color::Cyan(0, 255, 255); +inline constexpr Color Color::Transparent(0, 0, 0, 0); diff --git a/src/SFML/Graphics/CMakeLists.txt b/src/SFML/Graphics/CMakeLists.txt index 28d79137..ca2c5b24 100644 --- a/src/SFML/Graphics/CMakeLists.txt +++ b/src/SFML/Graphics/CMakeLists.txt @@ -6,7 +6,6 @@ set(SRCROOT ${PROJECT_SOURCE_DIR}/src/SFML/Graphics) set(SRC ${SRCROOT}/BlendMode.cpp ${INCROOT}/BlendMode.hpp - ${SRCROOT}/Color.cpp ${INCROOT}/Color.hpp ${INCROOT}/Color.inl ${INCROOT}/Export.hpp diff --git a/src/SFML/Graphics/Color.cpp b/src/SFML/Graphics/Color.cpp deleted file mode 100644 index 09970eb0..00000000 --- a/src/SFML/Graphics/Color.cpp +++ /dev/null @@ -1,46 +0,0 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2022 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 - - -namespace sf -{ -//////////////////////////////////////////////////////////// -// Static member data -//////////////////////////////////////////////////////////// -const Color Color::Black(0, 0, 0); -const Color Color::White(255, 255, 255); -const Color Color::Red(255, 0, 0); -const Color Color::Green(0, 255, 0); -const Color Color::Blue(0, 0, 255); -const Color Color::Yellow(255, 255, 0); -const Color Color::Magenta(255, 0, 255); -const Color Color::Cyan(0, 255, 255); -const Color Color::Transparent(0, 0, 0, 0); - -} // namespace sf diff --git a/test/Graphics/Color.cpp b/test/Graphics/Color.cpp index 9cf115a5..1a432b6f 100644 --- a/test/Graphics/Color.cpp +++ b/test/Graphics/Color.cpp @@ -98,5 +98,7 @@ TEST_CASE("sf::Color class - [graphics]") static_assert(c.a == 4); static_assert(c + c == sf::Color(2, 4, 6, 8)); + + static_assert(sf::Color::Black == sf::Color(0, 0, 0, 255)); } }