annotate src/video/SDL_rect.c @ 5168:2b1989f59674

Extended SDL_SetWindowData() to allow arbitrary named values.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 03 Feb 2011 11:16:57 -0800
parents 307ccc9c135e
children b530ef003506
rev   line source
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
1 /*
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
3697
f7b03b6838cb Fixed bug #926
Sam Lantinga <slouken@libsdl.org>
parents: 3542
diff changeset
3 Copyright (C) 1997-2010 Sam Lantinga
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
4
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
9
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
13 Lesser General Public License for more details.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
14
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
18
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
19 Sam Lantinga
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
20 slouken@libsdl.org
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
21 */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
22 #include "SDL_config.h"
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
23
5157
fb424691cfc7 Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
Sam Lantinga <slouken@libsdl.org>
parents: 4456
diff changeset
24 #include "SDL_rect.h"
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
25
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
26 SDL_bool
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
27 SDL_HasIntersection(const SDL_Rect * A, const SDL_Rect * B)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
28 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
29 int Amin, Amax, Bmin, Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
30
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
31 /* Horizontal intersection */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
32 Amin = A->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
33 Amax = Amin + A->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
34 Bmin = B->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
35 Bmax = Bmin + B->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
36 if (Bmin > Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
37 Amin = Bmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
38 if (Bmax < Amax)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
39 Amax = Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
40 if (Amax <= Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
41 return SDL_FALSE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
42
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
43 /* Vertical intersection */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
44 Amin = A->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
45 Amax = Amin + A->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
46 Bmin = B->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
47 Bmax = Bmin + B->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
48 if (Bmin > Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
49 Amin = Bmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
50 if (Bmax < Amax)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
51 Amax = Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
52 if (Amax <= Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
53 return SDL_FALSE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
54
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
55 return SDL_TRUE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
56 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
57
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
58 SDL_bool
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
59 SDL_IntersectRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
60 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
61 int Amin, Amax, Bmin, Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
62
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
63 /* Horizontal intersection */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
64 Amin = A->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
65 Amax = Amin + A->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
66 Bmin = B->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
67 Bmax = Bmin + B->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
68 if (Bmin > Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
69 Amin = Bmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
70 result->x = Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
71 if (Bmax < Amax)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
72 Amax = Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
73 result->w = Amax - Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
74
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
75 /* Vertical intersection */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
76 Amin = A->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
77 Amax = Amin + A->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
78 Bmin = B->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
79 Bmax = Bmin + B->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
80 if (Bmin > Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
81 Amin = Bmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
82 result->y = Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
83 if (Bmax < Amax)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
84 Amax = Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
85 result->h = Amax - Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
86
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
87 return !SDL_RectEmpty(result);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
88 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
89
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
90 void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
91 SDL_UnionRect(const SDL_Rect * A, const SDL_Rect * B, SDL_Rect * result)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
92 {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
93 int Amin, Amax, Bmin, Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
94
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
95 /* Horizontal union */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
96 Amin = A->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
97 Amax = Amin + A->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
98 Bmin = B->x;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
99 Bmax = Bmin + B->w;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
100 if (Bmin < Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
101 Amin = Bmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
102 result->x = Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
103 if (Bmax > Amax)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
104 Amax = Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
105 result->w = Amax - Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
106
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
107 /* Vertical intersection */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
108 Amin = A->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
109 Amax = Amin + A->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
110 Bmin = B->y;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
111 Bmax = Bmin + B->h;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
112 if (Bmin < Amin)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
113 Amin = Bmin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
114 result->y = Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
115 if (Bmax > Amax)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
116 Amax = Bmax;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
117 result->h = Amax - Amin;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
118 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
119
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
120 SDL_bool
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
121 SDL_EnclosePoints(const SDL_Point * points, int count, const SDL_Rect * clip,
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
122 SDL_Rect * result)
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
123 {
4456
0a5dbe96c3be Fixed variable use before initialize warnings
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
124 int minx = 0;
0a5dbe96c3be Fixed variable use before initialize warnings
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
125 int miny = 0;
0a5dbe96c3be Fixed variable use before initialize warnings
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
126 int maxx = 0;
0a5dbe96c3be Fixed variable use before initialize warnings
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
127 int maxy = 0;
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
128 int x, y, i;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
129
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
130 if (count < 1) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
131 return SDL_FALSE;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
132 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
133
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
134 if (clip) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
135 SDL_bool added = SDL_FALSE;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
136 int clip_minx = clip->x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
137 int clip_miny = clip->y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
138 int clip_maxx = clip->x+clip->w-1;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
139 int clip_maxy = clip->y+clip->h-1;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
140
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
141 for (i = 0; i < count; ++i) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
142 x = points[i].x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
143 y = points[i].y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
144
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
145 if (x < clip_minx || x > clip_maxx ||
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
146 y < clip_miny || y > clip_maxy) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
147 continue;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
148 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
149 if (!added) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
150 minx = maxx = x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
151 miny = maxy = y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
152 added = SDL_TRUE;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
153 continue;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
154 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
155 if (x < minx) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
156 minx = x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
157 } else if (x > maxx) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
158 maxx = x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
159 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
160 if (y < miny) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
161 miny = y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
162 } else if (y > maxy) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
163 maxy = y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
164 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
165 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
166 if (!added) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
167 return SDL_FALSE;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
168 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
169 } else {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
170 /* No clipping, always add the first point */
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
171 minx = maxx = points[0].x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
172 miny = maxy = points[0].y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
173
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
174 for (i = 1; i < count; ++i) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
175 x = points[i].x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
176 y = points[i].y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
177
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
178 if (x < minx) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
179 minx = x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
180 } else if (x > maxx) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
181 maxx = x;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
182 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
183 if (y < miny) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
184 miny = y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
185 } else if (y > maxy) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
186 maxy = y;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
187 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
188 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
189 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
190
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
191 if (result) {
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
192 result->x = minx;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
193 result->y = miny;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
194 result->w = (maxx-minx)+1;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
195 result->h = (maxy-miny)+1;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
196 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
197 return SDL_TRUE;
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
198 }
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
199
3541
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
200 /* Use the Cohen-Sutherland algorithm for line clipping */
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
201 #define CODE_BOTTOM 1
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
202 #define CODE_TOP 2
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
203 #define CODE_LEFT 4
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
204 #define CODE_RIGHT 8
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
205
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
206 static int ComputeOutCode(const SDL_Rect * rect, int x, int y)
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
207 {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
208 int code = 0;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
209 if (y < 0) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
210 code |= CODE_TOP;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
211 } else if (y >= rect->y + rect->h) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
212 code |= CODE_BOTTOM;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
213 }
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
214 if (x < 0) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
215 code |= CODE_LEFT;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
216 } else if (x >= rect->x + rect->w) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
217 code |= CODE_RIGHT;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
218 }
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
219 return code;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
220 }
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
221
3536
0267b8b1595c Added interfaces for batch drawing of points, lines and rects:
Sam Lantinga <slouken@libsdl.org>
parents: 3046
diff changeset
222 SDL_bool
2920
Sam Lantinga <slouken@libsdl.org>
parents: 2909
diff changeset
223 SDL_IntersectRectAndLine(const SDL_Rect * rect, int *X1, int *Y1, int *X2,
Sam Lantinga <slouken@libsdl.org>
parents: 2909
diff changeset
224 int *Y2)
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
225 {
4456
0a5dbe96c3be Fixed variable use before initialize warnings
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
226 int x = 0;
0a5dbe96c3be Fixed variable use before initialize warnings
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
227 int y = 0;
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
228 int x1, y1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
229 int x2, y2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
230 int rectx1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
231 int recty1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
232 int rectx2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
233 int recty2;
3541
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
234 int outcode1, outcode2;
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
235
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
236 if (!rect || !X1 || !Y1 || !X2 || !Y2) {
3046
47965eacde88 Fix recommended by Eddy L O Jansson
Sam Lantinga <slouken@libsdl.org>
parents: 3004
diff changeset
237 return SDL_FALSE;
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
238 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
239
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
240 x1 = *X1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
241 y1 = *Y1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
242 x2 = *X2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
243 y2 = *Y2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
244 rectx1 = rect->x;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
245 recty1 = rect->y;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
246 rectx2 = rect->x + rect->w - 1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
247 recty2 = rect->y + rect->h - 1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
248
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
249 /* Check to see if entire line is inside rect */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
250 if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
251 y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
252 return SDL_TRUE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
253 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
254
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
255 /* Check to see if entire line is to one side of rect */
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
256 if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
3004
f3d7226a8dfd Fixed lines intersecting the top corners of a rectangle
Sam Lantinga <slouken@libsdl.org>
parents: 2997
diff changeset
257 (y1 < recty1 && y2 < recty1) || (y1 > recty2 && y2 > recty2)) {
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
258 return SDL_FALSE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
259 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
260
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
261 if (y1 == y2) {
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
262 /* Horizontal line, easy to clip */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
263 if (x1 < rectx1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
264 *X1 = rectx1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
265 } else if (x1 > rectx2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
266 *X1 = rectx2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
267 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
268 if (x2 < rectx1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
269 *X2 = rectx1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
270 } else if (x2 > rectx2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
271 *X2 = rectx2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
272 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
273 return SDL_TRUE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
274 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
275
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
276 if (x1 == x2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
277 /* Vertical line, easy to clip */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
278 if (y1 < recty1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
279 *Y1 = recty1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
280 } else if (y1 > recty2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
281 *Y1 = recty2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
282 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
283 if (y2 < recty1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
284 *Y2 = recty1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
285 } else if (y2 > recty2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
286 *Y2 = recty2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
287 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
288 return SDL_TRUE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
289 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
290
3541
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
291 /* More complicated Cohen-Sutherland algorithm */
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
292 outcode1 = ComputeOutCode(rect, x1, y1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
293 outcode2 = ComputeOutCode(rect, x2, y2);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
294 while (outcode1 || outcode2) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
295 if (outcode1 & outcode2) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
296 return SDL_FALSE;
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
297 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
298
3541
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
299 if (outcode1) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
300 if (outcode1 & CODE_TOP) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
301 y = recty1;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
302 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
303 } else if (outcode1 & CODE_BOTTOM) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
304 y = recty2;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
305 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
306 } else if (outcode1 & CODE_LEFT) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
307 x = rectx1;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
308 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
309 } else if (outcode1 & CODE_RIGHT) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
310 x = rectx2;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
311 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
312 }
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
313 x1 = x;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
314 y1 = y;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
315 outcode1 = ComputeOutCode(rect, x, y);
3542
97eae5a705f9 Make sure we fully clip the first point before starting to adjust the second point.
Sam Lantinga <slouken@libsdl.org>
parents: 3541
diff changeset
316 } else {
3541
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
317 if (outcode2 & CODE_TOP) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
318 y = recty1;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
319 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
320 } else if (outcode2 & CODE_BOTTOM) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
321 y = recty2;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
322 x = x1 + ((x2 - x1) * (y - y1)) / (y2 - y1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
323 } else if (outcode2 & CODE_LEFT) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
324 x = rectx1;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
325 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
326 } else if (outcode2 & CODE_RIGHT) {
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
327 x = rectx2;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
328 y = y1 + ((y2 - y1) * (x - x1)) / (x2 - x1);
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
329 }
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
330 x2 = x;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
331 y2 = y;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
332 outcode2 = ComputeOutCode(rect, x, y);
2994
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
333 }
7563b99e9a49 Date: Sat, 3 Jan 2009 22:11:18 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 2920
diff changeset
334 }
3541
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
335 *X1 = x1;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
336 *Y1 = y1;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
337 *X2 = x2;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
338 *Y2 = y2;
0c429a5fda8a Added an automated test for rectangle routines, currently only testing line clipping.
Sam Lantinga <slouken@libsdl.org>
parents: 3536
diff changeset
339 return SDL_TRUE;
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
340 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
341
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
342 /* vi: set ts=4 sw=4 expandtab: */