view engine/core/util/structures/point.h @ 654:5d6b1820b953

* Added the ability to change screen modes on the fly. This works both in OpenGL and SDL modes. * Added IEngineChangeListener so the client can update the cameras viewport if the screen mode has been changed. I chose to do it this way because the engine has no way to know which camera it should update. It will be up to the client to do it. * The cursor surface is now correctly freed when exiting. * Added DeviceCaps::getNearestScreenMode() for the client to request a supported screen mode. closes[t:315]
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 21 Oct 2010 18:50:50 +0000
parents 07b1cf8e92b5
children bb26a76458c6
line wrap: on
line source

/***************************************************************************
 *   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          *
 ***************************************************************************/

#ifndef FIFE_VIDEO_POINT_H
#define FIFE_VIDEO_POINT_H

// Standard C++ library includes
#include <iostream>
#include <cassert>

// Platform specific 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 "util/base/fife_stdint.h"
#include "util/math/fife_math.h"

namespace FIFE {

	/** A 2D Point
	 *
	 * This is a small helper class to aid in 2d vector arithmetics.
	 * @see Rect
	 */
	template <typename T> class PointType2D {
	public:
		union {
			T val[2];
			struct {
				T x,y;
			};
		};

		/** Constructor
		 *
		 * Creates a with 0 as default values.
		 */
		explicit PointType2D(T _x = 0, T _y = 0): x(_x), y(_y) {
		}

		/** Copy Constructor
		 */
		PointType2D(const PointType2D<T>& rhs): x(rhs.x), y(rhs.y) {
		}

		/** Vector addition
		 */
		PointType2D<T> operator+(const PointType2D<T>& p) const {
			return PointType2D<T>(x + p.x, y + p.y);
		}

		/** Vector substraction
		 */
		PointType2D<T> operator-(const PointType2D<T>& p) const {
			return PointType2D<T>(x - p.x, y - p.y);
		}

		/** Vector inplace addition
		 */
		PointType2D<T>& operator+=(const PointType2D<T>& p) {
			x += p.x;
			y += p.y;
			return *this;
		}

		/** Vector inplace substraction
		 */
		PointType2D<T>& operator-=(const PointType2D<T>& p) {
			x -= p.x;
			y -= p.y;
			return *this;
		}

		/** Scalar multiplication with an integer value
		 */
		PointType2D<T> operator*(const T& i) const {
			return PointType2D<T>(x * i, y * i);
		}

		/** Scalar division with an integer value
		 */
		PointType2D<T> operator/(const T& i) const {
			return PointType2D<T>(x / i, y / i);
		}

		/** Equality comparision
		 */
		bool operator==(const PointType2D<T>& p) const {
			return x == p.x && y == p.y;
		}

		/** Equality comparision
		 */
		bool operator!=(const PointType2D<T>& p) const {
			return !(x == p.x && y == p.y);
		}

		/** Return length
		 */
		T length() const {
			double sq;
			sq = x*x + y*y;
			return static_cast<T>(sqrt(sq));
		}

		/** Normalizes the point
		 */
		void normalize() {
			T invLength = 1.0/length();

			//TODO: get rid of this static cast
			if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
				x = x * invLength;
				y = y * invLength;
			}
			else {
				x = 0;
				y = 0;
			}
		}

		/** Rotates the point around the origin
		 */
		void rotate(T angle){
			//TODO: get rid of this static cast
			T theta = (angle * static_cast<T>(Mathd::pi()))/180;
			T costheta = static_cast<T>(Mathd::Cos(theta));
			T sintheta = static_cast<T>(Mathd::Sin(theta));

			T nx = x;
			T ny = y;

			x = costheta * nx - sintheta * ny;
			y = sintheta * nx + costheta * ny;
		}

		/** Rotates the point around an origin
		 */
		void rotate(const PointType2D<T>& origin, T angle){
			//TODO: get rid of this static cast
			T theta = (angle * static_cast<T>(Mathd::pi()))/180;
			T costheta = static_cast<T>(Mathd::Cos(theta));
			T sintheta = static_cast<T>(Mathd::Sin(theta));

			T nx = x - origin.x;
			T ny = y - origin.y;

			x = costheta * nx - sintheta * ny;
			y = sintheta * nx + costheta * ny;
		}

