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

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