Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/orxonox/gamestates/GSGraphics.cc @ 2869

Last change on this file since 2869 was 2869, checked in by bknecht, 15 years ago

Including some features to use only mouse or keys in a GUI, also cleaning up the calls to GUIManager, also trying to show and hide a GUI just by tabbing a key. Anyways, more features to come soonsvn status

  • Property svn:eol-style set to native
File size: 6.6 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 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "GSGraphics.h"
31
32#include <boost/filesystem.hpp>
33#include <OgreRenderWindow.h>
34
35#include "util/Debug.h"
36#include "core/ConfigValueIncludes.h"
37#include "core/Clock.h"
38#include "core/ConsoleCommand.h"
39#include "core/Core.h"
40#include "core/CoreIncludes.h"
41#include "core/Game.h"
42#include "core/GameMode.h"
43#include "core/input/InputManager.h"
44#include "core/input/KeyBinder.h"
45#include "core/input/SimpleInputState.h"
46#include "core/Loader.h"
47#include "core/XMLFile.h"
48#include "overlays/console/InGameConsole.h"
49#include "gui/GUIManager.h"
50#include "GraphicsManager.h"
51
52namespace orxonox
53{
54    AddGameState(GSGraphics, "graphics");
55
56    GSGraphics::GSGraphics(const std::string& name)
57        : GameState(name)
58        , inputManager_(0)
59        , console_(0)
60        , guiManager_(0)
61        , graphicsManager_(0)
62        , masterKeyBinder_(0)
63        , masterInputState_(0)
64        , debugOverlay_(0)
65    {
66        RegisterRootObject(GSGraphics);
67    }
68
69    GSGraphics::~GSGraphics()
70    {
71    }
72
73    void GSGraphics::setConfigValues()
74    {
75    }
76
77    void GSGraphics::activate()
78    {
79        GameMode::setShowsGraphics(true);
80
81        setConfigValues();
82
83        // initialise graphics manager. Doesn't load the render window yet!
84        this->graphicsManager_ = new GraphicsManager();
85        this->graphicsManager_->initialise();
86
87        // load debug overlay
88        COUT(3) << "Loading Debug Overlay..." << std::endl;
89        this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string());
90        Loader::open(debugOverlay_);
91
92        // Calls the InputManager which sets up the input devices.
93        // The render window width and height are used to set up the mouse movement.
94        inputManager_ = new InputManager();
95        size_t windowHnd = 0;
96        Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow();
97        renderWindow->getCustomAttribute("WINDOW", &windowHnd);
98        inputManager_->initialise(windowHnd, renderWindow->getWidth(), renderWindow->getHeight(), true);
99
100        masterInputState_ = InputManager::getInstance().createInputState<SimpleInputState>("master", true);
101        masterKeyBinder_ = new KeyBinder();
102        masterKeyBinder_->loadBindings("masterKeybindings.ini");
103        masterInputState_->setKeyHandler(masterKeyBinder_);
104
105        // Load the InGameConsole
106        console_ = new InGameConsole();
107        console_->initialise(renderWindow->getWidth(), renderWindow->getHeight());
108
109        // load the CEGUI interface
110        guiManager_ = new GUIManager();
111        guiManager_->initialise(renderWindow);
112
113        FunctorMember<GSGraphics>* functor = createFunctor(&GSGraphics::toggleGUI);
114        functor->setObject(this);
115        this->ccToggleGUI_ = createConsoleCommand(functor, "toggleGUI");
116        CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_);
117
118
119        InputManager::getInstance().requestEnterState("master");
120    }
121
122    void GSGraphics::deactivate()
123    {
124
125        if (this->ccToggleGUI_)
126        {
127            delete this->ccToggleGUI_;
128            this->ccToggleGUI_ = 0;
129        }
130
131        masterInputState_->setHandler(0);
132        InputManager::getInstance().requestDestroyState("master");
133        delete this->masterKeyBinder_;
134
135        delete this->guiManager_;
136        delete this->console_;
137
138        Loader::unload(this->debugOverlay_);
139        delete this->debugOverlay_;
140
141        delete this->inputManager_;
142        this->inputManager_ = 0;
143
144        delete graphicsManager_;
145
146        GameMode::setShowsGraphics(false);
147    }
148
149    void GSGraphics::toggleGUI()
150    {
151            GUIManager::getInstance().executeCode("toggleGUI()");
152    }
153
154    /**
155    @note
156        A note about the Ogre::FrameListener: Even though we don't use them,
157        they still get called. However, the delta times are not correct (except
158        for timeSinceLastFrame, which is the most important). A little research
159        as shown that there is probably only one FrameListener that doesn't even
160        need the time. So we shouldn't run into problems.
161    */
162    void GSGraphics::update(const Clock& time)
163    {
164        if (this->getActivity().topState)
165        {
166            // This state can not 'survive' on its own.
167            // Load a user interface therefore
168            Game::getInstance().requestState("mainMenu");
169        }
170
171        uint64_t timeBeforeTick = time.getRealMicroseconds();
172
173        this->inputManager_->update(time);        // tick console
174        this->console_->update(time);
175        this->guiManager_->update(time);
176
177        uint64_t timeAfterTick = time.getRealMicroseconds();
178
179        // Also add our tick time
180        Game::getInstance().addTickTime(timeAfterTick - timeBeforeTick);
181
182        // Render
183        this->graphicsManager_->update(time);
184    }
185
186    /**
187    @brief
188        Window has resized.
189    @param rw
190        The render window it occured in
191    @note
192        GraphicsManager has a render window stored itself. This is the same
193        as rw. But we have to be careful when using multiple render windows!
194    */
195    void GSGraphics::windowResized(unsigned int newWidth, unsigned int newHeight)
196    {
197        // OIS needs this under linux even if we only use relative input measurement.
198        if (this->inputManager_)
199            this->inputManager_->setWindowExtents(newWidth, newHeight);
200    }
201
202    /**
203    @brief
204        Window focus has changed.
205    @param rw
206        The render window it occured in
207    */
208    void GSGraphics::windowFocusChanged()
209    {
210        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
211        if (this->inputManager_)
212            this->inputManager_->clearBuffers();
213    }
214
215}
Note: See TracBrowser for help on using the repository browser.