The Battle for Wesnoth  1.19.19+dev
test_sdl.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2023 - 2025
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-test"
16 
17 #include "sdl/rect.hpp"
18 #include "sdl/surface.hpp"
19 #include "sdl/utils.hpp"
20 
21 #include <algorithm>
22 #include <array>
23 #include <boost/test/unit_test.hpp>
24 
25 namespace
26 {
27 constexpr uint32_t alpha = 0x00'FF'FF'FF;
28 constexpr uint32_t red = 0xFF'FF'00'00;
29 constexpr uint32_t green = 0xFF'00'FF'00;
30 constexpr uint32_t blue = 0xFF'00'00'FF;
31 constexpr uint32_t yellow = 0xFF'FF'FF'00;
32 constexpr uint32_t white = 0xFF'FF'FF'FF;
33 constexpr uint32_t black = 0xFF'00'00'00;
34 
35 constexpr std::array img_4x4 {
36  red, white, green, black,
37  black, black, black, black,
38  blue, white, yellow, black,
39  black, black, black, black,
40 };
41 
42 constexpr std::array img_4x4_to_2x2_result {
43  red, green,
44  blue, yellow,
45 };
46 
47 constexpr std::array img_4x4_to_3x2_result {
48  red, white, green,
49  blue, white, yellow
50 };
51 
52 constexpr std::array img_4x4_with_alpha {
53  alpha, alpha, alpha, alpha,
54  alpha, black, alpha, alpha,
55  alpha, alpha, black, black,
56  alpha, black, alpha, alpha,
57 };
58 
59 constexpr std::array img_4x4_no_alpha {
60  black, black, black, black,
61  black, black, black, black,
62  black, black, black, black,
63  black, black, black, black,
64 };
65 
66 template<std::size_t w, std::size_t h>
67 surface array_to_surface(const std::array<uint32_t, w * h>& arr)
68 {
69  surface surf{w, h};
70 
71  {
72  surface_lock surf_lock{surf};
73  auto pixels = surf_lock.pixel_span();
74  for(std::size_t i = 0; i < w * h; ++i) {
75  pixels[i] = arr[i];
76  }
77  }
78 
79  return surf;
80 }
81 
82 } // namespace
83 
85 
86 BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
87 {
88  surface result = scale_surface_sharp(nullptr, 2, 2);
89  BOOST_CHECK_EQUAL(result, nullptr);
90 }
91 
92 BOOST_AUTO_TEST_CASE(test_scale_sharp_zero)
93 {
94  surface src = array_to_surface<4, 4>(img_4x4);
95  surface result = scale_surface_sharp(src, 0, 0);
96  BOOST_CHECK_EQUAL(result->w, 0);
97  BOOST_CHECK_EQUAL(result->h, 0);
98 }
99 
100 BOOST_AUTO_TEST_CASE(test_scale_sharp_round)
101 {
102  surface src = array_to_surface<4, 4>(img_4x4);
103  surface result = scale_surface_sharp(src, 2, 2);
104  const_surface_lock lock{result};
105  auto result_pixels = lock.pixel_span();
106  BOOST_CHECK_EQUAL_COLLECTIONS(
107  result_pixels.begin(), result_pixels.end(), img_4x4_to_2x2_result.begin(), img_4x4_to_2x2_result.end());
108 }
109 
110 BOOST_AUTO_TEST_CASE(test_scale_sharp_fractional)
111 {
112  surface src = array_to_surface<4, 4>(img_4x4);
113  surface result = scale_surface_sharp(src, 3, 2);
114  const_surface_lock lock{result};
115  auto result_pixels = lock.pixel_span();
116  BOOST_CHECK_EQUAL_COLLECTIONS(
117  result_pixels.begin(), result_pixels.end(), img_4x4_to_3x2_result.begin(), img_4x4_to_3x2_result.end());
118 }
119 
120 BOOST_AUTO_TEST_CASE(test_transparent_clip)
121 {
122  surface src = array_to_surface<4, 4>(img_4x4_with_alpha);
124  auto opaque = rect{1, 1, 3, 3};
125  BOOST_CHECK_EQUAL(result, opaque);
126 }
127 
128 BOOST_AUTO_TEST_CASE(test_transparent_clip_no_alpha)
129 {
130  surface src = array_to_surface<4, 4>(img_4x4_no_alpha);
132  auto opaque = rect{0, 0, src->w, src->h};
133  BOOST_CHECK_EQUAL(result, opaque);
134 }
135 
136 BOOST_AUTO_TEST_SUITE_END()
Helper class for pinning SDL surfaces into memory.
Definition: surface.hpp:98
std::size_t i
Definition: function.cpp:1031
int w
Definition: pathfind.cpp:188
Contains the SDL_Rect helper code.
surface surf
Image.
rect src
Non-transparent portion of the surface to compose.
An abstract description of a rectangle with integer coordinates.
Definition: rect.hpp:49
BOOST_AUTO_TEST_SUITE(filesystem)
BOOST_AUTO_TEST_CASE(test_scale_sharp_nullptr)
Definition: test_sdl.cpp:86
rect get_non_transparent_portion(const surface &surf)
Definition: utils.cpp:1411
surface scale_surface_sharp(const surface &surf, int w, int h)
Scale a surface using modified nearest neighbour algorithm.
Definition: utils.cpp:359
#define h