FIFE
2008.0
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
All
Classes
Namespaces
Functions
Variables
Enumerations
Enumerator
Pages
rect.h
1
/***************************************************************************
2
* Copyright (C) 2005-2008 by the FIFE team *
3
* http://www.fifengine.de *
4
* This file is part of FIFE. *
5
* *
6
* FIFE is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Lesser General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2.1 of the License, or (at your option) any later version. *
10
* *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14
* Lesser General Public License for more details. *
15
* *
16
* You should have received a copy of the GNU Lesser General Public *
17
* License along with this library; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20
***************************************************************************/
21
22
/***************************************************************************
23
24
Rectangle intersection code copied and modified from the guichan 0.4
25
source, which is released under the BSD license.
26
27
Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved.
28
29
* Redistribution and use in source and binary forms, with or without modification,
30
are permitted provided that the following conditions are met:
31
Redistributions of source code must retain the above copyright notice,
32
this list of conditions and the following disclaimer.
33
34
* Redistributions in binary form must reproduce the above copyright notice,
35
this list of conditions and the following disclaimer in the documentation
36
and/or other materials provided with the distribution.
37
38
* Neither the name of the Guichan nor the names of its contributors may be used
39
to endorse or promote products derived from this software without specific
40
prior written permission.
41
42
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
43
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
44
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
45
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
46
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
47
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
48
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
50
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51
52
For more Information about guichan see: http://guichan.sourceforge.net
53
54
****************************************************************************/
55
56
#ifndef FIFE_VIDEO_RECT_H
57
#define FIFE_VIDEO_RECT_H
58
59
// Standard C++ library includes
60
#include <iostream>
61
62
// 3rd party library includes
63
64
// FIFE includes
65
// These includes are split up in two parts, separated by one empty line
66
// First block: files included from the FIFE root src directory
67
// Second block: files included from the same folder
68
#include "point.h"
69
70
namespace
FIFE {
71
79
template
<
typename
T>
80
class
RectType
{
81
public
:
84
T
x
;
87
T
y
;
90
T
w
;
93
T
h
;
94
99
explicit
RectType
(T
x
= 0, T
y
= 0, T
w
= 0, T
h
= 0) :
x
(
x
),
y
(
y
),
w
(
w
),
h
(
h
) {
100
}
101
104
T
right
()
const
;
105
108
T
bottom
()
const
;
109
115
bool
operator==
(
const
RectType<T>
& rect )
const
;
116
122
bool
contains
(
const
PointType2D<T>
& point )
const
;
123
131
bool
intersects
(
const
RectType<T>
& rect )
const
;
132
139
bool
intersectInplace
(
const
RectType<T>
& rect );
140
141
};
142
148
template
<
typename
T>
149
std::ostream& operator<<(std::ostream& os, const RectType<T>& r) {
150
return
151
os <<
"("
<<r.x<<
","
<<r.y<<
")-("
<<r.w<<
","
<<r.h<<
")"
;
152
}
153
155
156
template
<
typename
T>
157
inline
T
RectType<T>::right
()
const
{
158
return
x + w;
159
}
160
161
template
<
typename
T>
162
inline
T
RectType<T>::bottom
()
const
{
163
return
y + h;
164
}
165
166
template
<
typename
T>
167
inline
bool
RectType<T>::operator==
(
const
RectType<T>
& rect )
const
{
168
return
169
x == rect.
x
&& y == rect.
y
&& w == rect.
w
&& h == rect.
h
;
170
}
171
172
template
<
typename
T>
173
inline
bool
RectType<T>::contains
(
const
PointType2D<T>
& point )
const
{
174
return
175
(((point.x >= x) && (point.x <= x + w))
176
&& ((point.y >= y) && (point.y <= y + h)));
177
}
178
179
180
template
<
typename
T>
181
inline
bool
RectType<T>::intersectInplace
(
const
RectType<T>
& rectangle ) {
182
x = x - rectangle.
x
;
183
y = y - rectangle.
y
;
184
185
186
if
(x < 0) {
187
w += x;
188
x = 0;
189
}
190
191
if
(y < 0) {
192
h += y;
193
y = 0;
194
}
195
196
if
(x + w > rectangle.
w
) {
197
w = rectangle.
w
- x;
198
}
199
200
if
(y + h > rectangle.
h
) {
201
h = rectangle.
h
- y;
202
}
203
204
x += rectangle.
x
;
205
y += rectangle.
y
;
206
207
if
(w <= 0 || h <= 0) {
208
h = 0;
209
w = 0;
210
return
false
;
211
}
212
return
true
;
213
}
214
215
template
<
typename
T>
216
inline
bool
RectType<T>::intersects
(
const
RectType<T>
& rectangle )
const
{
217
T _x = x - rectangle.
x
;
218
T _y = y - rectangle.
y
;
219
T _w = w;
220
T _h = h;
221
222
223
if
(_x < 0) {
224
_w += _x;
225
_x = 0;
226
}
227
228
if
(_y < 0) {
229
_h += _y;
230
_y = 0;
231
}
232
233
if
(_x + _w > rectangle.
w
) {
234
_w = rectangle.
w
- _x;
235
}
236
237
if
(_y + _h > rectangle.
h
) {
238
_h = rectangle.
h
- _y;
239
}
240
241
if
(_w <= 0 || _h <= 0) {
242
return
false
;
243
}
244
return
true
;
245
}
246
247
typedef
RectType<int>
Rect
;
248
typedef
RectType<float>
FloatRect
;
249
typedef
RectType<double>
DoubleRect
;
250
251
252
}
253
254
#endif
engine
core
util
structures
rect.h
Generated on Fri Jun 8 2012 20:34:14 for FIFE by
1.8.1