Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/GraphicsEngine.cc @ 1049

Last change on this file since 1049 was 1042, checked in by rgrieder, 17 years ago
  • excluded ogre logs to test for Oli
File size: 8.9 KB
RevLine 
[612]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
23 *   Co-authors:
[1032]24 *      Reto Grieder
[612]25 *
26 */
[1035]27
[612]28 /**
29    @file orxonox.cc
30    @brief Orxonox class
31  */
32
[1021]33#include "OrxonoxStableHeaders.h"
[1039]34#include "GraphicsEngine.h"
[612]35
36#include <OgreRoot.h>
[1021]37#include <OgreException.h>
[612]38#include <OgreConfigFile.h>
[1024]39#include <OgreLogManager.h>
[612]40#include <OgreTextureManager.h>
[1021]41#include <OgreRenderWindow.h>
[612]42
[1021]43#include "core/CoreIncludes.h"
44#include "core/Debug.h"
[1032]45
[612]46
47namespace orxonox {
48
49  using namespace Ogre;
50
[1032]51  /**
52    @brief Returns the singleton instance and creates it the first time.
53    @return The only instance of GraphicsEngine.
54  */
55  GraphicsEngine& GraphicsEngine::getSingleton()
56  {
57    static GraphicsEngine theOnlyInstance;
58    return theOnlyInstance;
59  }
60
61  /**
62    @brief Only use constructor to initialise variables and pointers!
63  */
[612]64  GraphicsEngine::GraphicsEngine()
65  {
[1021]66    RegisterObject(GraphicsEngine);
67    //this->bOverwritePath_ = false;
68    this->setConfigValues();
[612]69    // set to standard values
70    this->configPath_ = "";
[1021]71    this->root_ = 0;
72    this->scene_ = 0;
73    this->renderWindow_ = 0;
[1032]74    COUT(4) << "*** GraphicsEngine: Constructed" << std::endl;
[612]75  }
76
[1032]77  /**
78    @brief Called after main() --> call destroyObjects()!
79  */
[612]80  GraphicsEngine::~GraphicsEngine()
81  {
[1032]82    this->destroy();
83  }
84
85  /**
86    @brief Destroys all the internal objects. Call this method when you
87           normally would call the destructor.
88  */
89  void GraphicsEngine::destroy()
90  {
91    COUT(4) << "*** GraphicsEngine: Destroying objects..." << std::endl;
[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).
[1024]98    if (LogManager::getSingletonPtr() != 0)
99    {
100      LogManager::getSingleton().getDefaultLog()->removeListener(this);
101      LogManager::getSingleton().destroyLog(LogManager::getSingleton().getDefaultLog());
102      delete LogManager::getSingletonPtr();
103    }
[1032]104    COUT(4) << "*** GraphicsEngine: Destroying objects done" << std::endl;
[612]105  }
106
[1021]107  void GraphicsEngine::setConfigValues()
108  {
[1024]109    SetConfigValue(dataPath_, "../../media/").description("relative path to media data");
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
123    root_ = new Root(configPath_ + "plugins.cfg", configPath_ + "ogre.cfg",
124                     configPath_ + "Ogre.log");
125#else
126    root_ = new Root(NULL, configPath_ + "ogre.cfg", configPath_ + "Ogre.log");
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
[1042]135    /*LogManager *logger;
[1024]136                if(LogManager::getSingletonPtr() == 0)
137                        logger = new LogManager();
138    else
139      logger = 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
143    Log *myLog;
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
150    myLog->setLogDetail(LL_BOREME);
[1042]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;
[679]155    root_ = new Root(plugin_filename);
[1032]156    COUT(4) << "*** GraphicsEngine: Creating Ogre Root done" << std::endl;
[612]157  }
158
159  /**
160   * @return scene manager
161   */
162  SceneManager* GraphicsEngine::getSceneManager()
163  {
164    if(!scene_)
165    {
166      scene_ = root_->createSceneManager(ST_GENERIC, "Default SceneManager");
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");
[612]186    TextureManager::getSingleton().setDefaultNumMipmaps(5);
[1021]187    //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
[612]188    ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
189  }
190
[715]191  void GraphicsEngine::loadRessourceLocations(std::string dataPath)
[612]192  {
193    //TODO: Specify layout of data file and maybe use xml-loader
194    //TODO: Work with ressource groups (should be generated by a special loader)
195    // Load resource paths from data file using configfile ressource type
196    ConfigFile cf;
197    cf.load(dataPath + "resources.cfg");
198
199    // Go through all sections & settings in the file
200    ConfigFile::SectionIterator seci = cf.getSectionIterator();
201
[715]202    std::string secName, typeName, archName;
[612]203    while (seci.hasMoreElements())
204    {
205      secName = seci.peekNextKey();
206      ConfigFile::SettingsMultiMap *settings = seci.getNext();
207      ConfigFile::SettingsMultiMap::iterator i;
208      for (i = settings->begin(); i != settings->end(); ++i)
209      {
210        typeName = i->first; // for instance "FileSystem" or "Zip"
211        archName = i->second; // name (and location) of archive
212
213        ResourceGroupManager::getSingleton().addResourceLocation(
[715]214                                           std::string(dataPath + archName),
[612]215                                           typeName, secName);
216      }
217    }
218  }
219
[1021]220  /**
221    Returns the window handle of the render window.
222    At least the InputHandler uses this to create the OIS::InputManager
223    @return The window handle of the render window
224  */
225  size_t GraphicsEngine::getWindowHandle()
226  {
227    if (this->renderWindow_)
228    {
[1024]229      size_t windowHnd = 0;
[1032]230      this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
[1024]231      return windowHnd;
232    }
[1021]233    else
234      return 0;
235  }
[612]236
[1021]237  /**
238    Get the width of the current render window
239    @return The width of the current render window
240  */
241  int GraphicsEngine::getWindowWidth() const
242  {
243    if (this->renderWindow_)
244      return this->renderWindow_->getWidth();
245    else
246      return 0;
247  }
248
249  /**
250    Get the height of the current render window
251    @return The height of the current render window
252  */
253  int GraphicsEngine::getWindowHeight() const
254  {
255    if (this->renderWindow_)
256      return this->renderWindow_->getHeight();
257    else
258      return 0;
259  }
260
[1024]261  /**
262    @brief Method called by the LogListener interface from Ogre.
263    We use it to capture Ogre log messages and handle it ourselves.
264    @param message The message to be logged
265    @param lml The message level the log is using
266    @param maskDebug If we are printing to the console or not
267    @param logName the name of this log (so you can have several listeners
268                   for different logs, and identify them)
269  */
270  void GraphicsEngine::messageLogged(const std::string& message,
271    LogMessageLevel lml, bool maskDebug, const std::string &logName)
272  {
273    int orxonoxLevel;
274    switch (lml)
275    {
276      case LML_TRIVIAL:
277        orxonoxLevel = this->ogreLogLevelTrivial_;
278        break;
279      case LML_NORMAL:
280        orxonoxLevel = this->ogreLogLevelNormal_;
281        break;
282      case LML_CRITICAL:
283        orxonoxLevel = this->ogreLogLevelCritical_;
284        break;
285      default:
286        orxonoxLevel = 0;
287    }
288    OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
289        << "*** Ogre: " << message << std::endl;
290  }
[612]291}
Note: See TracBrowser for help on using the repository browser.