summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testintersections.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testintersections.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testintersections.c391
1 files changed, 391 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testintersections.c b/src/contrib/SDL-3.2.20/test/testintersections.c
new file mode 100644
index 0000000..cc58511
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testintersections.c
@@ -0,0 +1,391 @@
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12
13/* Simple program: draw as many random objects on the screen as possible */
14
15#include <SDL3/SDL_main.h>
16#include <SDL3/SDL_test_common.h>
17
18#ifdef SDL_PLATFORM_EMSCRIPTEN
19#include <emscripten/emscripten.h>
20#endif
21
22#define SWAP(typ, a, b) \
23 do { \
24 typ t = a; \
25 a = b; \
26 b = t; \
27 } while (0)
28#define NUM_OBJECTS 100
29
30static SDLTest_CommonState *state;
31static int num_objects;
32static bool cycle_color;
33static bool cycle_alpha;
34static int cycle_direction = 1;
35static int current_alpha = 255;
36static int current_color = 255;
37static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
38
39static float mouse_begin_x = -1.0f, mouse_begin_y = -1.0f;
40
41static void DrawPoints(SDL_Renderer *renderer)
42{
43 int i;
44 float x, y;
45 SDL_Rect viewport;
46
47 /* Query the sizes */
48 SDL_GetRenderViewport(renderer, &viewport);
49
50 for (i = 0; i < num_objects * 4; ++i) {
51 /* Cycle the color and alpha, if desired */
52 if (cycle_color) {
53 current_color += cycle_direction;
54 if (current_color < 0) {
55 current_color = 0;
56 cycle_direction = -cycle_direction;
57 }
58 if (current_color > 255) {
59 current_color = 255;
60 cycle_direction = -cycle_direction;
61 }
62 }
63 if (cycle_alpha) {
64 current_alpha += cycle_direction;
65 if (current_alpha < 0) {
66 current_alpha = 0;
67 cycle_direction = -cycle_direction;
68 }
69 if (current_alpha > 255) {
70 current_alpha = 255;
71 cycle_direction = -cycle_direction;
72 }
73 }
74 SDL_SetRenderDrawColor(renderer, 255, (Uint8)current_color,
75 (Uint8)current_color, (Uint8)current_alpha);
76
77 x = (float)SDL_rand(viewport.w);
78 y = (float)SDL_rand(viewport.h);
79 SDL_RenderPoint(renderer, x, y);
80 }
81}
82
83#define MAX_LINES 16
84static int num_lines = 0;
85static SDL_FRect lines[MAX_LINES];
86static int add_line(float x1, float y1, float x2, float y2)
87{
88 if (num_lines >= MAX_LINES) {
89 return 0;
90 }
91 if ((x1 == x2) && (y1 == y2)) {
92 return 0;
93 }
94
95 SDL_Log("adding line (%g, %g), (%g, %g)", x1, y1, x2, y2);
96 lines[num_lines].x = x1;
97 lines[num_lines].y = y1;
98 lines[num_lines].w = x2;
99 lines[num_lines].h = y2;
100
101 return ++num_lines;
102}
103
104static void DrawLines(SDL_Renderer *renderer)
105{
106 int i;
107 SDL_Rect viewport;
108
109 /* Query the sizes */
110 SDL_GetRenderViewport(renderer, &viewport);
111
112 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
113
114 for (i = 0; i < num_lines; ++i) {
115 if (i == -1) {
116 SDL_RenderLine(renderer, 0.0f, 0.0f, (float)(viewport.w - 1), (float)(viewport.h - 1));
117 SDL_RenderLine(renderer, 0.0f, (float)(viewport.h - 1), (float)(viewport.w - 1), 0.0f);
118 SDL_RenderLine(renderer, 0.0f, (float)(viewport.h / 2), (float)(viewport.w - 1), (float)(viewport.h / 2));
119 SDL_RenderLine(renderer, (float)(viewport.w / 2), 0.0f, (float)(viewport.w / 2), (float)(viewport.h - 1));
120 } else {
121 SDL_RenderLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
122 }
123 }
124}
125
126#define MAX_RECTS 16
127static int num_rects = 0;
128static SDL_FRect rects[MAX_RECTS];
129static int add_rect(float x1, float y1, float x2, float y2)
130{
131 if (num_rects >= MAX_RECTS) {
132 return 0;
133 }
134 if ((x1 == x2) || (y1 == y2)) {
135 return 0;
136 }
137
138 if (x1 > x2) {
139 SWAP(float, x1, x2);
140 }
141 if (y1 > y2) {
142 SWAP(float, y1, y2);
143 }
144
145 SDL_Log("adding rect (%g, %g), (%g, %g) [%gx%g]", x1, y1, x2, y2,
146 x2 - x1, y2 - y1);
147
148 rects[num_rects].x = x1;
149 rects[num_rects].y = y1;
150 rects[num_rects].w = x2 - x1;
151 rects[num_rects].h = y2 - y1;
152
153 return ++num_rects;
154}
155
156static void
157DrawRects(SDL_Renderer *renderer)
158{
159 SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
160 SDL_RenderFillRects(renderer, rects, num_rects);
161}
162
163static void
164DrawRectLineIntersections(SDL_Renderer *renderer)
165{
166 int i, j;
167
168 SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
169
170 for (i = 0; i < num_rects; i++) {
171 for (j = 0; j < num_lines; j++) {
172 float x1, y1, x2, y2;
173 SDL_FRect r;
174
175 r = rects[i];
176 x1 = lines[j].x;
177 y1 = lines[j].y;
178 x2 = lines[j].w;
179 y2 = lines[j].h;
180
181 if (SDL_GetRectAndLineIntersectionFloat(&r, &x1, &y1, &x2, &y2)) {
182 SDL_RenderLine(renderer, x1, y1, x2, y2);
183 }
184 }
185 }
186}
187
188static void
189DrawRectRectIntersections(SDL_Renderer *renderer)
190{
191 int i, j;
192
193 SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
194
195 for (i = 0; i < num_rects; i++) {
196 for (j = i + 1; j < num_rects; j++) {
197 SDL_FRect r;
198 if (SDL_GetRectIntersectionFloat(&rects[i], &rects[j], &r)) {
199 SDL_RenderFillRect(renderer, &r);
200 }
201 }
202 }
203}
204
205static void loop(void *arg)
206{
207 int i;
208 SDL_Event event;
209 int *done = (int *)arg;
210
211 /* Check for events */
212 while (SDL_PollEvent(&event)) {
213 SDLTest_CommonEvent(state, &event, done);
214 SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(&event)), &event);
215 switch (event.type) {
216 case SDL_EVENT_MOUSE_BUTTON_DOWN:
217 mouse_begin_x = event.button.x;
218 mouse_begin_y = event.button.y;
219 break;
220 case SDL_EVENT_MOUSE_BUTTON_UP:
221 if (event.button.button == 3) {
222 add_line(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
223 }
224 if (event.button.button == 1) {
225 add_rect(mouse_begin_x, mouse_begin_y, event.button.x, event.button.y);
226 }
227 break;
228 case SDL_EVENT_KEY_DOWN:
229 switch (event.key.key) {
230 case SDLK_L:
231 if (event.key.mod & SDL_KMOD_SHIFT) {
232 num_lines = 0;
233 } else {
234 add_line(
235 (float)SDL_rand(640),
236 (float)SDL_rand(480),
237 (float)SDL_rand(640),
238 (float)SDL_rand(480));
239 }
240 break;
241 case SDLK_R:
242 if (event.key.mod & SDL_KMOD_SHIFT) {
243 num_rects = 0;
244 } else {
245 add_rect(
246 (float)SDL_rand(640),
247 (float)SDL_rand(480),
248 (float)SDL_rand(640),
249 (float)SDL_rand(480));
250 }
251 break;
252 default:
253 break;
254 }
255 break;
256 default:
257 break;
258 }
259 }
260 for (i = 0; i < state->num_windows; ++i) {
261 SDL_Renderer *renderer = state->renderers[i];
262 if (state->windows[i] == NULL) {
263 continue;
264 }
265 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
266 SDL_RenderClear(renderer);
267
268 DrawRects(renderer);
269 DrawPoints(renderer);
270 DrawRectRectIntersections(renderer);
271 DrawLines(renderer);
272 DrawRectLineIntersections(renderer);
273
274 SDL_RenderPresent(renderer);
275 }
276#ifdef SDL_PLATFORM_EMSCRIPTEN
277 if (*done) {
278 emscripten_cancel_main_loop();
279 }
280#endif
281}
282
283int main(int argc, char *argv[])
284{
285 int i;
286 Uint64 then, now;
287 Uint32 frames;
288 int done;
289
290 /* Initialize parameters */
291 num_objects = -1; /* -1 means not initialized */
292
293 /* Initialize test framework */
294 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
295 if (!state) {
296 return 1;
297 }
298
299 for (i = 1; i < argc;) {
300 int consumed;
301
302 consumed = SDLTest_CommonArg(state, i);
303 if (consumed == 0) {
304 consumed = -1;
305 if (SDL_strcasecmp(argv[i], "--blend") == 0) {
306 if (argv[i + 1]) {
307 if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
308 blendMode = SDL_BLENDMODE_NONE;
309 consumed = 2;
310 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
311 blendMode = SDL_BLENDMODE_BLEND;
312 consumed = 2;
313 } else if (SDL_strcasecmp(argv[i + 1], "blend_premultiplied") == 0) {
314 blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
315 consumed = 2;
316 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
317 blendMode = SDL_BLENDMODE_ADD;
318 consumed = 2;
319 } else if (SDL_strcasecmp(argv[i + 1], "add_premultiplied") == 0) {
320 blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
321 consumed = 2;
322 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
323 blendMode = SDL_BLENDMODE_MOD;
324 consumed = 2;
325 } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) {
326 blendMode = SDL_BLENDMODE_MUL;
327 consumed = 2;
328 }
329 }
330 } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
331 cycle_color = true;
332 consumed = 1;
333 } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
334 cycle_alpha = true;
335 consumed = 1;
336 } else if (num_objects < 0 && SDL_isdigit(*argv[i])) {
337 char *endptr = NULL;
338 num_objects = (int)SDL_strtol(argv[i], &endptr, 0);
339 if (endptr != argv[i] && *endptr == '\0' && num_objects >= 0) {
340 consumed = 1;
341 }
342 }
343 }
344 if (consumed < 0) {
345 static const char *options[] = { "[--blend none|blend|blend_premultiplied|add|add_premultiplied|mod|mul]", "[--cyclecolor]", "[--cyclealpha]", "[count]", NULL };
346 SDLTest_CommonLogUsage(state, argv[0], options);
347 return 1;
348 }
349 i += consumed;
350 }
351 if (!SDLTest_CommonInit(state)) {
352 return 2;
353 }
354
355 if (num_objects < 0) {
356 num_objects = NUM_OBJECTS;
357 }
358
359 /* Create the windows and initialize the renderers */
360 for (i = 0; i < state->num_windows; ++i) {
361 SDL_Renderer *renderer = state->renderers[i];
362 SDL_SetRenderDrawBlendMode(renderer, blendMode);
363 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
364 SDL_RenderClear(renderer);
365 }
366
367 /* Main render loop */
368 frames = 0;
369 then = SDL_GetTicks();
370 done = 0;
371
372#ifdef SDL_PLATFORM_EMSCRIPTEN
373 emscripten_set_main_loop_arg(loop, &done, 0, 1);
374#else
375 while (!done) {
376 ++frames;
377 loop(&done);
378 }
379#endif
380
381 /* Print out some timing information */
382 now = SDL_GetTicks();
383
384 SDLTest_CommonQuit(state);
385
386 if (now > then) {
387 double fps = ((double)frames * 1000) / (now - then);
388 SDL_Log("%2.2f frames per second", fps);
389 }
390 return 0;
391}