[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 | { |
---|
[3280] | 59 | DeclareGameState(GSGraphics, "graphics", true, true); |
---|
[2896] | 60 | |
---|
[3280] | 61 | GSGraphics::GSGraphics(const GameStateConstrParams& params) |
---|
| 62 | : GameState(params) |
---|
[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 | |
---|
[3280] | 110 | // Load OGRE including the render window |
---|
[2896] | 111 | this->graphicsManager_ = new GraphicsManager(); |
---|
[2710] | 112 | |
---|
[2087] | 113 | // load debug overlay |
---|
| 114 | COUT(3) << "Loading Debug Overlay..." << std::endl; |
---|
[2759] | 115 | this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string()); |
---|
[2087] | 116 | Loader::open(debugOverlay_); |
---|
[1686] | 117 | |
---|
[2896] | 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 | |
---|
[1661] | 123 | // Calls the InputManager which sets up the input devices. |
---|
| 124 | inputManager_ = new InputManager(); |
---|
[2896] | 125 | inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true); |
---|
| 126 | |
---|
| 127 | // load master key bindings |
---|
| 128 | masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("master", true); |
---|
[2103] | 129 | masterKeyBinder_ = new KeyBinder(); |
---|
[2710] | 130 | masterKeyBinder_->loadBindings("masterKeybindings.ini"); |
---|
[2896] | 131 | masterInputState_->setKeyHandler(masterKeyBinder_); |
---|
[1661] | 132 | |
---|
[3196] | 133 | // Load the SoundManager |
---|
| 134 | soundManager_ = new SoundManager(); |
---|
| 135 | |
---|
[1661] | 136 | // Load the InGameConsole |
---|
| 137 | console_ = new InGameConsole(); |
---|
[2896] | 138 | console_->initialise(renderWindow->getWidth(), renderWindow->getHeight()); |
---|
[1661] | 139 | |
---|
| 140 | // load the CEGUI interface |
---|
| 141 | guiManager_ = new GUIManager(); |
---|
[2896] | 142 | guiManager_->initialise(renderWindow); |
---|
[1674] | 143 | |
---|
[2896] | 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"); |
---|
[1661] | 152 | } |
---|
| 153 | |
---|
[2896] | 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() |
---|
[1661] | 161 | { |
---|
[2928] | 162 | /* |
---|
[2896] | 163 | if (this->ccToggleGUI_) |
---|
| 164 | { |
---|
| 165 | delete this->ccToggleGUI_; |
---|
| 166 | this->ccToggleGUI_ = 0; |
---|
| 167 | } |
---|
[2928] | 168 | */ |
---|
[2662] | 169 | |
---|
[2896] | 170 | masterInputState_->setHandler(0); |
---|
| 171 | InputManager::getInstance().requestDestroyState("master"); |
---|
| 172 | delete this->masterKeyBinder_; |
---|
[1878] | 173 | |
---|
[1662] | 174 | delete this->guiManager_; |
---|
| 175 | delete this->console_; |
---|
[1661] | 176 | |
---|
[2087] | 177 | Loader::unload(this->debugOverlay_); |
---|
| 178 | delete this->debugOverlay_; |
---|
| 179 | |
---|
[3196] | 180 | delete this->soundManager_; |
---|
| 181 | |
---|
[2896] | 182 | delete this->inputManager_; |
---|
| 183 | this->inputManager_ = 0; |
---|
[2662] | 184 | |
---|
[2896] | 185 | delete graphicsManager_; |
---|
[1696] | 186 | |
---|
[2896] | 187 | GameMode::setShowsGraphics(false); |
---|
| 188 | } |
---|
[1824] | 189 | |
---|
[2896] | 190 | /** |
---|
| 191 | @brief |
---|
| 192 | Toggles the visibility of the current GUI |
---|
[1824] | 193 | |
---|
[2896] | 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 | { |
---|
[3280] | 199 | GUIManager::getInstance().executeCode("toggleGUI()"); |
---|
[1661] | 200 | } |
---|
| 201 | |
---|
[1662] | 202 | /** |
---|
[2662] | 203 | @note |
---|
[1662] | 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 | */ |
---|
[2896] | 210 | void GSGraphics::update(const Clock& time) |
---|
[1661] | 211 | { |
---|
[2896] | 212 | if (this->getActivity().topState) |
---|
[2087] | 213 | { |
---|
[2896] | 214 | // This state can not 'survive' on its own. |
---|
| 215 | // Load a user interface therefore |
---|
| 216 | Game::getInstance().requestState("mainMenu"); |
---|
[2087] | 217 | } |
---|
| 218 | |
---|
[2896] | 219 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
[1661] | 220 | |
---|
[3084] | 221 | this->inputManager_->update(time); |
---|
[2896] | 222 | this->console_->update(time); |
---|
[1661] | 223 | |
---|
[2896] | 224 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
[1661] | 225 | |
---|
[2896] | 226 | // Also add our tick time |
---|
| 227 | Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); |
---|
[1661] | 228 | |
---|
[3084] | 229 | // Process gui events |
---|
| 230 | this->guiManager_->update(time); |
---|
[2896] | 231 | // Render |
---|
| 232 | this->graphicsManager_->update(time); |
---|
[1661] | 233 | } |
---|
[1686] | 234 | |
---|
[1891] | 235 | /** |
---|
| 236 | @brief |
---|
[1686] | 237 | Window has resized. |
---|
| 238 | @param rw |
---|
| 239 | The render window it occured in |
---|
| 240 | @note |
---|
[2896] | 241 | GraphicsManager has a render window stored itself. This is the same |
---|
[1686] | 242 | as rw. But we have to be careful when using multiple render windows! |
---|
| 243 | */ |
---|
[2896] | 244 | void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight) |
---|
[1686] | 245 | { |
---|
[2087] | 246 | // OIS needs this under linux even if we only use relative input measurement. |
---|
| 247 | if (this->inputManager_) |
---|
[2896] | 248 | this->inputManager_->setWindowExtents(newWidth, newHeight); |
---|
[1686] | 249 | } |
---|
| 250 | |
---|
| 251 | /** |
---|
| 252 | @brief |
---|
| 253 | Window focus has changed. |
---|
| 254 | @param rw |
---|
| 255 | The render window it occured in |
---|
| 256 | */ |
---|
[2896] | 257 | void GSGraphics::windowFocusChanged() |
---|
[1686] | 258 | { |
---|
[1878] | 259 | // instruct InputManager to clear the buffers (core library so we cannot use the interface) |
---|
[2087] | 260 | if (this->inputManager_) |
---|
| 261 | this->inputManager_->clearBuffers(); |
---|
[1686] | 262 | } |
---|
| 263 | |
---|
[1661] | 264 | } |
---|