testintersections.c 10 KB

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