| 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 |  | 
|---|
| 32 | #include <boost/filesystem.hpp> | 
|---|
| 33 | #include <OgreRenderWindow.h> | 
|---|
| 34 |  | 
|---|
| 35 | #include "util/Debug.h" | 
|---|
| 36 | #include "core/ConfigValueIncludes.h" | 
|---|
| 37 | #include "core/CoreIncludes.h" | 
|---|
| 38 | #include "core/Core.h" | 
|---|
| 39 | #include "core/input/InputManager.h" | 
|---|
| 40 | #include "core/input/KeyBinder.h" | 
|---|
| 41 | #include "core/input/SimpleInputState.h" | 
|---|
| 42 | #include "core/Loader.h" | 
|---|
| 43 | #include "core/XMLFile.h" | 
|---|
| 44 | #include "overlays/console/InGameConsole.h" | 
|---|
| 45 | #include "gui/GUIManager.h" | 
|---|
| 46 |  | 
|---|
| 47 | // for compatibility | 
|---|
| 48 | #include "GraphicsManager.h" | 
|---|
| 49 |  | 
|---|
| 50 | namespace orxonox | 
|---|
| 51 | { | 
|---|
| 52 |     GSGraphics::GSGraphics() | 
|---|
| 53 |         : GameState<GSRoot>("graphics") | 
|---|
| 54 |         , inputManager_(0) | 
|---|
| 55 |         , console_(0) | 
|---|
| 56 |         , guiManager_(0) | 
|---|
| 57 |         , graphicsManager_(0) | 
|---|
| 58 |         , masterKeyBinder_(0) | 
|---|
| 59 |         , masterInputState_(0) | 
|---|
| 60 |         , debugOverlay_(0) | 
|---|
| 61 |     { | 
|---|
| 62 |         RegisterRootObject(GSGraphics); | 
|---|
| 63 |         setConfigValues(); | 
|---|
| 64 |     } | 
|---|
| 65 |  | 
|---|
| 66 |     GSGraphics::~GSGraphics() | 
|---|
| 67 |     { | 
|---|
| 68 |     } | 
|---|
| 69 |  | 
|---|
| 70 |     void GSGraphics::setConfigValues() | 
|---|
| 71 |     { | 
|---|
| 72 |     } | 
|---|
| 73 |  | 
|---|
| 74 |     void GSGraphics::enter() | 
|---|
| 75 |     { | 
|---|
| 76 |         Core::setShowsGraphics(true); | 
|---|
| 77 |  | 
|---|
| 78 |         // initialise graphics manager. Doesn't load the render window yet! | 
|---|
| 79 |         this->graphicsManager_ = new GraphicsManager(); | 
|---|
| 80 |         this->graphicsManager_->initialise(); | 
|---|
| 81 |  | 
|---|
| 82 |         // load debug overlay | 
|---|
| 83 |         COUT(3) << "Loading Debug Overlay..." << std::endl; | 
|---|
| 84 |         this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string()); | 
|---|
| 85 |         Loader::open(debugOverlay_); | 
|---|
| 86 |  | 
|---|
| 87 |         // Calls the InputManager which sets up the input devices. | 
|---|
| 88 |         // The render window width and height are used to set up the mouse movement. | 
|---|
| 89 |         inputManager_ = new InputManager(); | 
|---|
| 90 |         size_t windowHnd = 0; | 
|---|
| 91 |         Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow(); | 
|---|
| 92 |         renderWindow->getCustomAttribute("WINDOW", &windowHnd); | 
|---|
| 93 |         inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true); | 
|---|
| 94 |  | 
|---|
| 95 |         masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("master", true); | 
|---|
| 96 |         masterKeyBinder_ = new KeyBinder(); | 
|---|
| 97 |         masterKeyBinder_->loadBindings("masterKeybindings.ini"); | 
|---|
| 98 |         masterInputState_->setKeyHandler(masterKeyBinder_); | 
|---|
| 99 |  | 
|---|
| 100 |         // Load the InGameConsole | 
|---|
| 101 |         console_ = new InGameConsole(); | 
|---|
| 102 |         console_->initialise(renderWindow->getWidth(), renderWindow->getHeight()); | 
|---|
| 103 |  | 
|---|
| 104 |         // load the CEGUI interface | 
|---|
| 105 |         guiManager_ = new GUIManager(); | 
|---|
| 106 |         guiManager_->initialise(renderWindow); | 
|---|
| 107 |  | 
|---|
| 108 |         InputManager::getInstance().requestEnterState("master"); | 
|---|
| 109 |     } | 
|---|
| 110 |  | 
|---|
| 111 |     void GSGraphics::leave() | 
|---|
| 112 |     { | 
|---|
| 113 |         if (Core::showsGraphics()) | 
|---|
| 114 |             InputManager::getInstance().requestLeaveState("master"); | 
|---|
| 115 |  | 
|---|
| 116 |         delete this->guiManager_; | 
|---|
| 117 |  | 
|---|
| 118 |         delete this->console_; | 
|---|
| 119 |  | 
|---|
| 120 |         //inputManager_->getMasterInputState()->removeKeyHandler(this->masterKeyBinder_); | 
|---|
| 121 |         delete this->masterKeyBinder_; | 
|---|
| 122 |         delete this->inputManager_; | 
|---|
| 123 |  | 
|---|
| 124 |         Loader::unload(this->debugOverlay_); | 
|---|
| 125 |         delete this->debugOverlay_; | 
|---|
| 126 |  | 
|---|
| 127 |         delete graphicsManager_; | 
|---|
| 128 |  | 
|---|
| 129 |         if (Core::showsGraphics()) | 
|---|
| 130 |         { | 
|---|
| 131 |             masterInputState_->setHandler(0); | 
|---|
| 132 |             InputManager::getInstance().requestDestroyState("master"); | 
|---|
| 133 |             if (this->masterKeyBinder_) | 
|---|
| 134 |             { | 
|---|
| 135 |                 delete this->masterKeyBinder_; | 
|---|
| 136 |                 this->masterKeyBinder_ = 0; | 
|---|
| 137 |             } | 
|---|
| 138 |         } | 
|---|
| 139 |     } | 
|---|
| 140 |  | 
|---|
| 141 |     /** | 
|---|
| 142 |     @note | 
|---|
| 143 |         A note about the Ogre::FrameListener: Even though we don't use them, | 
|---|
| 144 |         they still get called. However, the delta times are not correct (except | 
|---|
| 145 |         for timeSinceLastFrame, which is the most important). A little research | 
|---|
| 146 |         as shown that there is probably only one FrameListener that doesn't even | 
|---|
| 147 |         need the time. So we shouldn't run into problems. | 
|---|
| 148 |     */ | 
|---|
| 149 |     void GSGraphics::ticked(const Clock& time) | 
|---|
| 150 |     { | 
|---|
| 151 |         uint64_t timeBeforeTick = time.getRealMicroseconds(); | 
|---|
| 152 |  | 
|---|
| 153 |         this->inputManager_->update(time);        // tick console | 
|---|
| 154 |         this->console_->update(time); | 
|---|
| 155 |         this->tickChild(time); | 
|---|
| 156 |  | 
|---|
| 157 |         uint64_t timeAfterTick = time.getRealMicroseconds(); | 
|---|
| 158 |  | 
|---|
| 159 |         // Also add our tick time to the list in GSRoot | 
|---|
| 160 |         this->getParent()->addTickTime(timeAfterTick - timeBeforeTick); | 
|---|
| 161 |  | 
|---|
| 162 |         // Update statistics overlay. Note that the values only change periodically in GSRoot. | 
|---|
| 163 |         GraphicsManager::getInstance().setAverageFramesPerSecond(this->getParent()->getAvgFPS()); | 
|---|
| 164 |         GraphicsManager::getInstance().setAverageTickTime(this->getParent()->getAvgTickTime()); | 
|---|
| 165 |  | 
|---|
| 166 |         this->graphicsManager_->update(time); | 
|---|
| 167 |     } | 
|---|
| 168 |  | 
|---|
| 169 |     /** | 
|---|
| 170 |     @brief | 
|---|
| 171 |         Window has resized. | 
|---|
| 172 |     @param rw | 
|---|
| 173 |         The render window it occured in | 
|---|
| 174 |     @note | 
|---|
| 175 |         GraphicsManager has a render window stored itself. This is the same | 
|---|
| 176 |         as rw. But we have to be careful when using multiple render windows! | 
|---|
| 177 |     */ | 
|---|
| 178 |     void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight) | 
|---|
| 179 |     { | 
|---|
| 180 |         // OIS needs this under linux even if we only use relative input measurement. | 
|---|
| 181 |         if (this->inputManager_) | 
|---|
| 182 |             this->inputManager_->setWindowExtents(newWidth, newHeight); | 
|---|
| 183 |     } | 
|---|
| 184 |  | 
|---|
| 185 |     /** | 
|---|
| 186 |     @brief | 
|---|
| 187 |         Window focus has changed. | 
|---|
| 188 |     @param rw | 
|---|
| 189 |         The render window it occured in | 
|---|
| 190 |     */ | 
|---|
| 191 |     void GSGraphics::windowFocusChanged() | 
|---|
| 192 |     { | 
|---|
| 193 |         // instruct InputManager to clear the buffers (core library so we cannot use the interface) | 
|---|
| 194 |         if (this->inputManager_) | 
|---|
| 195 |             this->inputManager_->clearBuffers(); | 
|---|
| 196 |     } | 
|---|
| 197 |  | 
|---|
| 198 | } | 
|---|