changeset 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 356634098bd9
children 684e5537eef7
files demos/shooter/scripts/common/baseobject.py demos/shooter/scripts/scene.py demos/shooter/scripts/ships/enemies.py engine/core/model/structures/map.h engine/core/util/structures/rect.cpp engine/core/util/structures/rect.h engine/core/util/structures/utilstructures.i engine/core/video/image.h engine/core/view/camera.h engine/python/fife/extensions/fife_math.py engine/swigwrappers/python/extensions.i.templ
diffstat 11 files changed, 116 insertions(+), 195 deletions(-) [+]
line wrap: on
line diff
--- a/demos/shooter/scripts/common/baseobject.py	Fri Oct 01 14:09:47 2010 +0000
+++ b/demos/shooter/scripts/common/baseobject.py	Fri Oct 01 15:32:55 2010 +0000
@@ -23,7 +23,7 @@
 
 from fife import fife
 from fife.extensions.fife_math import normalize
-from fife.extensions.fife_math import Rect
+from fife.fife import FloatRect as Rect
 
 
 SHTR_DEFAULT = 0
--- a/demos/shooter/scripts/scene.py	Fri Oct 01 14:09:47 2010 +0000
+++ b/demos/shooter/scripts/scene.py	Fri Oct 01 15:32:55 2010 +0000
@@ -26,7 +26,7 @@
 from scripts.ships.player import Player
 from scripts.ships.enemies import *
 from scripts.powerups import *
