00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "GameManager.h"
00021
00022
00023
00024
00025 GameManager::GameManager()
00026 : pDevice(0), mLastTime(0)
00027 {
00028 }
00029
00030
00031 GameManager::~GameManager()
00032 {
00033 clear();
00034 }
00035
00036
00037 GameManager* GameManager::pInstance = 0;
00038
00039 GameManager* GameManager::Instance()
00040 {
00041 if(pInstance == 0)
00042 pInstance = new GameManager();
00043
00044 return pInstance;
00045 }
00046
00047 GameManager& GameManager::Reference()
00048 {
00049 if(pInstance == 0)
00050 pInstance = new GameManager();
00051
00052 return *pInstance;
00053 }
00054
00055
00056 void GameManager::init()
00057 {
00058
00059 pDataStore = new DataStore();
00060 pEventManager = new EventManager();
00061
00062
00063 pDevice = createDevice(EDT_OPENGL, dimension2d<u32>(1024, 768), 32, false, false, false, pEventManager);
00064
00065
00066 pDriver = pDevice->getVideoDriver();
00067 pSceneManager = pDevice->getSceneManager();
00068 pGUIEnvironment = pDevice->getGUIEnvironment();
00069
00070
00071 pEntityManager = new EntityManager();
00072 pSoundManager = new SoundManager();
00073
00074 pScriptManager = new ScriptManager();
00075 pScriptManager->init();
00076 }
00077
00078
00079 void GameManager::update()
00080 {
00081
00082 u32 time = pDevice->getTimer()->getTime();
00083 f32 deltaTime = ( (float)time - (float)mLastTime ) *0.01f;
00084 mLastTime = time;
00085
00086 if(!mGameStates.empty() && deltaTime > 0.0f)
00087 {
00088 mGameStates.back()->onUpdate(deltaTime);
00089 mGameStates.back()->update(deltaTime);
00090 }
00091
00092
00093 if(pDevice->isWindowActive())
00094 {
00095 if(!mGameStates.empty())
00096 {
00097 pDriver->beginScene();
00098
00099 pSceneManager->drawAll();
00100 pGUIEnvironment->drawAll();
00101
00102 mGameStates.back()->onRender();
00103 mGameStates.back()->render();
00104
00105 pDriver->endScene();
00106 }
00107 }
00108 }
00109
00110
00111 void GameManager::clear()
00112 {
00113
00114 while(!mGameStates.empty())
00115 {
00116 mGameStates.back()->clear();
00117 mGameStates.pop_back();
00118 }
00119
00120
00121 pScriptManager->clear();
00122 delete pScriptManager;
00123
00124 delete pSoundManager;
00125 delete pEventManager;
00126 delete pEntityManager;
00127 delete pDataStore;
00128
00129
00130 pDevice->drop();
00131 }
00132
00133
00134 GameManager* GameManager::refFactory()
00135 {
00136 return GameManager::Instance();
00137 }
00138
00139
00140 void GameManager::refAdd()
00141 {
00142
00143 }
00144
00145
00146 void GameManager::refRelease()
00147 {
00148
00149 }
00150
00151
00152 void GameManager::changeState(GameState *state)
00153 {
00154
00155 popState();
00156
00157
00158 if(state != NULL)
00159 {
00160 mGameStates.push_back(state);
00161 state->init();
00162 }
00163 }
00164
00165
00166 void GameManager::changeState(asIScriptObject *object)
00167 {
00168
00169 popState();
00170
00171
00172 if(object != NULL)
00173 {
00174 ScriptedGameState *state = new ScriptedGameState(object);
00175 mGameStates.push_back(state);
00176 state->init();
00177 }
00178 }
00179
00180
00181 void GameManager::pushState(GameState *state)
00182 {
00183
00184 if(!mGameStates.empty())
00185 mGameStates.back()->onPause();
00186
00187
00188 if(state != NULL)
00189 {
00190 mGameStates.push_back(state);
00191 state->init();
00192 }
00193 }
00194
00195
00196 void GameManager::pushState(asIScriptObject *object)
00197 {
00198
00199 if(!mGameStates.empty())
00200 mGameStates.back()->onPause();
00201
00202
00203 if(object != NULL)
00204 {
00205 ScriptedGameState *state = new ScriptedGameState(object);
00206 mGameStates.push_back(state);
00207 state->init();
00208 }
00209 }
00210
00211
00212 void GameManager::popState()
00213 {
00214
00215 if(!mGameStates.empty())
00216 {
00217 mGameStates.back()->clear();
00218 delete mGameStates.back();
00219 mGameStates.pop_back();
00220 }
00221
00222
00223 if(!mGameStates.empty())
00224 mGameStates.back()->onUnPause();
00225 }
00226
00227
00228 IrrlichtDevice* GameManager::getDevice()
00229 {
00230 return pDevice;
00231 }
00232
00233
00234 IVideoDriver* GameManager::getDriver()
00235 {
00236 return pDriver;
00237 }
00238
00239
00240 ISceneManager* GameManager::getSceneManager()
00241 {
00242 return pSceneManager;
00243 }
00244
00245
00246 IGUIEnvironment* GameManager::getGUIEnvironment()
00247 {
00248 return pGUIEnvironment;
00249 }
00250
00251
00252 DataStore* GameManager::getDataStore()
00253 {
00254 return pDataStore;
00255 }
00256
00257
00258 EntityManager* GameManager::getEntityManager()
00259 {
00260 return pEntityManager;
00261 }
00262
00263
00264 EventManager* GameManager::getEventManager()
00265 {
00266 return pEventManager;
00267 }
00268
00269
00270 ScriptManager* GameManager::getScriptManager()
00271 {
00272 return pScriptManager;
00273 }
00274
00275
00276 SoundManager* GameManager::getSoundManager()
00277 {
00278 return pSoundManager;
00279 }
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
00296
00297 void bindGameManager(asIScriptEngine *engine)
00298 {
00299
00300 int r;
00301
00302
00303 r = engine->RegisterObjectType("GameManager", sizeof(GameManager), asOBJ_REF); assert(r >= 0);
00304
00305
00306 r = engine->RegisterObjectBehaviour("GameManager", asBEHAVE_FACTORY, "GameManager@ f()",
00307 asFUNCTION(GameManager::refFactory), asCALL_CDECL); assert(r >= 0);
00308 r = engine->RegisterObjectBehaviour("GameManager", asBEHAVE_ADDREF, "void f()",
00309 asMETHOD(GameManager, refAdd), asCALL_THISCALL); assert(r >= 0);
00310 r = engine->RegisterObjectBehaviour("GameManager", asBEHAVE_RELEASE, "void f()",
00311 asMETHOD(GameManager, refRelease), asCALL_THISCALL); assert(r >= 0);
00312
00313
00314 r = engine->RegisterGlobalFunction("GameManager& getGameManager()", asFUNCTION(GameManager::Instance),
00315 asCALL_CDECL); assert( r >= 0 );
00316
00317 r = engine->RegisterObjectMethod("GameManager", "void changeState(GameState @)",
00318 asMETHODPR(GameManager, changeState, (GameState*), void),
00319 asCALL_THISCALL); assert(r >= 0);
00320 r = engine->RegisterObjectMethod("GameManager", "void changeState(IGameState @)",
00321 asMETHODPR(GameManager, changeState, (asIScriptObject*), void),
00322 asCALL_THISCALL); assert(r >= 0);
00323 r = engine->RegisterObjectMethod("GameManager", "void pushState(GameState @)",
00324 asMETHODPR(GameManager, pushState, (GameState*), void),
00325 asCALL_THISCALL); assert(r >= 0);
00326 r = engine->RegisterObjectMethod("GameManager", "void pushState(IGameState @)",
00327 asMETHODPR(GameManager, pushState, (asIScriptObject*), void),
00328 asCALL_THISCALL); assert(r >= 0);
00329 r = engine->RegisterObjectMethod("GameManager", "void popState()",
00330 asMETHOD(GameManager, popState), asCALL_THISCALL); assert(r >= 0);
00331
00332 r = engine->RegisterObjectMethod("GameManager", "DataStore@ getDataStore()",
00333 asMETHOD(GameManager, getDataStore), asCALL_THISCALL); assert(r >= 0);
00334 r = engine->RegisterObjectMethod("GameManager", "EntityManager@ getEntityManager()",
00335 asMETHOD(GameManager, getEntityManager), asCALL_THISCALL); assert(r >= 0);
00336 r = engine->RegisterObjectMethod("GameManager", "EventManager@ getEventManager()",
00337 asMETHOD(GameManager, getEventManager), asCALL_THISCALL); assert(r >= 0);
00338 r = engine->RegisterObjectMethod("GameManager", "ScriptManager@ getScriptManager()",
00339 asMETHOD(GameManager, getScriptManager), asCALL_THISCALL); assert(r >= 0);
00340 r = engine->RegisterObjectMethod("GameManager", "SoundManager@ getSoundManager()",
00341 asMETHOD(GameManager, getSoundManager), asCALL_THISCALL); assert(r >= 0);
00342
00343 r = engine->RegisterObjectMethod("GameManager", "bool getIsRunning()",
00344 asMETHOD(GameManager, getIsRunning), asCALL_THISCALL); assert(r >= 0);
00345
00346 r = engine->RegisterObjectMethod("GameManager", "void setIsRunning(bool)",
00347 asMETHOD(GameManager, setIsRunning), asCALL_THISCALL); assert(r >= 0);
00348 }
00349
00350