ST_engine  0.3-ALPHA
renderer_sdl_tests.cpp
1 /*This file is part of the "ST" project.
2  * You may use, distribute or modify this code under the terms
3  * of the GNU General Public License version 2.
4  * See LICENCE.txt in the root directory of the project.
5  *
6  * Author: Maxim Atanasov
7  * E-mail: maxim.atanasov@protonmail.com
8  */
9 
10 #include <gtest/gtest.h>
11 
12 #define TESTING
13 
14 #include <ST_util/test_util.hpp>
15 #include "renderer_sdl.cpp"
16 #include <ST_util/string_util.hpp>
17 
19 // These these are purely visual - you have to watch them run and
20 // look out for anything wrong. This means that you won't get
21 // a test failure if for some reason nothing is rendered.
22 // However these are still useful as they provide you with a quick
23 // way to test if any of the specific rendering functions is broken.
24 class renderer_sdl_tests : public ::testing::Test {
25 
26 protected:
27 
28  //Adjust testing conditions here
29  uint32_t wait_duration = 1500;
30  uint32_t fullscreen = 1;
31 
32  SDL_Window *test_window{};
33  int16_t test_window_width = 0;
34  int16_t test_window_height = 0;
35 
36  void SetUp() override {
37  initialize_SDL();
38  SDL_DisplayMode DM{};
39  SDL_GetDisplayMode(0, 0, &DM);
40  test_window_width = static_cast<int16_t>(DM.w);
41  test_window_height = static_cast<int16_t>(DM.h);
42 
43  test_window = SDL_CreateWindow("TEST", 0, 0, test_window_width, test_window_height, 0);
44 
45  ST::renderer_sdl::initialize(test_window, test_window_width, test_window_height);
46 
47  SDL_SetWindowFullscreen(test_window, fullscreen);
48  SDL_ShowCursor(0);
49 
50  ST::renderer_sdl::clear_screen({0, 0, 0, 0});
51  }
52 
53  void TearDown() override {
55  SDL_DestroyWindow(test_window);
56  close_SDL();
57  }
58 };
59 
60 TEST_F(renderer_sdl_tests, test_set_draw_color) {
61  int counter = 0;
62  for (uint32_t i = 0; i < wait_duration / 180; i++) {
63  if (counter == 0) {
64  ST::renderer_sdl::set_draw_color(255, 0, 0, 255);
65  } else if (counter == 1) {
66  ST::renderer_sdl::set_draw_color(0, 255, 0, 255);
67  } else if (counter == 2) {
68  ST::renderer_sdl::set_draw_color(0, 0, 255, 255);
69  } else if (counter == 3) {
70  ST::renderer_sdl::set_draw_color(255, 0, 255, 255);
71  } else if (counter == 4) {
72  ST::renderer_sdl::set_draw_color(0, 255, 255, 255);
73  } else if (counter == 5) {
74  ST::renderer_sdl::set_draw_color(255, 255, 0, 255);
75  } else if (counter == 6) {
76  ST::renderer_sdl::set_draw_color(255, 100, 20, 255);
77  } else {
78  counter = 0;
79  }
80  counter++;
83  SDL_Delay(160);
84  }
85 }
86 
87 TEST_F(renderer_sdl_tests, test_draw_rectangle_white) {
88  ST::renderer_sdl::draw_rectangle(100, 100, 300, 300, {255, 255, 255, 255});
90  SDL_Delay(wait_duration);
91 }
92 
93 TEST_F(renderer_sdl_tests, test_draw_filled_rectangle_white) {
94  ST::renderer_sdl::draw_rectangle_filled(100, 100, 300, 300, {255, 255, 255, 255});
96  SDL_Delay(wait_duration);
97 }
98 
99 TEST_F(renderer_sdl_tests, test_draw_filled_rectangle_white_transparent) {
100  ST::renderer_sdl::draw_rectangle_filled(100, 100, 300, 300, {255, 255, 255, 50});
102  SDL_Delay(wait_duration);
103 }
104 
105 TEST_F(renderer_sdl_tests, test_draw_filled_rectangle_red) {
106  ST::renderer_sdl::draw_rectangle_filled(100, 100, 300, 300, {255, 0, 0, 255});
108  SDL_Delay(wait_duration);
109 }
110 
111 TEST_F(renderer_sdl_tests, test_draw_filled_rectangle_green) {
112  ST::renderer_sdl::draw_rectangle_filled(100, 100, 300, 300, {0, 255, 0, 255});
114  SDL_Delay(wait_duration);
115 }
116 
117 TEST_F(renderer_sdl_tests, test_draw_filled_rectangle_blue) {
118  ST::renderer_sdl::draw_rectangle_filled(100, 100, 300, 300, {0, 0, 255, 255});
120  SDL_Delay(wait_duration);
121 }
122 
123 TEST_F(renderer_sdl_tests, test_draw_background) {
124  SDL_Surface *test_surface = IMG_Load("test_image_2.png");
125  ASSERT_TRUE(static_cast<bool>(test_surface));
126  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
127  test_assets[1] = test_surface;
128  ST::renderer_sdl::upload_surfaces(&test_assets);
131  SDL_Delay(wait_duration);
132 }
133 
134 TEST_F(renderer_sdl_tests, test_draw_background_parallax) {
135  SDL_Surface *test_surface1 = IMG_Load("bg2_1.png");
136  SDL_Surface *test_surface2 = IMG_Load("bg2_3.png");
137  ASSERT_TRUE(static_cast<bool>(test_surface1));
138  ASSERT_TRUE(static_cast<bool>(test_surface2));
139  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
140  test_assets[0] = test_surface1;
141  test_assets[1] = test_surface2;
142  ST::renderer_sdl::upload_surfaces(&test_assets);
143  for (uint16_t i = 0; i < 1000; i++) {
144  for (uint8_t j = 0; j < 2; j++) {
145  ST::renderer_sdl::draw_background_parallax(j, (i * 30 * (j << 3)) / (1920 >> 1) % 1080);
146  }
148  }
149 }
150 
151 TEST_F(renderer_sdl_tests, test_draw_texture) {
152  SDL_Surface *test_surface = IMG_Load("test_image_1.png");
153  ASSERT_TRUE(static_cast<bool>(test_surface));
154  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
155  test_assets[1] = test_surface;
156  ST::renderer_sdl::upload_surfaces(&test_assets);
157  ST::renderer_sdl::draw_texture(1, 300, 300);
159  SDL_Delay(wait_duration);
160 }
161 
162 TEST_F(renderer_sdl_tests, test_draw_textures_from_atlas) {
163  SDL_Surface *test_surface1 = IMG_Load("crate1.png");
164  SDL_Surface *test_surface2 = IMG_Load("fence.png");
165  ASSERT_TRUE(static_cast<bool>(test_surface1));
166  ASSERT_TRUE(static_cast<bool>(test_surface2));
167  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
168  test_assets[1] = test_surface1;
169  test_assets[2] = test_surface2;
170  ST::renderer_sdl::upload_surfaces(&test_assets);
171  ST::renderer_sdl::draw_texture(1, 300, 300);
172  ST::renderer_sdl::draw_texture(2, 500, 600);
174  SDL_Delay(wait_duration);
175 }
176 
177 
178 TEST_F(renderer_sdl_tests, test_draw_texture_scaled) {
179  SDL_Surface *test_surface = IMG_Load("test_image_1.png");
180  ASSERT_TRUE(static_cast<bool>(test_surface));
181  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
182  test_assets[1] = test_surface;
183  ST::renderer_sdl::upload_surfaces(&test_assets);
184  ST::renderer_sdl::draw_texture(1, 300, 300);
185  ST::renderer_sdl::draw_texture_scaled(1, 800, 300, 0.5, 0.5);
186  ST::renderer_sdl::draw_texture_scaled(1, 300, 700, 2, 2);
187  ST::renderer_sdl::draw_texture_scaled(1, 800, 700, 2, 1);
189  SDL_Delay(wait_duration);
190 }
191 
192 TEST_F(renderer_sdl_tests, test_draw_font_english_small) {
193  uint8_t font_size = 20;
194  TTF_Font *test_font = TTF_OpenFont("test_font.ttf", font_size);
195  uint16_t font_hash = ST::hash_string("test_font.ttf " + std::to_string(font_size));
196  ASSERT_TRUE(static_cast<bool>(test_font));
197  ska::bytell_hash_map<uint16_t, TTF_Font *> test_assets;
198  test_assets[font_hash] = test_font;
199  ST::renderer_sdl::upload_fonts(&test_assets);
200  ST::renderer_sdl::draw_text_cached_glyphs(font_hash, "The quick brown fox!", 300, 300, {255, 0, 0, 255});
202  SDL_Delay(wait_duration);
203 }
204 
205 TEST_F(renderer_sdl_tests, test_draw_font_english_medium) {
206  uint8_t font_size = 50;
207  TTF_Font *test_font = TTF_OpenFont("test_font.ttf", font_size);
208  uint16_t font_hash = ST::hash_string("test_font.ttf " + std::to_string(font_size));
209  ASSERT_TRUE(static_cast<bool>(test_font));
210  ska::bytell_hash_map<uint16_t, TTF_Font *> test_assets;
211  test_assets[font_hash] = test_font;
212  ST::renderer_sdl::upload_fonts(&test_assets);
213  ST::renderer_sdl::draw_text_cached_glyphs(font_hash, "The quick brown fox!", 200, 300, {0, 255, 0, 255});
215  SDL_Delay(wait_duration);
216 }
217 
218 TEST_F(renderer_sdl_tests, test_draw_font_english_large) {
219  uint8_t font_size = 90;
220  TTF_Font *test_font = TTF_OpenFont("test_font.ttf", font_size);
221  uint16_t font_hash = ST::hash_string("test_font.ttf " + std::to_string(font_size));
222  ASSERT_TRUE(static_cast<bool>(test_font));
223  ska::bytell_hash_map<uint16_t, TTF_Font *> test_assets;
224  test_assets[font_hash] = test_font;
225  ST::renderer_sdl::upload_fonts(&test_assets);
226  ST::renderer_sdl::draw_text_cached_glyphs(font_hash, "The quick brown fox!", 100, 300, {0, 0, 255, 255});
228  SDL_Delay(wait_duration);
229 }
230 
231 TEST_F(renderer_sdl_tests, test_draw_font_russian_small) {
232  uint8_t font_size = 20;
233  TTF_Font *test_font = TTF_OpenFont("test_font.ttf", font_size);
234  uint16_t font_hash = ST::hash_string("test_font.ttf " + std::to_string(font_size));
235  ASSERT_TRUE(static_cast<bool>(test_font));
236  ska::bytell_hash_map<uint16_t, TTF_Font *> test_assets;
237  test_assets[font_hash] = test_font;
238  ST::renderer_sdl::upload_fonts(&test_assets);
239  ST::renderer_sdl::draw_text_lru_cached(font_hash, "Этот тест тестирует шрифты!", 300, 300, {255, 0, 0, 255});
241  SDL_Delay(wait_duration);
242 }
243 
244 TEST_F(renderer_sdl_tests, test_draw_font_russian_medium) {
245  uint8_t font_size = 50;
246  TTF_Font *test_font = TTF_OpenFont("test_font.ttf", font_size);
247  uint16_t font_hash = ST::hash_string("test_font.ttf " + std::to_string(font_size));
248  ASSERT_TRUE(static_cast<bool>(test_font));
249  ska::bytell_hash_map<uint16_t, TTF_Font *> test_assets;
250  test_assets[font_hash] = test_font;
251  ST::renderer_sdl::upload_fonts(&test_assets);
252  ST::renderer_sdl::draw_text_lru_cached(font_hash, "Этот тест тестирует шрифты!", 200, 300, {0, 255, 0, 255});
254  SDL_Delay(wait_duration);
255 }
256 
257 TEST_F(renderer_sdl_tests, test_draw_font_russian_large) {
258  uint8_t font_size = 90;
259  TTF_Font *test_font = TTF_OpenFont("test_font.ttf", font_size);
260  uint16_t font_hash = ST::hash_string("test_font.ttf " + std::to_string(font_size));
261  ASSERT_TRUE(static_cast<bool>(test_font));
262  ska::bytell_hash_map<uint16_t, TTF_Font *> test_assets;
263  test_assets[font_hash] = test_font;
264  ST::renderer_sdl::upload_fonts(&test_assets);
265  ST::renderer_sdl::draw_text_lru_cached(font_hash, "Этот тест тестирует шрифты!", 100, 300, {0, 0, 255, 255});
267  SDL_Delay(wait_duration);
268 }
269 
270 TEST_F(renderer_sdl_tests, test_draw_sprite_animated1) {
271  SDL_Surface *test_surface = IMG_Load("test_sprite.png");
272  ASSERT_TRUE(static_cast<bool>(test_surface));
273  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
274  test_assets[1] = test_surface;
275  ST::renderer_sdl::upload_surfaces(&test_assets);
276  for (uint32_t i = 0; i < wait_duration / 16; i++) {
277  uint32_t time = SDL_GetTicks() >> 7U;
278  ST::renderer_sdl::clear_screen({0, 0, 0, 0});
279  ST::renderer_sdl::draw_sprite(1, 300, 500, time % 6, 1, 6, 6);
281  SDL_Delay(16);
282  }
283 }
284 
285 TEST_F(renderer_sdl_tests, test_draw_sprite_animated2) {
286  SDL_Surface *test_surface = IMG_Load("test_sprite.png");
287  ASSERT_TRUE(static_cast<bool>(test_surface));
288  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
289  test_assets[1] = test_surface;
290  ST::renderer_sdl::upload_surfaces(&test_assets);
291  for (uint32_t i = 0; i < wait_duration / 16; i++) {
292  uint32_t time = SDL_GetTicks() >> 7U;
293  ST::renderer_sdl::clear_screen({0, 0, 0, 0});
294  ST::renderer_sdl::draw_sprite(1, 300, 500, time % 6, 3, 6, 6);
296  SDL_Delay(16);
297  }
298 }
299 
300 TEST_F(renderer_sdl_tests, test_draw_sprite_scaled) {
301  SDL_Surface *test_surface = IMG_Load("test_sprite.png");
302  ASSERT_TRUE(static_cast<bool>(test_surface));
303  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
304  test_assets[1] = test_surface;
305  ST::renderer_sdl::upload_surfaces(&test_assets);
306  for (uint32_t i = 0; i < wait_duration / 16; i++) {
307  uint32_t time = SDL_GetTicks() >> 7U;
308  ST::renderer_sdl::clear_screen({0, 0, 0, 0});
309  ST::renderer_sdl::draw_sprite(1, 300, 500, time % 6, 1, 6, 6);
310  ST::renderer_sdl::draw_sprite_scaled(1, 700, 500, time % 6, 1, 6, 6, 0.5, 0.5);
311  ST::renderer_sdl::draw_sprite_scaled(1, 1000, 800, time % 6, 1, 6, 6, 2, 2);
313  SDL_Delay(16);
314  }
315 }
316 
317 TEST_F(renderer_sdl_tests, test_draw_overlay) {
318  SDL_Surface *test_surface = IMG_Load("test_overlay.png");
319  ASSERT_TRUE(static_cast<bool>(test_surface));
320  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
321  test_assets[1] = test_surface;
322  ST::renderer_sdl::upload_surfaces(&test_assets);
323  for (uint32_t i = 0; i < wait_duration / 16; i++) {
324  uint32_t time = SDL_GetTicks() >> 7U;
325  ST::renderer_sdl::clear_screen({0, 0, 0, 0});
326  ST::renderer_sdl::draw_overlay(1, time % 6, 17);
328  SDL_Delay(16);
329  }
330 }
331 
335 TEST_F(renderer_sdl_tests, test_create_atlas) {
336  //Load test assets
337  SDL_Surface *test_surface1 = IMG_Load("atlas_test/1.png");
338  SDL_Surface *test_surface2 = IMG_Load("atlas_test/2.png");
339  SDL_Surface *test_surface3 = IMG_Load("atlas_test/3.png");
340  SDL_Surface *test_surface4 = IMG_Load("atlas_test/4.png");
341  SDL_Surface *test_surface5 = IMG_Load("atlas_test/5.png");
342  SDL_Surface *test_surface6 = IMG_Load("atlas_test/6.png");
343  SDL_Surface *test_surface7 = IMG_Load("atlas_test/7.png");
344  SDL_Surface *test_surface8 = IMG_Load("atlas_test/8.png");
345  SDL_Surface *test_surface9 = IMG_Load("atlas_test/9.png");
346  SDL_Surface *test_surface10 = IMG_Load("atlas_test/10.png");
347  SDL_Surface *test_surface11 = IMG_Load("atlas_test/11.png");
348  SDL_Surface *test_surface12 = IMG_Load("atlas_test/12.png");
349  SDL_Surface *test_surface13 = IMG_Load("atlas_test/13.png");
350  SDL_Surface *test_surface14 = IMG_Load("atlas_test/14.png");
351  SDL_Surface *test_surface15 = IMG_Load("atlas_test/15.png");
352  SDL_Surface *test_surface16 = IMG_Load("atlas_test/16.png");
353  SDL_Surface *test_surface17 = IMG_Load("atlas_test/17.png");
354 
355  //Check that all assets are successfully loaded
356  ASSERT_TRUE(static_cast<bool>(test_surface1));
357  ASSERT_TRUE(static_cast<bool>(test_surface2));
358  ASSERT_TRUE(static_cast<bool>(test_surface3));
359  ASSERT_TRUE(static_cast<bool>(test_surface4));
360  ASSERT_TRUE(static_cast<bool>(test_surface5));
361  ASSERT_TRUE(static_cast<bool>(test_surface6));
362  ASSERT_TRUE(static_cast<bool>(test_surface7));
363  ASSERT_TRUE(static_cast<bool>(test_surface8));
364  ASSERT_TRUE(static_cast<bool>(test_surface9));
365  ASSERT_TRUE(static_cast<bool>(test_surface10));
366  ASSERT_TRUE(static_cast<bool>(test_surface11));
367  ASSERT_TRUE(static_cast<bool>(test_surface12));
368  ASSERT_TRUE(static_cast<bool>(test_surface13));
369  ASSERT_TRUE(static_cast<bool>(test_surface14));
370  ASSERT_TRUE(static_cast<bool>(test_surface15));
371  ASSERT_TRUE(static_cast<bool>(test_surface16));
372  ASSERT_TRUE(static_cast<bool>(test_surface17));
373 
374  //Pass the assets to the renderer
375  ska::bytell_hash_map<uint16_t, SDL_Surface *> test_assets;
376  test_assets[1] = test_surface1;
377  test_assets[2] = test_surface2;
378  test_assets[3] = test_surface3;
379  test_assets[4] = test_surface4;
380  test_assets[5] = test_surface5;
381  for (int i = 0; i < 90 * 12; i += 17) {
382  test_assets[6 + i] = test_surface6;
383  test_assets[7 + i] = test_surface7;
384  test_assets[8 + i] = test_surface8;
385  test_assets[9 + i] = test_surface9;
386  test_assets[10 + i] = test_surface10;
387  test_assets[11 + i] = test_surface11;
388  test_assets[12 + i] = test_surface12;
389  test_assets[13 + i] = test_surface13;
390  test_assets[14 + i] = test_surface14;
391  test_assets[15 + i] = test_surface15;
392  test_assets[16 + i] = test_surface16;
393  test_assets[17 + i] = test_surface17;
394  }
395 
396  ST::renderer_sdl::upload_surfaces(&test_assets);
397  SDL_Rect dst_rect = {0, 0, 1024, 1024};
398  SDL_RenderCopy(sdl_renderer, textures[1].atlas, nullptr, &dst_rect);
400  SDL_Delay(10000);
401 }
402 
403 int main(int argc, char **argv) {
404  ::testing::InitGoogleTest(&argc, argv);
405  return RUN_ALL_TESTS();
406 }
Tests fixture for the renderer_sdl.
uint16_t draw_text_lru_cached(uint16_t font, const std::string &arg2, int x, int y, SDL_Color color_font)
void upload_fonts(ska::bytell_hash_map< uint16_t, TTF_Font * > *fonts)
void upload_surfaces(ska::bytell_hash_map< uint16_t, SDL_Surface * > *surfaces)
void draw_overlay(uint16_t arg, uint8_t sprite, uint8_t sprite_num)
void draw_sprite_scaled(uint16_t arg, int32_t x, int32_t y, uint8_t sprite, uint8_t animation, uint8_t animation_num, uint8_t sprite_num, float scale_x, float scale_y)
void draw_sprite(uint16_t arg, int32_t x, int32_t y, uint8_t sprite, uint8_t animation, uint8_t animation_num, uint8_t sprite_num)
void clear_screen(SDL_Color color)
void draw_texture(uint16_t arg, int32_t x, int32_t y)
uint16_t draw_text_cached_glyphs(uint16_t font, const std::string &text, int x, int y, SDL_Color color_font)
void draw_background_parallax(uint16_t arg, uint16_t offset)
int8_t initialize(SDL_Window *win, int16_t width, int16_t height)
void draw_texture_scaled(uint16_t arg, int32_t x, int32_t y, float scale_x, float scale_y)
void draw_background(uint16_t arg)
void draw_rectangle(int32_t x, int32_t y, int32_t w, int32_t h, SDL_Color color)
void set_draw_color(uint8_t, uint8_t, uint8_t, uint8_t)
void draw_rectangle_filled(int32_t x, int32_t y, int32_t w, int32_t h, SDL_Color color)