-from fife.extensions.fife_math import Rect
+from fife.fife import FloatRect as Rect
 
 class SceneNode(object):
 	"""
--- a/demos/shooter/scripts/ships/enemies.py	Fri Oct 01 14:09:47 2010 +0000
+++ b/demos/shooter/scripts/ships/enemies.py	Fri Oct 01 15:32:55 2010 +0000
@@ -24,7 +24,7 @@
 from fife import fife
 from scripts.ships.shipbase import *
 from scripts.common.baseobject import *
-from fife.extensions.fife_math import Rect
+from fife.fife import FloatRect as Rect
 from scripts.weapons import *
 
 
--- a/engine/core/model/structures/map.h	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/core/model/structures/map.h	Fri Oct 01 15:32:55 2010 +0000
@@ -36,6 +36,7 @@
 #include "util/base/resourceclass.h"
 #include "util/resource/resource.h"
 #include "model/metamodel/timeprovider.h"
+#include "util/structures/rect.h"
 
 #include "location.h"
 
@@ -49,7 +50,6 @@
 	class CellGrid;
 	class Map;
 	class Camera;
-	class Rect;
 
 	/** Listener interface for changes happening on map
 	 */
--- a/engine/core/util/structures/rect.cpp	Fri Oct 01 14:09:47 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,42 +0,0 @@
-/***************************************************************************
- *   Copyright (C) 2005-2008 by the FIFE team                              *
- *   http://www.fifengine.de                                               *
- *   This file is part of FIFE.                                            *
- *                                                                         *
- *   FIFE is free software; you can redistribute it and/or                 *
- *   modify it under the terms of the GNU Lesser General Public            *
- *   License as published by the Free Software Foundation; either          *
- *   version 2.1 of the License, or (at your option) any later version.    *
- *                                                                         *
- *   This library is distributed in the hope that it will be useful,       *
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
- *   Lesser General Public License for more details.                       *
- *                                                                         *
- *   You should have received a copy of the GNU Lesser General Public      *
- *   License along with this library; if not, write to the                 *
- *   Free Software Foundation, Inc.,                                       *
- *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
- ***************************************************************************/
-
-// Standard C++ library includes
-
-// 3rd party library includes
-
-// FIFE includes
-// These includes are split up in two parts, separated by one empty line
-// First block: files included from the FIFE root src directory
-// Second block: files included from the same folder
-#include "rect.h"
-
-namespace FIFE {
-
-	Rect::Rect(int x, int y, unsigned int w, unsigned int h) : x(x), y(y), w(w), h(h) {
-	}
-
-	std::ostream& operator<<(std::ostream& os, const Rect& r) {
-		return
-			os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")";
-	}
-
-}
--- a/engine/core/util/structures/rect.h	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/core/util/structures/rect.h	Fri Oct 01 15:32:55 2010 +0000
@@ -27,7 +27,7 @@
     Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson All rights reserved.
 
     * Redistribution and use in source and binary forms, with or without modification,
-      are permitted provided that the following conditions are met: 
+      are permitted provided that the following conditions are met:
       Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
 
@@ -76,48 +76,50 @@
 	 *
 	 * @see Point
 	 */
-	class Rect {
+	template <typename T>
+	class RectType {
 		public:
 			/** The X Coordinate.
 			 */
-			int x;
+			T x;
 			/** The Y Coordinate.
 			 */
-			int y;
+			T y;
 			/** Width of the rectangle.
 			 */
-			int w;
+			T w;
 			/** Height of the rectangle.
 			 */
-			int h;
+			T h;
 
 			/** Constructor.
-			 * 
+			 *
 			 * Creates a new Rect with the values defaulting to 0.
 			 */
-			explicit Rect(int x = 0, int y = 0, unsigned int width = 0, unsigned int height = 0);
+			explicit RectType(T x = 0, T y = 0, T w = 0, T h = 0) : x(x), y(y), w(w), h(h) {
+			}
 
 			/** The X coordinate of the right edge.
 			 */
-			int right() const;
+			T right() const;
 
 			/** The Y coordinate of the bottom edge.
 			 */
-			int bottom() const;
+			T bottom() const;
 
 			/** Equivalence operator.
 			 *
 			 * @param rect The rectangle to which this is compared.
 			 * @return True only if both rectangle values are all equal.
 			 */
-			bool operator==(const Rect& rect ) const;
+			bool operator==(const RectType<T>& rect ) const;
 
 			/** Checks whether a rectangle contains a Point.
 			 *
 			 * @param p The point that is checked.
 			 * @return True if the point lies inside the rectangle or on one of its borders.
 			 */
-			bool contains( const Point& point ) const;
+			bool contains( const PointType2D<T>& point ) const;
 
 			/** Check whether two rectangles share some area.
 			 *
@@ -126,7 +128,7 @@
 			 * This includes edges that cover each other.
 			 * @note This operation is commutative.
 			 */
-			bool intersects( const Rect& rect ) const;
+			bool intersects( const RectType<T>& rect ) const;
 
 			/** Calculate rectangle intersection in place
 			 *
@@ -134,7 +136,7 @@
 			 * @return True, if and only if both rectangles have some covered area in common.
 			 * This includes edges that cover each other.
 			 */
-			bool intersectInplace( const Rect& rect );
+			bool intersectInplace( const RectType<T>& rect );
 
 	};
 
@@ -143,61 +145,62 @@
 	 * Useful for debugging purposes, this will output the coordinates
 	 * of the rectangle to the stream.
 	 */
-	std::ostream& operator<<(std::ostream&, const Rect&);
-
+	template<typename T>
+	std::ostream& operator<<(std::ostream& os, const RectType<T>& r) {
+		return
+			os << "("<<r.x<<","<<r.y<<")-("<<r.w<<","<<r.h<<")";
+	}
 
 	//////////////// INLINE FUNCTIONS /////////////////////////
 
-	inline 
-	int Rect::right() const {
+	template<typename T>
+	inline T RectType<T>::right() const {
 		return x + w;
 	}
 
-	inline 
-	int Rect::bottom() const {
+	template<typename T>
+	inline T RectType<T>::bottom() const {
 		return y + h;
 	}
 
-
-	inline 
-	bool Rect::operator==(const Rect& rect ) const {
+	template<typename T>
+	inline bool RectType<T>::operator==(const RectType<T>& rect ) const {
 		return
 			x == rect.x && y == rect.y && w == rect.w && h == rect.h;
 	}
 
-
-	inline
-	bool Rect::contains( const Point& point ) const {
+	template<typename T>
+	inline bool RectType<T>::contains( const PointType2D<T>& point ) const {
 		return
 			(((point.x >= x) && (point.x <= x + w))
 				&& ((point.y >= y) && (point.y <= y + h)));
 	}
 
 
-	inline
-	bool Rect::intersectInplace( const Rect& rectangle ) {
+	template<typename T>
+	inline bool RectType<T>::intersectInplace( const RectType<T>& rectangle ) {
 		x = x - rectangle.x;
 		y = y - rectangle.y;
 
-	
+
 		if (x < 0) {
 			w += x;
 			x = 0;
 		}
-	
+
 		if (y < 0) {
 			h += y;
 			y = 0;
 		}
-	
+
 		if (x + w > rectangle.w) {
 			w = rectangle.w - x;
 		}
-	
+
 		if (y + h > rectangle.h) {
 			h = rectangle.h - y;
 		}
-	
+
 		x += rectangle.x;
 		y += rectangle.y;
 
@@ -209,39 +212,43 @@
 		return true;
 	}
 
+	template<typename T>
+	inline bool RectType<T>::intersects( const RectType<T>& rectangle ) const {
+		T _x = x - rectangle.x;
+		T _y = y - rectangle.y;
+		T _w = w;
+		T _h = h;
 
-	inline
-	bool Rect::intersects( const Rect& rectangle ) const {
-		int _x = x - rectangle.x;
-		int _y = y - rectangle.y;
-		int _w = w;
-		int _h = h;
 
-	
 		if (_x < 0) {
 			_w += _x;
 			_x = 0;
 		}
-	
+
 		if (_y < 0) {
 			_h += _y;
 			_y = 0;
 		}
-	
+
 		if (_x + _w > rectangle.w) {
 			_w = rectangle.w - _x;
 		}
-	
+
 		if (_y + _h > rectangle.h) {
 			_h = rectangle.h - _y;
 		}
-	
+
 		if (_w <= 0 || _h <= 0) {
 			return false;
 		}
 		return true;
 	}
 
+	typedef RectType<int> Rect;
+	typedef RectType<float> FloatRect;
+	typedef RectType<double> DoubleRect;
+
+
 }
 
 #endif
--- a/engine/core/util/structures/utilstructures.i	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/core/util/structures/utilstructures.i	Fri Oct 01 15:32:55 2010 +0000
@@ -81,19 +81,29 @@
 	%template(Point3D) PointType3D<int>;
 	%template(DoublePoint3D) PointType3D<double>;
 	
-	class Rect {
+	template <typename T> class RectType {
 	public:
-		int x;
-		int y;
-		int w;
-		int h;
-		explicit Rect(int x = 0, int y = 0, unsigned int width = 0, unsigned int height = 0);
-		int right() const;
-		int bottom() const;
-		bool operator==(const Rect& rect ) const;
-		bool contains( const Point& point ) const;
-		bool intersects( const Rect& rect ) const;
-		bool intersectInplace( const Rect& rect );
+		T x;
+		T y;
+		T w;
+		T h;
+		explicit RectType(T x = 0, T y = 0, T width = 0, T height = 0);
+		T right() const;
+		T bottom() const;
+		bool operator==(const RectType<T>& rect ) const;
+		bool contains( const PointType2D<T>& point ) const;
+		bool intersects( const RectType<T>& rect ) const;
+		bool intersectInplace( const RectType<T>& rect );
 	};
-	std::ostream& operator<<(std::ostream&, const Rect&);
+	
+	template<typename T>
+	std::ostream& operator<<(std::ostream&, const RectType<T>&);
+	
+	typedef RectType<int> Rect;
+	typedef RectType<float> FloatRect;
+	typedef RectType<double> DoubleRect;
+	
+	%template(Rect) RectType<int>;
+	%template(FloatRect) RectType<float>;
+	%template(DoubleRect) RectType<double>;
 }
--- a/engine/core/video/image.h	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/core/video/image.h	Fri Oct 01 15:32:55 2010 +0000
@@ -40,7 +40,6 @@
 #include "util/structures/rect.h"
 
 namespace FIFE {
-	class Rect;
 
 	class AbstractImage {
 	public:
--- a/engine/core/view/camera.h	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/core/view/camera.h	Fri Oct 01 15:32:55 2010 +0000
@@ -42,7 +42,6 @@
 
 	typedef Point3D ScreenPoint;
 	class Layer;
-	class Rect;
 	class Instance;
 	class ImagePool;
 	class AnimationPool;
--- a/engine/python/fife/extensions/fife_math.py	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/python/fife/extensions/fife_math.py	Fri Oct 01 15:32:55 2010 +0000
@@ -52,94 +52,6 @@
 	
 	return norm
 	
-class Rect(object):
-	"""
-	A simple rectangle class that allows floating point values.
-	
-	A class used to specify the bounding box of objects.  For use
-	with collision detection.  This was written in python because
-	FIFE does not provide a Rect class that can use floating point
-	values.
-	"""
-	def __init__(self, x = 0, y = 0, w = 0, h = 0):
-		"""
-		@param x: The x coordinate
-		@type x: C{int} or C{float}
-		@param y: The y coordinate
-		@type y: C{int} or C{float}
-		@param w: The width
-		@type w: C{int} or C{float}
-		@param h: The height
-		@type h: C{int} or C{float}
-		"""
-		self._x = x
-		self._y = y
-		self._w = w
-		self._h = h
-		
-	def intersects(self, rect):
-		"""
-		Tests for intersection of rect.
-		
-		@param rect: the L{Rect} to perform the test against.
-		@type rect: L{Rect}
-		
-		@return: True if the rectancles intersect, False if not.
-		@rtype: C{boolean}
-		"""
-		_x = self._x - rect.x;
-		_y = self._y - rect.y;
-		_w = self._w;
-		_h = self._h;
-
-		if _x < 0:
-			_w += _x
-			_x = 0
-
-		if _y < 0:
-			_h += _y
-			_y = 0
-
-		if _x + _w > rect.w:
-			_w = rect.w - _x
-
-		if _y + _h > rect.h:
-			_h = rect.h - _y
-			
-		if _w <= 0 or _h <= 0:
-			return False
-
-		return True
-
-	def _setX(self, x):
-		self._x = x
-		
-	def _getX(self):
-		return self._x
-
-	def _setY(self, y):
-		self._y = y
-		
-	def _getY(self):
-		return self._y
-		
-	def _setW(self, w):
-		self._w = w
-		
-	def _getW(self):
-		return self._w
-		
-	def _setH(self, h):
-		self._h = h
-		
-	def _getH(self):
-		return self._h
-		
-	x = property(_getX, _setX)
-	y = property(_getY, _setY)
-	w = property(_getW, _setW)
-	h = property(_getH, _setH)
-	
 def rotatePoint(origin, point, angle):
 	"""
 	Rotates a point around the specified origin.
