| [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: | 
|---|
| [2896] | 25 |  *      Benjamin Knecht | 
|---|
| [1661] | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
| [2896] | 29 | /** | 
|---|
| [3196] | 30 | @file | 
|---|
 | 31 | @brief | 
|---|
 | 32 |     Implementation of Graphics GameState class. | 
|---|
| [2896] | 33 |  */ | 
|---|
 | 34 |  | 
|---|
| [1661] | 35 | #include "GSGraphics.h" | 
|---|
 | 36 |  | 
|---|
| [2710] | 37 | #include <boost/filesystem.hpp> | 
|---|
| [1686] | 38 | #include <OgreRenderWindow.h> | 
|---|
| [1661] | 39 |  | 
|---|
| [2896] | 40 | #include "core/ConfigValueIncludes.h" | 
|---|
 | 41 | #include "core/Clock.h" | 
|---|
| [1662] | 42 | #include "core/ConsoleCommand.h" | 
|---|
| [2896] | 43 | #include "core/Core.h" | 
|---|
| [1686] | 44 | #include "core/CoreIncludes.h" | 
|---|
| [2896] | 45 | #include "core/Game.h" | 
|---|
 | 46 | #include "core/GameMode.h" | 
|---|
| [1661] | 47 | #include "core/input/InputManager.h" | 
|---|
| [1788] | 48 | #include "core/input/KeyBinder.h" | 
|---|
| [2896] | 49 | #include "core/input/SimpleInputState.h" | 
|---|
| [2087] | 50 | #include "core/Loader.h" | 
|---|
 | 51 | #include "core/XMLFile.h" | 
|---|
| [1661] | 52 | #include "overlays/console/InGameConsole.h" | 
|---|
 | 53 | #include "gui/GUIManager.h" | 
|---|
| [3196] | 54 | #include "sound/SoundManager.h" | 
|---|
| [2896] | 55 | #include "GraphicsManager.h" | 
|---|
| [1686] | 56 |  | 
|---|
| [1661] | 57 | namespace orxonox | 
|---|
 | 58 | { | 
|---|
| [3084] | 59 |     AddGameState(GSGraphics, "graphics", false); | 
|---|
| [2896] | 60 |  | 
|---|
| [3084] | 61 |     GSGraphics::GSGraphics(const std::string& name, bool countTickTime) | 
|---|
 | 62 |         : GameState(name, countTickTime) | 
|---|
| [1661] | 63 |         , inputManager_(0) | 
|---|
 | 64 |         , console_(0) | 
|---|
 | 65 |         , guiManager_(0) | 
|---|
| [2896] | 66 |         , graphicsManager_(0) | 
|---|
| [3196] | 67 |         , soundManager_(0) | 
|---|
| [1788] | 68 |         , masterKeyBinder_(0) | 
|---|
| [2896] | 69 |         , masterInputState_(0) | 
|---|
| [2087] | 70 |         , debugOverlay_(0) | 
|---|
| [1661] | 71 |     { | 
|---|
| [1686] | 72 |         RegisterRootObject(GSGraphics); | 
|---|
| [1661] | 73 |     } | 
|---|
 | 74 |  | 
|---|
 | 75 |     GSGraphics::~GSGraphics() | 
|---|
 | 76 |     { | 
|---|
 | 77 |     } | 
|---|
 | 78 |  | 
|---|
| [2896] | 79 |     /** | 
|---|
 | 80 |     @brief | 
|---|
 | 81 |         this function does nothing | 
|---|
 | 82 |  | 
|---|
 | 83 |         Indeed. Here goes nothing. | 
|---|
 | 84 |     */ | 
|---|
| [1661] | 85 |     void GSGraphics::setConfigValues() | 
|---|
 | 86 |     { | 
|---|
 | 87 |     } | 
|---|
 | 88 |  | 
|---|
| [2896] | 89 |     /** | 
|---|
 | 90 |     @brief | 
|---|
 | 91 |         This function is called when we enter this game state. | 
|---|
 | 92 |  | 
|---|
 | 93 |         Since graphics is very important for our game this function does quite a lot: | 
|---|
 | 94 |         \li starts graphics manager | 
|---|
 | 95 |         \li loads debug overlay | 
|---|
 | 96 |         \li manages render window | 
|---|
 | 97 |         \li creates input manager | 
|---|
 | 98 |         \li loads master key bindings | 
|---|
| [3196] | 99 |         \li loads the SoundManager | 
|---|
| [2896] | 100 |         \li loads ingame console | 
|---|
 | 101 |         \li loads GUI interface (GUIManager) | 
|---|
 | 102 |         \li creates console command to toggle GUI | 
|---|
 | 103 |     */ | 
|---|
 | 104 |     void GSGraphics::activate() | 
|---|
| [1661] | 105 |     { | 
|---|
| [2896] | 106 |         GameMode::setShowsGraphics(true); | 
|---|
| [1696] | 107 |  | 
|---|
| [2896] | 108 |         setConfigValues(); | 
|---|
| [1674] | 109 |  | 
|---|
| [2896] | 110 |         // initialise graphics manager. Doesn't load the render window yet! | 
|---|
 | 111 |         this->graphicsManager_ = new GraphicsManager(); | 
|---|
 | 112 |         this->graphicsManager_->initialise(); | 
|---|
| [2710] | 113 |  | 
|---|
| [2087] | 114 |         // load debug overlay | 
|---|
 | 115 |         COUT(3) << "Loading Debug Overlay..." << std::endl; | 
|---|
| [2759] | 116 |         this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string()); | 
|---|
| [2087] | 117 |         Loader::open(debugOverlay_); | 
|---|
| [1686] | 118 |  | 
|---|
| [2896] | 119 |         // The render window width and height are used to set up the mouse movement. | 
|---|
 | 120 |         size_t windowHnd = 0; | 
|---|
 | 121 |         Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow(); | 
|---|
 | 122 |         renderWindow->getCustomAttribute("WINDOW", &windowHnd); | 
|---|
 | 123 |  | 
|---|
| [1661] | 124 |         // Calls the InputManager which sets up the input devices. | 
|---|
 | 125 |         inputManager_ = new InputManager(); | 
|---|
| [2896] | 126 |         inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true); | 
|---|
 | 127 |  | 
|---|
 | 128 |         // load master key bindings | 
|---|
 | 129 |         masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("master", true); | 
|---|
| [2103] | 130 |         masterKeyBinder_ = new KeyBinder(); | 
|---|
| [2710] | 131 |         masterKeyBinder_->loadBindings("masterKeybindings.ini"); | 
|---|
| [2896] | 132 |         masterInputState_->setKeyHandler(masterKeyBinder_); | 
|---|
| [1661] | 133 |  | 
|---|
| [3196] | 134 |         // Load the SoundManager | 
|---|
 | 135 |         soundManager_ = new SoundManager(); | 
|---|
 | 136 |  | 
|---|
| [1661] | 137 |         // Load the InGameConsole | 
|---|
 | 138 |         console_ = new InGameConsole(); | 
|---|
| [2896] | 139 |         console_->initialise(renderWindow->getWidth(), renderWindow->getHeight()); | 
|---|
| [1661] | 140 |  | 
|---|
 | 141 |         // load the CEGUI interface | 
|---|
 | 142 |         guiManager_ = new GUIManager(); | 
|---|
| [2896] | 143 |         guiManager_->initialise(renderWindow); | 
|---|
| [1674] | 144 |  | 
|---|
| [2896] | 145 |         // add console command to toggle GUI | 
|---|
 | 146 |         FunctorMember<GSGraphics>* functor = createFunctor(&GSGraphics::toggleGUI); | 
|---|
 | 147 |         functor->setObject(this); | 
|---|
 | 148 |         this->ccToggleGUI_ = createConsoleCommand(functor, "toggleGUI"); | 
|---|
 | 149 |         CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_); | 
