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

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