Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
418 lines (367 loc) · 10.5 KB

SDL_rect.c

File metadata and controls

418 lines (367 loc) · 10.5 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_video.h"
#include "SDL_rect_c.h"
SDL_bool
SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
{
int Amin, Amax, Bmin, Bmax;
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return SDL_FALSE;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
if (Bmax < Amax)
Amax = Bmax;
if (Amax <= Amin)
return SDL_FALSE;
return SDL_TRUE;
}
SDL_bool
SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{
int Amin, Amax, Bmin, Bmax;
/* Horizontal intersection */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin > Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->w = Amax - Amin;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin > Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax < Amax)
Amax = Bmax;
result->h = Amax - Amin;
return !SDL_RectEmpty(result);
}
void
SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
{
int Amin, Amax, Bmin, Bmax;
/* Horizontal union */
Amin = A->x;
Amax = Amin + A->w;
Bmin = B->x;
Bmax = Bmin + B->w;
if (Bmin < Amin)
Amin = Bmin;
result->x = Amin;
if (Bmax > Amax)
Amax = Bmax;
result->w = Amax - Amin;
/* Vertical intersection */
Amin = A->y;
Amax = Amin + A->h;
Bmin = B->y;
Bmax = Bmin + B->h;
if (Bmin < Amin)
Amin = Bmin;
result->y = Amin;
if (Bmax > Amax)
Amax = Bmax;
result->h = Amax - Amin;
}
Dec 9, 2009
Dec 9, 2009
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
SDL_bool
SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
SDL_Rect * result)
{
int minx, miny;
int maxx, maxy;
int x, y, i;
if (count < 1) {
return SDL_FALSE;
}
if (clip) {
SDL_bool added = SDL_FALSE;
int clip_minx = clip->x;
int clip_miny = clip->y;
int clip_maxx = clip->x+clip->w-1;
int clip_maxy = clip->y+clip->h-1;
for (i = 0; i < count; ++i) {
x = points[i].x;
y = points[i].y;
if (x < clip_minx || x > clip_maxx ||
y < clip_miny || y > clip_maxy) {
continue;
}
if (!added) {
minx = maxx = x;
miny = maxy = y;
added = SDL_TRUE;
continue;
}
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
}
if (!added) {
return SDL_FALSE;
}
} else {
/* No clipping, always add the first point */
minx = maxx = points[0].x;
miny = maxy = points[0].y;
for (i = 1; i < count; ++i) {
x = points[i].x;
y = points[i].y;
if (x < minx) {
minx = x;
} else if (x > maxx) {
maxx = x;
}
if (y < miny) {
miny = y;
} else if (y > maxy) {
maxy = y;
}
}
}
if (result) {
result->x = minx;
result->y = miny;
result->w = (maxx-minx)+1;
result->h = (maxy-miny)+1;
}
return SDL_TRUE;
}
Dec 23, 2008
Dec 23, 2008
199
SDL_bool
Dec 25, 2008
Dec 25, 2008
200
201
SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
int *Y2)
Dec 23, 2008
Dec 23, 2008
202
203
204
205
206
207
208
209
210
{
int x1, y1;
int x2, y2;
int rectx1;
int recty1;
int rectx2;
int recty2;
if (!rect || !X1 || !Y1 || !X2 || !Y2) {
Jan 19, 2009
Jan 19, 2009
211
return SDL_FALSE;
Dec 23, 2008
Dec 23, 2008
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
}
x1 = *X1;
y1 = *Y1;
x2 = *X2;
y2 = *Y2;
rectx1 = rect->x;
recty1 = rect->y;
rectx2 = rect->x + rect->w - 1;
recty2 = rect->y + rect->h - 1;
/* Check to see if entire line is inside rect */
if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
return SDL_TRUE;
}
Jan 4, 2009
Jan 4, 2009
229
/* Check to see if entire line is to one side of rect */
Dec 23, 2008
Dec 23, 2008
230
if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
Jan 5, 2009
Jan 5, 2009
231
(y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
Dec 23, 2008
Dec 23, 2008
232
233
234
return SDL_FALSE;
}
Jan 4, 2009
Jan 4, 2009
235
if (y1 == y2) {
Dec 23, 2008
Dec 23, 2008
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/* Horizontal line, easy to clip */
if (x1 < rectx1) {
*X1 = rectx1;
} else if (x1 > rectx2) {
*X1 = rectx2;
}
if (x2 < rectx1) {
*X2 = rectx1;
} else if (x2 > rectx2) {
*X2 = rectx2;
}
return SDL_TRUE;
}
if (x1 == x2) {
/* Vertical line, easy to clip */
if (y1 < recty1) {
*Y1 = recty1;
} else if (y1 > recty2) {
*Y1 = recty2;
}
if (y2 < recty1) {
*Y2 = recty1;
} else if (y2 > recty2) {
*Y2 = recty2;
}
return SDL_TRUE;
}
Jan 4, 2009
Jan 4, 2009
265
266
267
268
269
270
271
272
273
274
else {
/* The task of clipping a line with finite slope ratios in a fixed-
* precision coordinate space is not as immediately simple as it is
* with coordinates of arbitrary precision. If the ratio of slopes
* between the input line segment and the result line segment is not
* a whole number, you have in fact *moved* the line segment a bit,
* and there can be no avoiding it without more precision
*/
int *x_result_[] = { X1, X2, NULL }, **x_result = x_result_;
int *y_result_[] = { Y1, Y2, NULL }, **y_result = y_result_;
Jan 4, 2009
Jan 4, 2009
275
276
277
278
279
SDL_bool intersection = SDL_FALSE;
double b, m, left, right, bottom, top;
int xl, xh, yl, yh;
/* solve mx+b line formula */
Jan 4, 2009
Jan 4, 2009
280
m = (double) (y1 - y2) / (double) (x1 - x2);
Jan 4, 2009
Jan 4, 2009
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
b = y2 - m * (double) x2;
/* find some linear intersections */
left = (m * (double) rectx1) + b;
right = (m * (double) rectx2) + b;
top = (recty1 - b) / m;
bottom = (recty2 - b) / m;
/* sort end-points' x and y components individually */
if (x1 < x2) {
xl = x1;
xh = x2;
} else {
xl = x2;
xh = x1;
}
if (y1 < y2) {
yl = y1;
yh = y2;
} else {
yl = y2;
yh = y1;
}
#define RISING(a, b, c) (((a)<=(b))&&((b)<=(c)))
/* check for a point that's entirely inside the rect */
if (RISING(rectx1, x1, rectx2) && RISING(recty1, y1, recty2)) {
x_result++;
y_result++;
intersection = SDL_TRUE;
Jan 4, 2009
Jan 4, 2009
312
313
} else
/* it was determined earlier that *both* end-points are not contained */
Jan 4, 2009
Jan 4, 2009
314
315
316
317
318
319
320
321
322
323
324
325
if (RISING(rectx1, x2, rectx2) && RISING(recty1, y2, recty2)) {
**(x_result++) = x2;
**(y_result++) = y2;
intersection = SDL_TRUE;
}
if (RISING(recty1, left, recty2) && RISING(xl, rectx1, xh)) {
**(x_result++) = rectx1;
**(y_result++) = (int) left;
intersection = SDL_TRUE;
}
Jan 4, 2009
Jan 4, 2009
326
327
if (*x_result == NULL)
return intersection;
Jan 4, 2009
Jan 4, 2009
328
329
330
331
332
333
if (RISING(recty1, right, recty2) && RISING(xl, rectx2, xh)) {
**(x_result++) = rectx2;
**(y_result++) = (int) right;
intersection = SDL_TRUE;
}
Jan 4, 2009
Jan 4, 2009
334
335
if (*x_result == NULL)
return intersection;
Jan 4, 2009
Jan 4, 2009
336
337
338
339
340
341
if (RISING(rectx1, top, rectx2) && RISING(yl, recty1, yh)) {
**(x_result++) = (int) top;
**(y_result++) = recty1;
intersection = SDL_TRUE;
}
Jan 4, 2009
Jan 4, 2009
342
343
if (*x_result == NULL)
return intersection;
Jan 4, 2009
Jan 4, 2009
344
345
346
347
348
349
350
351
352
if (RISING(rectx1, bottom, rectx2) && RISING(yl, recty2, yh)) {
**(x_result++) = (int) bottom;
**(y_result++) = recty2;
intersection = SDL_TRUE;
}
return intersection;
}
Dec 23, 2008
Dec 23, 2008
353
354
355
return SDL_FALSE;
}
356
357
358
359
void
SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
{
SDL_DirtyRect *dirty;
Aug 11, 2007
Aug 11, 2007
360
361
362
363
364
365
366
367
/* FIXME: At what point is this optimization too expensive? */
for (dirty = list->list; dirty; dirty = dirty->next) {
if (SDL_HasIntersection(&dirty->rect, rect)) {
SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
return;
}
}
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
if (list->free) {
dirty = list->free;
list->free = dirty->next;
} else {
dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
if (!dirty) {
return;
}
}
dirty->rect = *rect;
dirty->next = list->list;
list->list = dirty;
}
void
SDL_ClearDirtyRects(SDL_DirtyRectList * list)
{
Aug 11, 2007
Aug 11, 2007
386
387
388
389
390
391
392
393
394
395
396
397
398
399
SDL_DirtyRect *prev, *curr;
/* Skip to the end of the free list */
prev = NULL;
for (curr = list->free; curr; curr = curr->next) {
prev = curr;
}
/* Add the list entries to the end */
if (prev) {
prev->next = list->list;
} else {
list->free = list->list;
}
Aug 11, 2007
Aug 11, 2007
400
list->list = NULL;
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
}
void
SDL_FreeDirtyRects(SDL_DirtyRectList * list)
{
while (list->list) {
SDL_DirtyRect *elem = list->list;
list->list = elem->next;
SDL_free(elem);
}
while (list->free) {
SDL_DirtyRect *elem = list->free;
list->free = elem->next;
SDL_free(elem);
}
}
/* vi: set ts=4 sw=4 expandtab: */