[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 | |
---|
[3327] | 40 | #include "util/Convert.h" |
---|
[2896] | 41 | #include "core/Clock.h" |
---|
[3327] | 42 | #include "core/CommandExecutor.h" |
---|
[1662] | 43 | #include "core/ConsoleCommand.h" |
---|
[2896] | 44 | #include "core/Core.h" |
---|
| 45 | #include "core/Game.h" |
---|
| 46 | #include "core/GameMode.h" |
---|
[1661] | 47 | #include "core/input/InputManager.h" |
---|
[1788] | 48 | #include "core/input/KeyBinder.h" |
---|
[3327] | 49 | #include "core/input/InputState.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 | { |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | GSGraphics::~GSGraphics() |
---|
| 75 | { |
---|
| 76 | } |
---|
| 77 | |
---|
[2896] | 78 | /** |
---|
| 79 | @brief |
---|
| 80 | This function is called when we enter this game state. |
---|
| 81 | |
---|
| 82 | Since graphics is very important for our game this function does quite a lot: |
---|
| 83 | \li starts graphics manager |
---|
| 84 | \li loads debug overlay |
---|
| 85 | \li manages render window |
---|
| 86 | \li creates input manager |
---|
| 87 | \li loads master key bindings |
---|
[3196] | 88 | \li loads the SoundManager |
---|
[2896] | 89 | \li loads ingame console |
---|
| 90 | \li loads GUI interface (GUIManager) |
---|
| 91 | \li creates console command to toggle GUI |
---|
| 92 | */ |
---|
| 93 | void GSGraphics::activate() |
---|
[1661] | 94 | { |
---|
[2896] | 95 | GameMode::setShowsGraphics(true); |
---|
[1696] | 96 | |
---|
[3280] | 97 | // Load OGRE including the render window |
---|
[2896] | 98 | this->graphicsManager_ = new GraphicsManager(); |
---|
[2710] | 99 | |
---|
[2087] | 100 | // load debug overlay |
---|
| 101 | COUT(3) << "Loading Debug Overlay..." << std::endl; |
---|
[2759] | 102 | this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string()); |
---|
[2087] | 103 | Loader::open(debugOverlay_); |
---|
[1686] | 104 | |
---|
[2896] | 105 | // The render window width and height are used to set up the mouse movement. |
---|
| 106 | size_t windowHnd = 0; |
---|
| 107 | Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow(); |
---|
| 108 | renderWindow->getCustomAttribute("WINDOW", &windowHnd); |
---|
| 109 | |
---|
[1661] | 110 | // Calls the InputManager which sets up the input devices. |
---|
[3327] | 111 | inputManager_ = new InputManager(windowHnd); |
---|
[2896] | 112 | |
---|
| 113 | // load master key bindings |
---|
[3327] | 114 | masterInputState_ = InputManager::getInstance().createInputState("master", true); |
---|
[2103] | 115 | masterKeyBinder_ = new KeyBinder(); |
---|
[2710] | 116 | masterKeyBinder_->loadBindings("masterKeybindings.ini"); |
---|
[2896] | 117 | masterInputState_->setKeyHandler(masterKeyBinder_); |
---|
[1661] | 118 | |
---|
[3196] | 119 | // Load the SoundManager |
---|
| 120 | soundManager_ = new SoundManager(); |
---|
| 121 | |
---|
[1661] | 122 | // Load the InGameConsole |
---|
| 123 | console_ = new InGameConsole(); |
---|
[3327] | 124 | console_->initialise(); |
---|
[1661] | 125 | |
---|
| 126 | // load the CEGUI interface |
---|
| 127 | guiManager_ = new GUIManager(); |
---|
[2896] | 128 | guiManager_->initialise(renderWindow); |
---|
[1674] | 129 | |
---|
[2896] | 130 | // add console command to toggle GUI |
---|
| 131 | FunctorMember<GSGraphics>* functor = createFunctor(&GSGraphics::toggleGUI); |
---|
| 132 | functor->setObject(this); |
---|
| 133 | this->ccToggleGUI_ = createConsoleCommand(functor, "toggleGUI"); |
---|
| 134 | CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_); |
---|
| 135 | |
---|
| 136 | // enable master input |
---|
[3327] | 137 | InputManager::getInstance().enterState("master"); |
---|
[1661] | 138 | } |
---|
| 139 | |
---|
[2896] | 140 | /** |
---|
| 141 | @brief |
---|
| 142 | This function is called when the game state is left |
---|
| 143 | |
---|
| 144 | Created references, input states and console commands are deleted. |
---|
| 145 | */ |
---|
| 146 | void GSGraphics::deactivate() |
---|
[1661] | 147 | { |
---|
[2928] | 148 | /* |
---|
[2896] | 149 | if (this->ccToggleGUI_) |
---|
| 150 | { |
---|
| 151 | delete this->ccToggleGUI_; |
---|
| 152 | this->ccToggleGUI_ = 0; |
---|
| 153 | } |
---|
[2928] | 154 | */ |
---|
[2662] | 155 | |
---|
[2896] | 156 | masterInputState_->setHandler(0); |
---|
[3327] | 157 | InputManager::getInstance().destroyState("master"); |
---|
[2896] | 158 | delete this->masterKeyBinder_; |
---|
[1878] | 159 | |
---|
[1662] | 160 | delete this->guiManager_; |
---|
| 161 | delete this->console_; |
---|
[1661] | 162 | |
---|
[2087] | 163 | Loader::unload(this->debugOverlay_); |
---|
| 164 | delete this->debugOverlay_; |
---|
| 165 | |
---|
[3196] | 166 | delete this->soundManager_; |
---|
| 167 | |
---|
[2896] | 168 | delete this->inputManager_; |
---|
| 169 | this->inputManager_ = 0; |
---|
[2662] | 170 | |
---|
[2896] | 171 | delete graphicsManager_; |
---|
[1696] | 172 | |
---|
[2896] | 173 | GameMode::setShowsGraphics(false); |
---|
| 174 | } |
---|
[1824] | 175 | |
---|
[2896] | 176 | /** |
---|
| 177 | @brief |
---|
| 178 | Toggles the visibility of the current GUI |
---|
[1824] | 179 | |
---|
[2896] | 180 | This function just executes a Lua function in the main script of the GUI by accessing the GUIManager. |
---|
| 181 | For more details on this function check out the Lua code. |
---|
| 182 | */ |
---|
| 183 | void GSGraphics::toggleGUI() |
---|
| 184 | { |
---|
[3280] | 185 | GUIManager::getInstance().executeCode("toggleGUI()"); |
---|
[1661] | 186 | } |
---|
| 187 | |
---|
[1662] | 188 | /** |
---|
[2662] | 189 | @note |
---|
[1662] | 190 | A note about the Ogre::FrameListener: Even though we don't use them, |
---|
| 191 | they still get called. However, the delta times are not correct (except |
---|
| 192 | for timeSinceLastFrame, which is the most important). A little research |
---|
| 193 | as shown that there is probably only one FrameListener that doesn't even |
---|
| 194 | need the time. So we shouldn't run into problems. |
---|
| 195 | */ |
---|
[2896] | 196 | void GSGraphics::update(const Clock& time) |
---|
[1661] | 197 | { |
---|
[2896] | 198 | if (this->getActivity().topState) |
---|
[2087] | 199 | { |
---|
[2896] | 200 | // This state can not 'survive' on its own. |
---|
| 201 | // Load a user interface therefore |
---|
| 202 | Game::getInstance().requestState("mainMenu"); |
---|
[2087] | 203 | } |
---|
| 204 | |
---|
[2896] | 205 | uint64_t timeBeforeTick = time.getRealMicroseconds(); |
---|
[1661] | 206 | |
---|
[3084] | 207 | this->inputManager_->update(time); |
---|
[2896] | 208 | this->console_->update(time); |
---|
[1661] | 209 | |
---|
[2896] | 210 | uint64_t timeAfterTick = time.getRealMicroseconds(); |
---|
[1661] | 211 | |
---|
[2896] | 212 | // Also add our tick time |
---|
| 213 | Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick); |
---|
[1661] | 214 | |
---|
[3084] | 215 | // Process gui events |
---|
| 216 | this->guiManager_->update(time); |
---|
[2896] | 217 | // Render |
---|
| 218 | this->graphicsManager_->update(time); |
---|
[1661] | 219 | } |
---|
| 220 | } |
---|