Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ogre/src/orxonox/GraphicsEngine.cc @ 1251

Last change on this file since 1251 was 1249, checked in by rgrieder, 17 years ago
  • added initialiser lists
File size: 10.5 KB
RevLine 
[612]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[612]4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
24 *   Co-authors:
[1032]25 *      Reto Grieder
[612]26 *
27 */
[1035]28
[612]29 /**
30    @file orxonox.cc
31    @brief Orxonox class
32  */
33
[1021]34#include "OrxonoxStableHeaders.h"
[1039]35#include "GraphicsEngine.h"
[612]36
37#include <OgreRoot.h>
[1021]38#include <OgreException.h>
[612]39#include <OgreConfigFile.h>
[1024]40#include <OgreLogManager.h>
[612]41#include <OgreTextureManager.h>
[1214]42#include "core/InputManager.h"
[1052]43#include "core/CoreIncludes.h"
44#include "core/ConfigValueIncludes.h"
[1214]45#include "core/Debug.h"
[1032]46
[612]47
48namespace orxonox {
49
[1032]50  /**
51    @brief Returns the singleton instance and creates it the first time.
52    @return The only instance of GraphicsEngine.
53  */
[1249]54  /*static*/ GraphicsEngine& GraphicsEngine::getSingleton()
[1032]55  {
56    static GraphicsEngine theOnlyInstance;
57    return theOnlyInstance;
58  }
59
60  /**
61    @brief Only use constructor to initialise variables and pointers!
62  */
[1249]63  GraphicsEngine::GraphicsEngine() :
64    root_(0),
65    scene_(0),
66    renderWindow_(0),
67    //configPath_(""),
68    dataPath_(""),
69    ogreLogfile_("")
[612]70  {
[1021]71    RegisterObject(GraphicsEngine);
[1249]72
[1243]73    this->setConfigValues();
[1244]74    CCOUT(4) << "Constructed" << std::endl;
[612]75  }
76
[1249]77  void GraphicsEngine::setConfigValues()
78  {
79    SetConfigValue(dataPath_, "../../Media/").description("relative path to media data");
80    SetConfigValue(ogreLogfile_, "ogre.log").description("Logfile for messages from Ogre. Use \"\" to suppress log file creation.");
81    SetConfigValue(ogreLogLevelTrivial_ , 5).description("Corresponding orxonox debug level for ogre Trivial");
82    SetConfigValue(ogreLogLevelNormal_  , 4).description("Corresponding orxonox debug level for ogre Normal");
83    SetConfigValue(ogreLogLevelCritical_, 2).description("Corresponding orxonox debug level for ogre Critical");
84  }
85
[1032]86  /**
87    @brief Called after main() --> call destroyObjects()!
88  */
[612]89  GraphicsEngine::~GraphicsEngine()
90  {
[1032]91    this->destroy();
92  }
93
94  /**
95    @brief Destroys all the internal objects. Call this method when you
96           normally would call the destructor.
97  */
98  void GraphicsEngine::destroy()
99  {
[1244]100    COUT(4) << "Destroying objects..." << std::endl;
[1214]101    Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this);
[1024]102    if (this->root_)
[1021]103      delete this->root_;
[1032]104    this->root_ = 0;
105    this->scene_ = 0;
106    this->renderWindow_ = 0;
107    // delete the ogre log and the logManager (since we have created it).
[1052]108    if (Ogre::LogManager::getSingletonPtr() != 0)
[1024]109    {
[1052]110      Ogre::LogManager::getSingleton().getDefaultLog()->removeListener(this);
111      Ogre::LogManager::getSingleton().destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
112      delete Ogre::LogManager::getSingletonPtr();
[1024]113    }
[1244]114    COUT(4) << "Destroying objects done" << std::endl;
[612]115  }
116
[1024]117  /**
118    @brief Creates the Ogre Root object and sets up the ogre log.
119  */
[1243]120  bool GraphicsEngine::setup(std::string& dataPath)
[612]121  {
[1244]122    CCOUT(3) << "Setting up..." << std::endl;
[1243]123    // temporary overwrite of dataPath, change ini file for permanent change
124    if (dataPath != "")
125      dataPath_ = dataPath;
126    if (dataPath_ == "")
127      return false;
128    if (dataPath_[dataPath_.size() - 1] != '/')
129      dataPath_ += "/";
130
[612]131    //TODO: Check if file exists (maybe not here)
[1021]132#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC && defined(_DEBUG)
[715]133    std::string plugin_filename = "plugins_d.cfg";
[679]134#else
[715]135    std::string plugin_filename = "plugins.cfg";
[679]136#endif
[1024]137
138    // create a logManager
[1062]139    Ogre::LogManager *logger;
140                if (Ogre::LogManager::getSingletonPtr() == 0)
141                        logger = new Ogre::LogManager();
[1024]142    else
[1062]143      logger = Ogre::LogManager::getSingletonPtr();
[1244]144    CCOUT(4) << "Ogre LogManager created/assigned" << std::endl;
[1024]145
146    // create our own log that we can listen to
[1062]147    Ogre::Log *myLog;
[1024]148    if (this->ogreLogfile_ == "")
149      myLog = logger->createLog("ogre.log", true, false, true);
150    else
151      myLog = logger->createLog(this->ogreLogfile_, true, false, false);
[1032]152    COUT(4) << "*** GraphicsEngine: Ogre Log created" << std::endl;
[1024]153
[1062]154    myLog->setLogDetail(Ogre::LL_BOREME);
155    myLog->addListener(this);
[1024]156
157    // Root will detect that we've already created a Log
[1244]158    CCOUT(4) << "Creating Ogre Root..." << std::endl;
[1052]159    root_ = new Ogre::Root(plugin_filename);
[1244]160    CCOUT(4) << "Creating Ogre Root done" << std::endl;
[612]161
[1243]162    // specify where Ogre has to look for resources. This call doesn't parse anything yet!
163    declareRessourceLocations();
[612]164
[1244]165    CCOUT(3) << "Set up done." << std::endl;
[612]166    return true;
167  }
168
[1243]169  void GraphicsEngine::declareRessourceLocations()
[612]170  {
[1244]171    CCOUT(4) << "Declaring Resources" << std::endl;
[612]172    //TODO: Specify layout of data file and maybe use xml-loader
173    //TODO: Work with ressource groups (should be generated by a special loader)
174    // Load resource paths from data file using configfile ressource type
[1052]175    Ogre::ConfigFile cf;
[1243]176    cf.load(dataPath_ + "resources.cfg");
[612]177
178    // Go through all sections & settings in the file
[1052]179    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
[612]180
[715]181    std::string secName, typeName, archName;
[612]182    while (seci.hasMoreElements())
183    {
184      secName = seci.peekNextKey();
[1052]185      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
186      Ogre::ConfigFile::SettingsMultiMap::iterator i;
[612]187      for (i = settings->begin(); i != settings->end(); ++i)
188      {
189        typeName = i->first; // for instance "FileSystem" or "Zip"
190        archName = i->second; // name (and location) of archive
191
[1052]192        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
[1243]193                                           std::string(dataPath_ + archName),
[612]194                                           typeName, secName);
195      }
196    }
197  }
198
[1243]199  bool GraphicsEngine::loadRenderer()
200  {
[1244]201    CCOUT(4) << "Configuring Renderer" << std::endl;
[1243]202    if (!root_->restoreConfig() && !root_->showConfigDialog())
203      return false;
204
[1244]205    CCOUT(4) << "Creating render window" << std::endl;
[1243]206    this->renderWindow_ = root_->initialise(true, "OrxonoxV2");
207    if (!root_->isInitialised())
[1244]208    {
209      CCOUT(2) << "Error: Creating Ogre root object failed" << std::endl;
[1243]210      return false;
[1244]211    }
[1243]212    Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
213    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
214    return true;
215  }
216
[1244]217  bool GraphicsEngine::initialiseResources()
[1243]218  {
[1244]219    CCOUT(4) << "Initialising resources" << std::endl;
[1243]220    //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
[1244]221    try
222    {
223      Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
224    }
225    catch (Ogre::Exception e)
226    {
227      CCOUT(2) << "Error: There was an Error when initialising the resources." << std::endl;
228      CCOUT(2) << "ErrorMessage: " << e.getFullDescription() << std::endl;
229      return false;
230    }
231    return true;
[1243]232  }
233
[1021]234  /**
[1243]235   * @brief Creates the SceneManager
236   */
237  bool GraphicsEngine::createNewScene()
238  {
[1244]239    CCOUT(4) << "Creating new SceneManager" << std::endl;
[1243]240    if (scene_)
[1244]241    {
242      CCOUT(2) << "SceneManager already exists! Skipping." << std::endl;
[1243]243      return false;
[1244]244    }
[1243]245    scene_ = root_->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager");
[1244]246    CCOUT(3) << "Created SceneManager: " << scene_ << std::endl;
[1243]247    return true;
248  }
249
250  /**
[1021]251    Returns the window handle of the render window.
252    At least the InputHandler uses this to create the OIS::InputManager
253    @return The window handle of the render window
254  */
255  size_t GraphicsEngine::getWindowHandle()
256  {
257    if (this->renderWindow_)
258    {
[1024]259      size_t windowHnd = 0;
[1032]260      this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
[1024]261      return windowHnd;
262    }
[1021]263    else
264      return 0;
265  }
[612]266
[1021]267  /**
268    Get the width of the current render window
269    @return The width of the current render window
270  */
271  int GraphicsEngine::getWindowWidth() const
272  {
273    if (this->renderWindow_)
274      return this->renderWindow_->getWidth();
275    else
276      return 0;
277  }
278
279  /**
280    Get the height of the current render window
281    @return The height of the current render window
282  */
283  int GraphicsEngine::getWindowHeight() const
284  {
285    if (this->renderWindow_)
286      return this->renderWindow_->getHeight();
287    else
288      return 0;
289  }
290
[1024]291  /**
292    @brief Method called by the LogListener interface from Ogre.
293    We use it to capture Ogre log messages and handle it ourselves.
294    @param message The message to be logged
295    @param lml The message level the log is using
296    @param maskDebug If we are printing to the console or not
297    @param logName the name of this log (so you can have several listeners
298                   for different logs, and identify them)
299  */
300  void GraphicsEngine::messageLogged(const std::string& message,
[1052]301    Ogre::LogMessageLevel lml, bool maskDebug, const std::string &logName)
[1024]302  {
303    int orxonoxLevel;
304    switch (lml)
305    {
[1052]306      case Ogre::LML_TRIVIAL:
[1024]307        orxonoxLevel = this->ogreLogLevelTrivial_;
308        break;
[1052]309      case Ogre::LML_NORMAL:
[1024]310        orxonoxLevel = this->ogreLogLevelNormal_;
311        break;
[1052]312      case Ogre::LML_CRITICAL:
[1024]313        orxonoxLevel = this->ogreLogLevelCritical_;
314        break;
315      default:
316        orxonoxLevel = 0;
317    }
318    OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
[1243]319        << "Ogre: " << message << std::endl;
[1024]320  }
[1214]321
322    void GraphicsEngine::windowMoved(Ogre::RenderWindow *rw){
323        int w = rw->getWidth();
324        int h = rw->getHeight();
[1220]325        InputManager::setWindowExtents(w, h);
[1214]326    }
327
328    void GraphicsEngine::windowResized(Ogre::RenderWindow *rw){
329        int w = rw->getWidth();
330        int h = rw->getHeight();
[1220]331        InputManager::setWindowExtents(w, h);
[1214]332    }
333
334    void GraphicsEngine::windowFocusChanged(Ogre::RenderWindow *rw){
335        int w = rw->getWidth();
336        int h = rw->getHeight();
[1220]337        InputManager::setWindowExtents(w, h);
[1214]338    }
[612]339}
Note: See TracBrowser for help on using the repository browser.