SDL  2.0
testintersections.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2019 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 
25 #define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
26 #define NUM_OBJECTS 100
27 
29 static int num_objects;
32 static int cycle_direction = 1;
33 static int current_alpha = 255;
34 static int current_color = 255;
36 
38 int done;
39 
40 void
42 {
43  int i;
44  int x, y;
46 
47  /* Query the sizes */
49 
50  for (i = 0; i < num_objects * 4; ++i) {
51  /* Cycle the color and alpha, if desired */
52  if (cycle_color) {
54  if (current_color < 0) {
55  current_color = 0;
57  }
58  if (current_color > 255) {
59  current_color = 255;
61  }
62  }
63  if (cycle_alpha) {
65  if (current_alpha < 0) {
66  current_alpha = 0;
68  }
69  if (current_alpha > 255) {
70  current_alpha = 255;
72  }
73  }
76 
77  x = rand() % viewport.w;
78  y = rand() % viewport.h;
80  }
81 }
82 
83 #define MAX_LINES 16
84 int num_lines = 0;
86 static int
87 add_line(int x1, int y1, int x2, int y2)
88 {
89  if (num_lines >= MAX_LINES)
90  return 0;
91  if ((x1 == x2) && (y1 == y2))
92  return 0;
93 
94  SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
95  lines[num_lines].x = x1;
96  lines[num_lines].y = y1;
97  lines[num_lines].w = x2;
98  lines[num_lines].h = y2;
99 
100  return ++num_lines;
101 }
102 
103 
104 void
106 {
107  int i;
109 
110  /* Query the sizes */
112 
113  SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
114 
115  for (i = 0; i < num_lines; ++i) {
116  if (i == -1) {
117  SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
118  SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
121  } else {
123  }
124  }
125 }
126 
127 #define MAX_RECTS 16
128 int num_rects = 0;
130 static int
131 add_rect(int x1, int y1, int x2, int y2)
132 {
133  if (num_rects >= MAX_RECTS)
134  return 0;
135  if ((x1 == x2) || (y1 == y2))
136  return 0;
137 
138  if (x1 > x2)
139  SWAP(int, x1, x2);
140  if (y1 > y2)
141  SWAP(int, y1, y2);
142 
143  SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
144  x2 - x1, y2 - y1);
145 
146  rects[num_rects].x = x1;
147  rects[num_rects].y = y1;
148  rects[num_rects].w = x2 - x1;
149  rects[num_rects].h = y2 - y1;
150 
151  return ++num_rects;
152 }
153 
154 static void
156 {
157  SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
159 }
160 
161 static void
163 {
164  int i, j;
165 
166  SDL_SetRenderDrawColor(renderer, 0, 255, 55, 255);
167 
168  for (i = 0; i < num_rects; i++)
169  for (j = 0; j < num_lines; j++) {
170  int x1, y1, x2, y2;
171  SDL_Rect r;
172 
173  r = rects[i];
174  x1 = lines[j].x;
175  y1 = lines[j].y;
176  x2 = lines[j].w;
177  y2 = lines[j].h;
178 
179  if (SDL_IntersectRectAndLine(&r, &x1, &y1, &x2, &y2)) {
181  }
182  }
183 }
184 
185 static void
187 {
188  int i, j;
189 
190  SDL_SetRenderDrawColor(renderer, 255, 200, 0, 255);
191 
192  for (i = 0; i < num_rects; i++)
193  for (j = i + 1; j < num_rects; j++) {
194  SDL_Rect r;
195  if (SDL_IntersectRect(&rects[i], &rects[j], &r)) {
197  }
198  }
199 }
200 
201 void
203 {
204  int i;
206 
207  /* Check for events */
208  while (SDL_PollEvent(&event)) {
210  switch (event.type) {
211  case SDL_MOUSEBUTTONDOWN:
212  mouse_begin_x = event.button.x;
213  mouse_begin_y = event.button.y;
214  break;
215  case SDL_MOUSEBUTTONUP:
216  if (event.button.button == 3)
218  event.button.y);
219  if (event.button.button == 1)
221  event.button.y);
222  break;
223  case SDL_KEYDOWN:
224  switch (event.key.keysym.sym) {
225  case 'l':
226  if (event.key.keysym.mod & KMOD_SHIFT)
227  num_lines = 0;
228  else
229  add_line(rand() % 640, rand() % 480, rand() % 640,
230  rand() % 480);
231  break;
232  case 'r':
233  if (event.key.keysym.mod & KMOD_SHIFT)
234  num_rects = 0;
235  else
236  add_rect(rand() % 640, rand() % 480, rand() % 640,
237  rand() % 480);
238  break;
239  }
240  break;
241  default:
242  break;
243  }
244  }
245  for (i = 0; i < state->num_windows; ++i) {
247  if (state->windows[i] == NULL)
248  continue;
249  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
251 
257 
259  }
260 #ifdef __EMSCRIPTEN__
261  if (done) {
262  emscripten_cancel_main_loop();
263  }
264 #endif
265 }
266 
267 int
268 main(int argc, char *argv[])
269 {
270  int i;
271  Uint32 then, now, frames;
272 
273  /* Enable standard application logging */
275 
276  /* Initialize parameters */
278 
279  /* Initialize test framework */
281  if (!state) {
282  return 1;
283  }
284  for (i = 1; i < argc;) {
285  int consumed;
286 
287  consumed = SDLTest_CommonArg(state, i);
288  if (consumed == 0) {
289  consumed = -1;
290  if (SDL_strcasecmp(argv[i], "--blend") == 0) {
291  if (argv[i + 1]) {
292  if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
294  consumed = 2;
295  } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
297  consumed = 2;
298  } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
300  consumed = 2;
301  } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
303  consumed = 2;
304  }
305  }
306  } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
308  consumed = 1;
309  } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
311  consumed = 1;
312  } else if (SDL_isdigit(*argv[i])) {
313  num_objects = SDL_atoi(argv[i]);
314  consumed = 1;
315  }
316  }
317  if (consumed < 0) {
318  static const char *options[] = { "[--blend none|blend|add|mod]", "[--cyclecolor]", "[--cyclealpha]", NULL };
319  SDLTest_CommonLogUsage(state, argv[0], options);
320  return 1;
321  }
322  i += consumed;
323  }
324  if (!SDLTest_CommonInit(state)) {
325  return 2;
326  }
327 
328  /* Create the windows and initialize the renderers */
329  for (i = 0; i < state->num_windows; ++i) {
332  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
334  }
335 
336  srand(time(NULL));
337 
338  /* Main render loop */
339  frames = 0;
340  then = SDL_GetTicks();
341  done = 0;
342 
343 #ifdef __EMSCRIPTEN__
344  emscripten_set_main_loop(loop, 0, 1);
345 #else
346  while (!done) {
347  ++frames;
348  loop();
349  }
350 #endif
351 
353 
354  /* Print out some timing information */
355  now = SDL_GetTicks();
356  if (now > then) {
357  double fps = ((double) frames * 1000) / (now - then);
358  SDL_Log("%2.2f frames per second\n", fps);
359  }
360  return 0;
361 }
362 
363 /* vi: set ts=4 sw=4 expandtab: */
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
DrawRectLineIntersections
static void DrawRectLineIntersections(SDL_Renderer *renderer)
Definition: testintersections.c:162
SDL_RenderDrawPoint
#define SDL_RenderDrawPoint
Definition: SDL_dynapi_overrides.h:335
time
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
cycle_direction
static int cycle_direction
Definition: testintersections.c:32
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
DrawRects
static void DrawRects(SDL_Renderer *renderer)
Definition: testintersections.c:155
DrawPoints
void DrawPoints(SDL_Renderer *renderer)
Definition: testintersections.c:41
SDL_BLENDMODE_ADD
@ SDL_BLENDMODE_ADD
Definition: SDL_blendmode.h:47
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
NULL
#define NULL
Definition: begin_code.h:167
SDLTest_CommonState
Definition: SDL_test_common.h:51
SDL_SetRenderDrawBlendMode
#define SDL_SetRenderDrawBlendMode
Definition: SDL_dynapi_overrides.h:332
loop
void loop()
Definition: testintersections.c:202
r
GLdouble GLdouble GLdouble r
Definition: SDL_opengl.h:2079
SDL_MOUSEBUTTONUP
@ SDL_MOUSEBUTTONUP
Definition: SDL_events.h:107
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:341
viewport
SDL_Rect viewport
Definition: testviewport.c:28
SDL_IntersectRect
#define SDL_IntersectRect
Definition: SDL_dynapi_overrides.h:294
x1
GLuint GLfloat GLfloat GLfloat x1
Definition: SDL_opengl_glext.h:8583
SDL_KEYDOWN
@ SDL_KEYDOWN
Definition: SDL_events.h:96
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
DrawLines
void DrawLines(SDL_Renderer *renderer)
Definition: testintersections.c:105
SDL_Rect::x
int x
Definition: SDL_rect.h:79
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
SDL_Rect::w
int w
Definition: SDL_rect.h:80
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:59
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:419
num_rects
int num_rects
Definition: testintersections.c:128
SDL_RenderFillRects
#define SDL_RenderFillRects
Definition: SDL_dynapi_overrides.h:342
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1832
SDL_BLENDMODE_MOD
@ SDL_BLENDMODE_MOD
Definition: SDL_blendmode.h:50
current_color
static int current_color
Definition: testintersections.c:34
main
int main(int argc, char *argv[])
Definition: testintersections.c:268
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_BLENDMODE_NONE
@ SDL_BLENDMODE_NONE
Definition: SDL_blendmode.h:42
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_Renderer
Definition: SDL_sysrender.h:122
done
int done
Definition: testintersections.c:38
lines
SDL_Rect lines[MAX_LINES]
Definition: testintersections.c:85
x
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_test_common.h
SWAP
#define SWAP(typ, a, b)
Definition: testintersections.c:25
SDL_Rect::y
int y
Definition: SDL_rect.h:79
SDL_Rect::h
int h
Definition: SDL_rect.h:80
SDL_RenderGetViewport
#define SDL_RenderGetViewport
Definition: SDL_dynapi_overrides.h:325
add_line
static int add_line(int x1, int y1, int x2, int y2)
Definition: testintersections.c:87
num_lines
int num_lines
Definition: testintersections.c:84
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:117
SDL_isdigit
#define SDL_isdigit
Definition: SDL_dynapi_overrides.h:382
state
static SDLTest_CommonState * state
Definition: testintersections.c:28
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDLTest_CommonLogUsage
void SDLTest_CommonLogUsage(SDLTest_CommonState *state, const char *argv0, const char **options)
Logs command line usage info.
Definition: SDL_test_common.c:489
num_objects
static int num_objects
Definition: testintersections.c:29
MAX_RECTS
#define MAX_RECTS
Definition: testintersections.c:127
KMOD_SHIFT
#define KMOD_SHIFT
Definition: SDL_keycode.h:343
mouse_begin_x
int mouse_begin_x
Definition: testintersections.c:37
SDL_BLENDMODE_BLEND
@ SDL_BLENDMODE_BLEND
Definition: SDL_blendmode.h:44
y
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
NUM_OBJECTS
#define NUM_OBJECTS
Definition: testintersections.c:26
SDL_atoi
#define SDL_atoi
Definition: SDL_dynapi_overrides.h:410
cycle_color
static SDL_bool cycle_color
Definition: testintersections.c:30
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:764
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
frames
static Uint32 frames
Definition: testsprite2.c:40
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
blendMode
static SDL_BlendMode blendMode
Definition: testintersections.c:35
x2
GLfixed GLfixed x2
Definition: SDL_opengl_glext.h:4583
cycle_alpha
static SDL_bool cycle_alpha
Definition: testintersections.c:31
DrawRectRectIntersections
static void DrawRectRectIntersections(SDL_Renderer *renderer)
Definition: testintersections.c:186
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:77
j
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
current_alpha
static int current_alpha
Definition: testintersections.c:33
add_rect
static int add_rect(int x1, int y1, int x2, int y2)
Definition: testintersections.c:131
y1
GLfixed y1
Definition: SDL_opengl_glext.h:4583
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
mouse_begin_y
int mouse_begin_y
Definition: testintersections.c:37
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
y2
GLfixed GLfixed GLfixed y2
Definition: SDL_opengl_glext.h:4583
SDL_MOUSEBUTTONDOWN
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
SDL_IntersectRectAndLine
#define SDL_IntersectRectAndLine
Definition: SDL_dynapi_overrides.h:297
SDL_Event
General event structure.
Definition: SDL_events.h:557
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1501
SDL_RenderDrawLine
#define SDL_RenderDrawLine
Definition: SDL_dynapi_overrides.h:337
rects
EGLSurface EGLint * rects
Definition: eglext.h:282
MAX_LINES
#define MAX_LINES
Definition: testintersections.c:83
SDL_BlendMode
SDL_BlendMode
The blend mode used in SDL_RenderCopy() and drawing operations.
Definition: SDL_blendmode.h:40
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179