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

416 lines
13 KiB

  1. /*
  2. Copyright (C) 1997-2019 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. #define NUM_SPRITES 100
  20. #define MAX_SPEED 1
  21. static SDLTest_CommonState *state;
  22. static int num_sprites;
  23. static SDL_Texture **sprites;
  24. static SDL_bool cycle_color;
  25. static SDL_bool cycle_alpha;
  26. static int cycle_direction = 1;
  27. static int current_alpha = 0;
  28. static int current_color = 0;
  29. static SDL_Rect *positions;
  30. static SDL_Rect *velocities;
  31. static int sprite_w, sprite_h;
  32. static SDL_BlendMode blendMode = SDL_BLENDMODE_BLEND;
  33. static Uint32 next_fps_check, frames;
  34. static const Uint32 fps_check_delay = 5000;
  35. /* Number of iterations to move sprites - used for visual tests. */
  36. /* -1: infinite random moves (default); >=0: enables N deterministic moves */
  37. static int iterations = -1;
  38. int done;
  39. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  40. static void
  41. quit(int rc)
  42. {
  43. SDL_free(sprites);
  44. SDL_free(positions);
  45. SDL_free(velocities);
  46. SDLTest_CommonQuit(state);
  47. exit(rc);
  48. }
  49. int
  50. LoadSprite(const char *file)
  51. {
  52. int i;
  53. SDL_Surface *temp;
  54. /* Load the sprite image */
  55. temp = SDL_LoadBMP(file);
  56. if (temp == NULL) {
  57. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  58. return (-1);
  59. }
  60. sprite_w = temp->w;
  61. sprite_h = temp->h;
  62. /* Set transparent pixel as the pixel at (0,0) */
  63. if (temp->format->palette) {
  64. SDL_SetColorKey(temp, 1, *(Uint8 *) temp->pixels);
  65. } else {
  66. switch (temp->format->BitsPerPixel) {
  67. case 15:
  68. SDL_SetColorKey(temp, 1, (*(Uint16 *) temp->pixels) & 0x00007FFF);
  69. break;
  70. case 16:
  71. SDL_SetColorKey(temp, 1, *(Uint16 *) temp->pixels);
  72. break;
  73. case 24:
  74. SDL_SetColorKey(temp, 1, (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  75. break;
  76. case 32:
  77. SDL_SetColorKey(temp, 1, *(Uint32 *) temp->pixels);
  78. break;
  79. }
  80. }
  81. /* Create textures from the image */
  82. for (i = 0; i < state->num_windows; ++i) {
  83. SDL_Renderer *renderer = state->renderers[i];
  84. sprites[i] = SDL_CreateTextureFromSurface(renderer, temp);
  85. if (!sprites[i]) {
  86. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  87. SDL_FreeSurface(temp);
  88. return (-1);
  89. }
  90. if (SDL_SetTextureBlendMode(sprites[i], blendMode) < 0) {
  91. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s\n", SDL_GetError());
  92. SDL_FreeSurface(temp);
  93. SDL_DestroyTexture(sprites[i]);
  94. return (-1);
  95. }
  96. }
  97. SDL_FreeSurface(temp);
  98. /* We're ready to roll. :) */
  99. return (0);
  100. }
  101. void
  102. MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
  103. {
  104. int i;
  105. SDL_Rect viewport, temp;
  106. SDL_Rect *position, *velocity;
  107. /* Query the sizes */
  108. SDL_RenderGetViewport(renderer, &viewport);
  109. /* Cycle the color and alpha, if desired */
  110. if (cycle_color) {
  111. current_color += cycle_direction;
  112. if (current_color < 0) {
  113. current_color = 0;
  114. cycle_direction = -cycle_direction;
  115. }
  116. if (current_color > 255) {
  117. current_color = 255;
  118. cycle_direction = -cycle_direction;
  119. }
  120. SDL_SetTextureColorMod(sprite, 255, (Uint8) current_color,
  121. (Uint8) current_color);
  122. }
  123. if (cycle_alpha) {
  124. current_alpha += cycle_direction;
  125. if (current_alpha < 0) {
  126. current_alpha = 0;
  127. cycle_direction = -cycle_direction;
  128. }
  129. if (current_alpha > 255) {
  130. current_alpha = 255;
  131. cycle_direction = -cycle_direction;
  132. }
  133. SDL_SetTextureAlphaMod(sprite, (Uint8) current_alpha);
  134. }
  135. /* Draw a gray background */
  136. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  137. SDL_RenderClear(renderer);
  138. /* Test points */
  139. SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
  140. SDL_RenderDrawPoint(renderer, 0, 0);
  141. SDL_RenderDrawPoint(renderer, viewport.w-1, 0);
  142. SDL_RenderDrawPoint(renderer, 0, viewport.h-1);
  143. SDL_RenderDrawPoint(renderer, viewport.w-1, viewport.h-1);
  144. /* Test horizontal and vertical lines */
  145. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  146. SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
  147. SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
  148. SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
  149. SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
  150. /* Test fill and copy */
  151. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  152. temp.x = 1;
  153. temp.y = 1;
  154. temp.w = sprite_w;
  155. temp.h = sprite_h;
  156. SDL_RenderFillRect(renderer, &temp);
  157. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  158. temp.x = viewport.w-sprite_w-1;
  159. temp.y = 1;
  160. temp.w = sprite_w;
  161. temp.h = sprite_h;
  162. SDL_RenderFillRect(renderer, &temp);
  163. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  164. temp.x = 1;
  165. temp.y = viewport.h-sprite_h-1;
  166. temp.w = sprite_w;
  167. temp.h = sprite_h;
  168. SDL_RenderFillRect(renderer, &temp);
  169. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  170. temp.x = viewport.w-sprite_w-1;
  171. temp.y = viewport.h-sprite_h-1;
  172. temp.w = sprite_w;
  173. temp.h = sprite_h;
  174. SDL_RenderFillRect(renderer, &temp);
  175. SDL_RenderCopy(renderer, sprite, NULL, &temp);
  176. /* Test diagonal lines */
  177. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  178. SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
  179. viewport.w-sprite_w-2, viewport.h-sprite_h-2);
  180. SDL_RenderDrawLine(renderer, viewport.w-sprite_w-2, sprite_h,
  181. sprite_w, viewport.h-sprite_h-2);
  182. /* Conditionally move the sprites, bounce at the wall */
  183. if (iterations == -1 || iterations > 0) {
  184. for (i = 0; i < num_sprites; ++i) {
  185. position = &positions[i];
  186. velocity = &velocities[i];
  187. position->x += velocity->x;
  188. if ((position->x < 0) || (position->x >= (viewport.w - sprite_w))) {
  189. velocity->x = -velocity->x;
  190. position->x += velocity->x;
  191. }
  192. position->y += velocity->y;
  193. if ((position->y < 0) || (position->y >= (viewport.h - sprite_h))) {
  194. velocity->y = -velocity->y;
  195. position->y += velocity->y;
  196. }
  197. }
  198. /* Countdown sprite-move iterations and disable color changes at iteration end - used for visual tests. */
  199. if (iterations > 0) {
  200. iterations--;
  201. if (iterations == 0) {
  202. cycle_alpha = SDL_FALSE;
  203. cycle_color = SDL_FALSE;
  204. }
  205. }
  206. }
  207. /* Draw sprites */
  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. /* Update the screen! */
  214. SDL_RenderPresent(renderer);
  215. }
  216. void
  217. loop()
  218. {
  219. Uint32 now;
  220. int i;
  221. SDL_Event event;
  222. /* Check for events */
  223. while (SDL_PollEvent(&event)) {
  224. SDLTest_CommonEvent(state, &event, &done);
  225. }
  226. for (i = 0; i < state->num_windows; ++i) {
  227. if (state->windows[i] == NULL)
  228. continue;
  229. MoveSprites(state->renderers[i], sprites[i]);
  230. }
  231. #ifdef __EMSCRIPTEN__
  232. if (done) {
  233. emscripten_cancel_main_loop();
  234. }
  235. #endif
  236. frames++;
  237. now = SDL_GetTicks();
  238. if (SDL_TICKS_PASSED(now, next_fps_check)) {
  239. /* Print out some timing information */
  240. const Uint32 then = next_fps_check - fps_check_delay;
  241. const double fps = ((double) frames * 1000) / (now - then);
  242. SDL_Log("%2.2f frames per second\n", fps);
  243. next_fps_check = now + fps_check_delay;
  244. frames = 0;
  245. }
  246. }
  247. int
  248. main(int argc, char *argv[])
  249. {
  250. int i;
  251. Uint64 seed;
  252. const char *icon = "icon.bmp";
  253. /* Initialize parameters */
  254. num_sprites = NUM_SPRITES;
  255. /* Initialize test framework */
  256. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  257. if (!state) {
  258. return 1;
  259. }
  260. for (i = 1; i < argc;) {
  261. int consumed;
  262. consumed = SDLTest_CommonArg(state, i);
  263. if (consumed == 0) {
  264. consumed = -1;
  265. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  266. if (argv[i + 1]) {
  267. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  268. blendMode = SDL_BLENDMODE_NONE;
  269. consumed = 2;
  270. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  271. blendMode = SDL_BLENDMODE_BLEND;
  272. consumed = 2;
  273. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  274. blendMode = SDL_BLENDMODE_ADD;
  275. consumed = 2;
  276. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  277. blendMode = SDL_BLENDMODE_MOD;
  278. consumed = 2;
  279. } else if (SDL_strcasecmp(argv[i + 1], "sub") == 0) {
  280. blendMode = SDL_ComposeCustomBlendMode(SDL_BLENDFACTOR_SRC_ALPHA, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT, SDL_BLENDFACTOR_ZERO, SDL_BLENDFACTOR_ONE, SDL_BLENDOPERATION_SUBTRACT);
  281. consumed = 2;
  282. }
  283. }
  284. } else if (SDL_strcasecmp(argv[i], "--iterations") == 0) {
  285. if (argv[i + 1]) {
  286. iterations = SDL_atoi(argv[i + 1]);
  287. if (iterations < -1) iterations = -1;
  288. consumed = 2;
  289. }
  290. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  291. cycle_color = SDL_TRUE;
  292. consumed = 1;
  293. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  294. cycle_alpha = SDL_TRUE;
  295. consumed = 1;
  296. } else if (SDL_isdigit(*argv[i])) {
  297. num_sprites = SDL_atoi(argv[i]);
  298. consumed = 1;
  299. } else if (argv[i][0] != '-') {
  300. icon = argv[i];
  301. consumed = 1;
  302. }
  303. }
  304. if (consumed < 0) {
  305. static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", "[--iterations N]", "[num_sprites]", "[icon.bmp]", NULL };
  306. SDLTest_CommonLogUsage(state, argv[0], options);
  307. quit(1);
  308. }
  309. i += consumed;
  310. }
  311. if (!SDLTest_CommonInit(state)) {
  312. quit(2);
  313. }
  314. /* Create the windows, initialize the renderers, and load the textures */
  315. sprites =
  316. (SDL_Texture **) SDL_malloc(state->num_windows * sizeof(*sprites));
  317. if (!sprites) {
  318. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
  319. quit(2);
  320. }
  321. for (i = 0; i < state->num_windows; ++i) {
  322. SDL_Renderer *renderer = state->renderers[i];
  323. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  324. SDL_RenderClear(renderer);
  325. }
  326. if (LoadSprite(icon) < 0) {
  327. quit(2);
  328. }
  329. /* Allocate memory for the sprite info */
  330. positions = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
  331. velocities = (SDL_Rect *) SDL_malloc(num_sprites * sizeof(SDL_Rect));
  332. if (!positions || !velocities) {
  333. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!\n");
  334. quit(2);
  335. }
  336. /* Position sprites and set their velocities using the fuzzer */
  337. if (iterations >= 0) {
  338. /* Deterministic seed - used for visual tests */
  339. seed = (Uint64)iterations;
  340. } else {
  341. /* Pseudo-random seed generated from the time */
  342. seed = (Uint64)time(NULL);
  343. }
  344. SDLTest_FuzzerInit(seed);
  345. for (i = 0; i < num_sprites; ++i) {
  346. positions[i].x = SDLTest_RandomIntegerInRange(0, state->window_w - sprite_w);
  347. positions[i].y = SDLTest_RandomIntegerInRange(0, state->window_h - sprite_h);
  348. positions[i].w = sprite_w;
  349. positions[i].h = sprite_h;
  350. velocities[i].x = 0;
  351. velocities[i].y = 0;
  352. while (!velocities[i].x && !velocities[i].y) {
  353. velocities[i].x = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
  354. velocities[i].y = SDLTest_RandomIntegerInRange(-MAX_SPEED, MAX_SPEED);
  355. }
  356. }
  357. /* Main render loop */
  358. frames = 0;
  359. next_fps_check = SDL_GetTicks() + fps_check_delay;
  360. done = 0;
  361. #ifdef __EMSCRIPTEN__
  362. emscripten_set_main_loop(loop, 0, 1);
  363. #else
  364. while (!done) {
  365. loop();
  366. }
  367. #endif
  368. quit(0);
  369. return 0;
  370. }
  371. /* vi: set ts=4 sw=4 expandtab: */