testintersections.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. Copyright (C) 1997-2025 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 <SDL3/SDL_main.h>
  12. #include <SDL3/SDL_test_common.h>
  13. #ifdef SDL_PLATFORM_EMSCRIPTEN
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. #define SWAP(typ, a, b) \
  17. do { \
  18. typ t = a; \
  19. a = b; \
  20. b = t; \
  21. } while (0)
  22. #define NUM_OBJECTS 100
  23. static SDLTest_CommonState *state;
  24. static int num_objects;
  25. static bool cycle_color;
  26. static bool cycle_alpha;
  27. static int cycle_direction = 1;
  28. static int current_alpha = 255;
  29. static int current_color = 255;
  30. static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
  31. static float mouse_begin_x = -1.0f, mouse_begin_y = -1.0f;
  32. static void DrawPoints(SDL_Renderer *renderer)
  33. {
  34. int i;
  35. float x, y;
  36. SDL_Rect viewport;
  37. /* Query the sizes */
  38. SDL_GetRenderViewport(renderer, &viewport);
  39. for (i = 0; i < num_objects * 4; ++i) {
  40. /* Cycle the color and alpha, if desired */
  41. if (cycle_color) {
  42. current_color += cycle_direction;
  43. if (current_color < 0) {
  44. current_color = 0;
  45. cycle_direction = -cycle_direction;
  46. }
  47. if (current_color > 255) {
  48. current_color = 255;
  49. cycle_direction = -cycle_direction;
  50. }
  51. }
  52. if (cycle_alpha) {
  53. current_alpha += cycle_direction;
  54. if (current_alpha < 0) {
  55. current_alpha = 0;
  56. cycle_direction = -cycle_direction;
  57. }
  58. if (current_alpha > 255) {
  59. current_alpha = 255;
  60. cycle_direction = -cycle_direction;
  61. }
  62. }
  63. SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
  64. (Uint8)current_color, (Uint8)current_alpha);
  65. x = (float)SDL_rand(viewport.w);
  66. y = (float)SDL_rand(viewport.h);
  67. SDL_RenderPoint(renderer, x, y);
  68. }
  69. }
  70. #define MAX_LINES 16
  71. static int num_lines = 0;
  72. static SDL_FRect lines[MAX_LINES];
  73. static int add_line(float x1, float y1, float x2, float y2)
  74. {
  75. if (num_lines >= MAX_LINES) {
  76. return 0;
  77. }
  78. if ((x1 == x2) && (y1 == y2)) {
  79. return 0;
  80. }
  81. SDL_Log("adding line (%g, %g), (%g, %g)\n", x1, y1, x2, y2);
  82. lines[num_lines].x = x1;
  83. lines[num_lines].y = y1;
  84. lines[num_lines].w = x2;
  85. lines[num_lines].h = y2;
  86. return ++num_lines;
  87. }
  88. static void DrawLines(SDL_Renderer *renderer)
  89. {
  90. int i;
  91. SDL_Rect viewport;
  92. /* Query the sizes */
  93. SDL_GetRenderViewport(renderer, &viewport);
  94. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  95. for (i = 0; i < num_lines; ++i) {
  96. if (i == -1) {
  97. SDL_RenderLine(renderer, 0.0f, 0.0f, (float)(viewport.w - 1), (float)(viewport.h - 1));
  98. SDL_RenderLine(renderer, 0.0f, (float)(viewport.h - 1), (float)(viewport.w - 1), 0.0f);
  99. SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
  100. SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
  101. } else {
  102. SDL_RenderLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
  103. }
  104. }
  105. }
  106. #define MAX_RECTS 16
  107. static int num_rects = 0;
  108. static SDL_FRect rects[MAX_RECTS];
  109. static int add_rect(float x1, float y1, float x2, float y2)
  110. {
  111. if (num_rects >= MAX_RECTS) {
  112. return 0;
  113. }
  114. if ((x1 == x2) || (y1 == y2)) {
  115. return 0;
  116. }
  117. if (x1 > x2) {
  118. SWAP(float, x1, x2);
  119. }
  120. if (y1 > y2) {
  121. SWAP(float, y1, y2);
  122. }
  123. SDL_Log("adding rect (%g, %g), (%g, %g) [%gx%g]\n", x1, y1, x2, y2,
  124. x2 - x1, y2 - y1);
  125. rects[num_rects].x = x1;
  126. rects[num_rects].y = y1;
  127. rects[num_rects].w = x2 - x1;
  128. rects[num_rects].h = y2 - y1;
  129. return ++num_rects;
  130. }
  131. static void
  132. DrawRects(SDL_Renderer *renderer)
  133. {
  134. SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
  135. SDL_RenderFillRects(renderer, rects, num_rects);
  136. }
  137. static void
  138. DrawRectLineIntersections(SDL_Renderer *renderer)
  139. {
  140. int i, j;
  141. SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
  142. for (i = 0; i < num_rects; i++) {
  143. for (j = 0; j < num_lines; j++) {
  144. float x1, y1, x2, y2;
  145. SDL_FRect r;
  146. r = rects[i];
  147. x1 = lines[j].x;
  148. y1 = lines[j].y;
  149. x2 = lines[j].w;
  150. y2 = lines[j].h;
  151. if (SDL_GetRectAndLineIntersectionFloat(&r, &x1, &y1, &x2, &y2)) {
  152. SDL_RenderLine(renderer, x1, y1, x2, y2);
  153. }
  154. }
  155. }
  156. }
  157. static void
  158. DrawRectRectIntersections(SDL_Renderer *renderer)
  159. {
  160. int i, j;
  161. SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
  162. for (i = 0; i < num_rects; i++) {
  163. for (j = i + 1; j < num_rects; j++) {
  164. SDL_FRect r;
  165. if (SDL_GetRectIntersectionFloat(&rects[i], &rects[j], &r)) {
  166. SDL_RenderFillRect(renderer, &r);
  167. }
  168. }
  169. }
  170. }
  171. static void loop(void *arg)
  172. {
  173. int i;
  174. SDL_Event event;
  175. int *done = (int *)arg;
  176. /* Check for events */
  177. while (SDL_PollEvent(&event)) {
  178. SDLTest_CommonEvent(state, &event, done);
  179. SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(&event)), &event);
  180. switch (event.type) {
  181. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  182. mouse_begin_x = event.button.x;
  183. mouse_begin_y = event.button.y;
  184. break;
  185. case SDL_EVENT_MOUSE_BUTTON_UP:
  186. if (event.button.button == 3) {
  187. add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
  188. }
  189. if (event.button.button == 1) {
  190. add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
  191. }
  192. break;
  193. case SDL_EVENT_KEY_DOWN:
  194. switch (event.key.key) {
  195. case SDLK_L:
  196. if (event.key.mod & SDL_KMOD_SHIFT) {
  197. num_lines = 0;
  198. } else {
  199. add_line(
  200. (float)SDL_rand(640),
  201. (float)SDL_rand(480),
  202. (float)SDL_rand(640),
  203. (float)SDL_rand(480));
  204. }
  205. break;
  206. case SDLK_R:
  207. if (event.key.mod & SDL_KMOD_SHIFT) {
  208. num_rects = 0;
  209. } else {
  210. add_rect(
  211. (float)SDL_rand(640),
  212. (float)SDL_rand(480),
  213. (float)SDL_rand(640),
  214. (float)SDL_rand(480));
  215. }
  216. break;
  217. default:
  218. break;
  219. }
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. for (i = 0; i < state->num_windows; ++i) {
  226. SDL_Renderer *renderer = state->renderers[i];
  227. if (state->windows[i] == NULL) {
  228. continue;
  229. }
  230. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  231. SDL_RenderClear(renderer);
  232. DrawRects(renderer);
  233. DrawPoints(renderer);
  234. DrawRectRectIntersections(renderer);
  235. DrawLines(renderer);
  236. DrawRectLineIntersections(renderer);
  237. SDL_RenderPresent(renderer);
  238. }
  239. #ifdef SDL_PLATFORM_EMSCRIPTEN
  240. if (*done) {
  241. emscripten_cancel_main_loop();
  242. }
  243. #endif
  244. }
  245. int main(int argc, char *argv[])
  246. {
  247. int i;
  248. Uint64 then, now;
  249. Uint32 frames;
  250. int done;
  251. /* Initialize parameters */
  252. num_objects = -1; /* -1 means not initialized */
  253. /* Initialize test framework */
  254. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  255. if (!state) {
  256. return 1;
  257. }
  258. for (i = 1; i < argc;) {
  259. int consumed;
  260. consumed = SDLTest_CommonArg(state, i);
  261. if (consumed == 0) {
  262. consumed = -1;
  263. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  264. if (argv[i + 1]) {
  265. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  266. blendMode = SDL_BLENDMODE_NONE;
  267. consumed = 2;
  268. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  269. blendMode = SDL_BLENDMODE_BLEND;
  270. consumed = 2;
  271. } else if (SDL_strcasecmp(argv[i + 1], "blend_premultiplied") == 0) {
  272. blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
  273. consumed = 2;
  274. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  275. blendMode = SDL_BLENDMODE_ADD;
  276. consumed = 2;
  277. } else if (SDL_strcasecmp(argv[i + 1], "add_premultiplied") == 0) {
  278. blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
  279. consumed = 2;
  280. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  281. blendMode = SDL_BLENDMODE_MOD;
  282. consumed = 2;
  283. } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
  284. blendMode = SDL_BLENDMODE_MUL;
  285. consumed = 2;
  286. }
  287. }
  288. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  289. cycle_color = true;
  290. consumed = 1;
  291. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  292. cycle_alpha = true;
  293. consumed = 1;
  294. } else if (num_objects < 0 && SDL_isdigit(*argv[i])) {
  295. char *endptr = NULL;
  296. num_objects = (int)SDL_strtol(argv[i], &endptr, 0);
  297. if (endptr != argv[i] && *endptr == '\0' && num_objects >= 0) {
  298. consumed = 1;
  299. }
  300. }
  301. }
  302. if (consumed < 0) {
  303. static const char *options[] = { "[--blend none|blend|blend_premultiplied|add|add_premultiplied|mod|mul]", "[--cyclecolor]", "[--cyclealpha]", "[count]", NULL };
  304. SDLTest_CommonLogUsage(state, argv[0], options);
  305. return 1;
  306. }
  307. i += consumed;
  308. }
  309. if (!SDLTest_CommonInit(state)) {
  310. return 2;
  311. }
  312. if (num_objects < 0) {
  313. num_objects = NUM_OBJECTS;
  314. }
  315. /* Create the windows and initialize the renderers */
  316. for (i = 0; i < state->num_windows; ++i) {
  317. SDL_Renderer *renderer = state->renderers[i];
  318. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  319. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  320. SDL_RenderClear(renderer);
  321. }
  322. /* Main render loop */
  323. frames = 0;
  324. then = SDL_GetTicks();
  325. done = 0;
  326. #ifdef SDL_PLATFORM_EMSCRIPTEN
  327. emscripten_set_main_loop_arg(loop, &done, 0, 1);
  328. #else
  329. while (!done) {
  330. ++frames;
  331. loop(&done);
  332. }
  333. #endif
  334. /* Print out some timing information */
  335. now = SDL_GetTicks();
  336. SDLTest_CommonQuit(state);
  337. if (now > then) {
  338. double fps = ((double)frames * 1000) / (now - then);
  339. SDL_Log("%2.2f frames per second\n", fps);
  340. }
  341. return 0;
  342. }