SFML/test/Graphics/View.cpp

142 lines
6.0 KiB
C++
Raw Normal View History

2022-05-22 13:03:49 +08:00
#include <SFML/Graphics/View.hpp>
2022-07-18 06:18:40 +08:00
#include <doctest/doctest.h>
2022-05-22 13:03:49 +08:00
2022-07-05 00:20:58 +08:00
#include <GraphicsUtil.hpp>
#include <type_traits>
static_assert(std::is_copy_constructible_v<sf::View>);
static_assert(std::is_copy_assignable_v<sf::View>);
static_assert(std::is_nothrow_move_constructible_v<sf::View>);
static_assert(std::is_nothrow_move_assignable_v<sf::View>);
2022-07-05 00:20:58 +08:00
TEST_CASE("[Graphics] sf::View")
2022-05-22 13:03:49 +08:00
{
SUBCASE("Construction")
{
SUBCASE("Default constructor")
{
const sf::View view;
CHECK(view.getCenter() == sf::Vector2f(500, 500));
CHECK(view.getSize() == sf::Vector2f(1000, 1000));
CHECK(view.getRotation() == sf::Angle::Zero);
CHECK(view.getViewport() == sf::FloatRect({0, 0}, {1, 1}));
CHECK(view.getTransform() == sf::Transform(0.002f, 0, -1, 0, -0.002f, 1, 0, 0, 1));
CHECK(view.getInverseTransform() == Approx(sf::Transform(500, 0, 500, 0, -500, 500, 0, 0, 1)));
}
SUBCASE("Rectangle constructor")
{
const sf::View view(sf::FloatRect({10, 20}, {400, 600}));
CHECK(view.getCenter() == sf::Vector2f(210, 320));
CHECK(view.getSize() == sf::Vector2f(400, 600));
CHECK(view.getRotation() == sf::Angle::Zero);
CHECK(view.getViewport() == sf::FloatRect({0, 0}, {1, 1}));
CHECK(view.getTransform() == Approx(sf::Transform(0.005f, 0, -1.05f, 0, -0.00333333f, 1.06667f, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(200, 0, 210, 0, -300, 320, 0, 0, 1)));
}
SUBCASE("Center + size constructor")
{
const sf::View view({520, 960}, {1080, 1920});
CHECK(view.getCenter() == sf::Vector2f(520, 960));
CHECK(view.getSize() == sf::Vector2f(1080, 1920));
CHECK(view.getRotation() == sf::Angle::Zero);
CHECK(view.getViewport() == sf::FloatRect({0, 0}, {1, 1}));
CHECK(view.getTransform() == Approx(sf::Transform(0.00185185f, 0, -0.962963f, 0, -0.00104167f, 1, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(540, 0, 520, 0, -960, 960, 0, 0, 1)));
}
}
SUBCASE("Set/get center")
{
sf::View view;
view.setCenter({3.14f, 4.2f});
CHECK(view.getCenter() == sf::Vector2f(3.14f, 4.2f));
CHECK(view.getTransform() == Approx(sf::Transform(0.002f, 0, -0.00628f, 0, -0.002f, 0.0084f, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(500, 0, 3.14f, 0, -500, 4.2f, 0, 0, 1)));
}
SUBCASE("Set/get size")
{
sf::View view;
view.setSize({600, 900});
CHECK(view.getSize() == sf::Vector2f(600, 900));
CHECK(view.getTransform() == Approx(sf::Transform(0.00333333f, 0, -1.66667f, 0, -0.00222222f, 1.11111f, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(300, 0, 500, 0, -450, 500, 0, 0, 1)));
}
SUBCASE("Set/get rotation")
{
sf::View view;
view.setRotation(sf::degrees(-345));
CHECK(view.getRotation() == sf::degrees(15));
2022-07-05 00:20:58 +08:00
CHECK(view.getTransform() ==
Approx(sf::Transform(0.00193185f, 0.000517638f, -1.22474f, 0.000517638f, -0.00193185f, 0.707107f, 0, 0, 1)));
CHECK(view.getInverseTransform() ==
Approx(sf::Transform(482.963f, 129.41f, 500, 129.41f, -482.963f, 500, 0, 0, 1)));
2022-05-22 13:03:49 +08:00
view.setRotation(sf::degrees(400));
CHECK(view.getRotation() == sf::degrees(40));
2022-07-05 00:20:58 +08:00
CHECK(view.getTransform() ==
Approx(sf::Transform(0.00153209f, 0.00128558f, -1.40883f, 0.00128558f, -0.00153209f, 0.123257f, 0, 0, 1)));
CHECK(view.getInverseTransform() ==
Approx(sf::Transform(383.022f, 321.394f, 500, 321.394f, -383.022f, 500, 0, 0, 1)));
2022-05-22 13:03:49 +08:00
}
SUBCASE("Set/get viewport")
{
sf::View view;
view.setViewport({{150, 250}, {500, 750}});
CHECK(view.getViewport() == sf::FloatRect({150, 250}, {500, 750}));
CHECK(view.getTransform() == Approx(sf::Transform(0.002f, 0, -1, 0, -0.002f, 1, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(500, 0, 500, 0, -500, 500, 0, 0, 1)));
}
SUBCASE("reset()")
{
sf::View view;
view.setCenter({3.14f, 4.2f});
view.setSize({600, 900});
view.setRotation(sf::degrees(15));
view.setViewport({{150, 250}, {500, 750}});
view.reset({{1, 2}, {3, 4}});
CHECK(view.getCenter() == sf::Vector2f(2.5f, 4));
CHECK(view.getSize() == sf::Vector2f(3, 4));
CHECK(view.getRotation() == sf::Angle::Zero);
CHECK(view.getViewport() == sf::FloatRect({150, 250}, {500, 750}));
2022-07-05 00:20:58 +08:00
CHECK(view.getTransform() == Approx(sf::Transform(0.666667f, 0, -1.66667f, 0, -0.5f, 2, 0, 0, 1)));
2022-05-22 13:03:49 +08:00
CHECK(view.getInverseTransform() == Approx(sf::Transform(1.5f, 0, 2.5f, 0, -2, 4, 0, 0, 1)));
}
SUBCASE("move()")
{
sf::View view;
view.setCenter({25, 25});
view.move({15, 25});
CHECK(view.getCenter() == sf::Vector2f(40, 50));
CHECK(view.getTransform() == Approx(sf::Transform(0.002f, 0, -0.08f, 0, -0.002f, 0.1f, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(500, 0, 40, 0, -500, 50, 0, 0, 1)));
}
SUBCASE("rotate()")
{
sf::View view;
view.setRotation(sf::degrees(45));
view.rotate(sf::degrees(-15));
CHECK(view.getRotation() == sf::degrees(30));
2022-07-05 00:20:58 +08:00
CHECK(view.getTransform() ==
Approx(sf::Transform(0.00173205f, 0.001f, -1.36603f, 0.001f, -0.00173205f, 0.366025f, 0, 0, 1)));
2022-05-22 13:03:49 +08:00
CHECK(view.getInverseTransform() == Approx(sf::Transform(433.013f, 250, 500, 250, -433.013f, 500, 0, 0, 1)));
}
SUBCASE("zoom()")
{
sf::View view;
view.setSize({25, 25});
view.zoom(4);
CHECK(view.getSize() == sf::Vector2f(100, 100));
CHECK(view.getTransform() == Approx(sf::Transform(0.02f, 0, -10, 0, -0.02f, 10, 0, 0, 1)));
CHECK(view.getInverseTransform() == Approx(sf::Transform(50, 0, 500, 0, -50, 500, 0, 0, 1)));
}
}