|---|
 | 150 |  | 
|---|
 | 151 |         // enable master input | 
|---|
 | 152 |         InputManager::getInstance().requestEnterState("master"); | 
|---|
| [1661] | 153 |     } | 
|---|
 | 154 |  | 
|---|
| [2896] | 155 |     /** | 
|---|
 | 156 |     @brief | 
|---|
 | 157 |         This function is called when the game state is left | 
|---|
 | 158 |  | 
|---|
 | 159 |         Created references, input states and console commands are deleted. | 
|---|
 | 160 |     */ | 
|---|
 | 161 |     void GSGraphics::deactivate() | 
|---|
| [1661] | 162 |     { | 
|---|
| [2928] | 163 | /* | 
|---|
| [2896] | 164 |         if (this->ccToggleGUI_) | 
|---|
 | 165 |         { | 
|---|
 | 166 |             delete this->ccToggleGUI_; | 
|---|
 | 167 |             this->ccToggleGUI_ = 0; | 
|---|
 | 168 |         } | 
|---|
| [2928] | 169 | */ | 
|---|
| [2662] | 170 |  | 
|---|
| [2896] | 171 |         masterInputState_->setHandler(0); | 
|---|
 | 172 |         InputManager::getInstance().requestDestroyState("master"); | 
|---|
 | 173 |         delete this->masterKeyBinder_; | 
|---|
| [1878] | 174 |  | 
|---|
| [1662] | 175 |         delete this->guiManager_; | 
|---|
 | 176 |         delete this->console_; | 
|---|
| [1661] | 177 |  | 
|---|
| [2087] | 178 |         Loader::unload(this->debugOverlay_); | 
|---|
 | 179 |         delete this->debugOverlay_; | 
|---|
 | 180 |  | 
|---|
| [3196] | 181 |         delete this->soundManager_; | 
|---|
 | 182 |  | 
|---|
| [2896] | 183 |         delete this->inputManager_; | 
|---|
 | 184 |         this->inputManager_ = 0; | 
|---|
| [2662] | 185 |  | 
|---|
| [2896] | 186 |         delete graphicsManager_; | 
|---|
| [1696] | 187 |  | 
|---|
| [2896] | 188 |         GameMode::setShowsGraphics(false); | 
|---|
 | 189 |     } | 
