diff --git a/test/Graphics/RectangleShape.test.cpp b/test/Graphics/RectangleShape.test.cpp index cb676c4ad..b10439212 100644 --- a/test/Graphics/RectangleShape.test.cpp +++ b/test/Graphics/RectangleShape.test.cpp @@ -1,7 +1,10 @@ +#include #include +#include #include +#include #include #include @@ -46,4 +49,25 @@ TEST_CASE("[Graphics] sf::RectangleShape") CHECK(rectangle.getSize() == sf::Vector2f(5, 4)); CHECK(rectangle.getGeometricCenter() == sf::Vector2f(5.f, 4.f) / 2.f); } + + SUBCASE("Render rectangle shape") + { + sf::RectangleShape rectangle({50.f, 20.f}); + rectangle.setFillColor(sf::Color::Red); + rectangle.setOutlineColor(sf::Color::Green); + rectangle.setOutlineThickness(2.f); + rectangle.setPosition({10.f, 10.f}); + + sf::RenderTexture renderTexture; + REQUIRE(renderTexture.create({100, 100})); + + renderTexture.clear(); + renderTexture.draw(rectangle); + renderTexture.display(); + + auto image = renderTexture.getTexture().copyToImage(); + REQUIRE(image.saveToFile("testOutput.png")); + + REQUIRE(compareImages("expected/rectangleShape.png", "testOutput.png")); + } } diff --git a/test/Graphics/expected/rectangleShape.png b/test/Graphics/expected/rectangleShape.png new file mode 100644 index 000000000..bf524434f Binary files /dev/null and b/test/Graphics/expected/rectangleShape.png differ diff --git a/test/TestUtilities/GraphicsUtil.cpp b/test/TestUtilities/GraphicsUtil.cpp index 05ed0688d..1ff5b1ac3 100644 --- a/test/TestUtilities/GraphicsUtil.cpp +++ b/test/TestUtilities/GraphicsUtil.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -113,3 +114,40 @@ bool operator==(const sf::Transform& lhs, const Approx& rhs) lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) && lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]); } + +bool compareImages(const std::string& expected, const std::string& actual) +{ + sf::Image image1; + if (!image1.loadFromFile(expected)) + { + return false; + } + + sf::Image image2; + if (!image2.loadFromFile(actual)) + { + return false; + } + + if (image1.getSize() != image2.getSize()) + { + return false; + } + + auto size = image1.getSize(); + + double totalDiff = 0; + for (unsigned int y = 0; y < size.y; ++y) + { + for (unsigned int x = 0; x < size.x; ++x) + { + unsigned int index = ((y * x) + x) * 4; + totalDiff += std::abs(int(image1.getPixelsPtr()[index + 0]) - int(image2.getPixelsPtr()[index + 0])) / 255.0; + totalDiff += std::abs(int(image1.getPixelsPtr()[index + 1]) - int(image2.getPixelsPtr()[index + 1])) / 255.0; + totalDiff += std::abs(int(image1.getPixelsPtr()[index + 2]) - int(image2.getPixelsPtr()[index + 2])) / 255.0; + } + } + + const double diffPercentage = (totalDiff * 100) / (size.x * size.y * 3); + return diffPercentage <= 1.; +} diff --git a/test/TestUtilities/GraphicsUtil.hpp b/test/TestUtilities/GraphicsUtil.hpp index 0ae41b31e..1d0a456f8 100644 --- a/test/TestUtilities/GraphicsUtil.hpp +++ b/test/TestUtilities/GraphicsUtil.hpp @@ -29,6 +29,8 @@ std::ostream& operator<<(std::ostream& os, const Rect& rect); bool operator==(const sf::Transform& lhs, const Approx& rhs); +bool compareImages(const std::string& expected, const std::string& actual); + template bool operator==(const sf::Rect& lhs, const Approx>& rhs) {