common.h

Go to the documentation of this file.
00001 /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 5 -*- */
00002 
00003 /*
00004  * Combination of a first person action and a real time strategy battle game.
00005  */
00006 
00007 /*
00008  * common.h
00009  * MetalWarriors
00010  * $Revision: 1.32 $
00011  *
00012  * Created by Daniel Aarno on Sun Oct 12 2003.
00013  * Copyright (c) 2003 Daniel Aarno and Erik Ytterberg - All rights reserved.
00014  *
00015  * This program is free software; you can redistribute it and/or modify
00016  * it under the terms of the GNU General Public License as published by
00017  * the Free Software Foundation; either version 2 of the License, or
00018  * (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00028  */
00029 
00030 #ifndef _COMMON_
00031 #define _COMMON_
00032 
00033 //Project headers
00034 #include "ActionMenu.h"
00035 #include "ResourceHandler.h"
00036 #include "GLText.h"
00037 
00038 //System headers
00039 #include <cmath>
00040 #include <string>
00041 #include <list>
00042 #include <vector>
00043 
00044 //Forward declarations
00045 class Camera;
00046 class Player;
00047 class Hud;
00048 class Terrain;
00049 class Sprite;
00050 class SensitiveSprite;
00051 class SoundHandler;
00052 
00054 //Used for more efficient sqrt computations
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 &deg) { 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 &deg) {
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   //list of all transparent sprites that needs to be updated
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 //     Global function prototypes
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_

Generated on Sun Mar 11 15:33:23 2007 for MetalWarriors by  doxygen 1.4.7