CoorT_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 // Don't include this directly. Instead include CoorT.hpp
00023 
00024 
00025 #ifndef CoorT_FixedPoint_hpp
00026 #define CoorT_FixedPoint_hpp
00027 
00028 #include "util_math.hpp"
00029 #include "util/type/util_type.hpp"
00030 
00031 #ifdef SE_FIXED_POINT
00032 
00033 
00034 #ifdef max
00035 #undef max
00036 #endif
00037 
00038 namespace se_core {
00039     class _SeCoreExport CoorT {
00040     public:
00041         inline static coor_t half(coor_t t) { return t << 1; }
00042         inline static coor_t quarter(coor_t t) { return t << 2; }
00043 
00044 
00045         inline static coor_t abs(coor_t t) { return t > 0 ? t : -t; }
00046         inline static coor_double_t absd(coor_double_t t) { return t > 0 ? t : -t; }
00047         inline static coor_t sign(coor_t t) { return t > 0 ? 1 : -1; }
00048         inline static coor_t signd(coor_double_t t) { return t > 0 ? 1 : -1; }
00049         inline static coor_t max(coor_t a, coor_t b) { return (a < b) ? b : a; }
00050         inline static coor_t max(coor_t a, coor_t b, coor_t c) { return max(max(a, b), c); }
00051         inline static coor_t max(coor_t a, coor_t b, coor_t c, coor_t d) { return max(max(a, b), max(c, d)); }
00052 
00053         static coor_t sqrt(coor_double_t v) {
00054             coor_double_t max = COOR_T_MAX;
00055             coor_double_t min = 0;
00056             while(max > min + 1) {
00057                 coor_double_t middle = (max + min) >> 1;
00058                 if(middle * middle < v)
00059                     min = middle;
00060                 else
00061                     max = middle;
00062             }
00063             return static_cast<coor_t>(max);
00064         }
00065 
00071         static scale_t heightScaler(coor_t dy);
00072 
00073         inline static coor_double_t pow2(coor_double_t v) {
00074             return v * v;
00075         }
00076 
00077         inline static coor_t fromd(coor_double_t v) {
00078             return static_cast<coor_t>(signd(v) * (absd(v) >> COOR_SHIFT));
00079         }
00080 
00081         inline static coor_t fromScale(scale_t v) {
00082             return static_cast<coor_t>(signd(v) * (absd(v) >> SCALE_SHIFT));
00083         }
00084 
00085         inline static coor_double_t muld(coor_t p1, coor_t p2) {
00086             return static_cast<coor_double_t>(p1) * p2;
00087         }
00088 
00089         inline static coor_t mul(coor_t p1, coor_t p2) {
00090             return fromd(muld(p1, p2));
00091         }
00092 
00093 
00094         inline static coor_t div(coor_t p1, coor_t p2) {
00095             coor_double_t dp1 = p1 << COOR_SHIFT;
00096             return static_cast<coor_t>(dp1 / p2);
00097         }
00098 
00099         inline static float toFloat(coor_t value) {
00100             return value * COOR_SCALE;
00101         }
00102 
00103         inline static coor_t fromFloat(float value) {
00104             return static_cast<coor_t>(value * COOR_RES + 0.5);
00105         }
00106 
00107         inline static coor_t fromInt(int value) {
00108             return static_cast<coor_t>(value << COOR_SHIFT);
00109         }
00110 
00111         inline static int toInt(coor_t value) {
00112             return static_cast<int>(value >> COOR_SHIFT);
00113         }
00114 
00115         inline static coor_t scale(scale_t s, coor_t c) {
00116             return static_cast<coor_t>(sign(c) * ((s * abs(c)) >> SCALE_SHIFT));
00117         }
00118 
00119         inline static  coor_tile_t tile(coor_t v) {
00120             return (v >> COOR_SHIFT);
00121         }
00122 
00123         inline static coor_in_t inTile(coor_t v) {
00124             return (v & (COOR_RES - 1));
00125         }
00126 
00127         inline static coor_t fromTile(coor_tile_t value) {
00128             return static_cast<coor_t>(value << COOR_SHIFT);
00129         }
00130 
00131     };
00132 
00133 }
00134 
00135 #endif // SE_FIXED_POINT
00136 
00137 #endif
00138 
00139 
00140 

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