1 | /* |
2 | Simple DirectMedia Layer |
3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> |
4 | |
5 | This software is provided 'as-is', without any express or implied |
6 | warranty. In no event will the authors be held liable for any damages |
7 | arising from the use of this software. |
8 | |
9 | Permission is granted to anyone to use this software for any purpose, |
10 | including commercial applications, and to alter it and redistribute it |
11 | freely, subject to the following restrictions: |
12 | |
13 | 1. The origin of this software must not be misrepresented; you must not |
14 | claim that you wrote the original software. If you use this software |
15 | in a product, an acknowledgment in the product documentation would be |
16 | appreciated but is not required. |
17 | 2. Altered source versions must be plainly marked as such, and must not be |
18 | misrepresented as being the original software. |
19 | 3. This notice may not be removed or altered from any source distribution. |
20 | */ |
21 | #include "SDL_internal.h" |
22 | |
23 | #include "SDL_rect_c.h" |
24 | |
25 | /* There's no float version of this at the moment, because it's not a public API |
26 | and internally we only need the int version. */ |
27 | bool SDL_GetSpanEnclosingRect(int width, int height, |
28 | int numrects, const SDL_Rect *rects, SDL_Rect *span) |
29 | { |
30 | int i; |
31 | int span_y1, span_y2; |
32 | int rect_y1, rect_y2; |
33 | |
34 | if (width < 1) { |
35 | SDL_InvalidParamError("width" ); |
36 | return false; |
37 | } else if (height < 1) { |
38 | SDL_InvalidParamError("height" ); |
39 | return false; |
40 | } else if (!rects) { |
41 | SDL_InvalidParamError("rects" ); |
42 | return false; |
43 | } else if (!span) { |
44 | SDL_InvalidParamError("span" ); |
45 | return false; |
46 | } else if (numrects < 1) { |
47 | SDL_InvalidParamError("numrects" ); |
48 | return false; |
49 | } |
50 | |
51 | // Initialize to empty rect |
52 | span_y1 = height; |
53 | span_y2 = 0; |
54 | |
55 | for (i = 0; i < numrects; ++i) { |
56 | rect_y1 = rects[i].y; |
57 | rect_y2 = rect_y1 + rects[i].h; |
58 | |
59 | // Clip out of bounds rectangles, and expand span rect |
60 | if (rect_y1 < 0) { |
61 | span_y1 = 0; |
62 | } else if (rect_y1 < span_y1) { |
63 | span_y1 = rect_y1; |
64 | } |
65 | if (rect_y2 > height) { |
66 | span_y2 = height; |
67 | } else if (rect_y2 > span_y2) { |
68 | span_y2 = rect_y2; |
69 | } |
70 | } |
71 | if (span_y2 > span_y1) { |
72 | span->x = 0; |
73 | span->y = span_y1; |
74 | span->w = width; |
75 | span->h = (span_y2 - span_y1); |
76 | return true; |
77 | } |
78 | return false; |
79 | } |
80 | |
81 | // For use with the Cohen-Sutherland algorithm for line clipping, in SDL_rect_impl.h |
82 | #define CODE_BOTTOM 1 |
83 | #define CODE_TOP 2 |
84 | #define CODE_LEFT 4 |
85 | #define CODE_RIGHT 8 |
86 | |
87 | // Same code twice, for float and int versions... |
88 | #define RECTTYPE SDL_Rect |
89 | #define POINTTYPE SDL_Point |
90 | #define SCALARTYPE int |
91 | #define BIGSCALARTYPE Sint64 |
92 | #define COMPUTEOUTCODE ComputeOutCode |
93 | #define ENCLOSEPOINTS_EPSILON 1 |
94 | #define SDL_RECT_CAN_OVERFLOW SDL_RectCanOverflow |
95 | #define SDL_HASINTERSECTION SDL_HasRectIntersection |
96 | #define SDL_INTERSECTRECT SDL_GetRectIntersection |
97 | #define SDL_RECTEMPTY SDL_RectEmpty |
98 | #define SDL_UNIONRECT SDL_GetRectUnion |
99 | #define SDL_ENCLOSEPOINTS SDL_GetRectEnclosingPoints |
100 | #define SDL_INTERSECTRECTANDLINE SDL_GetRectAndLineIntersection |
101 | #include "SDL_rect_impl.h" |
102 | |
103 | #define RECTTYPE SDL_FRect |
104 | #define POINTTYPE SDL_FPoint |
105 | #define SCALARTYPE float |
106 | #define BIGSCALARTYPE double |
107 | #define COMPUTEOUTCODE ComputeOutCodeFloat |
108 | #define ENCLOSEPOINTS_EPSILON 0.0f |
109 | #define SDL_RECT_CAN_OVERFLOW SDL_RectCanOverflowFloat |
110 | #define SDL_HASINTERSECTION SDL_HasRectIntersectionFloat |
111 | #define SDL_INTERSECTRECT SDL_GetRectIntersectionFloat |
112 | #define SDL_RECTEMPTY SDL_RectEmptyFloat |
113 | #define SDL_UNIONRECT SDL_GetRectUnionFloat |
114 | #define SDL_ENCLOSEPOINTS SDL_GetRectEnclosingPointsFloat |
115 | #define SDL_INTERSECTRECTANDLINE SDL_GetRectAndLineIntersectionFloat |
116 | #include "SDL_rect_impl.h" |
117 | |