00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _COMMON_
00031 #define _COMMON_
00032
00033
00034 #include "ActionMenu.h"
00035 #include "ResourceHandler.h"
00036 #include "GLText.h"
00037
00038
00039 #include <cmath>
00040 #include <string>
00041 #include <list>
00042 #include <vector>
00043
00044
00045 class Camera;
00046 class Player;
00047 class Hud;
00048 class Terrain;
00049 class Sprite;
00050 class SensitiveSprite;
00051 class SoundHandler;
00052
00054
00055 extern float *g_sqrtTable;
00056 static const char FAST_SQRT_MAX = 5;
00057 static const float FAST_SQRT_STEP = 0.01f;
00058 template<typename T>
00059 inline float FAST_SQRT_UNSAFE(const T &f) {
00060 int idx = static_cast<int>(f / FAST_SQRT_STEP);
00061 return g_sqrtTable[idx];
00062 }
00063 template<typename T>
00064 inline float FAST_SQRT(const T &f) {
00065 return f < FAST_SQRT_MAX ? FAST_SQRT_UNSAFE(f) : sqrt(f);
00066 }
00069 template<typename T>
00070 inline T SQR(const T &a) { return a*a; }
00071
00073 template<typename T>
00074 inline T POW2(const T &a) { return SQR(a); }
00075
00077 template<typename T>
00078 inline double Deg2Rad(const T °) { return deg*M_PI/180.0; }
00079
00081 template<typename T>
00082 inline double Rad2Deg(const T &rad) { return rad*180.0/M_PI; }
00083
00086 template<typename T>
00087 inline void NormalizeDeg(T °) {
00088 while(deg < 180)
00089 deg += 360;
00090 while(deg > 180)
00091 deg -= 360;
00092 }
00093
00096 template<typename T>
00097 inline void NormalizeRad(T &rad) {
00098 while(rad < M_PI)
00099 rad += 2*M_PI;
00100 while(rad > M_PI)
00101 rad -= 2*M_PI;
00102 }
00103
00105 template<typename T>
00106 inline T Abs(const T &a) { return a < 0 ? -a : a; }
00107
00110 template<typename T>
00111 inline T AbsMin(const T &a, const T &b) { return Abs(a) < Abs(b) ? a : b; }
00112
00114 typedef std::list<Sprite*> SpriteList;
00116 typedef SpriteList::iterator SpriteIterator;
00117
00122 typedef struct {
00123 Player *player;
00124 int fpsRef;
00125 float trueFps;
00126 Camera *camera;
00127 Hud *hud;
00128 Terrain *terrain;
00129
00130 std::vector< Player* > playerList;
00132 std::list< Sprite* > sprites;
00136 std::list< Sprite* > transparentSprites;
00138 std::list<Sprite*> updateSprites;
00139
00140 std::list<Sprite*> updateTransSprites;
00142 bool fullScreen;
00143
00144 ResourceHandler resourceHandler;
00145 SoundHandler *sh;
00146 GLText glText;
00147 ActionMenu actionMenu;
00148 } Global;
00149
00151 extern Global g;
00152
00153
00154
00155
00156
00158 void Quit(int exitval=0);
00159
00178 template<typename T, bool zero=false>
00179 class ElemDeleter {
00180 public:
00182 void operator()(T* &o) {
00183 delete o;
00184 #ifdef NDEBUG
00185 if(zero)
00186 #endif
00187 o = 0;
00188 }
00189 };
00190
00191 template<typename T> inline ElemDeleter<T, false>
00192 DelElem() { return ElemDeleter<T, false>(); }
00193
00195 template<typename T>
00196 inline void DelNZero(T* &o) { delete o; o = NULL; }
00197
00202 template<typename T, typename X>
00203 class AtScopeExit {
00204 public:
00205 AtScopeExit(T func, X target) : m_func(func), m_target(target) {}
00206 ~AtScopeExit() { m_func(m_target); }
00207
00208 private:
00209 T m_func;
00210 X m_target;
00211 };
00212
00214 inline void DownCase(char &c) { c = tolower(c); }
00216 inline void UpCase(char &c) { c = toupper(c); }
00217
00218 template<typename PtrType, typename Collection>
00219 void ClearCollection(Collection &col)
00220 {
00221 std::for_each(col.begin(), col.end(), DelElem<PtrType>());
00222 col.clear();
00223 }
00224
00225 #endif //_COMMON_