Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/orxonox/GraphicsEngine.cc @ 1184

Last change on this file since 1184 was 1178, checked in by FelixSchulthess, 18 years ago

GraphicsEngine now listening to window events…

File size: 9.9 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>
[1178]42#include "core/InputManager.h"
[1052]43#include "core/CoreIncludes.h"
44#include "core/ConfigValueIncludes.h"
[1021]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  */
54  GraphicsEngine& GraphicsEngine::getSingleton()
55  {
56    static GraphicsEngine theOnlyInstance;
57    return theOnlyInstance;
58  }
59
60  /**
61    @brief Only use constructor to initialise variables and pointers!
62  */
[612]63  GraphicsEngine::GraphicsEngine()
64  {
[1021]65    RegisterObject(GraphicsEngine);
66    //this->bOverwritePath_ = false;
67    this->setConfigValues();
[612]68    // set to standard values
69    this->configPath_ = "";
[1021]70    this->root_ = 0;
71    this->scene_ = 0;
72    this->renderWindow_ = 0;
[1032]73    COUT(4) << "*** GraphicsEngine: Constructed" << std::endl;
[612]74  }
75
[1032]76  /**
77    @brief Called after main() --> call destroyObjects()!
78  */
[612]79  GraphicsEngine::~GraphicsEngine()
80  {
[1032]81    this->destroy();
82  }
83
84  /**
85    @brief Destroys all the internal objects. Call this method when you
86           normally would call the destructor.
87  */
88  void GraphicsEngine::destroy()
89  {
90    COUT(4) << "*** GraphicsEngine: Destroying objects..." << std::endl;
[1178]91    Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this);
[1024]92    if (this->root_)
[1021]93      delete this->root_;
[1032]94    this->root_ = 0;
95    this->scene_ = 0;
96    this->renderWindow_ = 0;
97    // delete the ogre log and the logManager (since we have created it).
[1052]98    if (Ogre::LogManager::getSingletonPtr() != 0)
[1024]99    {
[1052]100      Ogre::LogManager::getSingleton().getDefaultLog()->removeListener(this);
101      Ogre::LogManager::getSingleton().destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
102      delete Ogre::LogManager::getSingletonPtr();
[1024]103    }
[1032]104    COUT(4) << "*** GraphicsEngine: Destroying objects done" << std::endl;
[612]105  }
106
[1021]107  void GraphicsEngine::setConfigValues()
108  {
[1090]109    SetConfigValue(dataPath_, "../../Media/").description("relative path to media data");
[1024]110    SetConfigValue(ogreLogfile_, "ogre.log").description("Logfile for messages from Ogre. Use \"\" to suppress log file creation.");
[1032]111    SetConfigValue(ogreLogLevelTrivial_ , 5).description("Corresponding orxonox debug level for ogre Trivial");
112    SetConfigValue(ogreLogLevelNormal_  , 4).description("Corresponding orxonox debug level for ogre Normal");
113    SetConfigValue(ogreLogLevelCritical_, 2).description("Corresponding orxonox debug level for ogre Critical");
[1021]114  }
115
[1024]116  /**
117    @brief Creates the Ogre Root object and sets up the ogre log.
118  */
[612]119  void GraphicsEngine::setup()
120  {
121    //TODO: Check if file exists (maybe not here)
122/*#ifndef OGRE_STATIC_LIB
[1052]123    root_ = new Ogre::Root(configPath_ + "plugins.cfg", configPath_ + "ogre.cfg",
[612]124                     configPath_ + "Ogre.log");
125#else
[1052]126    root_ = new Ogre::Root(NULL, configPath_ + "ogre.cfg", configPath_ + "Ogre.log");
[612]127#endif*/
[1021]128#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC && defined(_DEBUG)
[715]129    std::string plugin_filename = "plugins_d.cfg";
[679]130#else
[715]131    std::string plugin_filename = "plugins.cfg";
[679]132#endif
[1024]133
134    // create a logManager
[1062]135    Ogre::LogManager *logger;
136                if (Ogre::LogManager::getSingletonPtr() == 0)
137                        logger = new Ogre::LogManager();
[1024]138    else
[1062]139      logger = Ogre::LogManager::getSingletonPtr();
[1032]140    COUT(4) << "*** GraphicsEngine: Ogre LogManager created/assigned" << std::endl;
[1024]141
142    // create our own log that we can listen to
[1062]143    Ogre::Log *myLog;
[1024]144    if (this->ogreLogfile_ == "")
145      myLog = logger->createLog("ogre.log", true, false, true);
146    else
147      myLog = logger->createLog(this->ogreLogfile_, true, false, false);
[1032]148    COUT(4) << "*** GraphicsEngine: Ogre Log created" << std::endl;
[1024]149
[1062]150    myLog->setLogDetail(Ogre::LL_BOREME);
151    myLog->addListener(this);
[1024]152
153    // Root will detect that we've already created a Log
[1032]154    COUT(4) << "*** GraphicsEngine: Creating Ogre Root..." << std::endl;
[1052]155    root_ = new Ogre::Root(plugin_filename);
[1032]156    COUT(4) << "*** GraphicsEngine: Creating Ogre Root done" << std::endl;
[612]157  }
158
159  /**
160   * @return scene manager
161   */
[1052]162  Ogre::SceneManager* GraphicsEngine::getSceneManager()
[612]163  {
164    if(!scene_)
165    {
[1052]166      scene_ = root_->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager");
[612]167      COUT(3) << "Info: Created SceneMan: " << scene_ << std::endl;
168    }
169    return scene_;
170  }
171
[1021]172  bool GraphicsEngine::load(std::string dataPath)
[612]173  {
[1021]174    // temporary overwrite of dataPath, change ini file for permanent change
175    if( dataPath != "" )
[1024]176      dataPath_ = dataPath + "/";
[612]177    loadRessourceLocations(this->dataPath_);
178    if (!root_->restoreConfig() && !root_->showConfigDialog())
179      return false;
180    return true;
181  }
182
[1021]183  void GraphicsEngine::initialise()
[612]184  {
[1021]185    this->renderWindow_ = root_->initialise(true, "OrxonoxV2");
[1178]186    Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
[1052]187    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
[1021]188    //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
[1052]189    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
[612]190  }
191
[715]192  void GraphicsEngine::loadRessourceLocations(std::string dataPath)
[612]193  {
194    //TODO: Specify layout of data file and maybe use xml-loader
195    //TODO: Work with ressource groups (should be generated by a special loader)
196    // Load resource paths from data file using configfile ressource type
[1052]197    Ogre::ConfigFile cf;
[612]198    cf.load(dataPath + "resources.cfg");
199
200    // Go through all sections & settings in the file
[1052]201    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
[612]202
[715]203    std::string secName, typeName, archName;
[612]204    while (seci.hasMoreElements())
205    {
206      secName = seci.peekNextKey();
[1052]207      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
208      Ogre::ConfigFile::SettingsMultiMap::iterator i;
[612]209      for (i = settings->begin(); i != settings->end(); ++i)
210      {
211        typeName = i->first; // for instance "FileSystem" or "Zip"
212        archName = i->second; // name (and location) of archive
213
[1052]214        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
[715]215                                           std::string(dataPath + archName),
[612]216                                           typeName, secName);
217      }
218    }
219  }
220
[1021]221  /**
222    Returns the window handle of the render window.
223    At least the InputHandler uses this to create the OIS::InputManager
224    @return The window handle of the render window
225  */
226  size_t GraphicsEngine::getWindowHandle()
227  {
228    if (this->renderWindow_)
229    {
[1024]230      size_t windowHnd = 0;
[1032]231      this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
[1024]232      return windowHnd;
233    }
[1021]234    else
235      return 0;
236  }
[612]237
[1021]238  /**
239    Get the width of the current render window
240    @return The width of the current render window
241  */
242  int GraphicsEngine::getWindowWidth() const
243  {
244    if (this->renderWindow_)
245      return this->renderWindow_->getWidth();
246    else
247      return 0;
248  }
249
250  /**
251    Get the height of the current render window
252    @return The height of the current render window
253  */
254  int GraphicsEngine::getWindowHeight() const
255  {
256    if (this->renderWindow_)
257      return this->renderWindow_->getHeight();
258    else
259      return 0;
260  }
261
[1024]262  /**
263    @brief Method called by the LogListener interface from Ogre.
264    We use it to capture Ogre log messages and handle it ourselves.
265    @param message The message to be logged
266    @param lml The message level the log is using
267    @param maskDebug If we are printing to the console or not
268    @param logName the name of this log (so you can have several listeners
269                   for different logs, and identify them)
270  */
271  void GraphicsEngine::messageLogged(const std::string& message,
[1052]272    Ogre::LogMessageLevel lml, bool maskDebug, const std::string &logName)
[1024]273  {
274    int orxonoxLevel;
275    switch (lml)
276    {
[1052]277      case Ogre::LML_TRIVIAL:
[1024]278        orxonoxLevel = this->ogreLogLevelTrivial_;
279        break;
[1052]280      case Ogre::LML_NORMAL:
[1024]281        orxonoxLevel = this->ogreLogLevelNormal_;
282        break;
[1052]283      case Ogre::LML_CRITICAL:
[1024]284        orxonoxLevel = this->ogreLogLevelCritical_;
285        break;
286      default:
287        orxonoxLevel = 0;
288    }
289    OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
290        << "*** Ogre: " << message << std::endl;
291  }
[1178]292
293    void GraphicsEngine::windowMoved(Ogre::RenderWindow *rw){
294        int w = rw->getWidth();
295        int h = rw->getHeight();
296        InputManager::getSingleton().setWindowExtents(w, h);
297    }
298
299    void GraphicsEngine::windowResized(Ogre::RenderWindow *rw){
300        int w = rw->getWidth();
301        int h = rw->getHeight();
302        InputManager::getSingleton().setWindowExtents(w, h);
303    }
304
305    void GraphicsEngine::windowFocusChanged(Ogre::RenderWindow *rw){
306        int w = rw->getWidth();
307        int h = rw->getHeight();
308        InputManager::getSingleton().setWindowExtents(w, h);
309    }
[612]310}
Note: See TracBrowser for help on using the repository browser.