Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1661 was 1661, checked in by rgrieder, 16 years ago

started implementing the GameStates. Not much for now, but most of the Orxonox.cc code has been copied to GSRoot, GSGraphics and GSLevel.
There is no level currently, but the main menu is shown. This is more of an svn save because I would really like to have Member ConsoleCommands.

  • Property svn:eol-style set to native
File size: 6.4 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 <OgreFrameListener.h>
33#include <OgreRoot.h>
34#include <OgreTimer.h>
35#include <OgreWindowEventUtilities.h>
36
37#include "core/input/InputManager.h"
38#include "core/input/SimpleInputState.h"
39#include "core/input/KeyBinder.h"
40#include "core/Core.h"
41
42#include "GraphicsEngine.h"
43#include "overlays/console/InGameConsole.h"
44#include "gui/GUIManager.h"
45
46namespace orxonox
47{
48    GSGraphics::GSGraphics()
49        : GameState("graphics")
50        , timer_(0)
51        , bAbort_(false)
52        , debugRefreshTime_(0.0f)
53        , inputManager_(0)
54        , console_(0)
55        , guiManager_(0)
56    {
57    }
58
59    GSGraphics::~GSGraphics()
60    {
61    }
62
63    void GSGraphics::setConfigValues()
64    {
65        SetConfigValue(debugRefreshTime_, 0.2).description("Sets the time interval at which average fps, etc. get updated.");
66    }
67
68    void GSGraphics::enter()
69    {
70        this->graphicsEngine_ = GraphicsEngine::getInstancePtr();
71
72        graphicsEngine_->loadRenderer();    // creates the render window
73
74        // TODO: Spread this so that this call only initialises things needed for the Console and GUI
75        graphicsEngine_->initialiseResources();
76
77        // Calls the InputManager which sets up the input devices.
78        // The render window width and height are used to set up the mouse movement.
79        inputManager_ = new InputManager();
80        inputManager_->initialise(graphicsEngine_->getWindowHandle(),
81            graphicsEngine_->getWindowWidth(), graphicsEngine_->getWindowHeight(), true);
82
83        // Load the InGameConsole
84        console_ = new InGameConsole();
85        console_->initialise();
86
87        // load the CEGUI interface
88        guiManager_ = new GUIManager();
89        guiManager_->initialise();
90    }
91
92    void GSGraphics::leave()
93    {
94        if (this->guiManager_)
95          delete guiManager_;
96
97        if (this->console_)
98          delete this->console_;
99
100        if (inputManager_)
101          delete inputManager_;
102
103        // TODO: deintialise graphics
104    }
105
106    bool GSGraphics::tick(float dt)
107    {
108        Ogre::Root& ogreRoot = Ogre::Root::getSingleton();
109
110        // use the ogre timer class to measure time.
111        if (!timer_)
112            timer_ = new Ogre::Timer();
113
114        unsigned long frameCount = 0;
115
116        const unsigned long refreshTime = (unsigned long)(debugRefreshTime_ * 1000000.0f);
117        unsigned long refreshStartTime = 0;
118        unsigned long tickTime = 0;
119        unsigned long oldFrameCount = 0;
120
121        unsigned long timeBeforeTick = 0;
122        unsigned long timeBeforeTickOld = 0;
123        unsigned long timeAfterTick = 0;
124
125        // TODO: Update time in seconds every 7 seconds to avoid any overflow (7 secs is very tight)
126
127        COUT(3) << "Orxonox: Starting the main loop." << std::endl;
128
129        try
130        {
131            timer_->reset();
132            while (!bAbort_)
133            {
134                // get current time
135                timeBeforeTickOld = timeBeforeTick;
136                timeBeforeTick    = timer_->getMicroseconds();
137                float dt = (timeBeforeTick - timeBeforeTickOld) / 1000000.0;
138
139
140                // tick the core (needs real time for input and tcl thread management)
141                // TODO: ticks of InputManager and tcl thread manager have to be separated.
142                Core::tick(dt);
143
144                // tick child state
145                if (this->getActiveChild())
146                    this->getActiveChild()->tick(dt);
147
148                // get current time once again
149                timeAfterTick = timer_->getMicroseconds();
150
151                tickTime += timeAfterTick - timeBeforeTick;
152                if (timeAfterTick > refreshStartTime + refreshTime)
153                {
154                    GraphicsEngine::getInstance().setAverageTickTime(
155                        (float)tickTime * 0.001 / (frameCount - oldFrameCount));
156                    float avgFPS = (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0;
157                    GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
158
159                    oldFrameCount = frameCount;
160                    tickTime = 0;
161                    refreshStartTime = timeAfterTick;
162                }
163
164                // don't forget to call _fireFrameStarted in ogre to make sure
165                // everything goes smoothly
166                Ogre::FrameEvent evt;
167                evt.timeSinceLastFrame = dt;
168                evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
169                ogreRoot._fireFrameStarted(evt);
170
171                // Pump messages in all registered RenderWindows
172                // This calls the WindowEventListener objects.
173                Ogre::WindowEventUtilities::messagePump();
174                // make sure the window stays active even when not focused
175                // (probably only necessary on windows)
176                GraphicsEngine::getInstance().setWindowActivity(true);
177
178                // render
179                ogreRoot._updateAllRenderTargets();
180
181                // again, just to be sure ogre works fine
182                ogreRoot._fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
183
184                ++frameCount;
185            }
186        }
187        catch (std::exception& ex)
188        {
189            // something went wrong.
190            COUT(1) << ex.what() << std::endl;
191            COUT(1) << "Main loop was stopped by an unhandled exception. Shutting down." << std::endl;
192        }        return true;
193    }
194}
Note: See TracBrowser for help on using the repository browser.