Main Page   Class Hierarchy   Compound List   File List   Compound Members  

glfixed.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
00067 */
00068 
00069 #ifndef GlFixed_MATH_INCLUDED
00070 #define GlFixed_MATH_INCLUDED
00071 
00072 #include <limits.h>
00073 #include "gltypes.h"
00074 #include "gldebug.h"
00075 
00076 #define GlFixed_0               ( 0 )           // Help the compliler - use 1<<16 instead of a const.
00077 #define GlFixed_1               ( 1<<16 )       // Help the compliler - use 1<<16 instead of a const.
00078 
00079 inline S32              GlIntToFixed( int x )                   { return x << 16; }
00080 inline S32              GlDoubleToFixed( double x )             { return S32( x * 65536.0 + 0.5); }
00081 inline double   GlFixedToDouble( S32 f )                { return double( f ) / 65536.0; }
00082 inline float    GlFixedToFloat( S32 f )                 { return float( f ) / 65536.0f; }
00083 inline int      GlFixedToInt( S32 f )                   { return f >> 16; }
00084 inline int              GlFixedToIntRound( S32 f )              { return ( f + 0x0800 ) >> 16; }
00085 inline int              GlFixedToIntRoundUp( S32 f )    { return ( f + 0xffff ) >> 16; }
00086 
00087 
00088 inline S32 GlFixedMult( S32 _x, S32 _y )
00089 {
00090         #ifdef VALIDATE_DEBUG
00091                 S64 x = _x;
00092                 S64 y = _y;
00093                 S64 ret = ( ( x * y ) >> 16 );
00094                 GLASSERT( ret <= INT_MAX && ret >= INT_MIN );
00095                 return S32( ret );
00096         #else
00097                 return (S32) ( ( S64( _x ) * S64( _y ) ) >> 16 );
00098         #endif
00099 }
00100 
00101 
00102 inline S32 GlFixedDiv( S32 _x, S32 _y )
00103 {
00104         #ifdef VALIDATE_DEBUG
00105                 S64 x = ( S64( _x ) << 16 );
00106                 S64 y = _y;
00107 
00108                 S64 ret =  x / y;
00109                 GLASSERT( ret <= INT_MAX && ret >= INT_MIN );
00110                 return S32( ret );
00111         #else
00112                 return S32( ( S64( _x ) << 16 ) / S64( _y ) );
00113         #endif
00114 }
00115 
00116 
00119 class GlFixed
00120 {
00121   public:
00122         GlFixed()                                                                       {}
00123 
00124         GlFixed( const GlFixed &x )                                     { v = x.v; }
00125         GlFixed( int x )                                                        { v = GlIntToFixed( x ); }
00126         GlFixed( const double x )                                       { v = GlDoubleToFixed( x ); }
00127 
00128         #ifdef DEBUG
00129         void DebugDump()                { GLOUTPUT( "fixed=%.2f\n", GlFixedToDouble( v ) ); }
00130         #endif
00131 
00132 
00133         // This could be done with overloaded operators, but I dislike overloaded
00134         // operators for casting because I think they are confusing when they
00135         // are used. -lee
00136 
00137         int    ToInt()  const                                   { return GlFixedToInt( v ); }
00138         int    ToIntRoundUp()   const                   { return GlFixedToIntRoundUp( v ); }
00139         int    ToIntRound() const                               { return GlFixedToIntRound( v ); }
00140         double ToDouble() const                                 { return GlFixedToDouble( v ); }
00141         float  ToFloat() const                                  { return GlFixedToFloat( v ); }
00142 
00143         GlFixed Abs()                                                   { GlFixed t; ( v < 0 ) ? t.v = -v : t.v = v; return t; }
00144 
00145         GlFixed& operator = (const GlFixed &x)          { v = x.v;                      return *this; }
00146         GlFixed& operator = (const int x)                       { v = GlIntToFixed( x );    return *this; }
00147         GlFixed& operator = (const double x)            { v = GlDoubleToFixed( x ); return *this; }
00148 
00149         GlFixed& operator +=  (const GlFixed x)         { v += x.v;          return *this; }
00150         GlFixed& operator +=  (const int x)                     { v += GlIntToFixed(x);    return *this; }
00151         GlFixed& operator +=  (const double x)          { v += GlDoubleToFixed(x);    return *this; }
00152 
00153         GlFixed& operator -=  (const GlFixed x)         { v -= x.v;          return *this; }
00154         GlFixed& operator -=  (const int x)                     { v -= GlIntToFixed(x);    return *this; }
00155         GlFixed& operator -=  (const double x)          { v -= GlDoubleToFixed(x);    return *this; }
00156 
00157         GlFixed& operator *=  (const GlFixed x)         { v = GlFixedMult(v, x.v);       return *this; }
00158         GlFixed& operator *=  (const int x)                     { v *= x;                     return *this; }
00159         GlFixed& operator *=  (const double x)          { v = GlDoubleToFixed(GlFixedToDouble(v) * x);  return *this; }
00160 
00161         GlFixed& operator /=  (const GlFixed x)         { v = GlFixedDiv(v, x.v);                                               return *this; }
00162         GlFixed& operator /=  (const int x)                     { v /= x;                                                                               return *this; }
00163         GlFixed& operator /=  (const double x)          { v = GlFixedDiv( v, GlDoubleToFixed(x) );                      return *this; }
00164 
00165         GlFixed& operator <<= (const int x)                     { v <<= x;           return *this; }
00166         GlFixed& operator >>= (const int x)                     { v >>= x;           return *this; }
00167 
00168         GlFixed& operator ++ ()                                         { v += GlFixed_1;    return *this; }
00169         GlFixed& operator -- ()                                         { v -= GlFixed_1;    return *this; }
00170 
00171         GlFixed operator ++ (int)                                               { GlFixed t;  t.v = v;   v += GlFixed_1;  return t; }
00172         GlFixed operator -- (int)                                               { GlFixed t;  t.v = v;   v -= GlFixed_1;  return t; }
00173 
00174         // Negation.
00175         GlFixed operator - () const                                     { GlFixed t;  t.v = -v;  return t; }
00176 
00177         inline friend GlFixed operator +  (const GlFixed x, const GlFixed y)    { GlFixed t;  t.v = x.v + y.v; return t; }
00178         inline friend GlFixed operator +  (const int x,    const GlFixed y)             { GlFixed t;  t.v = GlIntToFixed(x) + y.v; return t; }
00179         inline friend GlFixed operator +  (const GlFixed x, const int y      )  { GlFixed t;  t.v = x.v + GlIntToFixed(y); return t; }
00180 
00181         inline friend GlFixed operator -  (const GlFixed x, const GlFixed y)    { GlFixed t;  t.v = x.v - y.v;        return t; }
00182         inline friend GlFixed operator -  (const int       x, const GlFixed y)    { GlFixed t;  t.v = GlIntToFixed(x) - y.v;        return t; }
00183         inline friend GlFixed operator -  (const GlFixed x, const int       y)    { GlFixed t;  t.v = x.v - GlIntToFixed(y);        return t; }
00184         inline friend GlFixed operator -  (const double x,    const GlFixed y)    { GlFixed t;  t.v = GlDoubleToFixed(x) - y.v;     return t; }
00185         inline friend GlFixed operator -  (const GlFixed x, const double    y)    { GlFixed t;  t.v = x.v - GlDoubleToFixed(y);     return t; }
00186 
00187         inline friend GlFixed operator *  (const GlFixed x, const GlFixed y)    { GlFixed t;  t.v = GlFixedMult(x.v, y.v);        return t; }
00188         inline friend GlFixed operator *  (const int x,       const GlFixed y)    { GlFixed t;  t.v = GlFixedMult(GlIntToFixed(x), y.v);        return t; }
00189         inline friend GlFixed operator *  (const GlFixed x, const int y)                         { GlFixed t;  t.v = GlFixedMult(x.v, GlIntToFixed(y));         return t; }
00190         inline friend GlFixed operator *  (const double x,    const GlFixed y)    { GlFixed t;  t.v = GlFixedMult(GlDoubleToFixed(x), y.v);        return t; }
00191         inline friend GlFixed operator *  (const GlFixed x, const double y)              { GlFixed t;  t.v = GlFixedMult(x.v, GlDoubleToFixed(y));              return t; }
00192 
00193         inline friend GlFixed operator /  (const GlFixed x, const GlFixed y)    { GlFixed t;  t.v = GlFixedDiv(x.v, y.v);           return t; }
00194         inline friend GlFixed operator /  (const int       x, const GlFixed y)    { GlFixed t;  t.v = GlFixedDiv(GlIntToFixed(x), y.v);           return t; }
00195         inline friend GlFixed operator /  (const GlFixed x, const int       y)    { GlFixed t;  t.v = GlFixedDiv(x.v, GlIntToFixed(y));           return t; }
00196 
00197         inline friend GlFixed operator << (const GlFixed x, const int y)    { GlFixed t;  t.v = x.v << y;    return t; }
00198         inline friend GlFixed operator >> (const GlFixed x, const int y)    { GlFixed t;  t.v = x.v >> y;    return t; }
00199 
00200         inline friend bool operator == (const GlFixed x, const GlFixed y)    { return (x.v == y.v);       }
00201         inline friend bool operator == (const int x,       const GlFixed y)    { return (GlIntToFixed(x) == y.v);       }
00202         inline friend bool operator == (const GlFixed x, const int y)                   { return (x.v == GlIntToFixed(y));       }
00203 
00204         inline friend bool operator != (const GlFixed x, const GlFixed y)    { return (x.v != y.v);       }
00205         inline friend bool operator != (const int x,              const GlFixed y)    { return (GlIntToFixed(x) != y.v);       }
00206         inline friend bool operator != (const GlFixed x, const int y)                   { return (x.v !=GlIntToFixed(y));       }
00207 
00208         inline friend bool operator <  (const GlFixed x, const GlFixed y)   { return (x.v < y.v);        }
00209         inline friend bool operator <  (const int x,     const GlFixed y)   { return (GlIntToFixed(x) < y.v);        }
00210         inline friend bool operator <  (const GlFixed x, const int y)           { return (x.v < GlIntToFixed(y) );        }
00211 
00212         inline friend bool operator >  (const GlFixed x, const GlFixed y)   { return (x.v > y.v);        }
00213         inline friend bool operator >  (const int x,     const GlFixed y)   { return (GlIntToFixed(x) > y.v);        }
00214         inline friend bool operator >  (const GlFixed x, const int y)           { return (x.v > GlIntToFixed(y));        }
00215 
00216         inline friend bool operator <= (const GlFixed x, const GlFixed y)    { return (x.v <= y.v);       }
00217         inline friend bool operator <= (const int x,              const GlFixed y)    { return (GlIntToFixed(x) <= y.v);       }
00218         inline friend bool operator <= (const GlFixed x, const int y)                   { return (x.v <= GlIntToFixed(y));       }
00219 
00220         inline friend bool operator >= (const GlFixed x, const GlFixed y)    { return (x.v >= y.v);       }
00221         inline friend bool operator >= (const int x,              const GlFixed y)    { return (GlIntToFixed(x) >= y.v);       }
00222         inline friend bool operator >= (const GlFixed x, const int y)                   { return (x.v >= GlIntToFixed(y));       }
00223 
00224     S32 v;
00225 };
00226 
00227 
00228 #endif

Generated on Fri Feb 7 20:44:20 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001