testintersections.c 9.9 KB

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