Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/orxonox/gamestates/GSGraphics.cc @ 3274

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

Added a few more generic parts to the input library:

  • Created Mouse and Keyboard to join JoyStick and provided them with a templated base class (InputDeviceTemplated) that does most of the work (reduces quite some redundancy)
  • Created InputPrereqs.h from InputInterfaces.h and destroyed the latter
  • Exported InputHandler to its own file and replaced KeyHandler, MouseHandler and JoyStickHandler with the single InputHandler.
  • Deleted the SimpleInputState: There is only one class now which fulfills all our needs.

In general there is now less code and the code itself has more 'pluses'. However I haven't really thrown away any feature at all.

  • Property svn:eol-style set to native
File size: 7.9 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/ConfigValueIncludes.h"
42#include "core/Clock.h"
43#include "core/CommandExecutor.h"
44#include "core/ConsoleCommand.h"
45#include "core/Core.h"
46#include "core/CoreIncludes.h"
47#include "core/Game.h"
48#include "core/GameMode.h"
49#include "core/input/InputManager.h"
50#include "core/input/KeyBinder.h"
51#include "core/input/InputState.h"
52#include "core/Loader.h"
53#include "core/XMLFile.h"
54#include "overlays/console/InGameConsole.h"
55#include "gui/GUIManager.h"
56#include "sound/SoundManager.h"
57#include "GraphicsManager.h"
58
59namespace orxonox
60{
61    DeclareGameState(GSGraphics, "graphics", true, true);
62
63    GSGraphics::GSGraphics(const GameStateConstrParams& params)
64        : GameState(params)
65        , inputManager_(0)
66        , console_(0)
67        , guiManager_(0)
68        , graphicsManager_(0)
69        , soundManager_(0)
70        , masterKeyBinder_(0)
71        , masterInputState_(0)
72        , debugOverlay_(0)
73    {
74        RegisterRootObject(GSGraphics);
75    }
76
77    GSGraphics::~GSGraphics()
78    {
79    }
80
81    /**
82    @brief
83        this function does nothing
84
85        Indeed. Here goes nothing.
86    */
87    void GSGraphics::setConfigValues()
88    {
89    }
90
91    /**
92    @brief
93        This function is called when we enter this game state.
94
95        Since graphics is very important for our game this function does quite a lot:
96        \li starts graphics manager
97        \li loads debug overlay
98        \li manages render window
99        \li creates input manager
100        \li loads master key bindings
101        \li loads the SoundManager
102        \li loads ingame console
103        \li loads GUI interface (GUIManager)
104        \li creates console command to toggle GUI
105    */
106    void GSGraphics::activate()
107    {
108        GameMode::setShowsGraphics(true);
109
110        setConfigValues();
111
112        // Load OGRE including the render window
113        this->graphicsManager_ = new GraphicsManager();
114
115        // load debug overlay
116        COUT(3) << "Loading Debug Overlay..." << std::endl;
117        this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string());
118        Loader::open(debugOverlay_);
119
120        // The render window width and height are used to set up the mouse movement.
121        size_t windowHnd = 0;
122        Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow();
123        renderWindow->getCustomAttribute("WINDOW", &windowHnd);
124
125        // Calls the InputManager which sets up the input devices.
126        inputManager_ = new InputManager(windowHnd, renderWindow->getWidth(), renderWindow->getHeight());
127
128        // load master key bindings
129        masterInputState_ = InputManager::getInstance().createInputState("master", true);
130        masterKeyBinder_ = new KeyBinder();
131        masterKeyBinder_->loadBindings("masterKeybindings.ini");
132        masterInputState_->setKeyHandler(masterKeyBinder_);
133
134        // Load the SoundManager
135        soundManager_ = new SoundManager();
136
137        // Load the InGameConsole
138        console_ = new InGameConsole();
139        console_->initialise(renderWindow->getWidth(), renderWindow->getHeight());
140
141        // load the CEGUI interface
142        guiManager_ = new GUIManager();
143        guiManager_->initialise(renderWindow);
144
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");
153    }
154
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()
162    {
163/*
164        if (this->ccToggleGUI_)
165        {
166            delete this->ccToggleGUI_;
167            this->ccToggleGUI_ = 0;
168        }
169*/
170
171        masterInputState_->setHandler(0);
172        InputManager::getInstance().requestDestroyState("master");
173        delete this->masterKeyBinder_;
174
175        delete this->guiManager_;
176        delete this->console_;
177
178        Loader::unload(this->debugOverlay_);
179        delete this->debugOverlay_;
180
181        delete this->soundManager_;
182
183        delete this->inputManager_;
184        this->inputManager_ = 0;
185
186        delete graphicsManager_;
187
188        GameMode::setShowsGraphics(false);
189    }
190
191    /**
192    @brief
193        Toggles the visibility of the current GUI
194
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()");
201    }
202
203    /**
204    @note
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    */
211    void GSGraphics::update(const Clock& time)
212    {
213        if (this->getActivity().topState)
214        {
215            // This state can not 'survive' on its own.
216            // Load a user interface therefore
217            Game::getInstance().requestState("mainMenu");
218        }
219
220        uint64_t timeBeforeTick = time.getRealMicroseconds();
221
222        this->inputManager_->update(time);
223        this->console_->update(time);
224
225        uint64_t timeAfterTick = time.getRealMicroseconds();
226
227        // Also add our tick time
228        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
229
230        // Process gui events
231        this->guiManager_->update(time);
232        // Render
233        this->graphicsManager_->update(time);
234    }
235
236    /**
237    @brief
238        Window has resized.
239    @param rw
240        The render window it occured in
241    @note
242        GraphicsManager has a render window stored itself. This is the same
243        as rw. But we have to be careful when using multiple render windows!
244    */
245    void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight)
246    {
247        // OIS needs this under linux even if we only use relative input measurement.
248        // HACK:
249        CommandExecutor::execute("setWindowExtents_s " + multi_cast<std::string>(newWidth) + " " + multi_cast<std::string>(newHeight));
250    }
251
252    /**
253    @brief
254        Window focus has changed.
255    @param rw
256        The render window it occured in
257    */
258    void GSGraphics::windowFocusChanged()
259    {
260        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
261        if (this->inputManager_)
262            this->inputManager_->clearBuffers();
263    }
264
265}
Note: See TracBrowser for help on using the repository browser.