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

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