Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp3/src/orxonox/gamestates/GSGraphics.cc @ 2996

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

Fixed tick-time issue: Not all tick times were being taken into account.
As of now, only GSRoot and GSGraphics manage the tick times themselves (marked with "AddGameState(GSRoot, "root", false)")

  • Property svn:eol-style set to native
File size: 7.8 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", 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        , 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
166        masterInputState_->setHandler(0);
167        InputManager::getInstance().requestDestroyState("master");
168        delete this->masterKeyBinder_;
169
170        delete this->guiManager_;
171        delete this->console_;
172
173        Loader::unload(this->debugOverlay_);
174        delete this->debugOverlay_;
175
176        delete this->inputManager_;
177        this->inputManager_ = 0;
178
179        delete graphicsManager_;
180
181        GameMode::setShowsGraphics(false);
182    }
183
184    /**
185    @brief
186        Toggles the visibility of the current GUI
187
188        This function just executes a Lua function in the main script of the GUI by accessing the GUIManager.
189        For more details on this function check out the Lua code.
190    */
191    void GSGraphics::toggleGUI()
192    {
193            GUIManager::getInstance().executeCode("toggleGUI()");
194    }
195
196    /**
197    @note
198        A note about the Ogre::FrameListener: Even though we don't use them,
199        they still get called. However, the delta times are not correct (except
200        for timeSinceLastFrame, which is the most important). A little research
201        as shown that there is probably only one FrameListener that doesn't even
202        need the time. So we shouldn't run into problems.
203    */
204    void GSGraphics::update(const Clock& time)
205    {
206        if (this->getActivity().topState)
207        {
208            // This state can not 'survive' on its own.
209            // Load a user interface therefore
210            Game::getInstance().requestState("mainMenu");
211        }
212
213        uint64_t timeBeforeTick = time.getRealMicroseconds();
214
215        this->inputManager_->update(time);
216        this->console_->update(time);
217
218        uint64_t timeAfterTick = time.getRealMicroseconds();
219
220        // Also add our tick time
221        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
222
223        // Process gui events
224        this->guiManager_->update(time);
225        // Render
226        this->graphicsManager_->update(time);
227    }
228
229    /**
230    @brief
231        Window has resized.
232    @param rw
233        The render window it occured in
234    @note
235        GraphicsManager has a render window stored itself. This is the same
236        as rw. But we have to be careful when using multiple render windows!
237    */
238    void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight)
239    {
240        // OIS needs this under linux even if we only use relative input measurement.
241        if (this->inputManager_)
242            this->inputManager_->setWindowExtents(newWidth, newHeight);
243    }
244
245    /**
246    @brief
247        Window focus has changed.
248    @param rw
249        The render window it occured in
250    */
251    void GSGraphics::windowFocusChanged()
252    {
253        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
254        if (this->inputManager_)
255            this->inputManager_->clearBuffers();
256    }
257
258}
Note: See TracBrowser for help on using the repository browser.