C:/Users/Mike/Documents/Visual Studio Projects/GameEngine/src/GameApp.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 Copyright (C) 2006 Michael Harnetty
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018 -----------------------------------------------------------------------------
00019 */
00020 
00021 #include "GameApp.h"
00022 
00023 namespace GameEngine {
00024 
00025         GameApp::GameApp()
00026                 :       mRender(false), 
00027                         mInitialised(false), 
00028                         mEventManager(NULL), 
00029                         mRenderer(NULL), 
00030                         mWindow(NULL)
00031         {
00032                 
00033         }
00034 
00035 
00036         GameApp::~GameApp()
00037         {
00038                 if(mSoundManager)
00039                         delete mSoundManager;
00040 
00041                 if(mInputManager)
00042                         delete mInputManager;
00043 
00044                 if(mRenderer)
00045                         delete mRenderer;
00046 
00047                 if(mEventManager)
00048                         delete mEventManager;
00049 
00050         }
00051 
00052 
00053         void GameApp::start()
00054         {
00055                 mEventManager = new EventManager();
00056                 if(!mEventManager)
00057                         return;
00058 
00060 
00061                 mRenderer = new Ogre::Root("plugins.cfg","ogre.cfg");
00062                 if(!mRenderer)
00063                         return;
00064 
00065                 mRenderer->addFrameListener(this);
00066 
00067                 setupResources();
00068 
00069                 if (!loadConfiguration()) return;
00070 
00071                 mWindow = mRenderer->initialise(true);
00072 
00073                 Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
00074 
00075 
00077 
00078                 mInputManager = new InputManager(this);
00079                 if(!mInputManager)
00080                         return;
00081                 if(!mInputManager->init())
00082                         return;
00083 
00084                 mSoundManager = new SoundManager(this);
00085                 if(!mSoundManager)
00086                         return;
00087                 
00088                 if(!mSoundManager->init())
00089                         return;
00090 
00092 
00093                 mGame = createGameAndViews();
00094                 if(!mGame)
00095                         return;
00096 
00097                 mGame->loadGame();
00098 
00100                 
00101                 mRender = true;
00102 
00103                 mainLoop();
00104         }
00105 
00106 
00107         void GameApp::stop()
00108         {
00109                 mRender = false;
00110         }
00111 
00112 
00113         void GameApp::mainLoop()
00114         {
00115                 // Main game loop
00116                 while(mRender){
00117                         mRender = mRenderer->renderOneFrame(); // Render a single frame
00118                         onUpdate(mTimeSinceLastFrame); // Note to self: Should we update before or after frame rendered ? Having it before causes problems (eg Input which tells us to quit rendering is ove ridden by call to renderOneFrame
00119                 }
00120 
00121 
00122                 // Rendering finished, time to shutdown
00123                 mRenderer->shutdown();
00124         }
00125 
00126 
00127         void GameApp::onUpdate(const double& dt)
00128         {
00129                 mInputManager->update(dt);
00130                 mSoundManager->update(dt);
00131                 mEventManager->tick(TICK_LIMIT); // Send events off to relevant handlers
00132                 mGame->onUpdate(dt); // Update the game
00133         }
00134 
00135 
00136         
00138 
00139         bool GameApp::frameStarted( const Ogre::FrameEvent& evt )
00140         {
00141                 mTimeSinceLastFrame = evt.timeSinceLastFrame;
00142                 return true;
00143         }
00144 
00145         bool GameApp::frameEnded( const Ogre::FrameEvent& evt )
00146         {
00147                 return true;
00148         }
00149 
00150 
00152 
00153         void GameApp::setupResources()
00154         {
00155                 // Load resource paths from config file
00156                 Ogre::ConfigFile cf;
00157                 cf.load("resources.cfg");
00158 
00159                 // Go through all sections & settings in the file
00160                 Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
00161 
00162                 std::string secName, typeName, archName;
00163                 while (seci.hasMoreElements())
00164                 {
00165                         secName = seci.peekNextKey();
00166                         Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
00167                         Ogre::ConfigFile::SettingsMultiMap::iterator i;
00168                         for (i = settings->begin(); i != settings->end(); ++i)
00169                         {
00170                                 typeName = i->first;
00171                                 archName = i->second;
00172                                 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
00173                                         archName, typeName, secName);
00174                         }
00175                 }
00176         }
00177 
00178         bool GameApp::loadConfiguration() // May choose to load direct from file instead of showing config dialog here
00179         {
00180                 return mRenderer->showConfigDialog();
00181         }
00182 }

Generated on Thu Apr 19 09:47:02 2007 for GameEngine by  doxygen 1.5.2