Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1226 was 1220, checked in by rgrieder, 17 years ago

compiles now on tardis

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