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