Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/orxonox/gamestates/GSGraphics.cc @ 3346

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

Moved GraphicsManager and GUIManager to the core. Almost no actual code changes though, just moving (here was that Map-hack I had to move to GSGraphics).

  • Property svn:eol-style set to native
File size: 6.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
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 "util/Convert.h"
41#include "core/Clock.h"
42#include "core/CommandExecutor.h"
43#include "core/ConsoleCommand.h"
44#include "core/Core.h"
45#include "core/Game.h"
46#include "core/GameMode.h"
47#include "core/GraphicsManager.h"
48#include "core/GUIManager.h"
49#include "core/input/InputManager.h"
50#include "core/input/KeyBinder.h"
51#include "core/input/InputState.h"
52#include "core/Loader.h"
53#include "core/XMLFile.h"
54#include "overlays/console/InGameConsole.h"
55#include "sound/SoundManager.h"
56
57// HACK:
58#include "overlays/map/Map.h"
59
60namespace orxonox
61{
62    DeclareGameState(GSGraphics, "graphics", true, true);
63
64    GSGraphics::GSGraphics(const GameStateConstrParams& params)
65        : GameState(params)
66        , inputManager_(0)
67        , console_(0)
68        , guiManager_(0)
69        , graphicsManager_(0)
70        , soundManager_(0)
71        , masterKeyBinder_(0)
72        , masterInputState_(0)
73        , debugOverlay_(0)
74    {
75    }
76
77    GSGraphics::~GSGraphics()
78    {
79    }
80
81    /**
82    @brief
83        This function is called when we enter this game state.
84
85        Since graphics is very important for our game this function does quite a lot:
86        \li starts graphics manager
87        \li loads debug overlay
88        \li manages render window
89        \li creates input manager
90        \li loads master key bindings
91        \li loads the SoundManager
92        \li loads ingame console
93        \li loads GUI interface (GUIManager)
94        \li creates console command to toggle GUI
95    */
96    void GSGraphics::activate()
97    {
98        // Load OGRE, CEGUI and OIS
99        Core::getInstance().loadGraphics();
100
101        // Load OGRE including the render window
102        this->graphicsManager_ = new GraphicsManager();
103
104        // load debug overlay
105        COUT(3) << "Loading Debug Overlay..." << std::endl;
106        this->debugOverlay_ = new XMLFile((Core::getMediaPath() / "overlay" / "debug.oxo").string());
107        Loader::open(debugOverlay_);
108
109        // The render window width and height are used to set up the mouse movement.
110        size_t windowHnd = 0;
111        Ogre::RenderWindow* renderWindow = GraphicsManager::getInstance().getRenderWindow();
112        renderWindow->getCustomAttribute("WINDOW", &windowHnd);
113
114        // Calls the InputManager which sets up the input devices.
115        inputManager_ = new InputManager(windowHnd);
116
117        // load master key bindings
118        masterInputState_ = InputManager::getInstance().createInputState("master", true);
119        masterKeyBinder_ = new KeyBinder();
120        masterKeyBinder_->loadBindings("masterKeybindings.ini");
121        masterInputState_->setKeyHandler(masterKeyBinder_);
122
123        // Load the SoundManager
124        soundManager_ = new SoundManager();
125
126        // Load the InGameConsole
127        console_ = new InGameConsole();
128        console_->initialise();
129
130        // load the CEGUI interface
131        guiManager_ = new GUIManager(renderWindow);
132
133        // add console command to toggle GUI
134        FunctorMember<GSGraphics>* functor = createFunctor(&GSGraphics::toggleGUI);
135        functor->setObject(this);
136        this->ccToggleGUI_ = createConsoleCommand(functor, "toggleGUI");
137        CommandExecutor::addConsoleCommandShortcut(this->ccToggleGUI_);
138
139        // enable master input
140        InputManager::getInstance().enterState("master");
141    }
142
143    /**
144    @brief
145        This function is called when the game state is left
146
147        Created references, input states and console commands are deleted.
148    */
149    void GSGraphics::deactivate()
150    {
151/*
152        if (this->ccToggleGUI_)
153        {
154            delete this->ccToggleGUI_;
155            this->ccToggleGUI_ = 0;
156        }
157*/
158
159        masterInputState_->setHandler(0);
160        InputManager::getInstance().destroyState("master");
161        delete this->masterKeyBinder_;
162
163        delete this->guiManager_;
164        delete this->console_;
165
166        Loader::unload(this->debugOverlay_);
167        delete this->debugOverlay_;
168
169        delete this->soundManager_;
170
171        delete this->inputManager_;
172        this->inputManager_ = 0;
173
174        // HACK:
175        Map::hackDestroyMap();
176
177        delete graphicsManager_;
178
179        // Unload OGRE, CEGUI and OIS
180        Core::getInstance().loadGraphics();
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}
Note: See TracBrowser for help on using the repository browser.