Mercurial > fife-parpg
comparison engine/core/util/structures/rect.h @ 622:c0c3f64bfc2d
* Templatized Rect to extend it's functionality beyond integers
* Added some typedefs: Rect, FloatRect, DoubleRect
* Removed the Rect class from the fife_math extensions
* Modified the shooter demo to use the FIFE rect template class
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 01 Oct 2010 15:32:55 +0000 |
parents | 90005975cdbb |
children |
comparison
equal
deleted
inserted
replaced
621:356634098bd9 | 622:c0c3f64bfc2d |
---|---|
25 source, which is released under the BSD license. | 25 source, which is released under the BSD license. |
26 | 26 |
27 Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved. | 27 Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved. |
28 | 28 |
29 * Redistribution and use in source and binary forms, with or without modification, | 29 * Redistribution and use in source and binary forms, with or without modification, |
30 are permitted provided that the following conditions are met: | 30 are permitted provided that the following conditions are met: |
31 Redistributions of source code must retain the above copyright notice, | 31 Redistributions of source code must retain the above copyright notice, |
32 this list of conditions and the following disclaimer. | 32 this list of conditions and the following disclaimer. |
33 | 33 |
34 * Redistributions in binary form must reproduce the above copyright notice, | 34 * Redistributions in binary form must reproduce the above copyright notice, |
35 this list of conditions and the following disclaimer in the documentation | 35 this list of conditions and the following disclaimer in the documentation |
74 * This is a small helper class used for screen coordinate arithmetics. | 74 * This is a small helper class used for screen coordinate arithmetics. |
75 * The same thoughts reasong using @b int as value type as in Point apply. | 75 * The same thoughts reasong using @b int as value type as in Point apply. |
76 * | 76 * |
77 * @see Point | 77 * @see Point |
78 */ | 78 */ |
79 class Rect { | 79 template <typename T> |
80 class RectType { | |
80 public: | 81 public: |
81 /** The X Coordinate. | 82 /** The X Coordinate. |
82 */ | 83 */ |
83 int x; | 84 T x; |
84 /** The Y Coordinate. | 85 /** The Y Coordinate. |
85 */ | 86 */ |
86 int y; | 87 T y; |
87 /** Width of the rectangle. | 88 /** Width of the rectangle. |
88 */ | 89 */ |
89 int w; | 90 T w; |
90 /** Height of the rectangle. | 91 /** Height of the rectangle. |
91 */ | 92 */ |
92 int h; | 93 T h; |
93 | 94 |
94 /** Constructor. | 95 /** Constructor. |
95 * | 96 * |
96 * Creates a new Rect with the values defaulting to 0. | 97 * Creates a new Rect with the values defaulting to 0. |
97 */ | 98 */ |
98 explicit Rect(int x = 0, int y = 0, unsigned int width = 0, unsigned int height = 0); | 99 explicit RectType(T x = 0, T y = 0, T w = 0, T h = 0) : x(x), y(y), w(w), h(h) { |
100 } | |
99 | 101 |
100 /** The X coordinate of the right edge. | 102 /** The X coordinate of the right edge. |
101 */ | 103 */ |
102 int right() const; | 104 T right() const; |
103 | 105 |
104 /** The Y coordinate of the bottom edge. | 106 /** The Y coordinate of the bottom edge. |
105 */ | 107 */ |
106 int bottom() const; | 108 T bottom() const; |
107 | 109 |
108 /** Equivalence operator. | 110 /** Equivalence operator. |
109 * | 111 * |
110 * @param rect The rectangle to which this is compared. | 112 * @param rect The rectangle to which this is compared. |
111 * @return True only if both rectangle values are all equal. | 113 * @return True only if both rectangle values are all equal. |
112 */ | 114 */ |
113 bool operator==(const Rect& rect ) const; | 115 bool operator==(const RectType<T>& rect ) const; |
114 | 116 |
115 /** Checks whether a rectangle contains a Point. | 117 /** Checks whether a rectangle contains a Point. |
116 * | 118 * |
117 * @param p The point that is checked. | 119 * @param p The point that is checked. |
118 * @return True if the point lies inside the rectangle or on one of its borders. | 120 * @return True if the point lies inside the rectangle or on one of its borders. |
119 */ | 121 */ |
120 bool contains( const Point& point ) const; | 122 bool contains( const PointType2D<T>& point ) const; |
121 | 123 |
122 /** Check whether two rectangles share some area. | 124 /** Check whether two rectangles share some area. |
123 * | 125 * |
124 * @param rect The other rectangle that is checked. | 126 * @param rect The other rectangle that is checked. |
125 * @return True, if and only if both rectangles have some covered area in common. | 127 * @return True, if and only if both rectangles have some covered area in common. |
126 * This includes edges that cover each other. | 128 * This includes edges that cover each other. |
127 * @note This operation is commutative. | 129 * @note This operation is commutative. |
128 */ | 130 */ |
129 bool intersects( const Rect& rect ) const; | 131 bool intersects( const RectType<T>& rect ) const; |
130 | 132 |
131 /** Calculate rectangle intersection in place | 133 /** Calculate rectangle intersection in place |
132 * | 134 * |
133 * @param rect The other rectangle that is checked. | 135 * @param rect The other rectangle that is checked. |
134 * @return True, if and only if both rectangles have some covered area in common. | 136 * @return True, if and only if both rectangles have some covered area in common. |
135 * This includes edges that cover each other. | 137 * This includes edges that cover each other. |
136 */ | 138 */ |
137 bool intersectInplace( const Rect& rect ); | 139 bool intersectInplace( const RectType<T>& rect ); |
138 | 140 |
139 }; | 141 }; |
140 | 142 |
141 /** Stream output operator. | 143 /** Stream output operator. |
142 * | 144 * |
143 * Useful for debugging purposes, this will output the coordinates | 145 * Useful for debugging purposes, this will output the coordinates |
144 * of the rectangle to the stream. | 146 * of the rectangle to the stream. |
145 */ | 147 */ |
146 std::ostream& operator<<(std::ostream&, const Rect&); | 148 template<typename T> |
147 | 149 std::ostream& operator<<(std::ostream& os, const RectType<T>& r) { |
150 return | |
151 os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")"; | |
152 } | |
148 | 153 |
149 //////////////// INLINE FUNCTIONS ///////////////////////// | 154 //////////////// INLINE FUNCTIONS ///////////////////////// |
150 | 155 |
151 inline | 156 template<typename T> |
152 int Rect::right() const { | 157 inline T RectType<T>::right() const { |
153 return x + w; | 158 return x + w; |
154 } | 159 } |
155 | 160 |
156 inline | 161 template<typename T> |
157 int Rect::bottom() const { | 162 inline T RectType<T>::bottom() const { |
158 return y + h; | 163 return y + h; |
159 } | 164 } |
160 | 165 |
161 | 166 template<typename T> |
162 inline | 167 inline bool RectType<T>::operator==(const RectType<T>& rect ) const { |
163 bool Rect::operator==(const Rect& rect ) const { | |
164 return | 168 return |
165 x == rect.x && y == rect.y && w == rect.w && h == rect.h; | 169 x == rect.x && y == rect.y && w == rect.w && h == rect.h; |
166 } | 170 } |
167 | 171 |
168 | 172 template<typename T> |
169 inline | 173 inline bool RectType<T>::contains( const PointType2D<T>& point ) const { |
170 bool Rect::contains( const Point& point ) const { | |
171 return | 174 return |
172 (((point.x >= x) && (point.x <= x + w)) | 175 (((point.x >= x) && (point.x <= x + w)) |
173 && ((point.y >= y) && (point.y <= y + h))); | 176 && ((point.y >= y) && (point.y <= y + h))); |
174 } | 177 } |
175 | 178 |
176 | 179 |
177 inline | 180 template<typename T> |
178 bool Rect::intersectInplace( const Rect& rectangle ) { | 181 inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) { |
179 x = x - rectangle.x; | 182 x = x - rectangle.x; |
180 y = y - rectangle.y; | 183 y = y - rectangle.y; |
181 | 184 |
182 | 185 |
183 if (x < 0) { | 186 if (x < 0) { |
184 w += x; | 187 w += x; |
185 x = 0; | 188 x = 0; |
186 } | 189 } |
187 | 190 |
188 if (y < 0) { | 191 if (y < 0) { |
189 h += y; | 192 h += y; |
190 y = 0; | 193 y = 0; |
191 } | 194 } |
192 | 195 |
193 if (x + w > rectangle.w) { | 196 if (x + w > rectangle.w) { |
194 w = rectangle.w - x; | 197 w = rectangle.w - x; |
195 } | 198 } |
196 | 199 |
197 if (y + h > rectangle.h) { | 200 if (y + h > rectangle.h) { |
198 h = rectangle.h - y; | 201 h = rectangle.h - y; |
199 } | 202 } |
200 | 203 |
201 x += rectangle.x; | 204 x += rectangle.x; |
202 y += rectangle.y; | 205 y += rectangle.y; |
203 | 206 |
204 if (w <= 0 || h <= 0) { | 207 if (w <= 0 || h <= 0) { |
205 h = 0; | 208 h = 0; |
207 return false; | 210 return false; |
208 } | 211 } |
209 return true; | 212 return true; |
210 } | 213 } |
211 | 214 |
212 | 215 template<typename T> |
213 inline | 216 inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const { |
214 bool Rect::intersects( const Rect& rectangle ) const { | 217 T _x = x - rectangle.x; |
215 int _x = x - rectangle.x; | 218 T _y = y - rectangle.y; |
216 int _y = y - rectangle.y; | 219 T _w = w; |
217 int _w = w; | 220 T _h = h; |
218 int _h = h; | 221 |
219 | 222 |
220 | |
221 if (_x < 0) { | 223 if (_x < 0) { |
222 _w += _x; | 224 _w += _x; |
223 _x = 0; | 225 _x = 0; |
224 } | 226 } |
225 | 227 |
226 if (_y < 0) { | 228 if (_y < 0) { |
227 _h += _y; | 229 _h += _y; |
228 _y = 0; | 230 _y = 0; |
229 } | 231 } |
230 | 232 |
231 if (_x + _w > rectangle.w) { | 233 if (_x + _w > rectangle.w) { |
232 _w = rectangle.w - _x; | 234 _w = rectangle.w - _x; |
233 } | 235 } |
234 | 236 |
235 if (_y + _h > rectangle.h) { | 237 if (_y + _h > rectangle.h) { |
236 _h = rectangle.h - _y; | 238 _h = rectangle.h - _y; |
237 } | 239 } |
238 | 240 |
239 if (_w <= 0 || _h <= 0) { | 241 if (_w <= 0 || _h <= 0) { |
240 return false; | 242 return false; |
241 } | 243 } |
242 return true; | 244 return true; |
243 } | 245 } |
244 | 246 |
247 typedef RectType<int> Rect; | |
248 typedef RectType<float> FloatRect; | |
249 typedef RectType<double> DoubleRect; | |
250 | |
251 | |
245 } | 252 } |
246 | 253 |
247 #endif | 254 #endif |