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