Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSGraphics.cc @ 3201

Last change on this file since 3201 was 3196, checked in by rgrieder, 16 years ago

Merged pch branch back to trunk.

  • 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 "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
57namespace orxonox
58{
59    AddGameState(GSGraphics, "graphics", false);
60
61    GSGraphics::GSGraphics(const std::string& name, bool countTickTime)
62        : GameState(name, countTickTime)
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        // initialise graphics manager. Doesn't load the render window yet!
111        this->graphicsManager_ = new GraphicsManager();
112        this->graphicsManager_->initialise();
113
114        // load debug overlay
115        COUT(3) << "Loading Debug Overlay..." << std::endl;
116        this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string());
117        Loader::open(debugOverlay_);
118
119        // The render window width and height are used to set up the mouse movement.
120        size_t windowHnd = 0;
121        Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow();
122        renderWindow->getCustomAttribute("WINDOW", &windowHnd);
123
124        // Calls the InputManager which sets up the input devices.
125        inputManager_ = new InputManager();
126        inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true);
127
128        // load master key bindings
129        masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("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        if (this->inputManager_)
249            this->inputManager_->setWindowExtents(newWidth, 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.