testrendertarget.c 9.5 KB

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