testgeometry.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Copyright (C) 1997-2025 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: 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 sprite_w, sprite_h;
  25. static int translate_cx = 0;
  26. static int translate_cy = 0;
  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_LoadBMP step repeatedly, but that's OK for test code. */
  44. sprites[i] = LoadTexture(state->renderers[i], file, true, &sprite_w, &sprite_h);
  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. SDLTest_CommonEvent(state, &event, &done);
  93. }
  94. } else {
  95. SDLTest_CommonEvent(state, &event, &done);
  96. }
  97. }
  98. for (i = 0; i < state->num_windows; ++i) {
  99. SDL_Renderer *renderer = state->renderers[i];
  100. if (state->windows[i] == NULL) {
  101. continue;
  102. }
  103. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  104. SDL_RenderClear(renderer);
  105. {
  106. SDL_Rect viewport;
  107. SDL_Vertex verts[3];
  108. float a;
  109. float d;
  110. int cx, cy;
  111. /* Query the sizes */
  112. SDL_GetRenderViewport(renderer, &viewport);
  113. SDL_zeroa(verts);
  114. cx = viewport.x + viewport.w / 2;
  115. cy = viewport.y + viewport.h / 2;
  116. d = (viewport.w + viewport.h) / 5.f;
  117. cx += translate_cx;
  118. cy += translate_cy;
  119. a = (angle * SDL_PI_F) / 180.0f;
  120. verts[0].position.x = cx + d * SDL_cosf(a);
  121. verts[0].position.y = cy + d * SDL_sinf(a);
  122. verts[0].color.r = 1.0f;
  123. verts[0].color.g = 0;
  124. verts[0].color.b = 0;
  125. verts[0].color.a = 1.0f;
  126. a = ((angle + 120) * SDL_PI_F) / 180.0f;
  127. verts[1].position.x = cx + d * SDL_cosf(a);
  128. verts[1].position.y = cy + d * SDL_sinf(a);
  129. verts[1].color.r = 0;
  130. verts[1].color.g = 1.0f;
  131. verts[1].color.b = 0;
  132. verts[1].color.a = 1.0f;
  133. a = ((angle + 240) * SDL_PI_F) / 180.0f;
  134. verts[2].position.x = cx + d * SDL_cosf(a);
  135. verts[2].position.y = cy + d * SDL_sinf(a);
  136. verts[2].color.r = 0;
  137. verts[2].color.g = 0;
  138. verts[2].color.b = 1.0f;
  139. verts[2].color.a = 1.0f;
  140. if (use_texture) {
  141. verts[0].tex_coord.x = 0.5f;
  142. verts[0].tex_coord.y = 0.0f;
  143. verts[1].tex_coord.x = 1.0f;
  144. verts[1].tex_coord.y = 1.0f;
  145. verts[2].tex_coord.x = 0.0f;
  146. verts[2].tex_coord.y = 1.0f;
  147. }
  148. SDL_RenderGeometry(renderer, sprites[i], verts, 3, NULL, 0);
  149. }
  150. SDL_RenderPresent(renderer);
  151. }
  152. #ifdef SDL_PLATFORM_EMSCRIPTEN
  153. if (done) {
  154. emscripten_cancel_main_loop();
  155. }
  156. #endif
  157. }
  158. int main(int argc, char *argv[])
  159. {
  160. int i;
  161. const char *icon = "icon.bmp";
  162. Uint64 then, now;
  163. Uint32 frames;
  164. /* Initialize test framework */
  165. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  166. if (!state) {
  167. return 1;
  168. }
  169. for (i = 1; i < argc;) {
  170. int consumed;
  171. consumed = SDLTest_CommonArg(state, i);
  172. if (consumed == 0) {
  173. consumed = -1;
  174. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  175. if (argv[i + 1]) {
  176. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  177. blendMode = SDL_BLENDMODE_NONE;
  178. consumed = 2;
  179. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  180. blendMode = SDL_BLENDMODE_BLEND;
  181. consumed = 2;
  182. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  183. blendMode = SDL_BLENDMODE_ADD;
  184. consumed = 2;
  185. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  186. blendMode = SDL_BLENDMODE_MOD;
  187. consumed = 2;
  188. } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
  189. blendMode = SDL_BLENDMODE_MUL;
  190. consumed = 2;
  191. }
  192. }
  193. } else if (SDL_strcasecmp(argv[i], "--use-texture") == 0) {
  194. use_texture = true;
  195. consumed = 1;
  196. }
  197. }
  198. if (consumed < 0) {
  199. static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--use-texture]", NULL };
  200. SDLTest_CommonLogUsage(state, argv[0], options);
  201. return 1;
  202. }
  203. i += consumed;
  204. }
  205. if (!SDLTest_CommonInit(state)) {
  206. return 2;
  207. }
  208. /* Create the windows, initialize the renderers, and load the textures */
  209. sprites =
  210. (SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites));
  211. if (!sprites) {
  212. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!");
  213. quit(2);
  214. }
  215. /* Create the windows and initialize the renderers */
  216. for (i = 0; i < state->num_windows; ++i) {
  217. SDL_Renderer *renderer = state->renderers[i];
  218. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  219. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  220. SDL_RenderClear(renderer);
  221. sprites[i] = NULL;
  222. }
  223. if (use_texture) {
  224. if (LoadSprite(icon) < 0) {
  225. quit(2);
  226. }
  227. }
  228. /* Main render loop */
  229. frames = 0;
  230. then = SDL_GetTicks();
  231. done = 0;
  232. #ifdef SDL_PLATFORM_EMSCRIPTEN
  233. emscripten_set_main_loop(loop, 0, 1);
  234. #else
  235. while (!done) {
  236. ++frames;
  237. loop();
  238. }
  239. #endif
  240. /* Print out some timing information */
  241. now = SDL_GetTicks();
  242. if (now > then) {
  243. double fps = ((double)frames * 1000) / (now - then);
  244. SDL_Log("%2.2f frames per second", fps);
  245. }
  246. quit(0);
  247. return 0;
  248. }