Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

Fxp.h

Go to the documentation of this file.
00001 /***************************************************************************
00002            Fxp.h  - SDL-based fixed point macros, types, and classes.
00003                         -------------------
00004     begin                : Mon Mar 3 2003
00005     copyright            : (C) 2003 by Tyler Montbriand
00006     email                : tsm@accesscomm.ca
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef __FXP_H__
00019 #define __FXP_H__
00020 
00021 #include <SDL/SDL_types.h>
00022 /* Still needed for vector magnitudes, not much else */
00023 #include <math.h>
00024 
00025 #include <SDL/begin_code.h>
00026 
00027 #ifdef __cplusplus
00028 extern "C" {
00029 #endif
00030 
00031 
00032 #ifdef __GNUC__
00033 
00034 #undef Sint64
00035 #define Sint64 long long
00036 
00037 #undef Uint64
00038 #define Uint64 unsigned long long
00039 #endif/*__GNUC__*/
00040 
00048 
00054 typedef float FxpFloat;
00055 
00058 
00060 #define SFXP_BITS       10      /*10 bits precision, ~1/1000th accuracy */
00061 
00062 #define SFXP_SCALAR     (1<<SFXP_BITS)
00063 
00064 typedef Sint32          Sfxp;
00065 
00067 #define TO_SFXP(x)      (((Sint32)(x))<<SFXP_BITS)
00068 
00070 #define FROM_SFXP(x)    ((Sint32)((x+((SFXP_SCALAR>>1)))>>SFXP_BITS))
00071 
00073 #define F_TO_SFXP(x)    ((Sfxp)((x)*SFXP_SCALAR))
00074 
00076 #define SFXP_TO_F(x)    ( ((FxpFloat)(x)) / ((FxpFloat)SFXP_SCALAR) )
00077 
00079 #define MUL_SFXP(x,y)  FROM_SFXP(((Sint64)(x))*((Sint64)(y)))
00080 
00082 #define DIV_SFXP(x,y)  ((Sfxp)((TO_SFXP((Sint64)(x)))/y))
00083 
00085 
00088 
00090 #define AFXP_BITSIZE  (sizeof(Afxp)*8)
00091 
00092 #define AFXP_MAX      (1<<AFXP_BITSIZE)
00093 
00095 #define AFXP_BITS       8       /*16-bit angles, 8-bit decimals */
00096 
00097 typedef Uint16          Afxp;   /* Angles are a 16-bit type     */
00099 #define FXP_PI          3.14159f
00100 
00101 #define DEG_TO_AFXP(x)  ((Afxp)((x)*( ((double)AFXP_MAX) /360.)))
00102 
00103 #define RAD_TO_AFXP(x)  DEG_TO_AFXP((x)*(180./3.14159))
00104 
00105 #define AFXP_TO_DEG(x)  ((x)*(360./AFXP_MAX))
00106 
00107 
00110 
00112 DECLSPEC void SDLCALL FXP_Init();
00114 DECLSPEC Sfxp SDLCALL FXP_Sin(Afxp a);
00116 DECLSPEC Afxp SDLCALL FXP_Asin(Sfxp val);
00118 DECLSPEC Sfxp SDLCALL FXP_Cos(Afxp a);
00120 DECLSPEC Afxp SDLCALL FXP_Acos(Sfxp val);
00122 DECLSPEC Sfxp SDLCALL FXP_Tan(Afxp a);
00124 DECLSPEC Afxp SDLCALL FXP_Atan(Sfxp val);
00126 DECLSPEC Afxp SDLCALL FXP_Traj(Sfxp y, Sfxp x);
00128 
00129 #if defined(__cplusplus)
00130 
00141 class DECLSPEC SfxpCoord
00142 {
00143 public:
00145   inline SfxpCoord(FxpFloat xin, FxpFloat yin)
00146   { SetFloat(xin,yin);  }
00147   
00149   inline SfxpCoord(int xin=0,int yin=0)
00150   { SetInt(xin,yin);  }
00151 
00153   inline bool operator ==(SfxpCoord &coord)
00154   { return((x=coord.x)&&(y==coord.y));  }
00155 
00157   inline bool operator !=(SfxpCoord &coord)
00158   { return(!((*this)==coord));  }
00159 
00161   inline SfxpCoord operator +(SfxpCoord coord)
00162   {
00163     SfxpCoord ncoord;
00164 
00165     ncoord.SetFxp(x+coord.x,y+coord.y);
00166     return(ncoord);
00167   }
00168   
00170   inline SfxpCoord operator -(SfxpCoord coord)
00171   {
00172     SfxpCoord ncoord;
00173 
00174     ncoord.SetFxp(x-coord.x,y-coord.y);
00175     return(ncoord);
00176   }
00177 
00179   inline SfxpCoord operator +=(SfxpCoord coord)
00180   {
00181     x+=coord.x;
00182     y+=coord.y;
00183     return(*this);
00184   }
00185 
00187   inline SfxpCoord FxpScalar(Sfxp val)
00188   {
00189     x=MUL_SFXP(x,val);
00190     y=MUL_SFXP(y,val);
00191     return(*this);
00192   }
00193 
00195   inline Afxp Trajectory()
00196   { return(FXP_Traj(y,x)); }
00197 
00199   inline SfxpCoord IntScalar(int val)
00200   {
00201     x*=val;
00202     y*=val;
00203     return(*this);
00204   }
00205 
00207   inline bool MajorDigitsEqual(SfxpCoord coord) const
00208   {
00209     if((X()==coord.X())&&(Y()==coord.Y()))  return(true);
00210     else                                    return(false);
00211   }
00212 
00214   inline int X() const
00215   { return(FROM_SFXP(x)); }
00216 
00218   inline int Y() const
00219   { return(FROM_SFXP(y)); }
00220 
00222   inline int Xfxp() const
00223   { return(x);  }
00224 
00226   inline int Yfxp() const
00227   { return(y);  }
00228 
00230   inline FxpFloat Xfloat()
00231   { return((FxpFloat)x/(FxpFloat)SFXP_SCALAR);  }
00232 
00234   inline FxpFloat Yfloat()
00235   { return((FxpFloat)y/(FxpFloat)SFXP_SCALAR);  }
00236 
00238   inline SfxpCoord SetFloat(FxpFloat xin, FxpFloat yin)
00239   {
00240     Sfxp xsf,ysf;
00241 
00242     xsf=(Sfxp)(xin*(FxpFloat)SFXP_SCALAR);
00243     ysf=(Sfxp)(yin*(FxpFloat)SFXP_SCALAR);
00244     SetFxp(xsf,ysf);
00245 
00246     return(*this);
00247   }
00248 
00250   inline SfxpCoord SetFxp(Sfxp xin, Sfxp yin)
00251   {
00252     x=xin;
00253     y=yin;
00254     return(*this);
00255   }
00256 
00258   inline SfxpCoord SetInt(Sint32 xin, Sint32 yin)
00259   {
00260     x=TO_SFXP(xin);
00261     y=TO_SFXP(yin);
00262     return(*this);
00263   }  
00264 
00266   inline int Nonzero()
00267   { return(x|y);  }
00268 
00270   inline void Vector(Sfxp magnitude, Afxp angle)
00271   {
00272     x=MUL_SFXP(FXP_Cos(angle),magnitude);
00273     y=MUL_SFXP(FXP_Sin(angle),magnitude);
00274   }
00275 
00277   Sfxp Magnitude()
00278   {
00279     if(Nonzero())
00280     {
00281       FxpFloat x2=SFXP_TO_F(x)*SFXP_TO_F(x);
00282       FxpFloat y2=SFXP_TO_F(y)*SFXP_TO_F(y);
00283 
00284       return(F_TO_SFXP(sqrt(x2+y2)));
00285     }
00286     else
00287       return(0);
00288   }
00289 
00290 private:
00292   Sfxp x,y;
00293 };
00294 
00302 class DECLSPEC Angle
00303 {
00304 public:
00306   inline Angle(Afxp ain)
00307   { angle=ain;            }
00308 
00310   inline Angle(int degrees)
00311   { SetDegrees(degrees);  }
00312 
00314   inline Angle(FxpFloat radians)
00315   { SetRadians(radians);  }
00316 
00318   inline int Degrees() const
00319   { return((int)AFXP_TO_DEG(angle));  }
00320 
00322   inline FxpFloat Radians() const
00323   { return((FxpFloat)((FXP_PI/180.)*AFXP_TO_DEG(angle))); }
00324 
00326   inline Afxp Fxp() const
00327   { return(angle);  }
00328 
00330   inline Angle operator+=(Afxp ain)
00331   {
00332     angle+=ain;
00333     return(*this);
00334   }
00335 
00337   inline Angle operator-=(Afxp ain)
00338   {
00339     angle-=ain;
00340     return(*this);
00341   }
00342 
00344   inline Angle operator+(const Angle &ain)
00345   { return(Angle(angle+ain.Fxp())); }
00346 
00348   inline Angle SetDegrees(int degrees)
00349   {
00350     angle=DEG_TO_AFXP(degrees);
00351     return(*this);
00352   }
00354   inline Angle SetRadians(double radians)
00355   {
00356     angle=RAD_TO_AFXP(radians);
00357     return(*this);
00358   }
00359 
00361   inline Sfxp Sin() const
00362   { return(FXP_Sin(angle)); }
00363 
00365   inline Sfxp Cos() const
00366   { return(FXP_Cos(angle)); }
00367 
00369   inline Sfxp Tan() const
00370   { return(FXP_Tan(angle)); }
00371 
00372 private:
00374   Afxp angle;
00375 };
00376 #endif/*__cplusplus*/
00377 
00379 
00380 #ifdef __cplusplus
00381 }
00382 #endif
00383 
00384 #include <SDL/close_code.h>
00385 
00386 #endif/*__FXP_H__*/

Generated on Sat Oct 11 13:19:25 2003 for Spritelib by doxygen 1.3.4