|---|
| [1824] | 190 |  | 
|---|
| [2896] | 191 |     /** | 
|---|
 | 192 |     @brief | 
|---|
 | 193 |         Toggles the visibility of the current GUI | 
|---|
| [1824] | 194 |  | 
|---|
| [2896] | 195 |         This function just executes a Lua function in the main script of the GUI by accessing the GUIManager. | 
|---|
 | 196 |         For more details on this function check out the Lua code. | 
|---|
 | 197 |     */ | 
|---|
 | 198 |     void GSGraphics::toggleGUI() | 
|---|
 | 199 |     { | 
|---|
 | 200 |             GUIManager::getInstance().executeCode("toggleGUI()"); | 
|---|
| [1661] | 201 |     } | 
|---|
 | 202 |  | 
|---|
| [1662] | 203 |     /** | 
|---|
| [2662] | 204 |     @note | 
|---|
| [1662] | 205 |         A note about the Ogre::FrameListener: Even though we don't use them, | 
|---|
 | 206 |         they still get called. However, the delta times are not correct (except | 
|---|
 | 207 |         for timeSinceLastFrame, which is the most important). A little research | 
|---|
 | 208 |         as shown that there is probably only one FrameListener that doesn't even | 
|---|
 | 209 |         need the time. So we shouldn't run into problems. | 
|---|
 | 210 |     */ | 
|---|
| [2896] | 211 |     void GSGraphics::update(const Clock& time) | 
|---|
| [1661] | 212 |     { | 
|---|
| [2896] | 213 |         if (this->getActivity().topState) | 
|---|
| [2087] | 214 |         { | 
|---|
| [2896] | 215 |             // This state can not 'survive' on its own. | 
|---|
 | 216 |             // Load a user interface therefore | 
|---|
 | 217 |             Game::getInstance().requestState("mainMenu"); | 
|---|
| [2087] | 218 |         } | 
|---|
 | 219 |  | 
|---|
| [2896] | 220 |         uint64_t timeBeforeTick = time.getRealMicroseconds(); | 
|---|
| [1661] | 221 |  | 
|---|
| [3084] | 222 |         this->inputManager_->update(time); | 
|---|
| [2896] | 223 |         this->console_->update(time); | 
|---|
| [1661] | 224 |  | 
|---|
| [2896] | 225 |         uint64_t timeAfterTick = time.getRealMicroseconds(); | 
|---|
| [1661] | 226 |  | 
|---|
| [2896] | 227 |         // Also add our tick time | 
|---|
 | 228 |         Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); | 
|---|
| [1661] | 229 |  | 
|---|
| [3084] | 230 |         // Process gui events | 
|---|
 | 231 |         this->guiManager_->update(time); | 
|---|
| [2896] | 232 |         // Render | 
|---|
 | 233 |         this->graphicsManager_->update(time); | 
|---|
| [1661] | 234 |     } | 
|---|
| [1686] | 235 |  | 
|---|
| [1891] | 236 |     /** | 
|---|
 | 237 |     @brief | 
|---|
| [1686] | 238 |         Window has resized. | 
|---|
 | 239 |     @param rw | 
|---|
 | 240 |         The render window it occured in | 
|---|
 | 241 |     @note | 
|---|
| [2896] | 242 |         GraphicsManager has a render window stored itself. This is the same | 
|---|
| [1686] | 243 |         as rw. But we have to be careful when using multiple render windows! | 
|---|
 | 244 |     */ | 
|---|
| [2896] | 245 |     void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight) | 
|---|
| [1686] | 246 |     { | 
|---|
| [2087] | 247 |         // OIS needs this under linux even if we only use relative input measurement. | 
|---|
 | 248 |         if (this->inputManager_) | 
|---|
| [2896] | 249 |             this->inputManager_->setWindowExtents(newWidth, newHeight); | 
|---|
| [1686] | 250 |     } | 
|---|
 | 251 |  | 
|---|
 | 252 |     /** | 
|---|
 | 253 |     @brief | 
|---|
 | 254 |         Window focus has changed. | 
|---|
 | 255 |     @param rw | 
|---|
 | 256 |         The render window it occured in | 
|---|
 | 257 |     */ | 
|---|
| [2896] | 258 |     void GSGraphics::windowFocusChanged() | 
|---|
| [1686] | 259 |     { | 
|---|
| [1878] | 260 |         // instruct InputManager to clear the buffers (core library so we cannot use the interface) | 
|---|
| [2087] | 261 |         if (this->inputManager_) | 
|---|
 | 262 |             this->inputManager_->clearBuffers(); | 
|---|
| [1686] | 263 |     } | 
|---|
 | 264 |  | 
|---|
| [1661] | 265 | } | 
|---|