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

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