testsprite2.c 12 KB

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