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

285 lines
8.1 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_common.h"
  18. #include "testutils.h"
  19. static SDLTest_CommonState *state;
  20. typedef struct {
  21. SDL_Window *window;
  22. SDL_Renderer *renderer;
  23. SDL_Texture *background;
  24. SDL_Texture *sprite;
  25. SDL_Rect sprite_rect;
  26. int scale_direction;
  27. } DrawState;
  28. DrawState *drawstates;
  29. int done;
  30. SDL_bool test_composite = SDL_FALSE;
  31. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  32. static void
  33. quit(int rc)
  34. {
  35. SDLTest_CommonQuit(state);
  36. exit(rc);
  37. }
  38. SDL_bool
  39. DrawComposite(DrawState *s)
  40. {
  41. SDL_Rect viewport, R;
  42. SDL_Texture *target;
  43. static SDL_bool blend_tested = SDL_FALSE;
  44. if (!blend_tested) {
  45. SDL_Texture *A, *B;
  46. Uint32 P;
  47. A = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
  48. SDL_SetTextureBlendMode(A, SDL_BLENDMODE_BLEND);
  49. B = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
  50. SDL_SetTextureBlendMode(B, SDL_BLENDMODE_BLEND);
  51. SDL_SetRenderTarget(s->renderer, A);
  52. SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x80);
  53. SDL_RenderFillRect(s->renderer, NULL);
  54. SDL_SetRenderTarget(s->renderer, B);
  55. SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
  56. SDL_RenderFillRect(s->renderer, NULL);
  57. SDL_RenderCopy(s->renderer, A, NULL, NULL);
  58. SDL_RenderReadPixels(s->renderer, NULL, SDL_PIXELFORMAT_ARGB8888, &P, sizeof(P));
  59. SDL_Log("Blended pixel: 0x%8.8" SDL_PRIX32 "\n", P);
  60. SDL_DestroyTexture(A);
  61. SDL_DestroyTexture(B);
  62. blend_tested = SDL_TRUE;
  63. }
  64. SDL_RenderGetViewport(s->renderer, &viewport);
  65. target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
  66. SDL_SetTextureBlendMode(target, SDL_BLENDMODE_BLEND);
  67. SDL_SetRenderTarget(s->renderer, target);
  68. /* Draw the background.
  69. This is solid black so when the sprite is copied to it, any per-pixel alpha will be blended through.
  70. */
  71. SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
  72. SDL_RenderFillRect(s->renderer, NULL);
  73. /* Scale and draw the sprite */
  74. s->sprite_rect.w += s->scale_direction;
  75. s->sprite_rect.h += s->scale_direction;
  76. if (s->scale_direction > 0) {
  77. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  78. s->scale_direction = -1;
  79. }
  80. } else {
  81. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  82. s->scale_direction = 1;
  83. }
  84. }
  85. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  86. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  87. SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
  88. SDL_SetRenderTarget(s->renderer, NULL);
  89. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  90. SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_BLEND);
  91. SDL_SetRenderDrawColor(s->renderer, 0xff, 0x00, 0x00, 0x80);
  92. R.x = 0;
  93. R.y = 0;
  94. R.w = 100;
  95. R.h = 100;
  96. SDL_RenderFillRect(s->renderer, &R);
  97. SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_NONE);
  98. SDL_RenderCopy(s->renderer, target, NULL, NULL);
  99. SDL_DestroyTexture(target);
  100. /* Update the screen! */
  101. SDL_RenderPresent(s->renderer);
  102. return SDL_TRUE;
  103. }
  104. SDL_bool
  105. Draw(DrawState *s)
  106. {
  107. SDL_Rect viewport;
  108. SDL_Texture *target;
  109. SDL_RenderGetViewport(s->renderer, &viewport);
  110. target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
  111. if (!target) {
  112. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create render target texture: %s\n", SDL_GetError());
  113. return SDL_FALSE;
  114. }
  115. SDL_SetRenderTarget(s->renderer, target);
  116. /* Draw the background */
  117. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  118. /* Scale and draw the sprite */
  119. s->sprite_rect.w += s->scale_direction;
  120. s->sprite_rect.h += s->scale_direction;
  121. if (s->scale_direction > 0) {
  122. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  123. s->scale_direction = -1;
  124. }
  125. } else {
  126. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  127. s->scale_direction = 1;
  128. }
  129. }
  130. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  131. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  132. SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
  133. SDL_SetRenderTarget(s->renderer, NULL);
  134. SDL_RenderCopy(s->renderer, target, NULL, NULL);
  135. SDL_DestroyTexture(target);
  136. /* Update the screen! */
  137. SDL_RenderPresent(s->renderer);
  138. return SDL_TRUE;
  139. }
  140. void
  141. loop()
  142. {
  143. int i;
  144. SDL_Event event;
  145. /* Check for events */
  146. while (SDL_PollEvent(&event)) {
  147. SDLTest_CommonEvent(state, &event, &done);
  148. }
  149. for (i = 0; i < state->num_windows; ++i) {
  150. if (state->windows[i] == NULL)
  151. continue;
  152. if (test_composite) {
  153. if (!DrawComposite(&drawstates[i])) done = 1;
  154. } else {
  155. if (!Draw(&drawstates[i])) done = 1;
  156. }
  157. }
  158. #ifdef __EMSCRIPTEN__
  159. if (done) {
  160. emscripten_cancel_main_loop();
  161. }
  162. #endif
  163. }
  164. int
  165. main(int argc, char *argv[])
  166. {
  167. int i;
  168. int frames;
  169. Uint32 then, now;
  170. /* Enable standard application logging */
  171. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  172. /* Initialize test framework */
  173. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  174. if (!state) {
  175. return 1;
  176. }
  177. for (i = 1; i < argc;) {
  178. int consumed;
  179. consumed = SDLTest_CommonArg(state, i);
  180. if (consumed == 0) {
  181. consumed = -1;
  182. if (SDL_strcasecmp(argv[i], "--composite") == 0) {
  183. test_composite = SDL_TRUE;
  184. consumed = 1;
  185. }
  186. }
  187. if (consumed < 0) {
  188. static const char *options[] = { "[--composite]", NULL };
  189. SDLTest_CommonLogUsage(state, argv[0], options);
  190. quit(1);
  191. }
  192. i += consumed;
  193. }
  194. if (!SDLTest_CommonInit(state)) {
  195. quit(2);
  196. }
  197. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  198. for (i = 0; i < state->num_windows; ++i) {
  199. DrawState *drawstate = &drawstates[i];
  200. drawstate->window = state->windows[i];
  201. drawstate->renderer = state->renderers[i];
  202. if (test_composite) {
  203. drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", SDL_TRUE, NULL, NULL);
  204. } else {
  205. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
  206. }
  207. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL);
  208. if (!drawstate->sprite || !drawstate->background) {
  209. quit(2);
  210. }
  211. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  212. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  213. drawstate->scale_direction = 1;
  214. }
  215. /* Main render loop */
  216. frames = 0;
  217. then = SDL_GetTicks();
  218. done = 0;
  219. #ifdef __EMSCRIPTEN__
  220. emscripten_set_main_loop(loop, 0, 1);
  221. #else
  222. while (!done) {
  223. ++frames;
  224. loop();
  225. }
  226. #endif
  227. /* Print out some timing information */
  228. now = SDL_GetTicks();
  229. if (now > then) {
  230. double fps = ((double) frames * 1000) / (now - then);
  231. SDL_Log("%2.2f frames per second\n", fps);
  232. }
  233. SDL_stack_free(drawstates);
  234. quit(0);
  235. return 0;
  236. }
  237. /* vi: set ts=4 sw=4 expandtab: */