🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

374 lines
10 KiB

  1. /*
  2. Copyright (C) 1997-2023 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. #ifdef __EMSCRIPTEN__
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include "SDL_test_common.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. int mouse_begin_x = -1, mouse_begin_y = -1;
  34. int done;
  35. void DrawPoints(SDL_Renderer *renderer)
  36. {
  37. int i;
  38. int x, y;
  39. SDL_Rect viewport;
  40. /* Query the sizes */
  41. SDL_RenderGetViewport(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 = rand() % viewport.w;
  69. y = rand() % viewport.h;
  70. SDL_RenderDrawPoint(renderer, x, y);
  71. }
  72. }
  73. #define MAX_LINES 16
  74. int num_lines = 0;
  75. SDL_Rect lines[MAX_LINES];
  76. static int
  77. add_line(int x1, int y1, int x2, int 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 (%d, %d), (%d, %d)\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_RenderGetViewport(renderer, &viewport);
  98. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  99. for (i = 0; i < num_lines; ++i) {
  100. if (i == -1) {
  101. SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
  102. SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
  103. SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
  104. SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
  105. } else {
  106. SDL_RenderDrawLine(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_Rect rects[MAX_RECTS];
  113. static int
  114. add_rect(int x1, int y1, int x2, int 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(int, x1, x2);
  124. }
  125. if (y1 > y2) {
  126. SWAP(int, y1, y2);
  127. }
  128. SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\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. int x1, y1, x2, y2;
  150. SDL_Rect 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_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
  157. SDL_RenderDrawLine(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_Rect r;
  170. if (SDL_IntersectRect(&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_MOUSEBUTTONDOWN:
  185. mouse_begin_x = event.button.x;
  186. mouse_begin_y = event.button.y;
  187. break;
  188. case SDL_MOUSEBUTTONUP:
  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_KEYDOWN:
  197. switch (event.key.keysym.sym) {
  198. case 'l':
  199. if (event.key.keysym.mod & KMOD_SHIFT) {
  200. num_lines = 0;
  201. } else {
  202. add_line(rand() % 640, rand() % 480, rand() % 640,
  203. rand() % 480);
  204. }
  205. break;
  206. case 'r':
  207. if (event.key.keysym.mod & KMOD_SHIFT) {
  208. num_rects = 0;
  209. } else {
  210. add_rect(rand() % 640, rand() % 480, rand() % 640,
  211. rand() % 480);
  212. }
  213. break;
  214. }
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. for (i = 0; i < state->num_windows; ++i) {
  221. SDL_Renderer *renderer = state->renderers[i];
  222. if (state->windows[i] == NULL) {
  223. continue;
  224. }
  225. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  226. SDL_RenderClear(renderer);
  227. DrawRects(renderer);
  228. DrawPoints(renderer);
  229. DrawRectRectIntersections(renderer);
  230. DrawLines(renderer);
  231. DrawRectLineIntersections(renderer);
  232. SDL_RenderPresent(renderer);
  233. }
  234. #ifdef __EMSCRIPTEN__
  235. if (done) {
  236. emscripten_cancel_main_loop();
  237. }
  238. #endif
  239. }
  240. int main(int argc, char *argv[])
  241. {
  242. int i;
  243. Uint32 then, now, frames;
  244. /* Enable standard application logging */
  245. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  246. /* Initialize parameters */
  247. num_objects = NUM_OBJECTS;
  248. /* Initialize test framework */
  249. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  250. if (state == NULL) {
  251. return 1;
  252. }
  253. for (i = 1; i < argc;) {
  254. int consumed;
  255. consumed = SDLTest_CommonArg(state, i);
  256. if (consumed == 0) {
  257. consumed = -1;
  258. if (SDL_strcasecmp(argv[i], "--blend") == 0) {
  259. if (argv[i + 1]) {
  260. if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
  261. blendMode = SDL_BLENDMODE_NONE;
  262. consumed = 2;
  263. } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
  264. blendMode = SDL_BLENDMODE_BLEND;
  265. consumed = 2;
  266. } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
  267. blendMode = SDL_BLENDMODE_ADD;
  268. consumed = 2;
  269. } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
  270. blendMode = SDL_BLENDMODE_MOD;
  271. consumed = 2;
  272. }
  273. }
  274. } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
  275. cycle_color = SDL_TRUE;
  276. consumed = 1;
  277. } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
  278. cycle_alpha = SDL_TRUE;
  279. consumed = 1;
  280. } else if (SDL_isdigit(*argv[i])) {
  281. num_objects = SDL_atoi(argv[i]);
  282. consumed = 1;
  283. }
  284. }
  285. if (consumed < 0) {
  286. static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", NULL };
  287. SDLTest_CommonLogUsage(state, argv[0], options);
  288. return 1;
  289. }
  290. i += consumed;
  291. }
  292. if (!SDLTest_CommonInit(state)) {
  293. return 2;
  294. }
  295. /* Create the windows and initialize the renderers */
  296. for (i = 0; i < state->num_windows; ++i) {
  297. SDL_Renderer *renderer = state->renderers[i];
  298. SDL_SetRenderDrawBlendMode(renderer, blendMode);
  299. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  300. SDL_RenderClear(renderer);
  301. }
  302. srand((unsigned int)time(NULL));
  303. /* Main render loop */
  304. frames = 0;
  305. then = SDL_GetTicks();
  306. done = 0;
  307. #ifdef __EMSCRIPTEN__
  308. emscripten_set_main_loop(loop, 0, 1);
  309. #else
  310. while (!done) {
  311. ++frames;
  312. loop();
  313. }
  314. #endif
  315. SDLTest_CommonQuit(state);
  316. /* Print out some timing information */
  317. now = SDL_GetTicks();
  318. if (now > then) {
  319. double fps = ((double)frames * 1000) / (now - then);
  320. SDL_Log("%2.2f frames per second\n", fps);
  321. }
  322. return 0;
  323. }
  324. /* vi: set ts=4 sw=4 expandtab: */