Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2913 was 2896, checked in by landauf, 16 years ago

Merged gui branch back to trunk.

I did 2 small changes in IngameManager.cc on line 777 and 888 (yes, really), because const_reverse_iterator strangely doesn't work on MinGW.

  • 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 "OrxonoxStableHeaders.h"
35#include "GSGraphics.h"
36
37#include <boost/filesystem.hpp>
38#include <OgreRenderWindow.h>
39
40#include "util/Debug.h"
41#include "core/ConfigValueIncludes.h"
42#include "core/Clock.h"
43#include "core/ConsoleCommand.h"
44#include "core/Core.h"
45#include "core/CoreIncludes.h"
46#include "core/Game.h"
47#include "core/GameMode.h"
48#include "core/input/InputManager.h"
49#include "core/input/KeyBinder.h"
50#include "core/input/SimpleInputState.h"
51#include "core/Loader.h"
52#include "core/XMLFile.h"
53#include "overlays/console/InGameConsole.h"
54#include "gui/GUIManager.h"
55#include "GraphicsManager.h"
56
57namespace orxonox
58{
59    AddGameState(GSGraphics, "graphics");
60
61    GSGraphics::GSGraphics(const std::string& name)
62        : GameState(name)
63        , inputManager_(0)
64        , console_(0)
65        , guiManager_(0)
66        , graphicsManager_(0)
67        , masterKeyBinder_(0)
68        , masterInputState_(0)
69        , debugOverlay_(0)
70    {
71        RegisterRootObject(GSGraphics);
72    }
73
74    GSGraphics::~GSGraphics()
75    {
76    }
77
78    /**
79    @brief
80        this function does nothing
81
82        Indeed. Here goes nothing.
83    */
84    void GSGraphics::setConfigValues()
85    {
86    }
87
88    /**
89    @brief
90        This function is called when we enter this game state.
91
92        Since graphics is very important for our game this function does quite a lot:
93        \li starts graphics manager
94        \li loads debug overlay
95        \li manages render window
96        \li creates input manager
97        \li loads master key bindings
98        \li loads ingame console
99        \li loads GUI interface (GUIManager)
100        \li creates console command to toggle GUI
101    */
102    void GSGraphics::activate()
103    {
104        GameMode::setShowsGraphics(true);
105
106        setConfigValues();
107
108        // initialise graphics manager. Doesn't load the render window yet!
109        this->graphicsManager_ = new GraphicsManager();
110        this->graphicsManager_->initialise();
111
112        // load debug overlay
113        COUT(3) << "Loading Debug Overlay..." << std::endl;
114        this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string());
115        Loader::open(debugOverlay_);
116
117        // The render window width and height are used to set up the mouse movement.
118        size_t windowHnd = 0;
119        Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow();
120        renderWindow->getCustomAttribute("WINDOW", &windowHnd);
121
122        // Calls the InputManager which sets up the input devices.
123        inputManager_ = new InputManager();
124        inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true);
125
126        // load master key bindings
127        masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("master", true);
128        masterKeyBinder_ = new KeyBinder();
129        masterKeyBinder_->loadBindings("masterKeybindings.ini");
130        masterInputState_->setKeyHandler(masterKeyBinder_);
131
132        // Load the InGameConsole
133        console_ = new InGameConsole();
134        console_->initialise(renderWindow->getWidth(), renderWindow->getHeight());
135
136        // load the CEGUI interface
137        guiManager_ = new GUIManager();
138        guiManager_->initialise(renderWindow);
139
140        // add console command to toggle GUI
141        FunctorMember<GSGraphics>* functor = createFunctor(&GSGraphics::toggleGUI);
142        functor->setObject(this);
143        this->ccToggleGUI_ = createConsoleCommand(functor, "toggleGUI");
144        CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_);
145
146        // enable master input
147        InputManager::getInstance().requestEnterState("master");
148    }
149
150    /**
151    @brief
152        This function is called when the game state is left
153
154        Created references, input states and console commands are deleted.
155    */
156    void GSGraphics::deactivate()
157    {
158
159        if (this->ccToggleGUI_)
160        {
161            delete this->ccToggleGUI_;
162            this->ccToggleGUI_ = 0;
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);        // tick console
215        this->console_->update(time);
216        this->guiManager_->update(time);
217
218        uint64_t timeAfterTick = time.getRealMicroseconds();
219
220        // Also add our tick time
221        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
222
223        // Render
224        this->graphicsManager_->update(time);
225    }
226
227    /**
228    @brief
229        Window has resized.
230    @param rw
231        The render window it occured in
232    @note
233        GraphicsManager has a render window stored itself. This is the same
234        as rw. But we have to be careful when using multiple render windows!
235    */
236    void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight)
237    {
238        // OIS needs this under linux even if we only use relative input measurement.
239        if (this->inputManager_)
240            this->inputManager_->setWindowExtents(newWidth, newHeight);
241    }
242
243    /**
244    @brief
245        Window focus has changed.
246    @param rw
247        The render window it occured in
248    */
249    void GSGraphics::windowFocusChanged()
250    {
251        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
252        if (this->inputManager_)
253            this->inputManager_->clearBuffers();
254    }
255
256}
Note: See TracBrowser for help on using the repository browser.