annotate src/video/SDL_rect.c @ 2912:1d50666ed3d5

Fixed clipping source rect to match destination rect clipping
author Sam Lantinga <slouken@libsdl.org>
date Tue, 23 Dec 2008 04:51:36 +0000
parents 3da0bb421d83
children cdb01906cb7e
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
2859
99210400e8b9 Updated copyright date
Sam Lantinga <slouken@libsdl.org>
parents: 2275
diff changeset
3 Copyright (C) 1997-2009 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
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
24 #include "SDL_video.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
25 #include "SDL_rect_c.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
26
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_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
28 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
29 {
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 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
31
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 /* 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
33 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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43
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 /* 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
45 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
46 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
47 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
48 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
49 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
50 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
51 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
52 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
53 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
54 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
55
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 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
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
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_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
60 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
61 {
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 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
63
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 /* 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
65 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
66 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
67 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
68 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
69 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
70 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
71 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
72 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
73 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
74 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
75
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 /* 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87
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 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
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
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 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
92 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
93 {
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 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
95
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 /* 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107
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 /* 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
109 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
110 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
111 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
112 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
113 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
114 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
115 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
116 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
117 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
118 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
119 }
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
120
2909
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
121 SDL_bool
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
122 SDL_IntersectRectAndLine(const SDL_Rect *rect, int *X1, int *Y1, int *X2, int *Y2)
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
123 {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
124 int x1, y1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
125 int x2, y2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
126 int rectx1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
127 int recty1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
128 int rectx2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
129 int recty2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
130
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
131 if (!rect || !X1 || !Y1 || !X2 || !Y2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
132 SDL_FALSE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
133 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
134
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
135 x1 = *X1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
136 y1 = *Y1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
137 x2 = *X2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
138 y2 = *Y2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
139 rectx1 = rect->x;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
140 recty1 = rect->y;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
141 rectx2 = rect->x + rect->w - 1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
142 recty2 = rect->y + rect->h - 1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
143
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
144 /* Check to see if entire line is inside rect */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
145 if (x1 >= rectx1 && x1 <= rectx2 && x2 >= rectx1 && x2 <= rectx2 &&
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
146 y1 >= recty1 && y1 <= recty2 && y2 >= recty1 && y2 <= recty2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
147 return SDL_TRUE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
148 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
149
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
150 /* Check to see if entire line is outside rect */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
151 if ((x1 < rectx1 && x2 < rectx1) || (x1 > rectx2 && x2 > rectx2) ||
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
152 (y1 < recty1 && y2 < recty2) || (y1 > recty2 && y2 > recty2)) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
153 return SDL_FALSE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
154 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
155
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
156 if (y1 = y2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
157 /* Horizontal line, easy to clip */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
158 if (x1 < rectx1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
159 *X1 = rectx1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
160 } else if (x1 > rectx2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
161 *X1 = rectx2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
162 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
163 if (x2 < rectx1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
164 *X2 = rectx1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
165 } else if (x2 > rectx2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
166 *X2 = rectx2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
167 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
168 return SDL_TRUE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
169 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
170
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
171 if (x1 == x2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
172 /* Vertical line, easy to clip */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
173 if (y1 < recty1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
174 *Y1 = recty1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
175 } else if (y1 > recty2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
176 *Y1 = recty2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
177 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
178 if (y2 < recty1) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
179 *Y2 = recty1;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
180 } else if (y2 > recty2) {
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
181 *Y2 = recty2;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
182 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
183 return SDL_TRUE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
184 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
185
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
186 /* FIXME: need code to clip diagonal line to rect */
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
187 return SDL_FALSE;
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
188 }
3da0bb421d83 Added line clipping
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
189
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
190 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
191 SDL_AddDirtyRect(SDL_DirtyRectList * list, const SDL_Rect * rect)
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
192 {
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
193 SDL_DirtyRect *dirty;
2223
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
194
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
195 /* FIXME: At what point is this optimization too expensive? */
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
196 for (dirty = list->list; dirty; dirty = dirty->next) {
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
197 if (SDL_HasIntersection(&dirty->rect, rect)) {
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
198 SDL_UnionRect(&dirty->rect, rect, &dirty->rect);
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
199 return;
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
200 }
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
201 }
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
202
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
203 if (list->free) {
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
204 dirty = list->free;
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
205 list->free = dirty->next;
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
206 } else {
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
207 dirty = (SDL_DirtyRect *) SDL_malloc(sizeof(*dirty));
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
208 if (!dirty) {
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
209 return;
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
210 }
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
211 }
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
212 dirty->rect = *rect;
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
213 dirty->next = list->list;
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
214 list->list = dirty;
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
215 }
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
216
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
217 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
218 SDL_ClearDirtyRects(SDL_DirtyRectList * list)
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
219 {
2224
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
220 SDL_DirtyRect *prev, *curr;
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
221
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
222 /* Skip to the end of the free list */
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
223 prev = NULL;
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
224 for (curr = list->free; curr; curr = curr->next) {
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
225 prev = curr;
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
226 }
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
227
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
228 /* Add the list entries to the end */
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
229 if (prev) {
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
230 prev->next = list->list;
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
231 } else {
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
232 list->free = list->list;
29cc9e9c76bd Whoops, it's not quite that easy - fixed bug in SDL_ClearDirtyRects()
Sam Lantinga <slouken@libsdl.org>
parents: 2223
diff changeset
233 }
2223
175754591a13 Optimized dirty rect code
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
234 list->list = NULL;
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
235 }
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
236
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
237 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
238 SDL_FreeDirtyRects(SDL_DirtyRectList * list)
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
239 {
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
240 while (list->list) {
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
241 SDL_DirtyRect *elem = list->list;
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
242 list->list = elem->next;
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
243 SDL_free(elem);
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
244 }
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
245 while (list->free) {
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
246 SDL_DirtyRect *elem = list->free;
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
247 list->free = elem->next;
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
248 SDL_free(elem);
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
249 }
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
250 }
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
251
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
252 /* vi: set ts=4 sw=4 expandtab: */