[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 | |
---|
[1052] | 43 | #include "core/CoreIncludes.h" |
---|
| 44 | #include "core/ConfigValueIncludes.h" |
---|
[1021] | 45 | #include "core/Debug.h" |
---|
[1032] | 46 | |
---|
[612] | 47 | |
---|
| 48 | namespace orxonox { |
---|
| 49 | |
---|
[1032] | 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 | */ |
---|
[612] | 63 | GraphicsEngine::GraphicsEngine() |
---|
| 64 | { |
---|
[1021] | 65 | RegisterObject(GraphicsEngine); |
---|
| 66 | //this->bOverwritePath_ = false; |
---|
| 67 | this->setConfigValues(); |
---|
[612] | 68 | // set to standard values |
---|
| 69 | this->configPath_ = ""; |
---|
[1021] | 70 | this->root_ = 0; |
---|
| 71 | this->scene_ = 0; |
---|
| 72 | this->renderWindow_ = 0; |
---|
[1032] | 73 | COUT(4) << "*** GraphicsEngine: Constructed" << std::endl; |
---|
[612] | 74 | } |
---|
| 75 | |
---|
[1032] | 76 | /** |
---|
| 77 | @brief Called after main() --> call destroyObjects()! |
---|
| 78 | */ |
---|
[612] | 79 | GraphicsEngine::~GraphicsEngine() |
---|
| 80 | { |
---|
[1032] | 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; |
---|
[1024] | 91 | if (this->root_) |
---|
[1021] | 92 | delete this->root_; |
---|
[1032] | 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). |
---|
[1052] | 97 | if (Ogre::LogManager::getSingletonPtr() != 0) |
---|
[1024] | 98 | { |
---|
[1052] | 99 | Ogre::LogManager::getSingleton().getDefaultLog()->removeListener(this); |
---|
| 100 | Ogre::LogManager::getSingleton().destroyLog(Ogre::LogManager::getSingleton().getDefaultLog()); |
---|
| 101 | delete Ogre::LogManager::getSingletonPtr(); |
---|
[1024] | 102 | } |
---|
[1032] | 103 | COUT(4) << "*** GraphicsEngine: Destroying objects done" << std::endl; |
---|
[612] | 104 | } |
---|
| 105 | |
---|
[1021] | 106 | void GraphicsEngine::setConfigValues() |
---|
| 107 | { |
---|
[1024] | 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."); |
---|
[1032] | 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"); |
---|
[1021] | 113 | } |
---|
| 114 | |
---|
[1024] | 115 | /** |
---|
| 116 | @brief Creates the Ogre Root object and sets up the ogre log. |
---|
| 117 | */ |
---|
[612] | 118 | void GraphicsEngine::setup() |
---|
| 119 | { |
---|
| 120 | //TODO: Check if file exists (maybe not here) |
---|
| 121 | /*#ifndef OGRE_STATIC_LIB |
---|
[1052] | 122 | root_ = new Ogre::Root(configPath_ + "plugins.cfg", configPath_ + "ogre.cfg", |
---|
[612] | 123 | configPath_ + "Ogre.log"); |
---|
| 124 | #else |
---|
[1052] | 125 | root_ = new Ogre::Root(NULL, configPath_ + "ogre.cfg", configPath_ + "Ogre.log"); |
---|
[612] | 126 | #endif*/ |
---|
[1021] | 127 | #if ORXONOX_COMPILER == ORXONOX_COMPILER_MSVC && defined(_DEBUG) |
---|
[715] | 128 | std::string plugin_filename = "plugins_d.cfg"; |
---|
[679] | 129 | #else |
---|
[715] | 130 | std::string plugin_filename = "plugins.cfg"; |
---|
[679] | 131 | #endif |
---|
[1024] | 132 | |
---|
| 133 | // create a logManager |
---|
[1042] | 134 | /*LogManager *logger; |
---|
[1024] | 135 | if(LogManager::getSingletonPtr() == 0) |
---|
| 136 | logger = new LogManager(); |
---|
| 137 | else |
---|
| 138 | logger = LogManager::getSingletonPtr(); |
---|
[1032] | 139 | COUT(4) << "*** GraphicsEngine: Ogre LogManager created/assigned" << std::endl; |
---|
[1024] | 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); |
---|
[1032] | 147 | COUT(4) << "*** GraphicsEngine: Ogre Log created" << std::endl; |
---|
[1024] | 148 | |
---|
| 149 | myLog->setLogDetail(LL_BOREME); |
---|
[1042] | 150 | myLog->addListener(this);*/ |
---|
[1024] | 151 | |
---|
| 152 | // Root will detect that we've already created a Log |
---|
[1032] | 153 | COUT(4) << "*** GraphicsEngine: Creating Ogre Root..." << std::endl; |
---|
[1052] | 154 | root_ = new Ogre::Root(plugin_filename); |
---|
[1032] | 155 | COUT(4) << "*** GraphicsEngine: Creating Ogre Root done" << std::endl; |
---|
[612] | 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * @return scene manager |
---|
| 160 | */ |
---|
[1052] | 161 | Ogre::SceneManager* GraphicsEngine::getSceneManager() |
---|
[612] | 162 | { |
---|
| 163 | if(!scene_) |
---|
| 164 | { |
---|
[1052] | 165 | scene_ = root_->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager"); |
---|
[612] | 166 | COUT(3) << "Info: Created SceneMan: " << scene_ << std::endl; |
---|
| 167 | } |
---|
| 168 | return scene_; |
---|
| 169 | } |
---|
| 170 | |
---|
[1021] | 171 | bool GraphicsEngine::load(std::string dataPath) |
---|
[612] | 172 | { |
---|
[1021] | 173 | // temporary overwrite of dataPath, change ini file for permanent change |
---|
| 174 | if( dataPath != "" ) |
---|
[1024] | 175 | dataPath_ = dataPath + "/"; |
---|
[612] | 176 | loadRessourceLocations(this->dataPath_); |
---|
| 177 | if (!root_->restoreConfig() && !root_->showConfigDialog()) |
---|
| 178 | return false; |
---|
| 179 | return true; |
---|
| 180 | } |
---|
| 181 | |
---|
[1021] | 182 | void GraphicsEngine::initialise() |
---|
[612] | 183 | { |
---|
[1021] | 184 | this->renderWindow_ = root_->initialise(true, "OrxonoxV2"); |
---|
[1052] | 185 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); |
---|
[1021] | 186 | //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load... |
---|
[1052] | 187 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
[612] | 188 | } |
---|
| 189 | |
---|
[715] | 190 | void GraphicsEngine::loadRessourceLocations(std::string dataPath) |
---|
[612] | 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 |
---|
[1052] | 195 | Ogre::ConfigFile cf; |
---|
[612] | 196 | cf.load(dataPath + "resources.cfg"); |
---|
| 197 | |
---|
| 198 | // Go through all sections & settings in the file |
---|
[1052] | 199 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
[612] | 200 | |
---|
[715] | 201 | std::string secName, typeName, archName; |
---|
[612] | 202 | while (seci.hasMoreElements()) |
---|
| 203 | { |
---|
| 204 | secName = seci.peekNextKey(); |
---|
[1052] | 205 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 206 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
[612] | 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 | |
---|
[1052] | 212 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( |
---|
[715] | 213 | std::string(dataPath + archName), |
---|
[612] | 214 | typeName, secName); |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
[1021] | 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 | { |
---|
[1024] | 228 | size_t windowHnd = 0; |
---|
[1032] | 229 | this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd); |
---|
[1024] | 230 | return windowHnd; |
---|
| 231 | } |
---|
[1021] | 232 | else |
---|
| 233 | return 0; |
---|
| 234 | } |
---|
[612] | 235 | |
---|
[1021] | 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 | |
---|
[1024] | 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, |
---|
[1052] | 270 | Ogre::LogMessageLevel lml, bool maskDebug, const std::string &logName) |
---|
[1024] | 271 | { |
---|
| 272 | int orxonoxLevel; |
---|
| 273 | switch (lml) |
---|
| 274 | { |
---|
[1052] | 275 | case Ogre::LML_TRIVIAL: |
---|
[1024] | 276 | orxonoxLevel = this->ogreLogLevelTrivial_; |
---|
| 277 | break; |
---|
[1052] | 278 | case Ogre::LML_NORMAL: |
---|
[1024] | 279 | orxonoxLevel = this->ogreLogLevelNormal_; |
---|
| 280 | break; |
---|
[1052] | 281 | case Ogre::LML_CRITICAL: |
---|
[1024] | 282 | orxonoxLevel = this->ogreLogLevelCritical_; |
---|
| 283 | break; |
---|
| 284 | default: |
---|
| 285 | orxonoxLevel = 0; |
---|
| 286 | } |
---|
| 287 | OutputHandler::getOutStream().setOutputLevel(orxonoxLevel) |
---|
| 288 | << "*** Ogre: " << message << std::endl; |
---|
| 289 | } |
---|
[612] | 290 | } |
---|