testviewport.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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: Check viewports */
  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.h"
  18. #include "SDL_test_common.h"
  19. static SDLTest_CommonState *state;
  20. SDL_Rect viewport;
  21. int done, j;
  22. SDL_bool use_target = SDL_FALSE;
  23. #ifdef __EMSCRIPTEN__
  24. Uint32 wait_start;
  25. #endif
  26. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  27. static void
  28. quit(int rc)
  29. {
  30. SDLTest_CommonQuit(state);
  31. exit(rc);
  32. }
  33. void
  34. DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
  35. {
  36. SDL_Rect rect;
  37. /* Set the viewport */
  38. SDL_RenderSetViewport(renderer, &viewport);
  39. /* Draw a gray background */
  40. SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
  41. SDL_RenderClear(renderer);
  42. /* Test inside points */
  43. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
  44. SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
  45. SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
  46. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - 10);
  47. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + 10);
  48. /* Test horizontal and vertical lines */
  49. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  50. SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
  51. SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
  52. SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
  53. SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
  54. /* Test diagonal lines */
  55. SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
  56. SDL_RenderDrawLine(renderer, 0, 0,
  57. viewport.w-1, viewport.h-1);
  58. SDL_RenderDrawLine(renderer, viewport.w-1, 0,
  59. 0, viewport.h-1);
  60. /* Test outside points */
  61. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
  62. SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
  63. SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
  64. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - viewport.w);
  65. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + viewport.w);
  66. /* Add a box at the top */
  67. rect.w = 8;
  68. rect.h = 8;
  69. rect.x = (viewport.w - rect.w) / 2;
  70. rect.y = 0;
  71. SDL_RenderFillRect(renderer, &rect);
  72. }
  73. void
  74. loop()
  75. {
  76. #ifdef __EMSCRIPTEN__
  77. /* Avoid using delays */
  78. if(SDL_GetTicks() - wait_start < 1000)
  79. return;
  80. wait_start = SDL_GetTicks();
  81. #endif
  82. SDL_Event event;
  83. int i;
  84. /* Check for events */
  85. while (SDL_PollEvent(&event)) {
  86. SDLTest_CommonEvent(state, &event, &done);
  87. }
  88. /* Move a viewport box in steps around the screen */
  89. viewport.x = j * 100;
  90. viewport.y = viewport.x;
  91. viewport.w = 100 + j * 50;
  92. viewport.h = 100 + j * 50;
  93. j = (j + 1) % 4;
  94. SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
  95. for (i = 0; i < state->num_windows; ++i) {
  96. if (state->windows[i] == NULL)
  97. continue;
  98. /* Draw using viewport */
  99. DrawOnViewport(state->renderers[i], viewport);
  100. /* Update the screen! */
  101. if (use_target) {
  102. SDL_SetRenderTarget(state->renderers[i], NULL);
  103. SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
  104. SDL_RenderPresent(state->renderers[i]);
  105. SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
  106. } else {
  107. SDL_RenderPresent(state->renderers[i]);
  108. }
  109. }
  110. #ifdef __EMSCRIPTEN__
  111. if (done) {
  112. emscripten_cancel_main_loop();
  113. }
  114. #endif
  115. }
  116. int
  117. main(int argc, char *argv[])
  118. {
  119. int i;
  120. Uint32 then, now, frames;
  121. /* Initialize test framework */
  122. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  123. if (!state) {
  124. return 1;
  125. }
  126. for (i = 1; i < argc;) {
  127. int consumed;
  128. consumed = SDLTest_CommonArg(state, i);
  129. if (consumed == 0) {
  130. consumed = -1;
  131. if (SDL_strcasecmp(argv[i], "--target") == 0) {
  132. use_target = SDL_TRUE;
  133. consumed = 1;
  134. }
  135. }
  136. if (consumed < 0) {
  137. static const char *options[] = { "[--target]", NULL };
  138. SDLTest_CommonLogUsage(state, argv[0], options);
  139. quit(1);
  140. }
  141. i += consumed;
  142. }
  143. if (!SDLTest_CommonInit(state)) {
  144. quit(2);
  145. }
  146. if (use_target) {
  147. int w, h;
  148. for (i = 0; i < state->num_windows; ++i) {
  149. SDL_GetWindowSize(state->windows[i], &w, &h);
  150. state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
  151. SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
  152. }
  153. }
  154. for (i = 0; i < state->num_windows; ++i) {
  155. SDL_Renderer *renderer = state->renderers[i];
  156. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  157. SDL_RenderClear(renderer);
  158. }
  159. /* Main render loop */
  160. frames = 0;
  161. then = SDL_GetTicks();
  162. done = 0;
  163. j = 0;
  164. #ifdef __EMSCRIPTEN__
  165. wait_start = SDL_GetTicks();
  166. emscripten_set_main_loop(loop, 0, 1);
  167. #else
  168. while (!done) {
  169. ++frames;
  170. loop();
  171. SDL_Delay(1000);
  172. }
  173. #endif
  174. /* Print out some timing information */
  175. now = SDL_GetTicks();
  176. if (now > then) {
  177. double fps = ((double) frames * 1000) / (now - then);
  178. SDL_Log("%2.2f frames per second\n", fps);
  179. }
  180. quit(0);
  181. return 0;
  182. }
  183. /* vi: set ts=4 sw=4 expandtab: */