🛠️🐜 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.

590 lines
20 KiB

  1. /*
  2. Copyright (C) 1997-2022 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
  57. LoadSprite(const char *file)
  58. {
  59. int i;
  60. for (i = 0; i < state->num_windows; ++i) {
  61. /* This does the SDL_LoadBMP step repeatedly, but that's OK for test code. */
  62. sprites[i] = LoadTexture(state->renderers[i], file, SDL_TRUE, &sprite_w, &sprite_h);
  63. if (!sprites[i]) {
  64. return (-1);
  65. }
  66. if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) {
  67. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError());
  68. SDL_DestroyTexture(sprites[i]);
  69. return (-1);
  70. }
  71. }
  72. /* We're ready to roll. :) */
  73. return (0);
  74. }
  75. void
  76. MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
  77. {
  78. int i;
  79. SDL_Rect viewport, temp;
  80. SDL_Rect *position, *velocity;
  81. /* Query the sizes */
  82. SDL_RenderGetViewport(renderer, &viewport);
  83. /* Cycle the color and alpha, if desired */
  84. if (cycle_color) {
  85. current_color += cycle_direction;
  86. if (current_color < 0) {
  87. current_color = 0;
  88. cycle_direction = -cycle_direction;
  89. }
  90. if (current_color > 255) {
  91. current_color = 255;
  92. cycle_direction = -cycle_direction;
  93. }
  94. SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
  95. (Uint8) current_color);
  96. }
  97. if (cycle_alpha) {
  98. current_alpha += cycle_direction;
  99. if (current_alpha < 0) {
  100. current_alpha = 0;
  101. cycle_direction = -cycle_direction;
  102. }
  103. if (current_alpha > 255) {
  104. current_alpha = 255;
  105. cycle_direction = -cycle_direction;
  106. }
  107. SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
  108. }
  109. /* Draw a gray background */
  110. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  111. SDL_RenderClear(renderer);
  112. /* Test points */
  113. SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
  114. SDL_RenderDrawPoint(renderer, 0, 0);
  115. SDL_RenderDrawPoint(renderer, viewport.w-1, 0);
  116. SDL_RenderDrawPoint(renderer, 0, viewport.h-1);
  117. SDL_RenderDrawPoint(renderer, viewport.w-1, viewport.h-1);
  118. /* Test horizontal and vertical lines */
  119. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  120. SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
  121. SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
  122. SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
  123. SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
  124. /* Test fill and copy */
  125. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  126. temp.x = 1;
  127. temp.y = 1;
  128. temp.w = sprite_w;
  129. temp.h = sprite_h;
  130. if (use_rendergeometry == 0) {
  131. SDL_RenderFillRect(renderer, &temp);
  132. } else {
  133. /* Draw two triangles, filled, uniform */
  134. SDL_Color color;
  135. SDL_Vertex verts[3];
  136. SDL_zeroa(verts);
  137. color.r = 0xFF;
  138. color.g = 0xFF;
  139. color.b = 0xFF;
  140. color.a = 0xFF;
  141. verts[0].position.x = (float)temp.x;
  142. verts[0].position.y = (float)temp.y;
  143. verts[0].color = color;
  144. verts[1].position.x = (float)temp.x + temp.w;
  145. verts[1].position.y = (float)temp.y;
  146. verts[1].color = color;
  147. verts[2].position.x = (float)temp.x + temp.w;
  148. verts[2].position.y = (float)temp.y + temp.h;
  149. verts[2].color = color;
  150. SDL_RenderGeometry(renderer, NULL, verts, 3, NULL, 0);
  151. verts[1].position.x = (float)temp.x;
  152. verts[1].position.y = (float)temp.y + temp.h;
  153. verts[1].color = color;
  154. SDL_RenderGeometry(renderer, NULL, verts, 3, NULL, 0);
  155. }
  156. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  157. temp.x = viewport.w-sprite_w-1;
  158. temp.y = 1;
  159. temp.w = sprite_w;
  160. temp.h = sprite_h;
  161. SDL_RenderFillRect(renderer, &temp);
  162. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  163. temp.x = 1;
  164. temp.y = viewport.h-sprite_h-1;
  165. temp.w = sprite_w;
  166. temp.h = sprite_h;
  167. SDL_RenderFillRect(renderer, &temp);
  168. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  169. temp.x = viewport.w-sprite_w-1;
  170. temp.y = viewport.h-sprite_h-1;
  171. temp.w = sprite_w;
  172. temp.h = sprite_h;
  173. SDL_RenderFillRect(renderer, &temp);
  174. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  175. /* Test diagonal lines */
  176. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  177. SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
  178. viewport.w-sprite_w-2, viewport.h-sprite_h-2);
  179. SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
  180. sprite_w, viewport.h-sprite_h-2);
  181. /* Conditionally move the sprites, bounce at the wall */
  182. if (iterations == -1 || iterations > 0) {
  183. for (i = 0; i < num_sprites; ++i) {
  184. position = &positions[i];
  185. velocity = &velocities[i];
  186. position->x += velocity->x;
  187. if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
  188. velocity->x = -velocity->x;
  189. position->x += velocity->x;
  190. }
  191. position->y += velocity->y;
  192. if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
  193. velocity->y = -velocity->y;
  194. position->y += velocity->y;
  195. }
  196. }
  197. /* Countdown sprite-move iterations and disable color changes at iteration end - used for visual tests. */
  198. if (iterations > 0) {
  199. iterations--;
  200. if (iterations == 0) {
  201. cycle_alpha = SDL_FALSE;
  202. cycle_color = SDL_FALSE;
  203. }
  204. }
  205. }
  206. /* Draw sprites */
  207. if (use_rendergeometry == 0) {
  208. for (i = 0; i < num_sprites; ++i) {
  209. position = &positions[i];
  210. /* Blit the sprite onto the screen */
  211. SDL_RenderCopy(renderer, sprite, NULL, position);
  212. }
  213. } else if (use_rendergeometry == 1) {
  214. /*
  215. * 0--1
  216. * | /|
  217. * |/ |
  218. * 3--2
  219. *
  220. * Draw sprite2 as triangles that can be recombined as rect by software renderer
  221. */
  222. SDL_Vertex *verts = (SDL_Vertex *) SDL_malloc(num_sprites * sizeof (SDL_Vertex) * 6);
  223. SDL_Vertex *verts2 = verts;
  224. if (verts) {
  225. SDL_Color color;
  226. SDL_GetTextureColorMod(sprite, &color.r, &color.g, &color.b);
  227. SDL_GetTextureAlphaMod(sprite, &color.a);
  228. for (i = 0; i < num_sprites; ++i) {
  229. position = &positions[i];
  230. /* 0 */
  231. verts->position.x = (float)position->x;
  232. verts->position.y = (float)position->y;
  233. verts->color = color;
  234. verts->tex_coord.x = 0.0f;
  235. verts->tex_coord.y = 0.0f;
  236. verts++;
  237. /* 1 */
  238. verts->position.x = (float)position->x + position->w;
  239. verts->position.y = (float)position->y;
  240. verts->color = color;
  241. verts->tex_coord.x = 1.0f;
  242. verts->tex_coord.y = 0.0f;
  243. verts++;
  244. /* 2 */
  245. verts->position.x = (float)position->x + position->w;
  246. verts->position.y = (float)position->y + position->h;
  247. verts->color = color;
  248. verts->tex_coord.x = 1.0f;
  249. verts->tex_coord.y = 1.0f;
  250. verts++;
  251. /* 0 */
  252. verts->position.x = (float)position->x;
  253. verts->position.y = (float)position->y;
  254. verts->color = color;
  255. verts->tex_coord.x = 0.0f;
  256. verts->tex_coord.y = 0.0f;
  257. verts++;
  258. /* 2 */
  259. verts->position.x = (float)position->x + position->w;
  260. verts->position.y = (float)position->y + position->h;
  261. verts->color = color;
  262. verts->tex_coord.x = 1.0f;
  263. verts->tex_coord.y = 1.0f;
  264. verts++;
  265. /* 3 */
  266. verts->position.x = (float)position->x;
  267. verts->position.y = (float)position->y + position->h;
  268. verts->color = color;
  269. verts->tex_coord.x = 0.0f;
  270. verts->tex_coord.y = 1.0f;
  271. verts++;
  272. }
  273. /* Blit sprites as triangles onto the screen */
  274. SDL_RenderGeometry(renderer, sprite, verts2, num_sprites * 6, NULL, 0);
  275. SDL_free(verts2);
  276. }
  277. } else if (use_rendergeometry == 2) {
  278. /* 0-----1
  279. * |\ A /|
  280. * | \ / |
  281. * |D 2 B|
  282. * | / \ |
  283. * |/ C \|
  284. * 3-----4
  285. *
  286. * Draw sprite2 as triangles that can *not* be recombined as rect by software renderer
  287. * Use an 'indices' array
  288. */
  289. SDL_Vertex *verts = (SDL_Vertex *) SDL_malloc(num_sprites * sizeof (SDL_Vertex) * 5);
  290. SDL_Vertex *verts2 = verts;
  291. int *indices = (int *) SDL_malloc(num_sprites * sizeof (int) * 4 * 3);
  292. int *indices2 = indices;
  293. if (verts && indices) {
  294. int pos = 0;
  295. SDL_Color color;
  296. SDL_GetTextureColorMod(sprite, &color.r, &color.g, &color.b);
  297. SDL_GetTextureAlphaMod(sprite, &color.a);
  298. for (i = 0; i < num_sprites; ++i) {
  299. position = &positions[i];
  300. /* 0 */
  301. verts->position.x = (float)position->x;
  302. verts->position.y = (float)position->y;
  303. verts->color = color;
  304. verts->tex_coord.x = 0.0f;
  305. verts->tex_coord.y = 0.0f;
  306. verts++;
  307. /* 1 */
  308. verts->position.x = (float)position->x + position->w;
  309. verts->position.y = (float)position->y;
  310. verts->color = color;
  311. verts->tex_coord.x = 1.0f;
  312. verts->tex_coord.y = 0.0f;
  313. verts++;
  314. /* 2 */
  315. verts->position.x = (float)position->x + position->w / 2.0f;
  316. verts->position.y = (float)position->y + position->h / 2.0f;
  317. verts->color = color;
  318. verts->tex_coord.x = 0.5f;
  319. verts->tex_coord.y = 0.5f;
  320. verts++;
  321. /* 3 */
  322. verts->position.x = (float)position->x;
  323. verts->position.y = (float)position->y + position->h;
  324. verts->color = color;
  325. verts->tex_coord.x = 0.0f;
  326. verts->tex_coord.y = 1.0f;
  327. verts++;
  328. /* 4 */
  329. verts->position.x = (float)position->x + position->w;
  330. verts->position.y = (float)position->y + position->h;
  331. verts->color = color;
  332. verts->tex_coord.x = 1.0f;
  333. verts->tex_coord.y = 1.0f;
  334. verts++;
  335. /* A */
  336. *indices++ = pos + 0;
  337. *indices++ = pos + 1;
  338. *indices++ = pos + 2;
  339. /* B */
  340. *indices++ = pos + 1;
  341. *indices++ = pos + 2;
  342. *indices++ = pos + 4;
  343. /* C */
  344. *indices++ = pos + 3;
  345. *indices++ = pos + 2;
  346. *indices++ = pos + 4;
  347. /* D */
  348. *indices++ = pos + 3;
  349. *indices++ = pos + 2;
  350. *indices++ = pos + 0;
  351. pos += 5;
  352. }
  353. }
  354. /* Blit sprites as triangles onto the screen */
  355. SDL_RenderGeometry(renderer, sprite, verts2, num_sprites * 5, indices2, num_sprites * 4 * 3);
  356. SDL_free(verts2);
  357. SDL_free(indices2);
  358. }
  359. /* Update the screen! */
  360. SDL_RenderPresent(renderer);
  361. }
  362. void
  363. loop()
  364. {
  365. Uint32 now;
  366. int i;
  367. SDL_Event event;
  368. /* Check for events */
  369. while (SDL_PollEvent(&event)) {
  370. SDLTest_CommonEvent(state, &event, &done);
  371. }
  372. for (i = 0; i < state->num_windows; ++i) {
  373. if (state->windows[i] == NULL)
  374. continue;
  375. MoveSprites(state->renderers[i], sprites[i]);
  376. }
  377. #ifdef __EMSCRIPTEN__
  378. if (done) {
  379. emscripten_cancel_main_loop();
  380. }
  381. #endif
  382. frames++;
  383. now = SDL_GetTicks();
  384. if (SDL_TICKS_PASSED(now, next_fps_check)) {
  385. /* Print out some timing information */
  386. const Uint32 then = next_fps_check - fps_check_delay;
  387. const double fps = ((double) frames * 1000) / (now - then);
  388. SDL_Log("%2.2f frames per second\n", fps);
  389. next_fps_check = now + fps_check_delay;
  390. frames = 0;
  391. }
  392. }
  393. int
  394. main(int argc, char *argv[])
  395. {
  396. int i;
  397. Uint64 seed;
  398. const char *icon = "icon.bmp";
  399. /* Initialize parameters */
  400. num_sprites = NUM_SPRITES;
  401. /* Initialize test framework */
  402. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  403. if (!state) {
  404. return 1;
  405. }
  406. for (i = 1; i < argc;) {
  407. int consumed;
  408. consumed = SDLTest_CommonArg(state, i);
  409. if (consumed == 0) {
  410. consumed = -1;
  411. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  412. if (argv[i + 1]) {
  413. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  414. blendMode = SDL_BLENDMODE_NONE;
  415. consumed = 2;
  416. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  417. blendMode = SDL_BLENDMODE_BLEND;
  418. consumed = 2;
  419. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  420. blendMode = SDL_BLENDMODE_ADD;
  421. consumed = 2;
  422. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  423. blendMode = SDL_BLENDMODE_MOD;
  424. consumed = 2;
  425. } else if (SDL_strcasecmp(argv[i + 1], "sub") == 0) {
  426. blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT, SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT);
  427. consumed = 2;
  428. }
  429. }
  430. } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
  431. if (argv[i + 1]) {
  432. iterations = SDL_atoi(argv[i + 1]);
  433. if (iterations < -1) iterations = -1;
  434. consumed = 2;
  435. }
  436. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  437. cycle_color = SDL_TRUE;
  438. consumed = 1;
  439. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  440. cycle_alpha = SDL_TRUE;
  441. consumed = 1;
  442. } else if (SDL_strcasecmp(argv[i], "--use-rendergeometry") == 0) {
  443. if (argv[i + 1]) {
  444. if (SDL_strcasecmp(argv[i + 1], "mode1") == 0) {
  445. /* Draw sprite2 as triangles that can be recombined as rect by software renderer */
  446. use_rendergeometry = 1;
  447. } else if (SDL_strcasecmp(argv[i + 1], "mode2") == 0) {
  448. /* Draw sprite2 as triangles that can *not* be recombined as rect by software renderer
  449. * Use an 'indices' array */
  450. use_rendergeometry = 2;
  451. } else {
  452. return -1;
  453. }
  454. }
  455. consumed = 2;
  456. } else if (SDL_isdigit(*argv[i])) {
  457. num_sprites = SDL_atoi(argv[i]);
  458. consumed = 1;
  459. } else if (argv[i][0] != '-') {
  460. icon = argv[i];
  461. consumed = 1;
  462. }
  463. }
  464. if (consumed < 0) {
  465. static const char *options[] = {
  466. "[--blend none|blend|add|mod]",
  467. "[--cyclecolor]",
  468. "[--cyclealpha]",
  469. "[--iterations N]",
  470. "[--use-rendergeometry mode1|mode2]",
  471. "[num_sprites]",
  472. "[icon.bmp]",
  473. NULL };
  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) {
  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 || !velocities) {
  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: */