testrendertarget.c 9.3 KB

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