00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "GameManager.h"
00021 #include "../game/InitState.h"
00022
00023
00024
00025
00026 GameManager::GameManager()
00027 : pDevice(0), mLastTime(0)
00028 {
00029 }
00030
00031
00032 GameManager::~GameManager()
00033 {
00034 clear();
00035 }
00036
00037
00038 GameManager* GameManager::pInstance = 0;
00039
00040 GameManager* GameManager::Instance()
00041 {
00042 if(pInstance == 0)
00043 pInstance = new GameManager();
00044
00045 return pInstance;
00046 }
00047
00048 GameManager& GameManager::Reference()
00049 {
00050 if(pInstance == 0)
00051 pInstance = new GameManager();
00052
00053 return *pInstance;
00054 }
00055
00056
00057 void GameManager::init()
00058 {
00059
00060 pDataStore = new DataStore();
00061 pEventManager = new EventManager();
00062
00063
00064 pDevice = createDevice(EDT_OPENGL, dimension2d<u32>(1024, 768), 32, false, false, false, pEventManager);
00065
00066
00067 pDriver = pDevice->getVideoDriver();
00068 pSceneManager = pDevice->getSceneManager();
00069 pGUIEnvironment = pDevice->getGUIEnvironment();
00070
00071
00072 pAssetManager = new AssetManager();
00073 pAssetManager->init();
00074
00075 pEntityManager = new EntityManager();
00076
00077 #ifdef __COMPILE_WITH_SFML_AUDIO__
00078 pSoundManager = new SoundManager();
00079 #endif // __COMPILE_WITH_SFML_AUDIO__
00080
00081 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00082 pScriptManager = new ScriptManager();
00083 pScriptManager->init();
00084 #endif // __COMPILE_WITH_ANGELSCRIPT__
00085
00086
00087 changeState(new InitState());
00088 }
00089
00090
00091 void GameManager::update()
00092 {
00093
00094 u32 time = pDevice->getTimer()->getTime();
00095 f32 deltaTime = ( (float)time - (float)mLastTime ) *0.01f;
00096 mLastTime = time;
00097
00098 if(!mGameStates.empty() && deltaTime > 0.0f)
00099 {
00100 mGameStates.back()->onUpdate(deltaTime);
00101 mGameStates.back()->update(deltaTime);
00102 }
00103
00104
00105 if(pDevice->isWindowActive())
00106 {
00107 if(!mGameStates.empty())
00108 {
00109 pDriver->beginScene();
00110
00111 mGameStates.back()->render();
00112 mGameStates.back()->onRender();
00113
00114 pSceneManager->drawAll();
00115 pGUIEnvironment->drawAll();
00116
00117 pDriver->endScene();
00118 }
00119 }
00120 }
00121
00122
00123 void GameManager::clear()
00124 {
00125
00126 while(!mGameStates.empty())
00127 {
00128 mGameStates.back()->clear();
00129 mGameStates.pop_back();
00130 }
00131
00132
00133 #ifdef ____COMPILE_WITH_ANGELSCRIPT__
00134 pScriptManager->clear();
00135 delete pScriptManager;
00136 #endif // __COMPILE_WITH_ANGELSCRIPT__
00137
00138 #ifdef __COMPILE_WITH_SFML_AUDIO__
00139 delete pSoundManager;
00140 #endif // __COMPILE_WITH_SFML_AUDIO__
00141
00142 delete pEntityManager;
00143
00144 pAssetManager->clear();
00145 delete pAssetManager;
00146
00147 delete pEventManager;
00148 delete pDataStore;
00149
00150
00151 pDevice->drop();
00152 }
00153
00154
00155 void GameManager::grab()
00156 {
00157
00158 }
00159
00160
00161 void GameManager::drop()
00162 {
00163
00164 }
00165
00166
00167 void GameManager::changeState(GameState *state)
00168 {
00169
00170 popState();
00171
00172
00173 if(state != NULL)
00174 {
00175 mGameStates.push_back(state);
00176 state->grab();
00177 state->init();
00178 }
00179 }
00180
00181
00182 void GameManager::pushState(GameState *state)
00183 {
00184
00185 if(!mGameStates.empty())
00186 mGameStates.back()->onPause();
00187
00188
00189 if(state != NULL)
00190 {
00191 mGameStates.push_back(state);
00192 state->grab();
00193 state->init();
00194 }
00195 }
00196
00197
00198 void GameManager::popState()
00199 {
00200
00201 if(!mGameStates.empty())
00202 {
00203 mGameStates.back()->clear();
00204 mGameStates.back()->drop();
00205 mGameStates.pop_back();
00206 }
00207
00208
00209 if(!mGameStates.empty())
00210 mGameStates.back()->onUnPause();
00211 }
00212
00213
00214 IrrlichtDevice* GameManager::getDevice()
00215 {
00216 return pDevice;
00217 }
00218
00219
00220 IVideoDriver* GameManager::getDriver()
00221 {
00222 return pDriver;
00223 }
00224
00225
00226 ISceneManager* GameManager::getSceneManager()
00227 {
00228 return pSceneManager;
00229 }
00230
00231
00232 IGUIEnvironment* GameManager::getGUIEnvironment()
00233 {
00234 return pGUIEnvironment;
00235 }
00236
00237
00238 AssetManager* GameManager::getAssetManager()
00239 {
00240 return pAssetManager;
00241 }
00242
00243
00244 DataStore* GameManager::getDataStore()
00245 {
00246 return pDataStore;
00247 }
00248
00249
00250 EntityManager* GameManager::getEntityManager()
00251 {
00252 return pEntityManager;
00253 }
00254
00255
00256 EventManager* GameManager::getEventManager()
00257 {
00258 return pEventManager;
00259 }
00260
00261 #ifdef __COMPILE_WITH_ANGELSCRIPT__
00262
00263
00264 ScriptManager* GameManager::getScriptManager()
00265 {
00266 return pScriptManager;
00267 }
00268
00269 #endif // __COMPILE_WITH_ANGELSCRIPT__
00270
00271 #ifdef __COMPILE_WITH_SFML_AUDIO__
00272
00273
00274 SoundManager* GameManager::getSoundManager()
00275 {
00276 return pSoundManager;
00277 }
00278
00279 #endif // __COMPILE_WITH_SFML_AUDIO__
00280
00281
00282 bool GameManager::getIsRunning() const
00283 {
00284 return pDevice->run();
00285 }
00286
00287
00288
00289 void GameManager::setIsRunning(bool value)
00290 {
00291 if(value == false)
00292 pDevice->closeDevice();
00293 }
00294
00295