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