Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/gamestates/GSGraphics.cc @ 3149

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

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 7.7 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 Implementation of Graphics GameState class.
32 */
33
34#include "GSGraphics.h"
35
36#include <boost/filesystem.hpp>
37#include <OgreRenderWindow.h>
38
39#include "util/Debug.h"
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 "GraphicsManager.h"
55
56namespace orxonox
57{
58    AddGameState(GSGraphics, "graphics", false);
59
60    GSGraphics::GSGraphics(const std::string& name, bool countTickTime)
61        : GameState(name, countTickTime)
62        , inputManager_(0)
63        , console_(0)
64        , guiManager_(0)
65        , graphicsManager_(0)
66        , masterKeyBinder_(0)
67        , masterInputState_(0)
68        , debugOverlay_(0)
69    {
70        RegisterRootObject(GSGraphics);
71    }
72
73    GSGraphics::~GSGraphics()
74    {
75    }
76
77    /**
78    @brief
79        this function does nothing
80
81        Indeed. Here goes nothing.
82    */
83    void GSGraphics::setConfigValues()
84    {
85    }
86
87    /**
88    @brief
89        This function is called when we enter this game state.
90
91        Since graphics is very important for our game this function does quite a lot:
92        \li starts graphics manager
93        \li loads debug overlay
94        \li manages render window
95        \li creates input manager
96        \li loads master key bindings
97        \li loads ingame console
98        \li loads GUI interface (GUIManager)
99        \li creates console command to toggle GUI
100    */
101    void GSGraphics::activate()
102    {
103        GameMode::setShowsGraphics(true);
104
105        setConfigValues();
106
107        // initialise graphics manager. Doesn't load the render window yet!
108        this->graphicsManager_ = new GraphicsManager();
109        this->graphicsManager_->initialise();
110
111        // load debug overlay
112        COUT(3) << "Loading Debug Overlay..." << std::endl;
113        this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string());
114        Loader::open(debugOverlay_);
115
116        // The render window width and height are used to set up the mouse movement.
117        size_t windowHnd = 0;
118        Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow();
119        renderWindow->getCustomAttribute("WINDOW", &windowHnd);
120
121        // Calls the InputManager which sets up the input devices.
122        inputManager_ = new InputManager();
123        inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true);
124
125        // load master key bindings
126        masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("master", true);
127        masterKeyBinder_ = new KeyBinder();
128        masterKeyBinder_->loadBindings("masterKeybindings.ini");
129        masterInputState_->setKeyHandler(masterKeyBinder_);
130
131        // Load the InGameConsole
132        console_ = new InGameConsole();
133        console_->initialise(renderWindow->getWidth(), renderWindow->getHeight());
134
135        // load the CEGUI interface
136        guiManager_ = new GUIManager();
137        guiManager_->initialise(renderWindow);
138
139        // add console command to toggle GUI
140        FunctorMember<GSGraphics>* functor = createFunctor(&GSGraphics::toggleGUI);
141        functor->setObject(this);
142        this->ccToggleGUI_ = createConsoleCommand(functor, "toggleGUI");
143        CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_);
144
145        // enable master input
146        InputManager::getInstance().requestEnterState("master");
147    }
148
149    /**
150    @brief
151        This function is called when the game state is left
152
153        Created references, input states and console commands are deleted.
154    */
155    void GSGraphics::deactivate()
156    {
157/*
158        if (this->ccToggleGUI_)
159        {
160            delete this->ccToggleGUI_;
161            this->ccToggleGUI_ = 0;
162        }
163*/
164
165        masterInputState_->setHandler(0);
166        InputManager::getInstance().requestDestroyState("master");
167        delete this->masterKeyBinder_;
168
169        delete this->guiManager_;
170        delete this->console_;
171
172        Loader::unload(this->debugOverlay_);
173        delete this->debugOverlay_;
174
175        delete this->inputManager_;
176        this->inputManager_ = 0;
177
178        delete graphicsManager_;
179
180        GameMode::setShowsGraphics(false);
181    }
182
183    /**
184    @brief
185        Toggles the visibility of the current GUI
186
187        This function just executes a Lua function in the main script of the GUI by accessing the GUIManager.
188        For more details on this function check out the Lua code.
189    */
190    void GSGraphics::toggleGUI()
191    {
192            GUIManager::getInstance().executeCode("toggleGUI()");
193    }
194
195    /**
196    @note
197        A note about the Ogre::FrameListener: Even though we don't use them,
198        they still get called. However, the delta times are not correct (except
199        for timeSinceLastFrame, which is the most important). A little research
200        as shown that there is probably only one FrameListener that doesn't even
201        need the time. So we shouldn't run into problems.
202    */
203    void GSGraphics::update(const Clock& time)
204    {
205        if (this->getActivity().topState)
206        {
207            // This state can not 'survive' on its own.
208            // Load a user interface therefore
209            Game::getInstance().requestState("mainMenu");
210        }
211
212        uint64_t timeBeforeTick = time.getRealMicroseconds();
213
214        this->inputManager_->update(time);
215        this->console_->update(time);
216
217        uint64_t timeAfterTick = time.getRealMicroseconds();
218
219        // Also add our tick time
220        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
221
222        // Process gui events
223        this->guiManager_->update(time);
224        // Render
225        this->graphicsManager_->update(time);
226    }
227
228    /**
229    @brief
230        Window has resized.
231    @param rw
232        The render window it occured in
233    @note
234        GraphicsManager has a render window stored itself. This is the same
235        as rw. But we have to be careful when using multiple render windows!
236    */
237    void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight)
238    {
239        // OIS needs this under linux even if we only use relative input measurement.
240        if (this->inputManager_)
241            this->inputManager_->setWindowExtents(newWidth, newHeight);
242    }
243
244    /**
245    @brief
246        Window focus has changed.
247    @param rw
248        The render window it occured in
249    */
250    void GSGraphics::windowFocusChanged()
251    {
252        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
253        if (this->inputManager_)
254            this->inputManager_->clearBuffers();
255    }
256
257}
Note: See TracBrowser for help on using the repository browser.