testintersections.c 9.8 KB

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