[1661] | 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 | * Reto Grieder |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
| 30 | #include "GSGraphics.h" |
---|
| 31 | |
---|
[1686] | 32 | #include <fstream> |
---|
| 33 | #include <OgreConfigFile.h> |
---|
[1661] | 34 | #include <OgreFrameListener.h> |
---|
| 35 | #include <OgreRoot.h> |
---|
[1686] | 36 | #include <OgreException.h> |
---|
| 37 | #include <OgreRenderWindow.h> |
---|
[1828] | 38 | #include <OgreRenderSystem.h> |
---|
[1686] | 39 | #include <OgreTextureManager.h> |
---|
| 40 | #include <OgreViewport.h> |
---|
[1661] | 41 | #include <OgreWindowEventUtilities.h> |
---|
| 42 | |
---|
[1755] | 43 | #include "util/Debug.h" |
---|
[1764] | 44 | #include "util/Exception.h" |
---|
[1662] | 45 | #include "core/ConsoleCommand.h" |
---|
| 46 | #include "core/ConfigValueIncludes.h" |
---|
[1686] | 47 | #include "core/CoreIncludes.h" |
---|
[1661] | 48 | #include "core/input/InputManager.h" |
---|
[1788] | 49 | #include "core/input/KeyBinder.h" |
---|
| 50 | #include "core/input/ExtendedInputState.h" |
---|
[1661] | 51 | #include "overlays/console/InGameConsole.h" |
---|
| 52 | #include "gui/GUIManager.h" |
---|
[1686] | 53 | #include "tools/WindowEventListener.h" |
---|
| 54 | #include "Settings.h" |
---|
| 55 | |
---|
| 56 | // for compatibility |
---|
[1662] | 57 | #include "GraphicsEngine.h" |
---|
[1661] | 58 | |
---|
| 59 | namespace orxonox |
---|
| 60 | { |
---|
| 61 | GSGraphics::GSGraphics() |
---|
[1689] | 62 | : GameState<GSRoot>("graphics") |
---|
[1674] | 63 | , ogreRoot_(0) |
---|
[1661] | 64 | , inputManager_(0) |
---|
| 65 | , console_(0) |
---|
| 66 | , guiManager_(0) |
---|
[1788] | 67 | , masterKeyBinder_(0) |
---|
[1672] | 68 | , frameCount_(0) |
---|
[1674] | 69 | , statisticsRefreshCycle_(0) |
---|
| 70 | , statisticsStartTime_(0) |
---|
| 71 | , statisticsStartCount_(0) |
---|
| 72 | , tickTime_(0) |
---|
[1661] | 73 | { |
---|
[1686] | 74 | RegisterRootObject(GSGraphics); |
---|
[1661] | 75 | } |
---|
| 76 | |
---|
| 77 | GSGraphics::~GSGraphics() |
---|
| 78 | { |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void GSGraphics::setConfigValues() |
---|
| 82 | { |
---|
[1688] | 83 | SetConfigValue(resourceFile_, "resources.cfg").description("Location of the resources file in the data path."); |
---|
[1674] | 84 | SetConfigValue(statisticsRefreshCycle_, 200000).description("Sets the time in microseconds interval at which average fps, etc. get updated."); |
---|
[1661] | 85 | } |
---|
| 86 | |
---|
| 87 | void GSGraphics::enter() |
---|
| 88 | { |
---|
[1696] | 89 | Settings::_getInstance().bShowsGraphics_ = true; |
---|
| 90 | |
---|
[1674] | 91 | setConfigValues(); |
---|
| 92 | |
---|
[1688] | 93 | this->ogreRoot_ = getParent()->getOgreRoot(); |
---|
[1661] | 94 | |
---|
[1686] | 95 | this->declareResources(); |
---|
| 96 | this->loadRenderer(); // creates the render window |
---|
[1661] | 97 | // TODO: Spread this so that this call only initialises things needed for the Console and GUI |
---|
[1686] | 98 | this->initialiseResources(); |
---|
[1661] | 99 | |
---|
[1686] | 100 | |
---|
| 101 | // HACK: temporary: |
---|
[1688] | 102 | GraphicsEngine* graphicsEngine = getParent()->getGraphicsEngine(); |
---|
| 103 | graphicsEngine->renderWindow_ = this->renderWindow_; |
---|
| 104 | graphicsEngine->root_ = this->ogreRoot_; |
---|
| 105 | graphicsEngine->viewport_ = this->viewport_; |
---|
[1686] | 106 | |
---|
| 107 | |
---|
[1661] | 108 | // Calls the InputManager which sets up the input devices. |
---|
| 109 | // The render window width and height are used to set up the mouse movement. |
---|
| 110 | inputManager_ = new InputManager(); |
---|
[1686] | 111 | size_t windowHnd = 0; |
---|
| 112 | this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 113 | inputManager_->initialise(windowHnd, renderWindow_->getWidth(), renderWindow_->getHeight(), true); |
---|
[1788] | 114 | // Configure master input state with a KeyBinder |
---|
| 115 | //masterKeyBinder_ = new KeyBinder(); |
---|
| 116 | //inputManager_->getMasterInputState()->addKeyHandler(masterKeyBinder_); |
---|
[1661] | 117 | |
---|
| 118 | // Load the InGameConsole |
---|
| 119 | console_ = new InGameConsole(); |
---|
| 120 | console_->initialise(); |
---|
| 121 | |
---|
| 122 | // load the CEGUI interface |
---|
| 123 | guiManager_ = new GUIManager(); |
---|
[1686] | 124 | guiManager_->initialise(this->renderWindow_); |
---|
[1674] | 125 | |
---|
| 126 | // reset frame counter |
---|
| 127 | this->frameCount_ = 0; |
---|
| 128 | this->tickTime_ = 0; |
---|
| 129 | statisticsStartTime_ = 0; |
---|
| 130 | statisticsStartCount_ = 0; |
---|
[1686] | 131 | |
---|
| 132 | // add console commands |
---|
| 133 | FunctorMember<GSGraphics>* functor1 = createFunctor(&GSGraphics::printScreen); |
---|
| 134 | functor1->setObject(this); |
---|
| 135 | CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "printScreen")); |
---|
[1661] | 136 | } |
---|
| 137 | |
---|
| 138 | void GSGraphics::leave() |
---|
| 139 | { |
---|
[1824] | 140 | using namespace Ogre; |
---|
| 141 | |
---|
[1662] | 142 | delete this->guiManager_; |
---|
[1661] | 143 | |
---|
[1662] | 144 | delete this->console_; |
---|
[1661] | 145 | |
---|
[1788] | 146 | //inputManager_->getMasterInputState()->removeKeyHandler(this->masterKeyBinder_); |
---|
| 147 | //delete this->masterKeyBinder_; |
---|
[1662] | 148 | delete this->inputManager_; |
---|
| 149 | |
---|
[1824] | 150 | // destroy render window |
---|
| 151 | this->renderWindow_->removeAllViewports(); |
---|
| 152 | this->renderWindow_->removeAllListeners(); |
---|
| 153 | RenderSystem* renderer = this->ogreRoot_->getRenderSystem(); |
---|
| 154 | renderer->destroyRenderWindow("Orxonox"); |
---|
[1696] | 155 | |
---|
[1824] | 156 | // Does the opposite of initialise() |
---|
| 157 | ogreRoot_->shutdown(); |
---|
| 158 | |
---|
| 159 | // Remove all resources and resource groups |
---|
[1825] | 160 | //StringVector groups = ResourceGroupManager::getSingleton().getResourceGroups(); |
---|
| 161 | //for (StringVector::iterator it = groups.begin(); it != groups.end(); ++it) |
---|
| 162 | //{ |
---|
| 163 | // ResourceGroupManager::getSingleton().destroyResourceGroup(*it); |
---|
| 164 | //} |
---|
[1824] | 165 | |
---|
[1825] | 166 | //ParticleSystemManager::getSingleton().removeAllTemplates(); |
---|
[1824] | 167 | |
---|
| 168 | // Shutdown the render system |
---|
[1825] | 169 | //this->ogreRoot_->setRenderSystem(0); |
---|
[1824] | 170 | |
---|
[1696] | 171 | Settings::_getInstance().bShowsGraphics_ = false; |
---|
[1661] | 172 | } |
---|
| 173 | |
---|
[1662] | 174 | /** |
---|
| 175 | Main loop of the orxonox game. |
---|
| 176 | We use the Ogre::Timer to measure time since it uses the most precise |
---|
| 177 | method an a platform (however the windows timer lacks time when under |
---|
| 178 | heavy kernel load!). |
---|
| 179 | There is a simple mechanism to measure the average time spent in our |
---|
| 180 | ticks as it may indicate performance issues. |
---|
| 181 | A note about the Ogre::FrameListener: Even though we don't use them, |
---|
| 182 | they still get called. However, the delta times are not correct (except |
---|
| 183 | for timeSinceLastFrame, which is the most important). A little research |
---|
| 184 | as shown that there is probably only one FrameListener that doesn't even |
---|
| 185 | need the time. So we shouldn't run into problems. |
---|
| 186 | */ |
---|
[1674] | 187 | void GSGraphics::ticked(const Clock& time) |
---|
[1661] | 188 | { |
---|
[1724] | 189 | unsigned long long timeBeforeTick = time.getRealMicroseconds(); |
---|
[1674] | 190 | float dt = time.getDeltaTime(); |
---|
| 191 | |
---|
[1672] | 192 | this->inputManager_->tick(dt); |
---|
| 193 | // tick console |
---|
| 194 | this->console_->tick(dt); |
---|
[1674] | 195 | this->tickChild(time); |
---|
| 196 | |
---|
[1724] | 197 | unsigned long long timeAfterTick = time.getRealMicroseconds(); |
---|
[1661] | 198 | |
---|
[1674] | 199 | tickTime_ += (unsigned int)(timeAfterTick - timeBeforeTick); |
---|
| 200 | if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_) |
---|
| 201 | { |
---|
| 202 | GraphicsEngine::getInstance().setAverageTickTime( |
---|
| 203 | (float)tickTime_ * 0.001f / (frameCount_ - statisticsStartCount_)); |
---|
| 204 | float avgFPS = (float)(frameCount_ - statisticsStartCount_) |
---|
| 205 | / (timeAfterTick - statisticsStartTime_) * 1000000.0; |
---|
| 206 | GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS); |
---|
[1661] | 207 | |
---|
[1674] | 208 | tickTime_ = 0; |
---|
| 209 | statisticsStartCount_ = frameCount_; |
---|
| 210 | statisticsStartTime_ = timeAfterTick; |
---|
| 211 | } |
---|
[1661] | 212 | |
---|
[1672] | 213 | // don't forget to call _fireFrameStarted in ogre to make sure |
---|
| 214 | // everything goes smoothly |
---|
| 215 | Ogre::FrameEvent evt; |
---|
| 216 | evt.timeSinceLastFrame = dt; |
---|
| 217 | evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway |
---|
| 218 | ogreRoot_->_fireFrameStarted(evt); |
---|
[1661] | 219 | |
---|
[1672] | 220 | // Pump messages in all registered RenderWindows |
---|
| 221 | // This calls the WindowEventListener objects. |
---|
| 222 | Ogre::WindowEventUtilities::messagePump(); |
---|
| 223 | // make sure the window stays active even when not focused |
---|
| 224 | // (probably only necessary on windows) |
---|
[1686] | 225 | this->renderWindow_->setActive(true); |
---|
[1661] | 226 | |
---|
[1672] | 227 | // render |
---|
| 228 | ogreRoot_->_updateAllRenderTargets(); |
---|
[1661] | 229 | |
---|
[1672] | 230 | // again, just to be sure ogre works fine |
---|
| 231 | ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted |
---|
[1661] | 232 | |
---|
[1672] | 233 | ++frameCount_; |
---|
[1661] | 234 | } |
---|
[1686] | 235 | |
---|
| 236 | void GSGraphics::declareResources() |
---|
| 237 | { |
---|
| 238 | CCOUT(4) << "Declaring Resources" << std::endl; |
---|
| 239 | //TODO: Specify layout of data file and maybe use xml-loader |
---|
| 240 | //TODO: Work with ressource groups (should be generated by a special loader) |
---|
| 241 | |
---|
| 242 | if (resourceFile_ == "") |
---|
| 243 | { |
---|
| 244 | COUT(2) << "Warning: Ogre resource file set to \"\". Defaulting to resources.cfg" << std::endl; |
---|
| 245 | ModifyConfigValue(resourceFile_, tset, "resources.cfg"); |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | // Load resource paths from data file using configfile ressource type |
---|
| 249 | Ogre::ConfigFile cf; |
---|
| 250 | try |
---|
| 251 | { |
---|
| 252 | cf.load(Settings::getDataPath() + resourceFile_); |
---|
| 253 | } |
---|
| 254 | catch (...) |
---|
| 255 | { |
---|
| 256 | //COUT(1) << ex.getFullDescription() << std::endl; |
---|
| 257 | COUT(0) << "Have you forgotten to set the data path in orxnox.ini?" << std::endl; |
---|
| 258 | throw; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | // Go through all sections & settings in the file |
---|
| 262 | Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); |
---|
| 263 | |
---|
| 264 | std::string secName, typeName, archName; |
---|
| 265 | while (seci.hasMoreElements()) |
---|
| 266 | { |
---|
| 267 | try |
---|
| 268 | { |
---|
| 269 | secName = seci.peekNextKey(); |
---|
| 270 | Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); |
---|
| 271 | Ogre::ConfigFile::SettingsMultiMap::iterator i; |
---|
| 272 | for (i = settings->begin(); i != settings->end(); ++i) |
---|
| 273 | { |
---|
| 274 | typeName = i->first; // for instance "FileSystem" or "Zip" |
---|
| 275 | archName = i->second; // name (and location) of archive |
---|
| 276 | |
---|
| 277 | Ogre::ResourceGroupManager::getSingleton().addResourceLocation( |
---|
| 278 | std::string(Settings::getDataPath() + archName), typeName, secName); |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | catch (Ogre::Exception& ex) |
---|
| 282 | { |
---|
| 283 | COUT(1) << ex.getFullDescription() << std::endl; |
---|
| 284 | } |
---|
| 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | void GSGraphics::loadRenderer() |
---|
| 289 | { |
---|
| 290 | CCOUT(4) << "Configuring Renderer" << std::endl; |
---|
| 291 | |
---|
| 292 | if (!ogreRoot_->restoreConfig()) |
---|
| 293 | if (!ogreRoot_->showConfigDialog()) |
---|
| 294 | ThrowException(InitialisationFailed, "Could not show Ogre configuration dialogue."); |
---|
| 295 | |
---|
| 296 | CCOUT(4) << "Creating render window" << std::endl; |
---|
| 297 | |
---|
[1824] | 298 | this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox"); |
---|
[1686] | 299 | |
---|
| 300 | Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this); |
---|
| 301 | |
---|
[1820] | 302 | Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0); |
---|
[1686] | 303 | |
---|
| 304 | // create a full screen default viewport |
---|
| 305 | this->viewport_ = this->renderWindow_->addViewport(0, 0); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | void GSGraphics::initialiseResources() |
---|
| 309 | { |
---|
| 310 | CCOUT(4) << "Initialising resources" << std::endl; |
---|
| 311 | //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load... |
---|
[1824] | 312 | //try |
---|
| 313 | //{ |
---|
[1686] | 314 | Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); |
---|
| 315 | /*Ogre::StringVector str = Ogre::ResourceGroupManager::getSingleton().getResourceGroups(); |
---|
| 316 | for (unsigned int i = 0; i < str.size(); i++) |
---|
| 317 | { |
---|
| 318 | Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(str[i]); |
---|
| 319 | }*/ |
---|
[1824] | 320 | //} |
---|
| 321 | //catch (...) |
---|
| 322 | //{ |
---|
| 323 | // CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl; |
---|
| 324 | // throw; |
---|
| 325 | //} |
---|
[1686] | 326 | } |
---|
| 327 | |
---|
| 328 | |
---|
| 329 | /** |
---|
| 330 | @brief |
---|
| 331 | Window has moved. |
---|
| 332 | @param rw |
---|
| 333 | The render window it occured in |
---|
| 334 | */ |
---|
| 335 | void GSGraphics::windowMoved(Ogre::RenderWindow *rw) |
---|
| 336 | { |
---|
[1755] | 337 | for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it) |
---|
[1686] | 338 | it->windowMoved(); |
---|
| 339 | } |
---|
| 340 | |
---|
| 341 | /** |
---|
| 342 | @brief |
---|
| 343 | Window has resized. |
---|
| 344 | @param rw |
---|
| 345 | The render window it occured in |
---|
| 346 | @note |
---|
| 347 | GraphicsEngine has a render window stored itself. This is the same |
---|
| 348 | as rw. But we have to be careful when using multiple render windows! |
---|
| 349 | */ |
---|
| 350 | void GSGraphics::windowResized(Ogre::RenderWindow *rw) |
---|
| 351 | { |
---|
[1755] | 352 | for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it) |
---|
[1686] | 353 | it->windowResized(this->renderWindow_->getWidth(), this->renderWindow_->getHeight()); |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | /** |
---|
| 357 | @brief |
---|
| 358 | Window focus has changed. |
---|
| 359 | @param rw |
---|
| 360 | The render window it occured in |
---|
| 361 | */ |
---|
| 362 | void GSGraphics::windowFocusChanged(Ogre::RenderWindow *rw) |
---|
| 363 | { |
---|
[1755] | 364 | for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it) |
---|
[1686] | 365 | it->windowFocusChanged(); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | @brief |
---|
| 370 | Window was closed. |
---|
| 371 | @param rw |
---|
| 372 | The render window it occured in |
---|
| 373 | */ |
---|
| 374 | void GSGraphics::windowClosed(Ogre::RenderWindow *rw) |
---|
| 375 | { |
---|
| 376 | // using CommandExecutor in order to avoid depending on Orxonox.h. |
---|
| 377 | //CommandExecutor::execute("exit", false); |
---|
| 378 | this->requestState("root"); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | void GSGraphics::printScreen() |
---|
| 382 | { |
---|
| 383 | if (this->renderWindow_) |
---|
| 384 | { |
---|
| 385 | this->renderWindow_->writeContentsToTimestampedFile("shot_", ".jpg"); |
---|
| 386 | } |
---|
| 387 | } |
---|
[1661] | 388 | } |
---|