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

311 lines
9.3 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 as many random objects on the screen as possible */
  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. #define NUM_OBJECTS 100
  19. static SDLTest_CommonState *state;
  20. static int num_objects;
  21. static SDL_bool cycle_color;
  22. static SDL_bool cycle_alpha;
  23. static int cycle_direction = 1;
  24. static int current_alpha = 255;
  25. static int current_color = 255;
  26. static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  27. static Uint32 next_fps_check, frames;
  28. static const Uint32 fps_check_delay = 5000;
  29. int done;
  30. void DrawPoints(SDL_Renderer *renderer)
  31. {
  32. int i;
  33. int x, y;
  34. SDL_Rect viewport;
  35. /* Query the sizes */
  36. SDL_RenderGetViewport(renderer, &viewport);
  37. for (i = 0; i < num_objects * 4; ++i) {
  38. /* Cycle the color and alpha, if desired */
  39. if (cycle_color) {
  40. current_color += cycle_direction;
  41. if (current_color < 0) {
  42. current_color = 0;
  43. cycle_direction = -cycle_direction;
  44. }
  45. if (current_color > 255) {
  46. current_color = 255;
  47. cycle_direction = -cycle_direction;
  48. }
  49. }
  50. if (cycle_alpha) {
  51. current_alpha += cycle_direction;
  52. if (current_alpha < 0) {
  53. current_alpha = 0;
  54. cycle_direction = -cycle_direction;
  55. }
  56. if (current_alpha > 255) {
  57. current_alpha = 255;
  58. cycle_direction = -cycle_direction;
  59. }
  60. }
  61. SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
  62. (Uint8)current_color, (Uint8)current_alpha);
  63. x = rand() % viewport.w;
  64. y = rand() % viewport.h;
  65. SDL_RenderDrawPoint(renderer, x, y);
  66. }
  67. }
  68. void DrawLines(SDL_Renderer *renderer)
  69. {
  70. int i;
  71. int x1, y1, x2, y2;
  72. SDL_Rect viewport;
  73. /* Query the sizes */
  74. SDL_RenderGetViewport(renderer, &viewport);
  75. for (i = 0; i < num_objects; ++i) {
  76. /* Cycle the color and alpha, if desired */
  77. if (cycle_color) {
  78. current_color += cycle_direction;
  79. if (current_color < 0) {
  80. current_color = 0;
  81. cycle_direction = -cycle_direction;
  82. }
  83. if (current_color > 255) {
  84. current_color = 255;
  85. cycle_direction = -cycle_direction;
  86. }
  87. }
  88. if (cycle_alpha) {
  89. current_alpha += cycle_direction;
  90. if (current_alpha < 0) {
  91. current_alpha = 0;
  92. cycle_direction = -cycle_direction;
  93. }
  94. if (current_alpha > 255) {
  95. current_alpha = 255;
  96. cycle_direction = -cycle_direction;
  97. }
  98. }
  99. SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
  100. (Uint8)current_color, (Uint8)current_alpha);
  101. if (i == 0) {
  102. SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
  103. SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
  104. SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
  105. SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
  106. } else {
  107. x1 = (rand() % (viewport.w * 2)) - viewport.w;
  108. x2 = (rand() % (viewport.w * 2)) - viewport.w;
  109. y1 = (rand() % (viewport.h * 2)) - viewport.h;
  110. y2 = (rand() % (viewport.h * 2)) - viewport.h;
  111. SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  112. }
  113. }
  114. }
  115. void DrawRects(SDL_Renderer *renderer)
  116. {
  117. int i;
  118. SDL_Rect rect;
  119. SDL_Rect viewport;
  120. /* Query the sizes */
  121. SDL_RenderGetViewport(renderer, &viewport);
  122. for (i = 0; i < num_objects / 4; ++i) {
  123. /* Cycle the color and alpha, if desired */
  124. if (cycle_color) {
  125. current_color += cycle_direction;
  126. if (current_color < 0) {
  127. current_color = 0;
  128. cycle_direction = -cycle_direction;
  129. }
  130. if (current_color > 255) {
  131. current_color = 255;
  132. cycle_direction = -cycle_direction;
  133. }
  134. }
  135. if (cycle_alpha) {
  136. current_alpha += cycle_direction;
  137. if (current_alpha < 0) {
  138. current_alpha = 0;
  139. cycle_direction = -cycle_direction;
  140. }
  141. if (current_alpha > 255) {
  142. current_alpha = 255;
  143. cycle_direction = -cycle_direction;
  144. }
  145. }
  146. SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
  147. (Uint8)current_color, (Uint8)current_alpha);
  148. rect.w = rand() % (viewport.h / 2);
  149. rect.h = rand() % (viewport.h / 2);
  150. rect.x = (rand() % (viewport.w * 2) - viewport.w) - (rect.w / 2);
  151. rect.y = (rand() % (viewport.h * 2) - viewport.h) - (rect.h / 2);
  152. SDL_RenderFillRect(renderer, &rect);
  153. }
  154. }
  155. void loop()
  156. {
  157. Uint32 now;
  158. int i;
  159. SDL_Event event;
  160. /* Check for events */
  161. while (SDL_PollEvent(&event)) {
  162. SDLTest_CommonEvent(state, &event, &done);
  163. }
  164. for (i = 0; i < state->num_windows; ++i) {
  165. SDL_Renderer *renderer = state->renderers[i];
  166. if (state->windows[i] == NULL) {
  167. continue;
  168. }
  169. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  170. SDL_RenderClear(renderer);
  171. DrawRects(renderer);
  172. DrawLines(renderer);
  173. DrawPoints(renderer);
  174. SDL_RenderPresent(renderer);
  175. }
  176. #ifdef __EMSCRIPTEN__
  177. if (done) {
  178. emscripten_cancel_main_loop();
  179. }
  180. #endif
  181. frames++;
  182. now = SDL_GetTicks();
  183. if (SDL_TICKS_PASSED(now, next_fps_check)) {
  184. /* Print out some timing information */
  185. const Uint32 then = next_fps_check - fps_check_delay;
  186. const double fps = ((double)frames * 1000) / (now - then);
  187. SDL_Log("%2.2f frames per second\n", fps);
  188. next_fps_check = now + fps_check_delay;
  189. frames = 0;
  190. }
  191. }
  192. int main(int argc, char *argv[])
  193. {
  194. int i;
  195. /* Enable standard application logging */
  196. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  197. /* Initialize parameters */
  198. num_objects = NUM_OBJECTS;
  199. /* Initialize test framework */
  200. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  201. if (state == NULL) {
  202. return 1;
  203. }
  204. for (i = 1; i < argc;) {
  205. int consumed;
  206. consumed = SDLTest_CommonArg(state, i);
  207. if (consumed == 0) {
  208. consumed = -1;
  209. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  210. if (argv[i + 1]) {
  211. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  212. blendMode = SDL_BLENDMODE_NONE;
  213. consumed = 2;
  214. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  215. blendMode = SDL_BLENDMODE_BLEND;
  216. consumed = 2;
  217. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  218. blendMode = SDL_BLENDMODE_ADD;
  219. consumed = 2;
  220. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  221. blendMode = SDL_BLENDMODE_MOD;
  222. consumed = 2;
  223. }
  224. }
  225. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  226. cycle_color = SDL_TRUE;
  227. consumed = 1;
  228. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  229. cycle_alpha = SDL_TRUE;
  230. consumed = 1;
  231. } else if (SDL_isdigit(*argv[i])) {
  232. num_objects = SDL_atoi(argv[i]);
  233. consumed = 1;
  234. }
  235. }
  236. if (consumed < 0) {
  237. static const char *options[] = {
  238. "[--blend none|blend|add|mod]",
  239. "[--cyclecolor]",
  240. "[--cyclealpha]",
  241. "[num_objects]",
  242. NULL
  243. };
  244. SDLTest_CommonLogUsage(state, argv[0], options);
  245. return 1;
  246. }
  247. i += consumed;
  248. }
  249. if (!SDLTest_CommonInit(state)) {
  250. return 2;
  251. }
  252. /* Create the windows and initialize the renderers */
  253. for (i = 0; i < state->num_windows; ++i) {
  254. SDL_Renderer *renderer = state->renderers[i];
  255. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  256. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  257. SDL_RenderClear(renderer);
  258. }
  259. srand((unsigned int)time(NULL));
  260. /* Main render loop */
  261. frames = 0;
  262. next_fps_check = SDL_GetTicks() + fps_check_delay;
  263. done = 0;
  264. #ifdef __EMSCRIPTEN__
  265. emscripten_set_main_loop(loop, 0, 1);
  266. #else
  267. while (!done) {
  268. loop();
  269. }
  270. #endif
  271. SDLTest_CommonQuit(state);
  272. return 0;
  273. }
  274. /* vi: set ts=4 sw=4 expandtab: */