1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
#include <SFML/Graphics/Texture.hpp>
// Other 1st party headers
#include <SFML/Graphics/Image.hpp>
#include <SFML/System/Exception.hpp>
#include <SFML/System/FileInputStream.hpp>
#include <catch2/catch_test_macros.hpp>
#include <GraphicsUtil.hpp>
#include <WindowUtil.hpp>
#include <array>
#include <type_traits>
TEST_CASE("[Graphics] sf::Texture", runDisplayTests())
{
SECTION("Type traits")
{
STATIC_CHECK(std::is_copy_constructible_v<sf::Texture>);
STATIC_CHECK(std::is_copy_assignable_v<sf::Texture>);
STATIC_CHECK(std::is_nothrow_move_constructible_v<sf::Texture>);
STATIC_CHECK(std::is_nothrow_move_assignable_v<sf::Texture>);
STATIC_CHECK(std::is_nothrow_swappable_v<sf::Texture>);
}
SECTION("Construction")
{
SECTION("Default constructor")
{
const sf::Texture texture;
CHECK(texture.getSize() == sf::Vector2u());
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() == 0);
}
SECTION("Vector")
{
SECTION("At least one zero dimension")
{
CHECK_THROWS_AS(sf::Texture(sf::Vector2u()), sf::Exception);
CHECK_THROWS_AS(sf::Texture(sf::Vector2u(0, 1)), sf::Exception);
CHECK_THROWS_AS(sf::Texture(sf::Vector2u(1, 0)), sf::Exception);
}
SECTION("Valid size")
{
const sf::Texture texture(sf::Vector2u(100, 100));
CHECK(texture.getSize() == sf::Vector2u(100, 100));
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Too large")
{
CHECK_THROWS_AS(sf::Texture(sf::Vector2u(100'000, 100'000)), sf::Exception);
CHECK_THROWS_AS(sf::Texture(sf::Vector2u(1'000'000, 1'000'000)), sf::Exception);
}
}
SECTION("File")
{
const sf::Texture texture("Graphics/sfml-logo-big.png");
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Memory")
{
const auto memory = loadIntoMemory("Graphics/sfml-logo-big.png");
const sf::Texture texture(memory.data(), memory.size());
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Stream")
{
sf::FileInputStream stream("Graphics/sfml-logo-big.png");
const sf::Texture texture(stream);
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Image")
{
SECTION("Subarea of image")
{
const sf::Image image(sf::Vector2u(10, 15));
SECTION("Non-truncated area")
{
const sf::Texture texture(image, false, {{0, 0}, {5, 10}});
CHECK(texture.getSize() == sf::Vector2u(5, 10));
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Truncated area (negative position)")
{
const sf::Texture texture(image, false, {{-5, -5}, {4, 8}});
CHECK(texture.getSize() == sf::Vector2u(4, 8));
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Truncated area (width/height too big)")
{
const sf::Texture texture(image, false, {{5, 5}, {12, 18}});
CHECK(texture.getSize() == sf::Vector2u(5, 10));
CHECK(texture.getNativeHandle() != 0);
}
}
}
}
SECTION("Move semantics")
{
SECTION("Construction")
{
sf::Texture movedTexture;
const sf::Texture texture = std::move(movedTexture);
CHECK(texture.getSize() == sf::Vector2u());
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() == 0);
}
SECTION("Assignment")
{
sf::Texture movedTexture;
sf::Texture texture;
texture = std::move(movedTexture);
CHECK(texture.getSize() == sf::Vector2u());
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() == 0);
}
}
SECTION("Move semantics")
{
SECTION("Construction")
{
sf::Texture movedTexture(sf::Vector2u(64, 64));
const sf::Texture texture = std::move(movedTexture);
CHECK(texture.getSize() == sf::Vector2u(64, 64));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Assignment")
{
sf::Texture movedTexture(sf::Vector2u(64, 64));
sf::Texture texture(sf::Vector2u(128, 128));
texture = std::move(movedTexture);
CHECK(texture.getSize() == sf::Vector2u(64, 64));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
}
SECTION("resize()")
{
sf::Texture texture;
SECTION("At least one zero dimension")
{
CHECK(!texture.resize({}));
CHECK(!texture.resize({0, 1}));
CHECK(!texture.resize({1, 0}));
}
SECTION("Valid size")
{
CHECK(texture.resize({100, 100}));
CHECK(texture.getSize() == sf::Vector2u(100, 100));
CHECK(texture.getNativeHandle() != 0);
}
SECTION("Too large")
{
CHECK(!texture.resize({100'000, 100'000}));
CHECK(!texture.resize({1'000'000, 1'000'000}));
}
}
SECTION("loadFromFile()")
{
sf::Texture texture;
REQUIRE(texture.loadFromFile("Graphics/sfml-logo-big.png"));
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("loadFromMemory()")
{
const auto memory = loadIntoMemory("Graphics/sfml-logo-big.png");
sf::Texture texture;
REQUIRE(texture.loadFromMemory(memory.data(), memory.size()));
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("loadFromStream()")
{
sf::Texture texture;
sf::FileInputStream stream;
REQUIRE(stream.open("Graphics/sfml-logo-big.png"));
REQUIRE(texture.loadFromStream(stream));
CHECK(texture.getSize() == sf::Vector2u(1001, 304));
CHECK(!texture.isSmooth());
CHECK(!texture.isSrgb());
CHECK(!texture.isRepeated());
CHECK(texture.getNativeHandle() != 0);
}
SECTION("loadFromImage()")
{
SECTION("Empty image")
{
const sf::Image image;
sf::Texture texture;
REQUIRE(!texture.loadFromImage(image));
REQUIRE(!texture.loadFromImage(image, false, {{0, 0}, {1, 1}}));
}
SECTION("Subarea of image")
{
sf::Image image;
image.resize(sf::Vector2u(10, 15));
sf::Texture texture;
SECTION("Non-truncated area")
{
REQUIRE(texture.loadFromImage(image, false, {{0, 0}, {5, 10}}));
CHECK(texture.getSize() == sf::Vector2u(5, 10));
}
SECTION("Truncated area (negative position)")
{
REQUIRE(texture.loadFromImage(image, false, {{-5, -5}, {4, 8}}));
CHECK(texture.getSize() == sf::Vector2u(4, 8));
}
SECTION("Truncated area (width/height too big)")
{
REQUIRE(texture.loadFromImage(image, false, {{5, 5}, {12, 18}}));
CHECK(texture.getSize() == sf::Vector2u(5, 10));
}
CHECK(texture.getNativeHandle() != 0);
}
}
SECTION("Copy semantics")
{
static constexpr std::array<std::uint8_t, 8> red = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF};
sf::Texture texture(sf::Vector2u(1, 2));
texture.update(red.data());
SECTION("Construction")
{
const sf::Texture textureCopy(texture); // NOLINT(performance-unnecessary-copy-initialization)
REQUIRE(textureCopy.getSize() == sf::Vector2u(1, 2));
CHECK(textureCopy.copyToImage().getPixel(sf::Vector2u(0, 1)) == sf::Color::Red);
}
SECTION("Assignment")
{
sf::Texture textureCopy(sf::Vector2u(64, 64));
textureCopy = texture;
REQUIRE(textureCopy.getSize() == sf::Vector2u(1, 2));
CHECK(textureCopy.copyToImage().getPixel(sf::Vector2u(0, 1)) == sf::Color::Red);
}
}
SECTION("update()")
{
static constexpr std::array<std::uint8_t, 4> yellow = {0xFF, 0xFF, 0x00, 0xFF};
static constexpr std::array<std::uint8_t, 4> cyan = {0x00, 0xFF, 0xFF, 0xFF};
SECTION("Pixels")
{
sf::Texture texture(sf::Vector2u(1, 1));
texture.update(yellow.data());
CHECK(texture.copyToImage().getPixel(sf::Vector2u(0, 0)) == sf::Color::Yellow);
}
SECTION("Pixels, size and destination")
{
sf::Texture texture(sf::Vector2u(2, 1));
texture.update(yellow.data(), sf::Vector2u(1, 1), sf::Vector2u(0, 0));
texture.update(cyan.data(), sf::Vector2u(1, 1), sf::Vector2u(1, 0));
CHECK(texture.copyToImage().getPixel(sf::Vector2u(0, 0)) == sf::Color::Yellow);
CHECK(texture.copyToImage().getPixel(sf::Vector2u(1, 0)) == sf::Color::Cyan);
}
SECTION("Another texture")
{
sf::Texture otherTexture(sf::Vector2u(1, 1));
otherTexture.update(cyan.data());
sf::Texture texture(sf::Vector2u(1, 1));
texture.update(otherTexture);
CHECK(texture.copyToImage().getPixel(sf::Vector2u(0, 0)) == sf::Color::Cyan);
}
SECTION("Another texture and destination")
{
sf::Texture texture(sf::Vector2u(2, 1));
sf::Texture otherTexture1(sf::Vector2u(1, 1));
otherTexture1.update(cyan.data());
sf::Texture otherTexture2(sf::Vector2u(1, 1));
otherTexture2.update(yellow.data());
texture.update(otherTexture1, sf::Vector2u(0, 0));
texture.update(otherTexture2, sf::Vector2u(1, 0));
CHECK(texture.copyToImage().getPixel(sf::Vector2u(0, 0)) == sf::Color::Cyan);
CHECK(texture.copyToImage().getPixel(sf::Vector2u(1, 0)) == sf::Color::Yellow);
}
SECTION("Image")
{
sf::Texture texture(sf::Vector2u(16, 32));
const sf::Image image(sf::Vector2u(16, 32), sf::Color::Red);
texture.update(image);
CHECK(texture.copyToImage().getPixel(sf::Vector2u(7, 15)) == sf::Color::Red);
}
SECTION("Image and destination")
{
sf::Texture texture(sf::Vector2u(16, 32));
const sf::Image image1(sf::Vector2u(16, 16), sf::Color::Red);
texture.update(image1);
const sf::Image image2(sf::Vector2u(16, 16), sf::Color::Green);
texture.update(image1, sf::Vector2u(0, 0));
texture.update(image2, sf::Vector2u(0, 16));
CHECK(texture.copyToImage().getPixel(sf::Vector2u(7, 7)) == sf::Color::Red);
CHECK(texture.copyToImage().getPixel(sf::Vector2u(7, 22)) == sf::Color::Green);
}
}
SECTION("Set/get smooth")
{
sf::Texture texture(sf::Vector2u(64, 64));
CHECK(!texture.isSmooth());
texture.setSmooth(true);
CHECK(texture.isSmooth());
texture.setSmooth(false);
CHECK(!texture.isSmooth());
}
SECTION("Set/get repeated")
{
sf::Texture texture(sf::Vector2u(64, 64));
CHECK(!texture.isRepeated());
texture.setRepeated(true);
CHECK(texture.isRepeated());
texture.setRepeated(false);
CHECK(!texture.isRepeated());
}
SECTION("generateMipmap()")
{
sf::Texture texture(sf::Vector2u(100, 100));
CHECK(texture.generateMipmap());
}
SECTION("swap()")
{
static constexpr std::array<std::uint8_t, 4> blue = {0x00, 0x00, 0xFF, 0xFF};
static constexpr std::array<std::uint8_t, 8> green = {0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF};
sf::Texture texture1(sf::Vector2u(1, 1), true);
texture1.update(blue.data());
texture1.setSmooth(false);
texture1.setRepeated(true);
sf::Texture texture2(sf::Vector2u(2, 1), false);
texture2.update(green.data());
texture2.setSmooth(true);
texture2.setRepeated(false);
sf::swap(texture1, texture2);
CHECK_FALSE(texture1.isSrgb());
CHECK(texture1.isSmooth());
CHECK_FALSE(texture1.isRepeated());
// Cannot check texture2.isSrgb() because Srgb is sometimes disabled when using OpenGL ES
CHECK_FALSE(texture2.isSmooth());
CHECK(texture2.isRepeated());
const sf::Image image1 = texture1.copyToImage();
const sf::Image image2 = texture2.copyToImage();
REQUIRE(image1.getSize() == sf::Vector2u(2, 1));
REQUIRE(image2.getSize() == sf::Vector2u(1, 1));
CHECK(image1.getPixel(sf::Vector2u(1, 0)) == sf::Color::Green);
CHECK(image2.getPixel(sf::Vector2u(0, 0)) == sf::Color::Blue);
}
SECTION("Get Maximum Size")
{
CHECK(sf::Texture::getMaximumSize() > 0);
}
}
|