From a0c6381c2b8f80e4fc556c23c61fb5107a25d28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20D=C3=BCrrenberger?= Date: Mon, 18 Jul 2022 09:02:06 +0200 Subject: [PATCH] Add a graphical test for RectangleShape --- test/Graphics/RectangleShape.test.cpp | 24 ++++++++++++++ test/Graphics/expected/rectangleShape.png | Bin 0 -> 566 bytes test/TestUtilities/GraphicsUtil.cpp | 38 ++++++++++++++++++++++ test/TestUtilities/GraphicsUtil.hpp | 2 ++ 4 files changed, 64 insertions(+) create mode 100644 test/Graphics/expected/rectangleShape.png 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 0000000000000000000000000000000000000000..bf524434f18295cf7b45f6f1191457e0bd88a9cb GIT binary patch literal 566 zcmeAS@N?(olHy`uVBq!ia0vp^DIm{fg*L)OUW(t(Vnc2O()#nKl~{RL#s;&#*ideN)aMErshi(7PL zvrgD&dh<3!Pk7yl+WiYWqJVB+K!oxa%i>&1D$GV~P8S=?SvxM-DP z@*QMN>4^+IiM7sco>CHPuebxve3Y{#NFK-p+6%Vv8BdGXnM7t`Kn1e!y9_Kc~>IRWbW^FY@FJp@!~l~VE|1?FCm zDRWGbOaage^C%fi|3F{leFHvF?O| zckyCDBPqqjM-}!7NSp=x6ll|giwh@TjB&{0fO_v8C=`J{jsXTN&tww=U?_&YT72{- v)O$_9Pz3t;EbA7IMxbv%?nr;g&B!3A!gl)H4iPbP0l+XkKvo`uU literal 0 HcmV?d00001 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) {