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

305 lines
8.9 KiB

  1. /*
  2. Copyright (C) 1997-2019 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. int done;
  28. void
  29. DrawPoints(SDL_Renderer * renderer)
  30. {
  31. int i;
  32. int x, y;
  33. SDL_Rect viewport;
  34. /* Query the sizes */
  35. SDL_RenderGetViewport(renderer, &viewport);
  36. for (i = 0; i < num_objects * 4; ++i) {
  37. /* Cycle the color and alpha, if desired */
  38. if (cycle_color) {
  39. current_color += cycle_direction;
  40. if (current_color < 0) {
  41. current_color = 0;
  42. cycle_direction = -cycle_direction;
  43. }
  44. if (current_color > 255) {
  45. current_color = 255;
  46. cycle_direction = -cycle_direction;
  47. }
  48. }
  49. if (cycle_alpha) {
  50. current_alpha += cycle_direction;
  51. if (current_alpha < 0) {
  52. current_alpha = 0;
  53. cycle_direction = -cycle_direction;
  54. }
  55. if (current_alpha > 255) {
  56. current_alpha = 255;
  57. cycle_direction = -cycle_direction;
  58. }
  59. }
  60. SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
  61. (Uint8) current_color, (Uint8) current_alpha);
  62. x = rand() % viewport.w;
  63. y = rand() % viewport.h;
  64. SDL_RenderDrawPoint(renderer, x, y);
  65. }
  66. }
  67. void
  68. 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
  116. DrawRects(SDL_Renderer * renderer)
  117. {
  118. int i;
  119. SDL_Rect rect;
  120. SDL_Rect viewport;
  121. /* Query the sizes */
  122. SDL_RenderGetViewport(renderer, &viewport);
  123. for (i = 0; i < num_objects / 4; ++i) {
  124. /* Cycle the color and alpha, if desired */
  125. if (cycle_color) {
  126. current_color += cycle_direction;
  127. if (current_color < 0) {
  128. current_color = 0;
  129. cycle_direction = -cycle_direction;
  130. }
  131. if (current_color > 255) {
  132. current_color = 255;
  133. cycle_direction = -cycle_direction;
  134. }
  135. }
  136. if (cycle_alpha) {
  137. current_alpha += cycle_direction;
  138. if (current_alpha < 0) {
  139. current_alpha = 0;
  140. cycle_direction = -cycle_direction;
  141. }
  142. if (current_alpha > 255) {
  143. current_alpha = 255;
  144. cycle_direction = -cycle_direction;
  145. }
  146. }
  147. SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
  148. (Uint8) current_color, (Uint8) current_alpha);
  149. rect.w = rand() % (viewport.h / 2);
  150. rect.h = rand() % (viewport.h / 2);
  151. rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
  152. rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
  153. SDL_RenderFillRect(renderer, &rect);
  154. }
  155. }
  156. void
  157. loop()
  158. {
  159. int i;
  160. SDL_Event event;
  161. /* Check for events */
  162. while (SDL_PollEvent(&event)) {
  163. SDLTest_CommonEvent(state, &event, &done);
  164. }
  165. for (i = 0; i < state->num_windows; ++i) {
  166. SDL_Renderer *renderer = state->renderers[i];
  167. if (state->windows[i] == NULL)
  168. continue;
  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. }
  182. int
  183. main(int argc, char *argv[])
  184. {
  185. int i;
  186. Uint32 then, now, frames;
  187. /* Enable standard application logging */
  188. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  189. /* Initialize parameters */
  190. num_objects = NUM_OBJECTS;
  191. /* Initialize test framework */
  192. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  193. if (!state) {
  194. return 1;
  195. }
  196. for (i = 1; i < argc;) {
  197. int consumed;
  198. consumed = SDLTest_CommonArg(state, i);
  199. if (consumed == 0) {
  200. consumed = -1;
  201. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  202. if (argv[i + 1]) {
  203. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  204. blendMode = SDL_BLENDMODE_NONE;
  205. consumed = 2;
  206. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  207. blendMode = SDL_BLENDMODE_BLEND;
  208. consumed = 2;
  209. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  210. blendMode = SDL_BLENDMODE_ADD;
  211. consumed = 2;
  212. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  213. blendMode = SDL_BLENDMODE_MOD;
  214. consumed = 2;
  215. }
  216. }
  217. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  218. cycle_color = SDL_TRUE;
  219. consumed = 1;
  220. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  221. cycle_alpha = SDL_TRUE;
  222. consumed = 1;
  223. } else if (SDL_isdigit(*argv[i])) {
  224. num_objects = SDL_atoi(argv[i]);
  225. consumed = 1;
  226. }
  227. }
  228. if (consumed < 0) {
  229. static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", NULL };
  230. SDLTest_CommonLogUsage(state, argv[0], options);
  231. return 1;
  232. }
  233. i += consumed;
  234. }
  235. if (!SDLTest_CommonInit(state)) {
  236. return 2;
  237. }
  238. /* Create the windows and initialize the renderers */
  239. for (i = 0; i < state->num_windows; ++i) {
  240. SDL_Renderer *renderer = state->renderers[i];
  241. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  242. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  243. SDL_RenderClear(renderer);
  244. }
  245. srand((unsigned int)time(NULL));
  246. /* Main render loop */
  247. frames = 0;
  248. then = SDL_GetTicks();
  249. done = 0;
  250. #ifdef __EMSCRIPTEN__
  251. emscripten_set_main_loop(loop, 0, 1);
  252. #else
  253. while (!done) {
  254. ++frames;
  255. loop();
  256. }
  257. #endif
  258. SDLTest_CommonQuit(state);
  259. /* Print out some timing information */
  260. now = SDL_GetTicks();
  261. if (now > then) {
  262. double fps = ((double) frames * 1000) / (now - then);
  263. SDL_Log("%2.2f frames per second\n", fps);
  264. }
  265. return 0;
  266. }
  267. /* vi: set ts=4 sw=4 expandtab: */