FixedPoint.hpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #ifndef base_math_FixedPoint_hpp
00023 #define base_math_FixedPoint_hpp
00024 
00025 namespace se_core {
00026     class _SeCoreExport FixedPoint {
00027     public:
00028         typedef short value_t;
00029         typedef int dvalue_t;
00030 
00031         FixedPoint() {}
00032         FixedPoint(value_t v) : value(v) {}
00033 
00034         static inline value_t fromFloat(const float v) {
00035             return static_cast<value_t>(v * RESOLUTION);
00036         }
00037 
00038         static inline value_t fromInt(const int v) {
00039             return v << SHIFT;
00040         }
00041 
00042         static inline value_t fromShort(const short v) {
00043             return v << SHIFT;
00044         }
00045 
00046         static inline float toFloat(const value_t v) {
00047             return v / static_cast<float>(RESOLUTION);
00048         }
00049 
00050         inline operator float() {
00051             return toFloat(value);
00052         }
00053 
00054         inline operator int() {
00055             return value;
00056         }
00057 
00058         inline operator short() {
00059             return value;
00060         }
00061 
00062         inline FixedPoint& operator=(const float& v) {
00063             value = fromFloat(v);
00064             return *this;
00065         }
00066 
00067         inline FixedPoint& operator=(const int& v) {
00068             value = fromInt(v);
00069             return *this;
00070         }
00071 
00072         inline FixedPoint& operator=(const short& v) {
00073             value = fromShort(v);
00074             return *this;
00075         }
00076 
00077         inline FixedPoint& operator=(const FixedPoint& v) {
00078             value = v.value;
00079             return *this;
00080         }
00081 
00082         inline bool operator < (const FixedPoint& v)  const {
00083             return value < v.value;
00084         }
00085 
00086         inline bool operator < (const value_t v)  const {
00087             return value < v;
00088         }
00089 
00090         inline bool operator < (const int v)  const {
00091             return value < v;
00092         }
00093 
00094         inline bool operator > (const FixedPoint& v)  const {
00095             return value > v.value;
00096         }
00097 
00098         /*
00099         inline bool operator > (const value_t v)  const {
00100             return value > v;
00101         }
00102         */
00103 
00104         inline bool operator == (const FixedPoint& v)  const {
00105             return value == v.value;
00106         }
00107 
00108         inline bool operator == (const float& v) const {
00109             return value == v;
00110         }
00111 
00112         inline FixedPoint& operator+=(const FixedPoint& v) {
00113             value += v.value;
00114             return *this;
00115         }
00116 
00117         inline FixedPoint& operator-=(const FixedPoint& v) {
00118             value -= v.value;
00119             return *this;
00120         }
00121 
00122         inline FixedPoint& operator*=(const FixedPoint& v) {
00123             value = (static_cast<dvalue_t>(value) * v.value) >> SHIFT;
00124             return *this;
00125         }
00126 
00127         inline FixedPoint& operator*=(const float v) {
00128             value = static_cast<value_t>(value * v);
00129             return *this;
00130         }
00131 
00132         inline FixedPoint& operator*=(const value_t v) {
00133             //TODO
00134             return *this;
00135         }
00136 
00137         inline FixedPoint& operator/=(const FixedPoint& v) {
00138             value = (static_cast<dvalue_t>(value) << SHIFT) / v.value;
00139             return *this;
00140         }
00141 
00142         inline FixedPoint& operator/=(const float v) {
00143             value = static_cast<value_t>(value / v);
00144             return *this;
00145         }
00146 
00147         inline FixedPoint& operator/=(const value_t v) {
00148             // TODO
00149             return *this;
00150         }
00151 
00152         inline FixedPoint operator+(const FixedPoint& v) const {
00153             return (FixedPoint(*this)).operator+=(v);
00154         }
00155 
00156         inline FixedPoint operator-(const FixedPoint& v) const {
00157             return (FixedPoint(*this)).operator-=(v);
00158         }
00159 
00160         inline FixedPoint operator*(const FixedPoint& v) const {
00161             return (FixedPoint(*this)).operator*=(v);
00162         }
00163 
00164         inline FixedPoint operator*(const float& v) const {
00165             return (FixedPoint(*this)).operator*=(v);
00166         }
00167 
00168         inline FixedPoint operator*(const value_t v) const {
00169             return (FixedPoint(*this)).operator*=(v);
00170         }
00171 
00172         inline FixedPoint operator/(const FixedPoint& v) const {
00173             return (FixedPoint(*this)).operator/=(v);
00174         }
00175 
00176 
00177         inline FixedPoint operator>>(const value_t v) const {
00178             return (FixedPoint(*this));
00179         }
00180 
00181 
00182         inline bool operator!() const {
00183             return value == 0;
00184         }
00185 
00186         inline FixedPoint operator-() const {
00187             return FixedPoint(-value);
00188         }
00189 
00190     private:
00191         value_t value;
00192         static const short SHIFT = 8;
00193         static const short RESOLUTION = 1 << SHIFT;
00194         static const short MASK = RESOLUTION - 1;
00195     };
00196 
00197 
00198 
00199 }
00200 
00201 #endif

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo