Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1053 was 1052, checked in by landauf, 17 years ago

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

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