Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/orxonox/gamestates/GSGraphics.cc @ 3338

Last change on this file since 3338 was 3338, checked in by rgrieder, 15 years ago

Improved exception-safety in the GUIManager.

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