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

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