Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/input/src/orxonox/GraphicsEngine.cc @ 1045

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