		inline T& operator[] (int ind) {
			assert(ind > -1 && ind < 2);
			return val[ind];
		}
	};

	/** Print coords of the Point to a stream
	 */
	template<typename T>
	std::ostream& operator<<(std::ostream& os, const PointType2D<T>& p) {
		return os << "(" << p.x << ":" << p.y << ")";
	}

	typedef PointType2D<int> Point;
	typedef PointType2D<double> DoublePoint;

	/** A 3D Point
	 *
	 * This is a small helper class to aid in 3d vector arithmetics.
	 * @see Rect
	 */
	template <typename T> class PointType3D {
	public:
		union {
			T val[3];
			struct {
				T x,y,z;
			};
		};

		/** Constructor
		 *
		 * Creates a with 0 as default values.
		 */
		explicit PointType3D(T _x = 0, T _y = 0, T _z = 0): x(_x), y(_y), z(_z) {
		}

		/** Copy Constructor
		 */
		PointType3D(const PointType3D<T>& rhs): x(rhs.x), y(rhs.y), z(rhs.z) {
		}

		/** Vector addition
		 */
		PointType3D<T> operator+(const PointType3D<T>& p) const {
			return PointType3D<T>(x + p.x, y + p.y, z + p.z);
		}

		/** Vector substraction
		 */
		PointType3D<T> operator-(const PointType3D<T>& p) const {
			return PointType3D<T>(x - p.x, y - p.y, z - p.z);
		}

		/** Vector inplace addition
		 */
		PointType3D<T>& operator+=(const PointType3D<T>& p) {
			x += p.x;
			y += p.y;
			z += p.z;
			return *this;
		}

		/** Vector inplace substraction
		 */
		PointType3D<T>& operator-=(const PointType3D<T>& p) {
			x -= p.x;
			y -= p.y;
			z -= p.z;
			return *this;
		}

		/** Scalar multiplication with an integer value
		 */
		PointType3D<T> operator*(const T& i) const {
			return PointType3D<T>(x * i, y * i, z * i);
		}

		/** Scalar division with an integer value
		 */
		PointType3D<T> operator/(const T& i) const {
			return PointType3D<T>(x / i, y / i, z / i);
		}

		/** Equality comparision
		 */
		bool operator==(const PointType3D<T>& p) const {
			return x == p.x && y == p.y && z == p.z;
		}

		/** Equality comparision
		 */
		bool operator!=(const PointType3D<T>& p) const {
			return !(x == p.x && y == p.y && z == p.z);
		}

		/** Return length
		 */
		T length() const {
			double sq;
			sq = x*x + y*y + z*z;
			return static_cast<T>(sqrt(sq));
		}

		/** Normalizes the point
		 */
		void normalize() {
			T invLength = 1.0/length();

			//TODO: get rid of this static cast
			if (invLength > static_cast<T>(Mathd::zeroTolerance())) {
				x = x * invLength;
				y = y * invLength;
				z = z * invLength;
			}
			else {
				x = 0;
				y = 0;
				z = 0;
			}
		}

		inline T& operator[] (int ind) {
			assert(ind > -1 && ind < 3);
			return val[ind];
		}
	};

	/** Print coords of the Point to a stream
	 */
	template<typename T>
	std::ostream& operator<<(std::ostream& os, const PointType3D<T>& p) {
		return os << "(" << p.x << ":" << p.y << ":" << p.z << ")";
	}

	typedef PointType3D<int> Point3D;
	typedef PointType3D<double> DoublePoint3D;

	/** Convert from 2D double point to 2D int point
	 */
	inline Point doublePt2intPt(DoublePoint pt) {
		Point tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)));
		return tmp;
	}

	/** Convert from 3D double point to 3D int point
	 */
	inline Point3D doublePt2intPt(DoublePoint3D pt) {
		Point3D tmp(static_cast<int>(round(pt.x)), static_cast<int>(round(pt.y)), static_cast<int>(round(pt.z)));
		return tmp;
	}

	/** Convert from 2D int point to 2D double point
	 */
	inline DoublePoint intPt2doublePt(Point pt) {
		DoublePoint tmp(static_cast<double>(pt.x), static_cast<double>(pt.y));
		return tmp;
	}

	/** Convert from 3D int point to 3D double point
	 */
	inline DoublePoint3D intPt2doublePt(Point3D pt) {
		DoublePoint3D tmp(static_cast<double>(pt.x), static_cast<double>(pt.y), static_cast<double>(pt.z));
		return tmp;
	}

}

#endif