testgeometry.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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: draw a RGB triangle, with texture */
  11. #include <stdlib.h>
  12. #include "testutils.h"
  13. #include <SDL3/SDL.h>
  14. #include <SDL3/SDL_main.h>
  15. #include <SDL3/SDL_test_common.h>
  16. #ifdef SDL_PLATFORM_EMSCRIPTEN
  17. #include <emscripten/emscripten.h>
  18. #endif
  19. static SDLTest_CommonState *state;
  20. static bool use_texture = false;
  21. static SDL_Texture **sprites;
  22. static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  23. static float angle = 0.0f;
  24. static int translate_cx = 0;
  25. static int translate_cy = 0;
  26. static float pinch_scale = 1.0f;
  27. static int done;
  28. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  29. static void
  30. quit(int rc)
  31. {
  32. SDL_free(sprites);
  33. SDLTest_CommonQuit(state);
  34. /* Let 'main()' return normally */
  35. if (rc != 0) {
  36. exit(rc);
  37. }
  38. }
  39. static int LoadSprite(const char *file)
  40. {
  41. int i;
  42. for (i = 0; i < state->num_windows; ++i) {
  43. /* This does the SDL_LoadPNG step repeatedly, but that's OK for test code. */
  44. sprites[i] = LoadTexture(state->renderers[i], file, true);
  45. if (!sprites[i]) {
  46. return -1;
  47. }
  48. if (!SDL_SetTextureBlendMode(sprites[i], blendMode)) {
  49. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s", SDL_GetError());
  50. SDL_DestroyTexture(sprites[i]);
  51. return -1;
  52. }
  53. }
  54. /* We're ready to roll. :) */
  55. return 0;
  56. }
  57. static void loop(void)
  58. {
  59. int i;
  60. SDL_Event event;
  61. /* Check for events */
  62. while (SDL_PollEvent(&event)) {
  63. if (event.type == SDL_EVENT_MOUSE_MOTION) {
  64. if (event.motion.state) {
  65. float xrel, yrel;
  66. int window_w, window_h;
  67. SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID);
  68. SDL_GetWindowSize(window, &window_w, &window_h);
  69. xrel = event.motion.xrel;
  70. yrel = event.motion.yrel;
  71. if (event.motion.y < (float)window_h / 2.0f) {
  72. angle += xrel;
  73. } else {
  74. angle -= xrel;
  75. }
  76. if (event.motion.x < (float)window_w / 2.0f) {
  77. angle -= yrel;
  78. } else {
  79. angle += yrel;
  80. }
  81. }
  82. } else if (event.type == SDL_EVENT_KEY_DOWN) {
  83. if (event.key.key == SDLK_LEFT) {
  84. translate_cx -= 1;
  85. } else if (event.key.key == SDLK_RIGHT) {
  86. translate_cx += 1;
  87. } else if (event.key.key == SDLK_UP) {
  88. translate_cy -= 1;
  89. } else if (event.key.key == SDLK_DOWN) {
  90. translate_cy += 1;
  91. } else {
  92. }
  93. } else if (event.type == SDL_EVENT_PINCH_BEGIN) {
  94. } else if (event.type == SDL_EVENT_PINCH_UPDATE) {
  95. pinch_scale *= event.pinch.scale;
  96. } else if (event.type == SDL_EVENT_PINCH_END) {
  97. }
  98. SDLTest_CommonEvent(state, &event, &done);
  99. }
  100. for (i = 0; i < state->num_windows; ++i) {
  101. SDL_Renderer *renderer = state->renderers[i];
  102. if (state->windows[i] == NULL) {
  103. continue;
  104. }
  105. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  106. SDL_RenderClear(renderer);
  107. {
  108. SDL_Rect viewport;
  109. SDL_Vertex verts[3];
  110. float a;
  111. float d;
  112. int cx, cy;
  113. /* Query the sizes */
  114. SDL_GetRenderViewport(renderer, &viewport);
  115. SDL_zeroa(verts);
  116. cx = viewport.x + viewport.w / 2;
  117. cy = viewport.y + viewport.h / 2;
  118. d = (viewport.w + viewport.h) / 5.f;
  119. cx += translate_cx;
  120. cy += translate_cy;
  121. a = (angle * SDL_PI_F) / 180.0f;
  122. verts[0].position.x = cx + (d * SDL_cosf(a)) * pinch_scale;
  123. verts[0].position.y = cy + (d * SDL_sinf(a)) * pinch_scale;
  124. verts[0].color.r = 1.0f;
  125. verts[0].color.g = 0;
  126. verts[0].color.b = 0;
  127. verts[0].color.a = 1.0f;
  128. a = ((angle + 120) * SDL_PI_F) / 180.0f;
  129. verts[1].position.x = cx + (d * SDL_cosf(a)) * pinch_scale;
  130. verts[1].position.y = cy + (d * SDL_sinf(a)) * pinch_scale;
  131. verts[1].color.r = 0;
  132. verts[1].color.g = 1.0f;
  133. verts[1].color.b = 0;
  134. verts[1].color.a = 1.0f;
  135. a = ((angle + 240) * SDL_PI_F) / 180.0f;
  136. verts[2].position.x = cx + (d * SDL_cosf(a)) * pinch_scale;
  137. verts[2].position.y = cy + (d * SDL_sinf(a)) * pinch_scale;
  138. verts[2].color.r = 0;
  139. verts[2].color.g = 0;
  140. verts[2].color.b = 1.0f;
  141. verts[2].color.a = 1.0f;
  142. if (use_texture) {
  143. verts[0].tex_coord.x = 0.5f;
  144. verts[0].tex_coord.y = 0.0f;
  145. verts[1].tex_coord.x = 1.0f;
  146. verts[1].tex_coord.y = 1.0f;
  147. verts[2].tex_coord.x = 0.0f;
  148. verts[2].tex_coord.y = 1.0f;
  149. }
  150. SDL_RenderGeometry(renderer, sprites[i], verts, 3, NULL, 0);
  151. }
  152. SDL_RenderPresent(renderer);
  153. }
  154. #ifdef SDL_PLATFORM_EMSCRIPTEN
  155. if (done) {
  156. emscripten_cancel_main_loop();
  157. }
  158. #endif
  159. }
  160. int main(int argc, char *argv[])
  161. {
  162. int i;
  163. const char *icon = "icon.png";
  164. Uint64 then, now;
  165. Uint32 frames;
  166. /* Initialize test framework */
  167. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  168. if (!state) {
  169. return 1;
  170. }
  171. for (i = 1; i < argc;) {
  172. int consumed;
  173. consumed = SDLTest_CommonArg(state, i);
  174. if (consumed == 0) {
  175. consumed = -1;
  176. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  177. if (argv[i + 1]) {
  178. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  179. blendMode = SDL_BLENDMODE_NONE;
  180. consumed = 2;
  181. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  182. blendMode = SDL_BLENDMODE_BLEND;
  183. consumed = 2;
  184. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  185. blendMode = SDL_BLENDMODE_ADD;
  186. consumed = 2;
  187. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  188. blendMode = SDL_BLENDMODE_MOD;
  189. consumed = 2;
  190. } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
  191. blendMode = SDL_BLENDMODE_MUL;
  192. consumed = 2;
  193. }
  194. }
  195. } else if (SDL_strcasecmp(argv[i], "--use-texture") == 0) {
  196. use_texture = true;
  197. consumed = 1;
  198. }
  199. }
  200. if (consumed < 0) {
  201. static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--use-texture]", NULL };
  202. SDLTest_CommonLogUsage(state, argv[0], options);
  203. return 1;
  204. }
  205. i += consumed;
  206. }
  207. if (!SDLTest_CommonInit(state)) {
  208. return 2;
  209. }
  210. /* Create the windows, initialize the renderers, and load the textures */
  211. sprites =
  212. (SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites));
  213. if (!sprites) {
  214. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  215. quit(2);
  216. }
  217. /* Create the windows and initialize the renderers */
  218. for (i = 0; i < state->num_windows; ++i) {
  219. SDL_Renderer *renderer = state->renderers[i];
  220. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  221. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  222. SDL_RenderClear(renderer);
  223. sprites[i] = NULL;
  224. }
  225. if (use_texture) {
  226. if (LoadSprite(icon) < 0) {
  227. quit(2);
  228. }
  229. }
  230. /* Main render loop */
  231. frames = 0;
  232. then = SDL_GetTicks();
  233. done = 0;
  234. #ifdef SDL_PLATFORM_EMSCRIPTEN
  235. emscripten_set_main_loop(loop, 0, 1);
  236. #else
  237. while (!done) {
  238. ++frames;
  239. loop();
  240. }
  241. #endif
  242. /* Print out some timing information */
  243. now = SDL_GetTicks();
  244. if (now > then) {
  245. double fps = ((double)frames * 1000) / (now - then);
  246. SDL_Log("%2.2f frames per second", fps);
  247. }
  248. quit(0);
  249. return 0;
  250. }