eng_class.hh

Go to the documentation of this file.
00001 /*
00008  * LEGAL:   COPYRIGHT (C) 2004 JIM E. BROOKS
00009  *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
00010  *          OF THE GNU GENERAL PUBLIC LICENSE VERSION 2 (GPL 2).
00011  *****************************************************************************/
00012 
00013 // REMINDER: Must lock Engine methods.
00014 
00015 #ifndef ENG_CLASS_HH
00016 #define ENG_CLASS_HH 1
00017 
00018 namespace eng {
00019 
00027 class Engine : public Threadable
00028 {
00029 
00030 
00031 // Related classes such as Eye can obtain Engine's lock.
00032 #if COMPILE_THREADS
00033 #define ENGINE_AUTO_LOCK() THREAD_CODE( AutoLock<SlowLock> lock( &Engine::mLock ); )
00034 #else
00035 #define ENGINE_AUTO_LOCK()
00036 #endif
00037 
00038 //------------------------------------------------------------------------------
00039 // Initialization and destruction:
00040 //------------------------------------------------------------------------------
00041 
00042 // Methods:
00043 public:                     Engine( void );
00044 public:                     ~Engine();
00045 public:  void               Init( int argc, char** argv, const string& programName );
00046 public:  void               Start( void );
00047 public:  bool               IfReady( void ) { return mReady; }
00048 public:  void               SpawnZombie( SharedPtr<Object> obj );
00049 
00050 private: void               InitPreCmdarg( void );
00051 private: void               InitCmdarg( int argc, char** argv );
00052 private: void               InitPostCmdarg( void );
00053 private: void               SelfCheck( void );
00054 private: CLASS_METHOD void  AtExit( void );
00055 private: void               KillZombies( void );
00056 
00057 // Data:
00058 private: bool           mReady;
00059 
00060 //------------------------------------------------------------------------------
00061 // Events:
00062 //------------------------------------------------------------------------------
00063 
00064 // Data:
00065 public:  Events         mEvents;
00066 
00067 //------------------------------------------------------------------------------
00068 // Drawing:
00069 //------------------------------------------------------------------------------
00070 
00071 // Methods:
00072 private: void           Draw( void );
00073 public:  bool           IfDrawing( void ) { return mDrawing; }
00074 public:  enum eDrawMode { eDrawMode_NORMAL, eDrawMode_FAST };
00075 public:  void           SetDrawMode( const eDrawMode drawMode ) { ENGINE_AUTO_LOCK(); mDrawMode = drawMode; }
00076 public:  eDrawMode      GetDrawMode( void ) { return mDrawMode; }
00077 public:  bool           IfDrawModeFast( void ) { return mDrawMode == eDrawMode_FAST; }
00078 public:  uint           GetFPS( void );
00079 #if DEBUG
00080 // SetPolygonMode() affects VisitorDraw (not GFX).
00081 public:  GFX::ePolygonMode GetPolygonMode( void );
00082 public:  void              SetPolygonMode( const GFX::ePolygonMode polygonMode );
00083 // For debugging normal vectors.
00084 public:  void              SetDrawNormalVectors( bool f, fp scale );
00085 public:  bool              IfDrawNormalVectors( void ) { return mDrawNormalVectors; }
00086 #endif
00087 
00088 // Data:
00089 private: bool              mDrawing;        
00090 private: eDrawMode         mDrawMode;       
00091 #if DEBUG
00092 private: bool              mDrawNormalVectors;
00093 #endif
00094 
00095 //------------------------------------------------------------------------------
00096 // View/Eye:
00097 //------------------------------------------------------------------------------
00098 
00099 // View methods:
00100 public:  void           SetView( SharedPtr<View> view );
00101 public:  shptr<View>    GetView( void )  
00102                         {
00103                         ENGINE_AUTO_LOCK();
00104                             return mCurrentView;
00105                         }
00106 public:  SharedPtr<View>    GetMainView( void )
00107                         {
00108                         ENGINE_AUTO_LOCK();
00109                         ASSERT( not mViews.empty() );
00110                             return mViews.begin()->second;
00111                         }
00112 public:  void           AddView( SharedPtr<View> view, int viewNum );
00113 public:  void           WindowReshape( WidthHeight<int> wh );
00114 
00115 public: 
00117     // Alternate name: IfInsideViewFrustum()
00118     bool IfVisibleWorldBoxVolume( const BoxVolume& worldBoxVolume )
00119     {
00120     ENGINE_AUTO_LOCK();
00121         // Transform box's midpoint from World Space to Eye Space.
00122         // BoxVolume::GetPosition() returns center/midpoint.
00123         const EyeVertex ev = GetEyeMatrix()->TranslateRotate<EyeVertex,Vector3>( worldBoxVolume.GetPosition() );
00124         return GFX::IfVisibleSphere( ev, worldBoxVolume.GetRadius() );
00125     }
00126 
00127 // Eye methods:
00128 public:  SharedPtr<const Matrix>    GetEyeMatrix( void )
00129                                 {
00130                                 ENGINE_AUTO_LOCK();
00131                                     return GetView()->mEye.GetMatrix();
00132                                 }
00133 public:  void                   SetEyeMatrix( const Matrix& matrix )
00134                                 {
00135                                 ENGINE_AUTO_LOCK();
00136                                     GetView()->mEye.SetMatrix( matrix );
00137                                 }
00138 public:  WorldVertex            GetEyePosition( void )
00139                                 {
00140                                 ENGINE_AUTO_LOCK();
00141                                     return GetView()->mEye.GetPosition();
00142                                 }
00143 public:  void                   SetEyePosition( const WorldVertex& pos )
00144                                 {
00145                                 ENGINE_AUTO_LOCK();
00146                                     GetView()->mEye.SetPosition( pos );
00147                                 }
00148 
00150 struct DrawStats
00151 {
00152     void Reset( void )
00153     {
00154     ENGINE_AUTO_LOCK();
00155         mObjectCntDrawn = mObjectCntCulledBSP = mObjectCntCulledGraph = 0;
00156     }
00157 
00158     uint GetObjectCntCulled( void )
00159     {
00160     ENGINE_AUTO_LOCK();
00161         return mObjectCntCulledBSP + mObjectCntCulledGraph;  // total
00162     }
00163 
00164     uint    mObjectCntDrawn;        
00165     uint    mObjectCntCulledBSP;    
00166     uint    mObjectCntCulledGraph;  
00167 };
00168 
00169 // Data:
00170 private: typedef multimap<int,shptr<View> > Views;  // View,viewNum
00171 private: Views          mViews;
00172 private: SharedPtr<View>    mCurrentView;
00173 public:  DrawStats      mDrawStats;
00174 
00175 //------------------------------------------------------------------------------
00176 // Collision-detection:
00177 //------------------------------------------------------------------------------
00178 
00179 // Methods:
00180 public:  void           EnableCollisionDetection( bool f )
00181                         {
00182                         ENGINE_AUTO_LOCK();
00183                             mCollisionDetection = f;
00184                         }
00185 public:  bool           IfCollisionDetection( void )
00186                         {
00187                         ENGINE_AUTO_LOCK();
00188                             return mCollisionDetection;
00189                         }
00190 
00191 // Data:
00192 private: bool       mCollisionDetection;  
00193 
00194 //------------------------------------------------------------------------------
00195 // Textures:
00196 //------------------------------------------------------------------------------
00197 
00198 // During loading textures from Targa files, an event is broadcast to let a module pre-filter a texture.
00199 // A listening module may respond by calling Engine::SetTextureFilterFunc().
00200 
00201 private: typedef map<string,SharedPtr<Texture> > TextureMap;
00202 
00203 // Methods:
00204 public:  void                   LoadTextureFromFile( const string& filename, const string& pathname );
00205 private: void                   InitTextures( void );
00206 private: void                   InitTexturesTgaFiles( const string& dirName );
00207 public:  void                   SetTextureDirs( const vector<string>& textureDirs ) { mTextureDirs = textureDirs; }
00208 public:  SharedPtr<Texture>         GetTexture( const string& textureName )
00209                                 {
00210                                 ENGINE_AUTO_LOCK();
00211                                 ASSERT( not textureName.empty() );
00212                                     return GetTexture( textureName.c_str() );
00213                                 }
00214 public:  SharedPtr<Texture>         GetTexture( const char* textureName );
00215 public:  TargaFilterFuncType    GetTextureFilterFunc( void ) { return mTextureFilterFunc; }
00216 public:  void                   SetTextureFilterFunc( TargaFilterFuncType filterFunc )
00217                                 {
00218                                 ENGINE_AUTO_LOCK();
00219                                     mTextureFilterFunc = filterFunc;
00220                                 }
00221 private: void                   ReadTextureConf( const string& dirName );
00222 
00223 private:
00225     class TextureConfReader : public ConfReader
00226     {
00227     public:
00228         void ProcessField( string& field, ifstream& config );
00229     };
00230 
00231 // Data:
00232 private: TextureMap             mTextureMap;
00233 private: vector<string>         mTextureDirs;     
00234 private: TargaFilterFuncType    mTextureFilterFunc;
00235 
00236 //------------------------------------------------------------------------------
00237 // User-selected LOD:
00238 //------------------------------------------------------------------------------
00239 
00240 public: UserLod     mUserLod;
00241 
00242 //------------------------------------------------------------------------------
00243 // Listeners:
00244 //------------------------------------------------------------------------------
00245 
00246 private:
00247     // Timer-tick handler.
00248     struct ListenerTick : public Event<gfx::Events::TickInfo>::ListenerFunctor
00249     {
00250         void operator()( gfx::Events::TickInfo tickInfo );
00251     };
00252     friend class ListenerTick;
00253 
00254     // Redisplay (repaint) handler.
00255     struct ListenerDisplay : public Event<>::ListenerFunctor
00256     {
00257         void operator()( Void nothing );  // just "void" is wrong
00258     };
00259     friend class ListenerDisplay;
00260 
00261     // Window reshape event.
00262     struct ListenerWindow : public Event< WidthHeight<int> >::ListenerFunctor
00263     {
00264         void operator()( WidthHeight<int> wh );
00265     };
00266 
00267 //------------------------------------------------------------------------------
00268 // Debug, stats:
00269 //------------------------------------------------------------------------------
00270 
00271 public:  void           PrintException( const Exception& exception, const string& functionName );
00272 #if DEBUG
00273 public:  string         GetCurrentObjectName( void ) { return mCurrentObjectName; }
00274 public:  void           SetCurrentObjectName( const string& name ) { mCurrentObjectName = name; }
00275 private: string         mCurrentObjectName;  
00276 friend ostream& operator<<( ostream& strm, const EngineStats& o );
00277 #endif // DEBUG
00278 
00279 #if STATS
00280 public:  EngineStats        mStats;
00281 #endif
00282 
00283 //------------------------------------------------------------------------------
00284 // Other data members:
00285 //------------------------------------------------------------------------------
00286 
00287 #if COMPILE_THREADS
00288 public:  CLASS_VAR SlowLock mLock;                  
00289 #endif
00290 private: typedef vector< shptr<Object> > Zombies;   
00291 private: Zombies            mZombies;
00292 
00293 }; // class Engine
00294 
00295 } // namespace eng
00296 
00297 #endif // ENG_CLASS_HH
Palomino 3D Engine documents generated by doxygen 1.5.3 on Fri Nov 23 11:26:08 2007