🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

588 lines
20 KiB

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