Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

small fixes with game states.

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