Add a graphical test for RectangleShape

This commit is contained in:
Lukas Dürrenberger 2022-07-18 09:02:06 +02:00
parent 37ac80dbe5
commit a0c6381c2b
4 changed files with 64 additions and 0 deletions

View File

@ -1,7 +1,10 @@
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <SFML/Graphics/RenderTexture.hpp>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <GraphicsUtil.hpp>
#include <SystemUtil.hpp> #include <SystemUtil.hpp>
#include <type_traits> #include <type_traits>
@ -46,4 +49,25 @@ TEST_CASE("[Graphics] sf::RectangleShape")
CHECK(rectangle.getSize() == sf::Vector2f(5, 4)); CHECK(rectangle.getSize() == sf::Vector2f(5, 4));
CHECK(rectangle.getGeometricCenter() == sf::Vector2f(5.f, 4.f) / 2.f); 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"));
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 566 B

View File

@ -1,5 +1,6 @@
#include <SFML/Graphics/BlendMode.hpp> #include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/StencilMode.hpp> #include <SFML/Graphics/StencilMode.hpp>
#include <SFML/Graphics/Transform.hpp> #include <SFML/Graphics/Transform.hpp>
@ -113,3 +114,40 @@ bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs)
lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) && lhs.getMatrix()[7] == Approx(rhs.value.getMatrix()[7]) &&
lhs.getMatrix()[15] == Approx(rhs.value.getMatrix()[15]); 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.;
}

View File

@ -29,6 +29,8 @@ std::ostream& operator<<(std::ostream& os, const Rect<T>& rect);
bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs); bool operator==(const sf::Transform& lhs, const Approx<sf::Transform>& rhs);
bool compareImages(const std::string& expected, const std::string& actual);
template <typename T> template <typename T>
bool operator==(const sf::Rect<T>& lhs, const Approx<sf::Rect<T>>& rhs) bool operator==(const sf::Rect<T>& lhs, const Approx<sf::Rect<T>>& rhs)
{ {