00001 #include "BreakoutApp.h"
00002
00003 #include "GameConfig.h"
00004 #include "BreakoutLGLines.h"
00005 #include "EventManager.h"
00006 #include "InputEvents.h"
00007 #include "ApplicationEvents.h"
00008 #include "BreakoutEvents.h"
00009
00010 #include "InputHandler.h"
00011 #include "SoundManager.h"
00012
00013 #include "BreakoutGameScene.h"
00014 #include "BreakoutSplashScreenScene.h"
00015 #include "BreakoutMainMenuScene.h"
00016 #include "BreakoutPauseScene.h"
00017 #include "BreakoutGameOverScene.h"
00018 #include "BreakoutErrorScene.h"
00019
00020 #include "Profiler.h"
00021 #include "GameException.h"
00022 #include <iostream>
00023
00024 template<> BreakoutApp* TSingleton<BreakoutApp>::m_pInstance = NULL;
00025
00026 BreakoutApp::BreakoutApp()
00027 : m_currentScene(eSplashScreenScene), m_bQuitGame(false), m_pScreen(0), m_bgMusicID(0)
00028 {
00029 for(int i = 0; i < eNumSceneTypes; i++)
00030 m_sceneArray[i] = 0;
00031 }
00032
00033 BreakoutApp::~BreakoutApp()
00034 {
00035 }
00036
00037 void BreakoutApp::Run()
00038 {
00039 SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_AUDIO);
00040
00041 try
00042 {
00043 Initialise();
00044 }
00045 catch(GameException e)
00046 {
00047 DebugUtil::outputDebugText("BreakoutApp: Error occured during initialisation, quitting game.\n");
00048 m_bQuitGame = true;
00049 }
00050
00051 if(!m_bQuitGame)
00052 {
00053 ChangeScene(eMainMenuScene);
00054 SoundManager::getSingleton()->PlaySoundID(m_bgMusicID, true);
00055 }
00056
00057 unsigned int lastUpdate = 0, lastFPSUpdate = 0, numFramesThisSecond = 0;
00058 while(!m_bQuitGame)
00059 {
00060 try
00061 {
00062 InputHandler::getSingleton()->Update();
00063 if(lastUpdate + GAME_TICK_RATE < SDL_GetTicks())
00064 {
00065 EventManager::getSingleton()->ProcessQueue();
00066 Update();
00067 lastUpdate = SDL_GetTicks();
00068 }
00069
00070 SoundManager::getSingleton()->Update();
00071
00072 Draw();
00073 numFramesThisSecond++;
00074
00075 if(lastFPSUpdate + 1000 < SDL_GetTicks())
00076 {
00077 DebugUtil::outputDebugText("Frames Per Second: %d\n", (int)numFramesThisSecond);
00078 numFramesThisSecond = 0;
00079 lastFPSUpdate = SDL_GetTicks();
00080 }
00081 }
00082 catch(GameException e)
00083 {
00084 switch(e.GetSeverity())
00085 {
00086 case eFatalError:
00087 m_bQuitGame = true;
00088 break;
00089
00090 case eMinorError:
00091 case eMajorError:
00092 if(m_currentScene != eErrorScene)
00093 ChangeScene(eErrorScene);
00094 break;
00095 }
00096 }
00097 }
00098
00099 Shutdown();
00100 }
00101
00102 void BreakoutApp::Draw()
00103 {
00104 if(!m_bQuitGame)
00105 {
00106 m_sceneArray[m_currentScene]->Draw();
00107 SDL_Flip(m_pScreen);
00108 }
00109 }
00110
00111 void BreakoutApp::Update()
00112 {
00113 if(!m_bQuitGame)
00114 {
00115 m_sceneArray[m_currentScene]->Update();
00116
00117 InputHandler* pInput = InputHandler::getSingleton();
00118 if(pInput->IsKeyDown(eKeyButtonL) && pInput->IsKeyDown(eKeyButtonR) && pInput->IsKeyDown(eKeyButtonStart))
00119 m_bQuitGame = true;
00120 }
00121 }
00122
00123 void BreakoutApp::Initialise()
00124 {
00125 Profiler* pProfile = new Profiler("Visualisation");
00126 pProfile->Start();
00127 Visualisation::getSingleton()->Initialise(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_DEPTH);
00128 pProfile->End();
00129 pProfile->PrintResults();
00130 delete pProfile;
00131 DebugUtil::outputDebugText("Visualisation initialised.\n");
00132
00133 #ifdef _WIN32
00134 SDL_WM_SetCaption("Break Down!", 0);
00135 #endif
00136
00137 m_pScreen = Visualisation::getSingleton()->GetScreen();
00138
00139 pProfile = new Profiler("SoundManager");
00140 pProfile->Start();
00141 SoundManager::getSingleton()->Initialise(22050, 512);
00142 pProfile->End();
00143 pProfile->PrintResults();
00144 delete pProfile;
00145 DebugUtil::outputDebugText("SoundManager initialised.\n");
00146
00147 pProfile = new Profiler("Breakout Scenes");
00148 pProfile->Start();
00149 InitialiseScenes();
00150 pProfile->End();
00151 pProfile->PrintResults();
00152 delete pProfile;
00153 DebugUtil::outputDebugText("Breakout scenes initialised.\n");
00154
00155
00156
00157 m_sceneArray[m_currentScene]->Activate();
00158 Draw();
00159
00160 pProfile = new Profiler("Song Creation");
00161 pProfile->Start();
00162 m_bgMusicID = SoundManager::getSingleton()->CreateSoundSong(true, 60, 5, 12, 4000);
00163 pProfile->End();
00164 pProfile->PrintResults();
00165 delete pProfile;
00166 DebugUtil::outputDebugText("Test song created.\n");
00167
00168 for(int i = 0; i < NUM_BOX_COLOURS; i++)
00169 {
00170 ColourRGB randomColour = ColourRGB::GetRandomColour();
00171 ColourRGB darkerRColour = randomColour;
00172 darkerRColour.r /= 2;
00173 darkerRColour.g /= 2;
00174 darkerRColour.b /= 2;
00175 SpriteID sprID = Visualisation::getSingleton()->CreateSpriteBox(MEDIUM_BOX_WIDTH, MEDIUM_BOX_HEIGHT, darkerRColour, randomColour, 2);
00176 m_vBoxSprites.push_back(sprID);
00177 }
00178
00179
00180 EventManager::getSingleton()->AddEventListener(ApplicationQuitEvent::cEventName, this);
00181 EventManager::getSingleton()->AddEventListener(BreakoutStartGameEvent::cEventName, this);
00182 EventManager::getSingleton()->AddEventListener(BreakoutPauseGameEvent::cEventName, this);
00183 EventManager::getSingleton()->AddEventListener(BreakoutUnpauseGameEvent::cEventName, this);
00184 EventManager::getSingleton()->AddEventListener(BreakoutGameOverEvent::cEventName, this);
00185
00186 unsigned int splashStartTime = SDL_GetTicks();
00187 while(splashStartTime + SPLASH_SCREEN_TIME > SDL_GetTicks())
00188 Draw();
00189 }
00190
00191 void BreakoutApp::InitialiseScenes()
00192 {
00193 m_sceneArray[eGameScene] = new BreakoutGameScene();
00194 DebugUtil::outputDebugText("Game Scene Created.\n");
00195
00196 m_sceneArray[eMainMenuScene] = new BreakoutMainMenuScene();
00197 DebugUtil::outputDebugText("Menu Scene Created.\n");
00198
00199 m_sceneArray[eGameOverScene] = new BreakoutGameOverScene();
00200 DebugUtil::outputDebugText("Game Over Scene Created.\n");
00201
00202 m_sceneArray[ePauseScene] = new BreakoutPauseScene();
00203 DebugUtil::outputDebugText("Pause Scene Created.\n");
00204
00205 m_sceneArray[eSplashScreenScene] = new BreakoutSplashScreenScene();
00206 DebugUtil::outputDebugText("Splash Screen Scene Created.\n");
00207
00208 m_sceneArray[eErrorScene] = new BreakoutErrorScene();
00209 DebugUtil::outputDebugText("Error Scene Created.\n");
00210
00211 m_sceneArray[eGameScene]->Initialise();
00212 DebugUtil::outputDebugText("Game Scene Initialised.\n");
00213 m_sceneArray[eMainMenuScene]->Initialise();
00214 DebugUtil::outputDebugText("Menu Scene Initialised.\n");
00215 m_sceneArray[ePauseScene]->Initialise();
00216 DebugUtil::outputDebugText("Pause Scene Initialised.\n");
00217 m_sceneArray[eGameOverScene]->Initialise();
00218 DebugUtil::outputDebugText("Game Over Scene Initialised.\n");
00219 m_sceneArray[eSplashScreenScene]->Initialise();
00220 DebugUtil::outputDebugText("Splash Screen Scene Initialised.\n");
00221 m_sceneArray[eErrorScene]->Initialise();
00222 DebugUtil::outputDebugText("Error Scene Initialised.\n");
00223
00224
00225
00226
00227 }
00228
00229 void BreakoutApp::Shutdown()
00230 {
00231 for(int i = 0; i < eNumSceneTypes; i++)
00232 {
00233 if(m_sceneArray[i])
00234 {
00235 delete m_sceneArray[i];
00236 m_sceneArray[i] = 0;
00237 }
00238 }
00239
00240 SoundManager::getSingleton()->Shutdown();
00241 SoundManager::deleteSingleton();
00242
00243 Visualisation::getSingleton()->Shutdown();
00244 Visualisation::deleteSingleton();
00245
00246 GameException::OutputLog(std::cout);
00247 }
00248
00249 bool BreakoutApp::HandleEvent(IEvent* pEvent)
00250 {
00251 HashString evtType = pEvent->GetEventType();
00252 if(evtType == ApplicationQuitEvent::cEventName)
00253 {
00254 m_bQuitGame = true;
00255 return true;
00256 }
00257
00258 if(evtType == BreakoutStartGameEvent::cEventName)
00259 {
00260 if(m_currentScene != eGameScene)
00261 {
00262 BreakoutGameScene* pGameScene = (BreakoutGameScene*)m_sceneArray[eGameScene];
00263 m_sceneArray[m_currentScene]->Deactivate();
00264 pGameScene->Reset();
00265 pGameScene->Activate();
00266 m_currentScene = eGameScene;
00267 }
00268
00269 return true;
00270 }
00271
00272 if(evtType == BreakoutPauseGameEvent::cEventName)
00273 {
00274 if(m_currentScene != ePauseScene)
00275 ChangeScene(ePauseScene);
00276
00277 return true;
00278 }
00279
00280 if(evtType == BreakoutUnpauseGameEvent::cEventName)
00281 {
00282 if(m_currentScene == ePauseScene || m_currentScene == eErrorScene)
00283 ChangeScene(eGameScene);
00284
00285 return true;
00286 }
00287
00288 if(evtType == BreakoutGameOverEvent::cEventName)
00289 {
00290 if(m_currentScene != eGameOverScene)
00291 ChangeScene(eGameOverScene);
00292
00293 return true;
00294 }
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309 return false;
00310 }
00311
00312 void BreakoutApp::ChangeScene(const eSceneType &sceneType)
00313 {
00314 m_sceneArray[m_currentScene]->Deactivate();
00315 m_currentScene = sceneType;
00316 m_sceneArray[m_currentScene]->Activate();
00317 }