Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ogre/src/orxonox/GraphicsEngine.cc @ 1243

Last change on this file since 1243 was 1243, checked in by rgrieder, 16 years ago
  • rearranged function calls in Orxonox.cc has yet to be looked at clearly, commented and COUTed
  • works so far, but using 3 little hacks… in hacky classes
File size: 9.8 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
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    // set to standard values
67    this->configPath_ = "";
68    this->root_ = 0;
69    this->scene_ = 0;
70    this->renderWindow_ = 0;
71    this->setConfigValues();
72    COUT(4) << "*** GraphicsEngine: Constructed" << std::endl;
73  }
74
75  /**
76    @brief Called after main() --> call destroyObjects()!
77  */
78  GraphicsEngine::~GraphicsEngine()
79  {
80    this->destroy();
81  }
82
83  /**
84    @brief Destroys all the internal objects. Call this method when you
85           normally would call the destructor.
86  */
87  void GraphicsEngine::destroy()
88  {
89    COUT(4) << "*** GraphicsEngine: Destroying objects..." << std::endl;
90    Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this);
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  bool GraphicsEngine::setup(std::string& dataPath)
119  {
120    // temporary overwrite of dataPath, change ini file for permanent change
121    if (dataPath != "")
122      dataPath_ = dataPath;
123    if (dataPath_ == "")
124      return false;
125    if (dataPath_[dataPath_.size() - 1] != '/')
126      dataPath_ += "/";
127
128    //TODO: Check if file exists (maybe not here)
129#if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC && defined(_DEBUG)
130    std::string plugin_filename = "plugins_d.cfg";
131#else
132    std::string plugin_filename = "plugins.cfg";
133#endif
134
135    // create a logManager
136    Ogre::LogManager *logger;
137                if (Ogre::LogManager::getSingletonPtr() == 0)
138                        logger = new Ogre::LogManager();
139    else
140      logger = Ogre::LogManager::getSingletonPtr();
141    COUT(4) << "*** GraphicsEngine: Ogre LogManager created/assigned" << std::endl;
142
143    // create our own log that we can listen to
144    Ogre::Log *myLog;
145    if (this->ogreLogfile_ == "")
146      myLog = logger->createLog("ogre.log", true, false, true);
147    else
148      myLog = logger->createLog(this->ogreLogfile_, true, false, false);
149    COUT(4) << "*** GraphicsEngine: Ogre Log created" << std::endl;
150
151    myLog->setLogDetail(Ogre::LL_BOREME);
152    myLog->addListener(this);
153
154    // Root will detect that we've already created a Log
155    COUT(4) << "*** GraphicsEngine: Creating Ogre Root..." << std::endl;
156    root_ = new Ogre::Root(plugin_filename);
157    COUT(4) << "*** GraphicsEngine: Creating Ogre Root done" << std::endl;
158
159    // specify where Ogre has to look for resources. This call doesn't parse anything yet!
160    declareRessourceLocations();
161
162    return true;
163  }
164
165  void GraphicsEngine::declareRessourceLocations()
166  {
167    //TODO: Specify layout of data file and maybe use xml-loader
168    //TODO: Work with ressource groups (should be generated by a special loader)
169    // Load resource paths from data file using configfile ressource type
170    Ogre::ConfigFile cf;
171    cf.load(dataPath_ + "resources.cfg");
172
173    // Go through all sections & settings in the file
174    Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
175
176    std::string secName, typeName, archName;
177    while (seci.hasMoreElements())
178    {
179      secName = seci.peekNextKey();
180      Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
181      Ogre::ConfigFile::SettingsMultiMap::iterator i;
182      for (i = settings->begin(); i != settings->end(); ++i)
183      {
184        typeName = i->first; // for instance "FileSystem" or "Zip"
185        archName = i->second; // name (and location) of archive
186
187        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
188                                           std::string(dataPath_ + archName),
189                                           typeName, secName);
190      }
191    }
192  }
193
194  bool GraphicsEngine::loadRenderer()
195  {
196    if (!root_->restoreConfig() && !root_->showConfigDialog())
197      return false;
198
199    this->renderWindow_ = root_->initialise(true, "OrxonoxV2");
200    if (!root_->isInitialised())
201      return false;
202    Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
203    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
204    return true;
205  }
206
207  void GraphicsEngine::initialiseResources()
208  {
209    //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
210    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
211  }
212
213  /**
214   * @brief Creates the SceneManager
215   */
216  bool GraphicsEngine::createNewScene()
217  {
218    if (scene_)
219      return false;
220    scene_ = root_->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager");
221    COUT(3) << "Info: Created SceneManager: " << scene_ << std::endl;
222    return true;
223  }
224
225  /**
226    Returns the window handle of the render window.
227    At least the InputHandler uses this to create the OIS::InputManager
228    @return The window handle of the render window
229  */
230  size_t GraphicsEngine::getWindowHandle()
231  {
232    if (this->renderWindow_)
233    {
234      size_t windowHnd = 0;
235      this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
236      return windowHnd;
237    }
238    else
239      return 0;
240  }
241
242  /**
243    Get the width of the current render window
244    @return The width of the current render window
245  */
246  int GraphicsEngine::getWindowWidth() const
247  {
248    if (this->renderWindow_)
249      return this->renderWindow_->getWidth();
250    else
251      return 0;
252  }
253
254  /**
255    Get the height of the current render window
256    @return The height of the current render window
257  */
258  int GraphicsEngine::getWindowHeight() const
259  {
260    if (this->renderWindow_)
261      return this->renderWindow_->getHeight();
262    else
263      return 0;
264  }
265
266  /**
267    @brief Method called by the LogListener interface from Ogre.
268    We use it to capture Ogre log messages and handle it ourselves.
269    @param message The message to be logged
270    @param lml The message level the log is using
271    @param maskDebug If we are printing to the console or not
272    @param logName the name of this log (so you can have several listeners
273                   for different logs, and identify them)
274  */
275  void GraphicsEngine::messageLogged(const std::string& message,
276    Ogre::LogMessageLevel lml, bool maskDebug, const std::string &logName)
277  {
278    int orxonoxLevel;
279    switch (lml)
280    {
281      case Ogre::LML_TRIVIAL:
282        orxonoxLevel = this->ogreLogLevelTrivial_;
283        break;
284      case Ogre::LML_NORMAL:
285        orxonoxLevel = this->ogreLogLevelNormal_;
286        break;
287      case Ogre::LML_CRITICAL:
288        orxonoxLevel = this->ogreLogLevelCritical_;
289        break;
290      default:
291        orxonoxLevel = 0;
292    }
293    OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
294        << "Ogre: " << message << std::endl;
295  }
296
297    void GraphicsEngine::windowMoved(Ogre::RenderWindow *rw){
298        int w = rw->getWidth();
299        int h = rw->getHeight();
300        InputManager::setWindowExtents(w, h);
301    }
302
303    void GraphicsEngine::windowResized(Ogre::RenderWindow *rw){
304        int w = rw->getWidth();
305        int h = rw->getHeight();
306        InputManager::setWindowExtents(w, h);
307    }
308
309    void GraphicsEngine::windowFocusChanged(Ogre::RenderWindow *rw){
310        int w = rw->getWidth();
311        int h = rw->getHeight();
312        InputManager::setWindowExtents(w, h);
313    }
314}
Note: See TracBrowser for help on using the repository browser.