00001 #include "Game.h"
00002
00003 #include "Level.h"
00004 #include "GameListener.h"
00005 #include "Updater.h"
00006
00007 #include "OgreRoot.h"
00008 #include "OgreException.h"
00009 using Ogre::Root;
00010 using Ogre::Exception;
00011
00012
00013
00014 template<> ASR::Game* Singleton<ASR::Game>::ms_Singleton = 0;
00015
00016
00017 namespace ASR
00018 {
00019
00020 Game::Game ( SceneManager* sceneMgr, RenderWindow* win )
00021 : mSceneMgr ( sceneMgr ), mWindow ( win )
00022 {
00023 mLevel = NULL;
00024 mListener = NULL;
00025 }
00026
00027
00028
00029 Game::~Game ()
00030 {
00031 destroyLevel ();
00032 }
00033
00034
00035
00036 Game* Game::getSingletonPtr(void)
00037 {
00038 return ms_Singleton;
00039 }
00040 Game& Game::getSingleton(void)
00041 {
00042 assert( ms_Singleton ); return ( *ms_Singleton );
00043 }
00044
00045
00046
00047 Level* Game::createLevel ()
00048 {
00049 if ( mLevel != NULL )
00050 throw Exception ( Exception::ERR_DUPLICATE_ITEM, "Level already created", "Game::createLevel" );
00051
00052 if ( mListener != NULL )
00053 throw Exception ( Exception::ERR_DUPLICATE_ITEM, "There is already an existing Listener", "Game::createLevel" );
00054
00055 mListener = new GameListener ( mWindow );
00056 Ogre::Root::getSingleton().addFrameListener( mListener );
00057
00058 mLevel = new Level ();
00059 return mLevel;
00060 }
00061
00062
00063
00064 SceneManager* Game::getSceneManager() const
00065 {
00066 return mSceneMgr;
00067 }
00068
00069
00070
00071 Level* Game::getLevel () const
00072 {
00073 if ( mLevel == NULL )
00074 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Level does not exist", "Game::getLevel" );
00075
00076 return mLevel;
00077 }
00078
00079
00080
00081 void Game::destroyLevel ()
00082 {
00083 if ( mLevel == NULL )
00084 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Level does not exist", "Game::getLevel" );
00085
00086 Ogre::Root::getSingleton().removeFrameListener ( mListener );
00087 delete mListener;
00088 mListener = NULL;
00089
00090 delete mLevel;
00091 mLevel = NULL;
00092 }
00093
00094
00095
00096 void Game::addUpdater ( Updater* updater )
00097 {
00098 OgreAssert ( mListener != NULL, "Listener has not been created - Game::addUpdater" );
00099 mListener->addUpdater ( updater );
00100 }
00101 }