@@ -169,4 +81,4 @@
 	
 	return newp
 
-__all__ = ['normalize','Rect','rotatePoint']
+__all__ = ['normalize','rotatePoint']
--- a/engine/swigwrappers/python/extensions.i.templ	Fri Oct 01 14:09:47 2010 +0000
+++ b/engine/swigwrappers/python/extensions.i.templ	Fri Oct 01 15:32:55 2010 +0000
@@ -42,7 +42,7 @@
 		}
 	};
 		
-	%extend Rect {
+	%extend RectType<int> {
 		int getX() { return $self->x; }
 		int getY() { return $self->y; }
 		void setX(int _x) { $self->x = _x; }
@@ -59,4 +59,40 @@
 			return  str.str();
 		}
 	};
+			
+	%extend RectType<float> {
+		float getX() { return $self->x; }
+		float getY() { return $self->y; }
+		void setX(float _x) { $self->x = _x; }
+		void setY(float _y) { $self->y = _y; }
+		
+		float getW() { return $self->w; }
+		float getH() { return $self->h; }
+		void setW(float _w) { $self->w = _w; }
+		void setH(float _h) { $self->h = _h; }
+		
+		std::string __str__() {
+			std::stringstream str;
+			str << "FloatRect" << *$self;
+			return  str.str();
+		}
+	};
+	
+	%extend RectType<double> {
+		double getX() { return $self->x; }
+		double getY() { return $self->y; }
+		void setX(double _x) { $self->x = _x; }
+		void setY(double _y) { $self->y = _y; }
+		
+		double getW() { return $self->w; }
+		double getH() { return $self->h; }
+		void setW(double _w) { $self->w = _w; }
+		void setH(double _h) { $self->h = _h; }
+		
+		std::string __str__() {
+			std::stringstream str;
+			str << "DoubleRect" << *$self;
+			return  str.str();
+		}
+	};
 }