testdraw2.c 9.3 KB

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