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

363 lines
9.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 SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
  19. #define NUM_OBJECTS 100
  20. static SDLTest_CommonState *state;
  21. static int num_objects;
  22. static SDL_bool cycle_color;
  23. static SDL_bool cycle_alpha;
  24. static int cycle_direction = 1;
  25. static int current_alpha = 255;
  26. static int current_color = 255;
  27. static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  28. int mouse_begin_x = -1, mouse_begin_y = -1;
  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. #define MAX_LINES 16
  70. int num_lines = 0;
  71. SDL_Rect lines[MAX_LINES];
  72. static int
  73. add_line(int x1, int y1, int x2, int y2)
  74. {
  75. if (num_lines >= MAX_LINES)
  76. return 0;
  77. if ((x1 == x2) && (y1 == y2))
  78. return 0;
  79. SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
  80. lines[num_lines].x = x1;
  81. lines[num_lines].y = y1;
  82. lines[num_lines].w = x2;
  83. lines[num_lines].h = y2;
  84. return ++num_lines;
  85. }
  86. void
  87. DrawLines(SDL_Renderer * renderer)
  88. {
  89. int i;
  90. SDL_Rect viewport;
  91. /* Query the sizes */
  92. SDL_RenderGetViewport(renderer, &viewport);
  93. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  94. for (i = 0; i < num_lines; ++i) {
  95. if (i == -1) {
  96. SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
  97. SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
  98. SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
  99. SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
  100. } else {
  101. SDL_RenderDrawLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
  102. }
  103. }
  104. }
  105. #define MAX_RECTS 16
  106. int num_rects = 0;
  107. SDL_Rect rects[MAX_RECTS];
  108. static int
  109. add_rect(int x1, int y1, int x2, int y2)
  110. {
  111. if (num_rects >= MAX_RECTS)
  112. return 0;
  113. if ((x1 == x2) || (y1 == y2))
  114. return 0;
  115. if (x1 > x2)
  116. SWAP(int, x1, x2);
  117. if (y1 > y2)
  118. SWAP(int, y1, y2);
  119. SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
  120. x2 - x1, y2 - y1);
  121. rects[num_rects].x = x1;
  122. rects[num_rects].y = y1;
  123. rects[num_rects].w = x2 - x1;
  124. rects[num_rects].h = y2 - y1;
  125. return ++num_rects;
  126. }
  127. static void
  128. DrawRects(SDL_Renderer * renderer)
  129. {
  130. SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
  131. SDL_RenderFillRects(renderer, rects, num_rects);
  132. }
  133. static void
  134. DrawRectLineIntersections(SDL_Renderer * renderer)
  135. {
  136. int i, j;
  137. SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
  138. for (i = 0; i < num_rects; i++)
  139. for (j = 0; j < num_lines; j++) {
  140. int x1, y1, x2, y2;
  141. SDL_Rect r;
  142. r = rects[i];
  143. x1 = lines[j].x;
  144. y1 = lines[j].y;
  145. x2 = lines[j].w;
  146. y2 = lines[j].h;
  147. if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
  148. SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
  149. }
  150. }
  151. }
  152. static void
  153. DrawRectRectIntersections(SDL_Renderer * renderer)
  154. {
  155. int i, j;
  156. SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
  157. for (i = 0; i < num_rects; i++)
  158. for (j = i + 1; j < num_rects; j++) {
  159. SDL_Rect r;
  160. if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
  161. SDL_RenderFillRect(renderer, &r);
  162. }
  163. }
  164. }
  165. void
  166. loop()
  167. {
  168. int i;
  169. SDL_Event event;
  170. /* Check for events */
  171. while (SDL_PollEvent(&event)) {
  172. SDLTest_CommonEvent(state, &event, &done);
  173. switch (event.type) {
  174. case SDL_MOUSEBUTTONDOWN:
  175. mouse_begin_x = event.button.x;
  176. mouse_begin_y = event.button.y;
  177. break;
  178. case SDL_MOUSEBUTTONUP:
  179. if (event.button.button == 3)
  180. add_line(mouse_begin_x, mouse_begin_y, event.button.x,
  181. event.button.y);
  182. if (event.button.button == 1)
  183. add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
  184. event.button.y);
  185. break;
  186. case SDL_KEYDOWN:
  187. switch (event.key.keysym.sym) {
  188. case 'l':
  189. if (event.key.keysym.mod & KMOD_SHIFT)
  190. num_lines = 0;
  191. else
  192. add_line(rand() % 640, rand() % 480, rand() % 640,
  193. rand() % 480);
  194. break;
  195. case 'r':
  196. if (event.key.keysym.mod & KMOD_SHIFT)
  197. num_rects = 0;
  198. else
  199. add_rect(rand() % 640, rand() % 480, rand() % 640,
  200. rand() % 480);
  201. break;
  202. }
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. for (i = 0; i < state->num_windows; ++i) {
  209. SDL_Renderer *renderer = state->renderers[i];
  210. if (state->windows[i] == NULL)
  211. continue;
  212. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  213. SDL_RenderClear(renderer);
  214. DrawRects(renderer);
  215. DrawPoints(renderer);
  216. DrawRectRectIntersections(renderer);
  217. DrawLines(renderer);
  218. DrawRectLineIntersections(renderer);
  219. SDL_RenderPresent(renderer);
  220. }
  221. #ifdef __EMSCRIPTEN__
  222. if (done) {
  223. emscripten_cancel_main_loop();
  224. }
  225. #endif
  226. }
  227. int
  228. main(int argc, char *argv[])
  229. {
  230. int i;
  231. Uint32 then, now, frames;
  232. /* Enable standard application logging */
  233. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  234. /* Initialize parameters */
  235. num_objects = NUM_OBJECTS;
  236. /* Initialize test framework */
  237. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  238. if (!state) {
  239. return 1;
  240. }
  241. for (i = 1; i < argc;) {
  242. int consumed;
  243. consumed = SDLTest_CommonArg(state, i);
  244. if (consumed == 0) {
  245. consumed = -1;
  246. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  247. if (argv[i + 1]) {
  248. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  249. blendMode = SDL_BLENDMODE_NONE;
  250. consumed = 2;
  251. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  252. blendMode = SDL_BLENDMODE_BLEND;
  253. consumed = 2;
  254. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  255. blendMode = SDL_BLENDMODE_ADD;
  256. consumed = 2;
  257. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  258. blendMode = SDL_BLENDMODE_MOD;
  259. consumed = 2;
  260. }
  261. }
  262. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  263. cycle_color = SDL_TRUE;
  264. consumed = 1;
  265. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  266. cycle_alpha = SDL_TRUE;
  267. consumed = 1;
  268. } else if (SDL_isdigit(*argv[i])) {
  269. num_objects = SDL_atoi(argv[i]);
  270. consumed = 1;
  271. }
  272. }
  273. if (consumed < 0) {
  274. static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", NULL };
  275. SDLTest_CommonLogUsage(state, argv[0], options);
  276. return 1;
  277. }
  278. i += consumed;
  279. }
  280. if (!SDLTest_CommonInit(state)) {
  281. return 2;
  282. }
  283. /* Create the windows and initialize the renderers */
  284. for (i = 0; i < state->num_windows; ++i) {
  285. SDL_Renderer *renderer = state->renderers[i];
  286. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  287. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  288. SDL_RenderClear(renderer);
  289. }
  290. srand(time(NULL));
  291. /* Main render loop */
  292. frames = 0;
  293. then = SDL_GetTicks();
  294. done = 0;
  295. #ifdef __EMSCRIPTEN__
  296. emscripten_set_main_loop(loop, 0, 1);
  297. #else
  298. while (!done) {
  299. ++frames;
  300. loop();
  301. }
  302. #endif
  303. SDLTest_CommonQuit(state);
  304. /* Print out some timing information */
  305. now = SDL_GetTicks();
  306. if (now > then) {
  307. double fps = ((double) frames * 1000) / (now - then);
  308. SDL_Log("%2.2f frames per second\n", fps);
  309. }
  310. return 0;
  311. }
  312. /* vi: set ts=4 sw=4 expandtab: */