testintersections.c 11 KB

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