Debian Codegraph @

test/testsprite2.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12 /* Simple program: Move N sprites around on the screen as fast as possible */
13
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <time.h>
17
18 #ifdef __EMSCRIPTEN__
19 #include <emscripten/emscripten.h>
20 #endif
21
22 #include "SDL_test.h"
23 #include "SDL_test_common.h"
24 #include "testutils.h"
25
26 #define NUM_SPRITES 100
27 #define MAX_SPEED 1
28
29 static SDLTest_CommonState *state;
30 static int num_sprites;
31 static SDL_Texture **sprites;
32 static SDL_bool cycle_color;
33 static SDL_bool cycle_alpha;
34 static int cycle_direction = 1;
35 static int current_alpha = 0;
36 static int current_color = 0;
37 static SDL_Rect *positions;
38 static SDL_Rect *velocities;
39 static int sprite_w, sprite_h;
40 static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
41 static Uint32 next_fps_check, frames;
42 static const Uint32 fps_check_delay = 5000;
43 static int use_rendergeometry = 0;
44
45 /* Number of iterations to move sprites - used for visual tests. */
46 /* -1: infinite random moves (default); >=0: enables N deterministic moves */
47 static int iterations = -1;
48
49 int done;
50
51 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
52 static void
53 quit(int rc)
54 {
55 SDL_free(sprites);
56 SDL_free(positions);
57 SDL_free(velocities);
58 SDLTest_CommonQuit(state);
59 /* If rc is 0, just let main return normally rather than calling exit.
60 * This allows testing of platforms where SDL_main is required and does meaningful cleanup.
61 */
62 if (rc != 0) {
63 exit(rc);
64 }
65 }
66
67 int LoadSprite(const char *file)
68 {
69 int i;
70
71 for (i = 0; i < state->num_windows; ++i) {
72 /* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
73 sprites[i] = LoadTexture(state->renderers[i], file, SDL_TRUE, &sprite_w, &sprite_h);
74 if (!sprites[i]) {
75 return -1;
76 }
77 if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) {
78 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError());
79 SDL_DestroyTexture(sprites[i]);
80 return -1;
81 }
82 }
83
84 /* We're ready to roll. :) */
85 return 0;
86 }
87
88 void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
89 {
90 int i;
91 SDL_Rect viewport, temp;
92 SDL_Rect *position, *velocity;
93
94 /* Query the sizes */
95 SDL_RenderGetViewport(renderer, &viewport);
96
97 /* Cycle the color and alpha, if desired */
98 if (cycle_color) {
99 current_color += cycle_direction;
100 if (current_color < 0) {
101 current_color = 0;
102 cycle_direction = -cycle_direction;
103 }
104 if (current_color > 255) {
105 current_color = 255;
106 cycle_direction = -cycle_direction;
107 }
108 SDL_SetTextureColorMod(sprite, 255, (Uint8)current_color,
109 (Uint8)current_color);
110 }
111 if (cycle_alpha) {
112 current_alpha += cycle_direction;
113 if (current_alpha < 0) {
114 current_alpha = 0;
115 cycle_direction = -cycle_direction;
116 }
117 if (current_alpha > 255) {
118 current_alpha = 255;
119 cycle_direction = -cycle_direction;
120 }
121 SDL_SetTextureAlphaMod(sprite, (Uint8)current_alpha);
122 }
123
124 /* Draw a gray background */
125 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
126 SDL_RenderClear(renderer);
127
128 /* Test points */
129 SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
130 SDL_RenderDrawPoint(renderer, 0, 0);
131 SDL_RenderDrawPoint(renderer, viewport.w - 1, 0);
132 SDL_RenderDrawPoint(renderer, 0, viewport.h - 1);
133 SDL_RenderDrawPoint(renderer, viewport.w - 1, viewport.h - 1);
134
135 /* Test horizontal and vertical lines */
136 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
137 SDL_RenderDrawLine(renderer, 1, 0, viewport.w - 2, 0);
138 SDL_RenderDrawLine(renderer, 1, viewport.h - 1, viewport.w - 2, viewport.h - 1);
139 SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h - 2);
140 SDL_RenderDrawLine(renderer, viewport.w - 1, 1, viewport.w - 1, viewport.h - 2);
141
142 /* Test fill and copy */
143 SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
144 temp.x = 1;
145 temp.y = 1;
146 temp.w = sprite_w;
147 temp.h = sprite_h;
148 if (use_rendergeometry == 0) {
149 SDL_RenderFillRect(renderer, &temp);
150 } else {
151 /* Draw two triangles, filled, uniform */
152 SDL_Color color;
153 SDL_Vertex verts[3];
154 SDL_zeroa(verts);
155 color.r = 0xFF;
156 color.g = 0xFF;
157 color.b = 0xFF;
158 color.a = 0xFF;
159
160 verts[0].position.x = (float)temp.x;
161 verts[0].position.y = (float)temp.y;
162 verts[0].color = color;
163
164 verts[1].position.x = (float)temp.x + temp.w;
165 verts[1].position.y = (float)temp.y;
166 verts[1].color = color;
167
168 verts[2].position.x = (float)temp.x + temp.w;
169 verts[2].position.y = (float)temp.y + temp.h;
170 verts[2].color = color;
171
172 SDL_RenderGeometry(renderer, NULL, verts, 3, NULL, 0);
173
174 verts[1].position.x = (float)temp.x;
175 verts[1].position.y = (float)temp.y + temp.h;
176 verts[1].color = color;
177
178 SDL_RenderGeometry(renderer, NULL, verts, 3, NULL, 0);
179 }
180 SDL_RenderCopy(renderer, sprite, NULL, &temp);
181 temp.x = viewport.w - sprite_w - 1;
182 temp.y = 1;
183 temp.w = sprite_w;
184 temp.h = sprite_h;
185 SDL_RenderFillRect(renderer, &temp);
186 SDL_RenderCopy(renderer, sprite, NULL, &temp);
187 temp.x = 1;
188 temp.y = viewport.h - sprite_h - 1;
189 temp.w = sprite_w;
190 temp.h = sprite_h;
191 SDL_RenderFillRect(renderer, &temp);
192 SDL_RenderCopy(renderer, sprite, NULL, &temp);
193 temp.x = viewport.w - sprite_w - 1;
194 temp.y = viewport.h - sprite_h - 1;
195 temp.w = sprite_w;
196 temp.h = sprite_h;
197 SDL_RenderFillRect(renderer, &temp);
198 SDL_RenderCopy(renderer, sprite, NULL, &temp);
199
200 /* Test diagonal lines */
201 SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
202 SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
203 viewport.w - sprite_w - 2, viewport.h - sprite_h - 2);
204 SDL_RenderDrawLine(renderer, viewport.w - sprite_w - 2, sprite_h,
205 sprite_w, viewport.h - sprite_h - 2);
206
207 /* Conditionally move the sprites, bounce at the wall */
208 if (iterations == -1 || iterations > 0) {
209 for (i = 0; i < num_sprites; ++i) {
210 position = &positions[i];
211 velocity = &velocities[i];
212 position->x += velocity->x;
213 if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
214 velocity->x = -velocity->x;
215 position->x += velocity->x;
216 }
217 position->y += velocity->y;
218 if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
219 velocity->y = -velocity->y;
220 position->y += velocity->y;
221 }
222 }
223
224 /* Countdown sprite-move iterations and disable color changes at iteration end - used for visual tests. */
225 if (iterations > 0) {
226 iterations--;
227 if (iterations == 0) {
228 cycle_alpha = SDL_FALSE;
229 cycle_color = SDL_FALSE;
230 }
231 }
232 }
233
234 /* Draw sprites */
235 if (use_rendergeometry == 0) {
236 for (i = 0; i < num_sprites; ++i) {
237 position = &positions[i];
238
239 /* Blit the sprite onto the screen */
240 SDL_RenderCopy(renderer, sprite, NULL, position);
241 }
242 } else if (use_rendergeometry == 1) {
243 /*
244 * 0--1
245 * | /|
246 * |/ |
247 * 3--2
248 *
249 * Draw sprite2 as triangles that can be recombined as rect by software renderer
250 */
251 SDL_Vertex *verts = (SDL_Vertex *)SDL_malloc(num_sprites * sizeof(SDL_Vertex) * 6);
252 SDL_Vertex *verts2 = verts;
253 if (verts) {
254 SDL_Color color;
255 SDL_GetTextureColorMod(sprite, &color.r, &color.g, &color.b);
256 SDL_GetTextureAlphaMod(sprite, &color.a);
257 for (i = 0; i < num_sprites; ++i) {
258 position = &positions[i];
259 /* 0 */
260 verts->position.x = (float)position->x;
261 verts->position.y = (float)position->y;
262 verts->color = color;
263 verts->tex_coord.x = 0.0f;
264 verts->tex_coord.y = 0.0f;
265 verts++;
266 /* 1 */
267 verts->position.x = (float)position->x + position->w;
268 verts->position.y = (float)position->y;
269 verts->color = color;
270 verts->tex_coord.x = 1.0f;
271 verts->tex_coord.y = 0.0f;
272 verts++;
273 /* 2 */
274 verts->position.x = (float)position->x + position->w;
275 verts->position.y = (float)position->y + position->h;
276 verts->color = color;
277 verts->tex_coord.x = 1.0f;
278 verts->tex_coord.y = 1.0f;
279 verts++;
280 /* 0 */
281 verts->position.x = (float)position->x;
282 verts->position.y = (float)position->y;
283 verts->color = color;
284 verts->tex_coord.x = 0.0f;
285 verts->tex_coord.y = 0.0f;
286 verts++;
287 /* 2 */
288 verts->position.x = (float)position->x + position->w;
289 verts->position.y = (float)position->y + position->h;
290 verts->color = color;
291 verts->tex_coord.x = 1.0f;
292 verts->tex_coord.y = 1.0f;
293 verts++;
294 /* 3 */
295 verts->position.x = (float)position->x;
296 verts->position.y = (float)position->y + position->h;
297 verts->color = color;
298 verts->tex_coord.x = 0.0f;
299 verts->tex_coord.y = 1.0f;
300 verts++;
301 }
302
303 /* Blit sprites as triangles onto the screen */
304 SDL_RenderGeometry(renderer, sprite, verts2, num_sprites * 6, NULL, 0);
305 SDL_free(verts2);
306 }
307 } else if (use_rendergeometry == 2) {
308 /* 0-----1
309 * |\ A /|
310 * | \ / |
311 * |D 2 B|
312 * | / \ |
313 * |/ C \|
314 * 3-----4
315 *
316 * Draw sprite2 as triangles that can *not* be recombined as rect by software renderer
317 * Use an 'indices' array
318 */
319 SDL_Vertex *verts = (SDL_Vertex *)SDL_malloc(num_sprites * sizeof(SDL_Vertex) * 5);
320 SDL_Vertex *verts2 = verts;
321 int *indices = (int *)SDL_malloc(num_sprites * sizeof(int) * 4 * 3);
322 int *indices2 = indices;
323 if (verts && indices) {
324 int pos = 0;
325 SDL_Color color;
326 SDL_GetTextureColorMod(sprite, &color.r, &color.g, &color.b);
327 SDL_GetTextureAlphaMod(sprite, &color.a);
328 for (i = 0; i < num_sprites; ++i) {
329 position = &positions[i];
330 /* 0 */
331 verts->position.x = (float)position->x;
332 verts->position.y = (float)position->y;
333 verts->color = color;
334 verts->tex_coord.x = 0.0f;
335 verts->tex_coord.y = 0.0f;
336 verts++;
337 /* 1 */
338 verts->position.x = (float)position->x + position->w;
339 verts->position.y = (float)position->y;
340 verts->color = color;
341 verts->tex_coord.x = 1.0f;
342 verts->tex_coord.y = 0.0f;
343 verts++;
344 /* 2 */
345 verts->position.x = (float)position->x + position->w / 2.0f;
346 verts->position.y = (float)position->y + position->h / 2.0f;
347 verts->color = color;
348 verts->tex_coord.x = 0.5f;
349 verts->tex_coord.y = 0.5f;
350 verts++;
351 /* 3 */
352 verts->position.x = (float)position->x;
353 verts->position.y = (float)position->y + position->h;
354 verts->color = color;
355 verts->tex_coord.x = 0.0f;
356 verts->tex_coord.y = 1.0f;
357 verts++;
358 /* 4 */
359 verts->position.x = (float)position->x + position->w;
360 verts->position.y = (float)position->y + position->h;
361 verts->color = color;
362 verts->tex_coord.x = 1.0f;
363 verts->tex_coord.y = 1.0f;
364 verts++;
365 /* A */
366 *indices++ = pos + 0;
367 *indices++ = pos + 1;
368 *indices++ = pos + 2;
369 /* B */
370 *indices++ = pos + 1;
371 *indices++ = pos + 2;
372 *indices++ = pos + 4;
373 /* C */
374 *indices++ = pos + 3;
375 *indices++ = pos + 2;
376 *indices++ = pos + 4;
377 /* D */
378 *indices++ = pos + 3;
379 *indices++ = pos + 2;
380 *indices++ = pos + 0;
381 pos += 5;
382 }
383 }
384
385 /* Blit sprites as triangles onto the screen */
386 SDL_RenderGeometry(renderer, sprite, verts2, num_sprites * 5, indices2, num_sprites * 4 * 3);
387 SDL_free(verts2);
388 SDL_free(indices2);
389 }
390
391 /* Update the screen! */
392 SDL_RenderPresent(renderer);
393 }
394
395 static void MoveAllSprites(void)
396 {
397 int i;
398
399 for (i = 0; i < state->num_windows; ++i) {
400 if (state->windows[i] == NULL) {
401 continue;
402 }
403 MoveSprites(state->renderers[i], sprites[i]);
404 }
405 }
406
407 void loop(void)
408 {
409 Uint32 now;
410 SDL_Event event;
411
412 /* Check for events */
413 while (SDL_PollEvent(&event)) {
414 SDLTest_CommonEvent(state, &event, &done);
415 }
416
417 MoveAllSprites();
418
419 #ifdef __EMSCRIPTEN__
420 if (done) {
421 emscripten_cancel_main_loop();
422 }
423 #endif
424
425 frames++;
426 now = SDL_GetTicks();
427 if (SDL_TICKS_PASSED(now, next_fps_check)) {
428 /* Print out some timing information */
429 const Uint32 then = next_fps_check - fps_check_delay;
430 const double fps = ((double)frames * 1000) / (now - then);
431 SDL_Log("%2.2f frames per second\n", fps);
432 next_fps_check = now + fps_check_delay;
433 frames = 0;
434 }
435 }
436
437 static int SDLCALL ExposeEventWatcher(void *userdata, SDL_Event *event)
438 {
439 if (event->type == SDL_WINDOWEVENT && event->window.event == SDL_WINDOWEVENT_EXPOSED) {
440 MoveAllSprites();
441 }
442 return 0;
443 }
444
445 int main(int argc, char *argv[])
446 {
447 int i;
448 Uint64 seed;
449 const char *icon = "icon.bmp";
450
451 /* Initialize parameters */
452 num_sprites = NUM_SPRITES;
453
454 /* Initialize test framework */
455 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
456 if (!state) {
457 return 1;
458 }
459
460 for (i = 1; i < argc;) {
461 int consumed;
462
463 consumed = SDLTest_CommonArg(state, i);
464 if (consumed == 0) {
465 consumed = -1;
466 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
467 if (argv[i + 1]) {
468 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
469 blendMode = SDL_BLENDMODE_NONE;
470 consumed = 2;
471 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
472 blendMode = SDL_BLENDMODE_BLEND;
473 consumed = 2;
474 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
475 blendMode = SDL_BLENDMODE_ADD;
476 consumed = 2;
477 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
478 blendMode = SDL_BLENDMODE_MOD;
479 consumed = 2;
480 } else if (SDL_strcasecmp(argv[i + 1], "sub") == 0) {
481 blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT, SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT);
482 consumed = 2;
483 }
484 }
485 } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
486 if (argv[i + 1]) {
487 iterations = SDL_atoi(argv[i + 1]);
488 if (iterations < -1) {
489 iterations = -1;
490 }
491 consumed = 2;
492 }
493 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
494 cycle_color = SDL_TRUE;
495 consumed = 1;
496 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
497 cycle_alpha = SDL_TRUE;
498 consumed = 1;
499 } else if (SDL_strcasecmp(argv[i], "--use-rendergeometry") == 0) {
500 if (argv[i + 1]) {
501 if (SDL_strcasecmp(argv[i + 1], "mode1") == 0) {
502 /* Draw sprite2 as triangles that can be recombined as rect by software renderer */
503 use_rendergeometry = 1;
504 } else if (SDL_strcasecmp(argv[i + 1], "mode2") == 0) {
505 /* Draw sprite2 as triangles that can *not* be recombined as rect by software renderer
506 * Use an 'indices' array */
507 use_rendergeometry = 2;
508 } else {
509 return -1;
510 }
511 }
512 consumed = 2;
513 } else if (SDL_isdigit(*argv[i])) {
514 num_sprites = SDL_atoi(argv[i]);
515 consumed = 1;
516 } else if (argv[i][0] != '-') {
517 icon = argv[i];
518 consumed = 1;
519 }
520 }
521 if (consumed < 0) {
522 static const char *options[] = {
523 "[--blend none|blend|add|mod]",
524 "[--cyclecolor]",
525 "[--cyclealpha]",
526 "[--iterations N]",
527 "[--use-rendergeometry mode1|mode2]",
528 "[num_sprites]",
529 "[icon.bmp]",
530 NULL
531 };
532 SDLTest_CommonLogUsage(state, argv[0], options);
533 quit(1);
534 }
535 i += consumed;
536 }
537 if (!SDLTest_CommonInit(state)) {
538 quit(2);
539 }
540
541 /* Create the windows, initialize the renderers, and load the textures */
542 sprites =
543 (SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites));
544 if (!sprites) {
545 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
546 quit(2);
547 }
548 for (i = 0; i < state->num_windows; ++i) {
549 SDL_Renderer *renderer = state->renderers[i];
550 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
551 SDL_RenderClear(renderer);
552 }
553 if (LoadSprite(icon) < 0) {
554 quit(2);
555 }
556
557 /* Allocate memory for the sprite info */
558 positions = (SDL_Rect *)SDL_malloc(num_sprites * sizeof(SDL_Rect));
559 velocities = (SDL_Rect *)SDL_malloc(num_sprites * sizeof(SDL_Rect));
560 if (!positions || !velocities) {
561 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
562 quit(2);
563 }
564
565 /* Position sprites and set their velocities using the fuzzer */
566 if (iterations >= 0) {
567 /* Deterministic seed - used for visual tests */
568 seed = (Uint64)iterations;
569 } else {
570 /* Pseudo-random seed generated from the time */
571 seed = (Uint64)time(NULL);
572 }
573 SDLTest_FuzzerInit(seed);
574 for (i = 0; i < num_sprites; ++i) {
575 positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w);
576 positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h);
577 positions[i].w = sprite_w;
578 positions[i].h = sprite_h;
579 velocities[i].x = 0;
580 velocities[i].y = 0;
581 while (!velocities[i].x && !velocities[i].y) {
582 velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
583 velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
584 }
585 }
586
587 /* Add an event watcher to redraw from within modal window resize/move loops */
588 SDL_AddEventWatch(ExposeEventWatcher, NULL);
589
590 /* Main render loop */
591 frames = 0;
592 next_fps_check = SDL_GetTicks() + fps_check_delay;
593 done = 0;
594
595 #ifdef __EMSCRIPTEN__
596 emscripten_set_main_loop(loop, 0, 1);
597 #else
598 while (!done) {
599 loop();
600 }
601 #endif
602
603 quit(0);
604 return 0;
605 }
606
607 /* vi: set ts=4 sw=4 expandtab: */