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

279 lines
7.8 KiB

  1. /*
  2. Copyright (C) 1997-2022 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. static SDL_Rect viewport;
  21. static int done, j;
  22. static SDL_bool use_target = SDL_FALSE;
  23. #ifdef __EMSCRIPTEN__
  24. static Uint32 wait_start;
  25. #endif
  26. static SDL_Texture *sprite;
  27. static int sprite_w, sprite_h;
  28. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  29. static void
  30. quit(int rc)
  31. {
  32. SDLTest_CommonQuit(state);
  33. exit(rc);
  34. }
  35. int
  36. LoadSprite(const char *file, SDL_Renderer *renderer)
  37. {
  38. SDL_Surface *temp;
  39. /* Load the sprite image */
  40. temp = SDL_LoadBMP(file);
  41. if (temp == NULL) {
  42. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
  43. return (-1);
  44. }
  45. sprite_w = temp->w;
  46. sprite_h = temp->h;
  47. /* Set transparent pixel as the pixel at (0,0) */
  48. if (temp->format->palette) {
  49. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  50. } else {
  51. switch (temp->format->BitsPerPixel) {
  52. case 15:
  53. SDL_SetColorKey(temp, SDL_TRUE,
  54. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  55. break;
  56. case 16:
  57. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  58. break;
  59. case 24:
  60. SDL_SetColorKey(temp, SDL_TRUE,
  61. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  62. break;
  63. case 32:
  64. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  65. break;
  66. }
  67. }
  68. /* Create textures from the image */
  69. sprite = SDL_CreateTextureFromSurface(renderer, temp);
  70. if (!sprite) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  72. SDL_FreeSurface(temp);
  73. return (-1);
  74. }
  75. SDL_FreeSurface(temp);
  76. /* We're ready to roll. :) */
  77. return (0);
  78. }
  79. void
  80. DrawOnViewport(SDL_Renderer * renderer)
  81. {
  82. SDL_Rect rect;
  83. /* Set the viewport */
  84. SDL_RenderSetViewport(renderer, &viewport);
  85. /* Draw a gray background */
  86. SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
  87. SDL_RenderClear(renderer);
  88. /* Test inside points */
  89. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
  90. SDL_RenderDrawPoint(renderer, viewport.h/2 + 20, viewport.w/2);
  91. SDL_RenderDrawPoint(renderer, viewport.h/2 - 20, viewport.w/2);
  92. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - 20);
  93. SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + 20);
  94. /* Test horizontal and vertical lines */
  95. SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
  96. SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
  97. SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
  98. SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
  99. SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
  100. /* Test diagonal lines */
  101. SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
  102. SDL_RenderDrawLine(renderer, 0, 0, viewport.w-1, viewport.h-1);
  103. SDL_RenderDrawLine(renderer, viewport.w-1, 0, 0, viewport.h-1);
  104. /* Test outside points */
  105. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
  106. SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
  107. SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
  108. SDL_RenderDrawPoint(renderer, viewport.h/2, viewport.w/2 - viewport.w);
  109. SDL_RenderDrawPoint(renderer, viewport.h/2, viewport.w/2 + viewport.w);
  110. /* Add a box at the top */
  111. rect.w = 8;
  112. rect.h = 8;
  113. rect.x = (viewport.w - rect.w) / 2;
  114. rect.y = 0;
  115. SDL_RenderFillRect(renderer, &rect);
  116. /* Add a clip rect and fill it with the sprite */
  117. SDL_QueryTexture(sprite, NULL, NULL, &rect.w, &rect.h);
  118. rect.x = (viewport.w - rect.w) / 2;
  119. rect.y = (viewport.h - rect.h) / 2;
  120. SDL_RenderSetClipRect(renderer, &rect);
  121. SDL_RenderCopy(renderer, sprite, NULL, &rect);
  122. SDL_RenderSetClipRect(renderer, NULL);
  123. }
  124. void
  125. loop()
  126. {
  127. #ifdef __EMSCRIPTEN__
  128. /* Avoid using delays */
  129. if(SDL_GetTicks() - wait_start < 1000)
  130. return;
  131. wait_start = SDL_GetTicks();
  132. #endif
  133. SDL_Event event;
  134. int i;
  135. /* Check for events */
  136. while (SDL_PollEvent(&event)) {
  137. SDLTest_CommonEvent(state, &event, &done);
  138. }
  139. /* Move a viewport box in steps around the screen */
  140. viewport.x = j * 100;
  141. viewport.y = viewport.x;
  142. viewport.w = 100 + j * 50;
  143. viewport.h = 100 + j * 50;
  144. j = (j + 1) % 4;
  145. SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
  146. for (i = 0; i < state->num_windows; ++i) {
  147. if (state->windows[i] == NULL)
  148. continue;
  149. /* Draw using viewport */
  150. DrawOnViewport(state->renderers[i]);
  151. /* Update the screen! */
  152. if (use_target) {
  153. SDL_SetRenderTarget(state->renderers[i], NULL);
  154. SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
  155. SDL_RenderPresent(state->renderers[i]);
  156. SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
  157. } else {
  158. SDL_RenderPresent(state->renderers[i]);
  159. }
  160. }
  161. #ifdef __EMSCRIPTEN__
  162. if (done) {
  163. emscripten_cancel_main_loop();
  164. }
  165. #endif
  166. }
  167. int
  168. main(int argc, char *argv[])
  169. {
  170. int i;
  171. Uint32 then, now, frames;
  172. /* Initialize test framework */
  173. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  174. if (!state) {
  175. return 1;
  176. }
  177. for (i = 1; i < argc;) {
  178. int consumed;
  179. consumed = SDLTest_CommonArg(state, i);
  180. if (consumed == 0) {
  181. consumed = -1;
  182. if (SDL_strcasecmp(argv[i], "--target") == 0) {
  183. use_target = SDL_TRUE;
  184. consumed = 1;
  185. }
  186. }
  187. if (consumed < 0) {
  188. static const char *options[] = { "[--target]", NULL };
  189. SDLTest_CommonLogUsage(state, argv[0], options);
  190. quit(1);
  191. }
  192. i += consumed;
  193. }
  194. if (!SDLTest_CommonInit(state)) {
  195. quit(2);
  196. }
  197. if (LoadSprite("icon.bmp", state->renderers[0]) < 0) {
  198. quit(2);
  199. }
  200. if (use_target) {
  201. int w, h;
  202. for (i = 0; i < state->num_windows; ++i) {
  203. SDL_GetWindowSize(state->windows[i], &w, &h);
  204. state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
  205. SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
  206. }
  207. }
  208. for (i = 0; i < state->num_windows; ++i) {
  209. SDL_Renderer *renderer = state->renderers[i];
  210. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  211. SDL_RenderClear(renderer);
  212. }
  213. /* Main render loop */
  214. frames = 0;
  215. then = SDL_GetTicks();
  216. done = 0;
  217. j = 0;
  218. #ifdef __EMSCRIPTEN__
  219. wait_start = SDL_GetTicks();
  220. emscripten_set_main_loop(loop, 0, 1);
  221. #else
  222. while (!done) {
  223. ++frames;
  224. loop();
  225. SDL_Delay(1000);
  226. }
  227. #endif
  228. /* Print out some timing information */
  229. now = SDL_GetTicks();
  230. if (now > then) {
  231. double fps = ((double) frames * 1000) / (now - then);
  232. SDL_Log("%2.2f frames per second\n", fps);
  233. }
  234. quit(0);
  235. return 0;
  236. }
  237. /* vi: set ts=4 sw=4 expandtab: */