| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2005-2019 by the FIFE team * |
| 3 |
* http://www.fifengine.net * |
| 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 |
|
| 72 |
/** A Rectangle on screen. |
| 73 |
* |
| 74 |
* This is a small helper class used for screen coordinate arithmetics. |
| 75 |
* The same thoughts reasong using @b int32_t as value type as in Point apply. |
| 76 |
* |
| 77 |
* @see Point |
| 78 |
*/ |
| 79 |
template <typename T> |
| 80 |
class RectType { |
| 81 |
public: |
| 82 |
/** The X Coordinate. |
| 83 |
*/ |
| 84 |
T x; |
| 85 |
/** The Y Coordinate. |
| 86 |
*/ |
| 87 |
T y; |
| 88 |
/** Width of the rectangle. |
| 89 |
*/ |
| 90 |
T w; |
| 91 |
/** Height of the rectangle. |
| 92 |
*/ |
| 93 |
T h; |
| 94 |
|
| 95 |
/** Constructor. |
| 96 |
* |
| 97 |
* Creates a new Rect with the values defaulting to 0. |
| 98 |
*/ |
| 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 |
|
| 102 |
/** Constructor. |
| 103 |
* |
| 104 |
* Creates a new Rect of type T from the given Rect of type U |
| 105 |
*/ |
| 106 |
template<typename U> |
| 107 |
explicit RectType(const RectType<U>& r) |
| 108 |
: x(static_cast<T>(r.x)), |
| 109 |
y(static_cast<T>(r.y)), |
| 110 |
w(static_cast<T>(r.w)), |
| 111 |
h(static_cast<T>(r.h)) { |
| 112 |
} |
| 113 |
|
| 114 |
/** The X coordinate of the right edge. |
| 115 |
*/ |
| 116 |
T right() const; |
| 117 |
|
| 118 |
/** The Y coordinate of the bottom edge. |
| 119 |
*/ |
| 120 |
T bottom() const; |
| 121 |
|
| 122 |
/** Equivalence operator. |
| 123 |
* |
| 124 |
* @param rect The rectangle to which this is compared. |
| 125 |
* @return True only if both rectangle values are all equal. |
| 126 |
*/ |
| 127 |
bool operator==(const RectType<T>& rect ) const; |
| 128 |
|
| 129 |
/** Checks whether a rectangle contains a Point. |
| 130 |
* |
| 131 |
* @param point The point that is checked. |
| 132 |
* @return True if the point lies inside the rectangle or on one of its borders. |
| 133 |
*/ |
| 134 |
bool contains( const PointType2D<T>& point ) const; |
| 135 |
|
| 136 |
/** Check whether two rectangles share some area. |
| 137 |
* |
| 138 |
* @param rect The other rectangle that is checked. |
| 139 |
* @return True, if and only if both rectangles have some covered area in common. |
| 140 |
* This includes edges that cover each other. |
| 141 |
* @note This operation is commutative. |
| 142 |
*/ |
| 143 |
bool intersects( const RectType<T>& rect ) const; |
| 144 |
|
| 145 |
/** Calculate rectangle intersection in place |
| 146 |
* |
| 147 |
* @param rect The other rectangle that is checked. |
| 148 |
* @return True, if and only if both rectangles have some covered area in common. |
| 149 |
* This includes edges that cover each other. |
| 150 |
*/ |
| 151 |
bool intersectInplace( const RectType<T>& rect ); |
| 152 |
}; |
| 153 |
|
| 154 |
/** Stream output operator. |
| 155 |
* |
| 156 |
* Useful for debugging purposes, this will output the coordinates |
| 157 |
* of the rectangle to the stream. |
| 158 |
*/ |
| 159 |
template<typename T> |
| 160 |
std::ostream& operator<<(std::ostream& os, const RectType<T>& r) { |
| 161 |
return |
| 162 |
os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")"; |
| 163 |
} |
| 164 |
|
| 165 |
//////////////// INLINE FUNCTIONS ///////////////////////// |
| 166 |
|
| 167 |
template<typename T> |
| 168 |
inline T RectType<T>::right() const { |
| 169 |
return x + w; |
| 170 |
} |
| 171 |
|
| 172 |
template<typename T> |
| 173 |
inline T RectType<T>::bottom() const { |
| 174 |
return y + h; |
| 175 |
} |
| 176 |
|
| 177 |
template<typename T> |
| 178 |
inline bool RectType<T>::operator==(const RectType<T>& rect ) const { |
| 179 |
return |
| 180 |
x == rect.x && y == rect.y && w == rect.w && h == rect.h; |
| 181 |
} |
| 182 |
|
| 183 |
template<typename T> |
| 184 |
inline bool RectType<T>::contains( const PointType2D<T>& point ) const { |
| 185 |
return |
| 186 |
(((point.x >= x) && (point.x <= x + w)) |
| 187 |
&& ((point.y >= y) && (point.y <= y + h))); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
template<typename T> |
| 192 |
inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) { |
| 193 |
x = x - rectangle.x; |
| 194 |
y = y - rectangle.y; |
| 195 |
|
| 196 |
|
| 197 |
if (x < 0) { |
| 198 |
w += x; |
| 199 |
x = 0; |
| 200 |
} |
| 201 |
|
| 202 |
if (y < 0) { |
| 203 |
h += y; |
| 204 |
y = 0; |
| 205 |
} |
| 206 |
|
| 207 |
if (x + w > rectangle.w) { |
| 208 |
w = rectangle.w - x; |
| 209 |
} |
| 210 |
|
| 211 |
if (y + h > rectangle.h) { |
| 212 |
h = rectangle.h - y; |
| 213 |
} |
| 214 |
|
| 215 |
x += rectangle.x; |
| 216 |
y += rectangle.y; |
| 217 |
|
| 218 |
if (w <= 0 || h <= 0) { |
| 219 |
h = 0; |
| 220 |
w = 0; |
| 221 |
return false; |
| 222 |
} |
| 223 |
return true; |
| 224 |
} |
| 225 |
|
| 226 |
template<typename T> |
| 227 |
inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const { |
| 228 |
T _x = x - rectangle.x; |
| 229 |
T _y = y - rectangle.y; |
| 230 |
T _w = w; |
| 231 |
T _h = h; |
| 232 |
|
| 233 |
|
| 234 |
if (_x < 0) { |
| 235 |
_w += _x; |
| 236 |
_x = 0; |
| 237 |
} |
| 238 |
|
| 239 |
if (_y < 0) { |
| 240 |
_h += _y; |
| 241 |
_y = 0; |
| 242 |
} |
| 243 |
|
| 244 |
if (_x + _w > rectangle.w) { |
| 245 |
_w = rectangle.w - _x; |
| 246 |
} |
| 247 |
|
| 248 |
if (_y + _h > rectangle.h) { |
| 249 |
_h = rectangle.h - _y; |
| 250 |
} |
| 251 |
|
| 252 |
if (_w <= 0 || _h <= 0) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
typedef RectType<int32_t> Rect; |
| 259 |
typedef RectType<float> FloatRect; |
| 260 |
typedef RectType<double> DoubleRect; |
| 261 |
|
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
#endif |