testrendercopyex.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  30. static void
  31. quit(int rc)
  32. {
  33. SDLTest_CommonQuit(state);
  34. exit(rc);
  35. }
  36. SDL_Texture *
  37. LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
  38. {
  39. SDL_Surface *temp;
  40. SDL_Texture *texture;
  41. /* Load the sprite image */
  42. temp = SDL_LoadBMP(file);
  43. if (temp == NULL) {
  44. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  45. return NULL;
  46. }
  47. /* Set transparent pixel as the pixel at (0,0) */
  48. if (transparent) {
  49. if (temp->format->palette) {
  50. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  51. } else {
  52. switch (temp->format->BitsPerPixel) {
  53. case 15:
  54. SDL_SetColorKey(temp, SDL_TRUE,
  55. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  56. break;
  57. case 16:
  58. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  59. break;
  60. case 24:
  61. SDL_SetColorKey(temp, SDL_TRUE,
  62. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  63. break;
  64. case 32:
  65. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  66. break;
  67. }
  68. }
  69. }
  70. /* Create textures from the image */
  71. texture = SDL_CreateTextureFromSurface(renderer, temp);
  72. if (!texture) {
  73. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  74. SDL_FreeSurface(temp);
  75. return NULL;
  76. }
  77. SDL_FreeSurface(temp);
  78. /* We're ready to roll. :) */
  79. return texture;
  80. }
  81. void
  82. Draw(DrawState *s)
  83. {
  84. SDL_Rect viewport;
  85. SDL_Texture *target;
  86. SDL_Point *center=NULL;
  87. SDL_Point origin = {0,0};
  88. SDL_RenderGetViewport(s->renderer, &viewport);
  89. target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
  90. SDL_SetRenderTarget(s->renderer, target);
  91. /* Draw the background */
  92. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  93. /* Scale and draw the sprite */
  94. s->sprite_rect.w += s->scale_direction;
  95. s->sprite_rect.h += s->scale_direction;
  96. if (s->scale_direction > 0) {
  97. center = &origin;
  98. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  99. s->scale_direction = -1;
  100. }
  101. } else {
  102. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  103. s->scale_direction = 1;
  104. }
  105. }
  106. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  107. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  108. SDL_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, (SDL_RendererFlip)s->scale_direction);
  109. SDL_SetRenderTarget(s->renderer, NULL);
  110. SDL_RenderCopy(s->renderer, target, NULL, NULL);
  111. SDL_DestroyTexture(target);
  112. /* Update the screen! */
  113. SDL_RenderPresent(s->renderer);
  114. /* SDL_Delay(10); */
  115. }
  116. void loop()
  117. {
  118. int i;
  119. SDL_Event event;
  120. /* Check for events */
  121. while (SDL_PollEvent(&event)) {
  122. SDLTest_CommonEvent(state, &event, &done);
  123. }
  124. for (i = 0; i < state->num_windows; ++i) {
  125. if (state->windows[i] == NULL)
  126. continue;
  127. Draw(&drawstates[i]);
  128. }
  129. #ifdef __EMSCRIPTEN__
  130. if (done) {
  131. emscripten_cancel_main_loop();
  132. }
  133. #endif
  134. }
  135. int
  136. main(int argc, char *argv[])
  137. {
  138. int i;
  139. int frames;
  140. Uint32 then, now;
  141. /* Enable standard application logging */
  142. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  143. /* Initialize test framework */
  144. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  145. if (!state) {
  146. return 1;
  147. }
  148. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  149. SDLTest_CommonQuit(state);
  150. return 1;
  151. }
  152. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  153. for (i = 0; i < state->num_windows; ++i) {
  154. DrawState *drawstate = &drawstates[i];
  155. drawstate->window = state->windows[i];
  156. drawstate->renderer = state->renderers[i];
  157. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
  158. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
  159. if (!drawstate->sprite || !drawstate->background) {
  160. quit(2);
  161. }
  162. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  163. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  164. drawstate->scale_direction = 1;
  165. }
  166. /* Main render loop */
  167. frames = 0;
  168. then = SDL_GetTicks();
  169. done = 0;
  170. #ifdef __EMSCRIPTEN__
  171. emscripten_set_main_loop(loop, 0, 1);
  172. #else
  173. while (!done) {
  174. ++frames;
  175. loop();
  176. }
  177. #endif
  178. /* Print out some timing information */
  179. now = SDL_GetTicks();
  180. if (now > then) {
  181. double fps = ((double) frames * 1000) / (now - then);
  182. SDL_Log("%2.2f frames per second\n", fps);
  183. }
  184. SDL_stack_free(drawstates);
  185. quit(0);
  186. return 0;
  187. }
  188. /* vi: set ts=4 sw=4 expandtab: */