FIFE 2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2005-2008 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 /*************************************************************************** 00023 00024 Rectangle intersection code copied and modified from the guichan 0.4 00025 source, which is released under the BSD license. 00026 00027 Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved. 00028 00029 * Redistribution and use in source and binary forms, with or without modification, 00030 are permitted provided that the following conditions are met: 00031 Redistributions of source code must retain the above copyright notice, 00032 this list of conditions and the following disclaimer. 00033 00034 * Redistributions in binary form must reproduce the above copyright notice, 00035 this list of conditions and the following disclaimer in the documentation 00036 and/or other materials provided with the distribution. 00037 00038 * Neither the name of the Guichan nor the names of its contributors may be used 00039 to endorse or promote products derived from this software without specific 00040 prior written permission. 00041 00042 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 00043 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00044 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 00045 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00046 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00047 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00048 AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00049 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00050 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00051 00052 For more Information about guichan see: http://guichan.sourceforge.net 00053 00054 ****************************************************************************/ 00055 00056 #ifndef FIFE_VIDEO_RECT_H 00057 #define FIFE_VIDEO_RECT_H 00058 00059 // Standard C++ library includes 00060 #include <iostream> 00061 00062 // 3rd party library includes 00063 00064 // FIFE includes 00065 // These includes are split up in two parts, separated by one empty line 00066 // First block: files included from the FIFE root src directory 00067 // Second block: files included from the same folder 00068 #include "point.h" 00069 00070 namespace FIFE { 00071 00079 template <typename T> 00080 class RectType { 00081 public: 00084 T x; 00087 T y; 00090 T w; 00093 T h; 00094 00099 explicit RectType(T x = 0, T y = 0, T w = 0, T h = 0) : x(x), y(y), w(w), h(h) { 00100 } 00101 00104 T right() const; 00105 00108 T bottom() const; 00109 00115 bool operator==(const RectType<T>& rect ) const; 00116 00122 bool contains( const PointType2D<T>& point ) const; 00123 00131 bool intersects( const RectType<T>& rect ) const; 00132 00139 bool intersectInplace( const RectType<T>& rect ); 00140 00141 }; 00142 00148 template<typename T> 00149 std::ostream& operator<<(std::ostream& os, const RectType<T>& r) { 00150 return 00151 os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")"; 00152 } 00153 00155 00156 template<typename T> 00157 inline T RectType<T>::right() const { 00158 return x + w; 00159 } 00160 00161 template<typename T> 00162 inline T RectType<T>::bottom() const { 00163 return y + h; 00164 } 00165 00166 template<typename T> 00167 inline bool RectType<T>::operator==(const RectType<T>& rect ) const { 00168 return 00169 x == rect.x && y == rect.y && w == rect.w && h == rect.h; 00170 } 00171 00172 template<typename T> 00173 inline bool RectType<T>::contains( const PointType2D<T>& point ) const { 00174 return 00175 (((point.x >= x) && (point.x <= x + w)) 00176 && ((point.y >= y) && (point.y <= y + h))); 00177 } 00178 00179 00180 template<typename T> 00181 inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) { 00182 x = x - rectangle.x; 00183 y = y - rectangle.y; 00184 00185 00186 if (x < 0) { 00187 w += x; 00188 x = 0; 00189 } 00190 00191 if (y < 0) { 00192 h += y; 00193 y = 0; 00194 } 00195 00196 if (x + w > rectangle.w) { 00197 w = rectangle.w - x; 00198 } 00199 00200 if (y + h > rectangle.h) { 00201 h = rectangle.h - y; 00202 } 00203 00204 x += rectangle.x; 00205 y += rectangle.y; 00206 00207 if (w <= 0 || h <= 0) { 00208 h = 0; 00209 w = 0; 00210 return false; 00211 } 00212 return true; 00213 } 00214 00215 template<typename T> 00216 inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const { 00217 T _x = x - rectangle.x; 00218 T _y = y - rectangle.y; 00219 T _w = w; 00220 T _h = h; 00221 00222 00223 if (_x < 0) { 00224 _w += _x; 00225 _x = 0; 00226 } 00227 00228 if (_y < 0) { 00229 _h += _y; 00230 _y = 0; 00231 } 00232 00233 if (_x + _w > rectangle.w) { 00234 _w = rectangle.w - _x; 00235 } 00236 00237 if (_y + _h > rectangle.h) { 00238 _h = rectangle.h - _y; 00239 } 00240 00241 if (_w <= 0 || _h <= 0) { 00242 return false; 00243 } 00244 return true; 00245 } 00246 00247 typedef RectType<int> Rect; 00248 typedef RectType<float> FloatRect; 00249 typedef RectType<double> DoubleRect; 00250 00251 00252 } 00253 00254 #endif