Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1674 was 1674, checked in by rgrieder, 16 years ago
  • Wrote Clock() class: It starts an internal clock when GSRoot starts and gets handed to all GameState ticks as reference. You can then either query the ticked time or the real time (for instance for statistical measurements)
  • general clean up in all the 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 <OgreFrameListener.h>
33#include <OgreRoot.h>
34#include <OgreWindowEventUtilities.h>
35#include <OgreRenderWindow.h>
36
37#include "core/ConsoleCommand.h"
38#include "core/ConfigValueIncludes.h"
39#include "core/input/InputManager.h"
40#include "overlays/console/InGameConsole.h"
41#include "gui/GUIManager.h"
42#include "GraphicsEngine.h"
43
44namespace orxonox
45{
46    GSGraphics::GSGraphics()
47        : GameState("graphics")
48        , ogreRoot_(0)
49        , graphicsEngine_(0)
50        , inputManager_(0)
51        , console_(0)
52        , guiManager_(0)
53        , frameCount_(0)
54        , statisticsRefreshCycle_(0)
55        , statisticsStartTime_(0)
56        , statisticsStartCount_(0)
57        , tickTime_(0)
58    {
59    }
60
61    GSGraphics::~GSGraphics()
62    {
63    }
64
65    void GSGraphics::setConfigValues()
66    {
67        SetConfigValue(statisticsRefreshCycle_, 200000).description("Sets the time in microseconds interval at which average fps, etc. get updated.");
68    }
69
70    void GSGraphics::enter()
71    {
72        setConfigValues();
73
74        this->ogreRoot_ = Ogre::Root::getSingletonPtr();
75        this->graphicsEngine_ = GraphicsEngine::getInstancePtr();
76
77        graphicsEngine_->loadRenderer();    // creates the render window
78
79        // TODO: Spread this so that this call only initialises things needed for the Console and GUI
80        graphicsEngine_->initialiseResources();
81
82        // Calls the InputManager which sets up the input devices.
83        // The render window width and height are used to set up the mouse movement.
84        inputManager_ = new InputManager();
85        inputManager_->initialise(graphicsEngine_->getWindowHandle(),
86            graphicsEngine_->getWindowWidth(), graphicsEngine_->getWindowHeight(), true);
87
88        // Load the InGameConsole
89        console_ = new InGameConsole();
90        console_->initialise();
91
92        // load the CEGUI interface
93        guiManager_ = new GUIManager();
94        guiManager_->initialise();
95
96        // reset frame counter
97        this->frameCount_ = 0;
98        this->tickTime_ = 0;
99        statisticsStartTime_ = 0;
100        statisticsStartCount_ = 0;
101    }
102
103    void GSGraphics::leave()
104    {
105        delete this->guiManager_;
106
107        delete this->console_;
108
109        delete this->inputManager_;
110
111        this->ogreRoot_->detachRenderTarget(GraphicsEngine::getInstance().getRenderWindow());
112        delete GraphicsEngine::getInstance().getRenderWindow();
113        //this->ogreRoot_->shutdown
114        // TODO: destroy render window
115    }
116
117    /**
118        Main loop of the orxonox game.
119        We use the Ogre::Timer to measure time since it uses the most precise
120        method an a platform (however the windows timer lacks time when under
121        heavy kernel load!).
122        There is a simple mechanism to measure the average time spent in our
123        ticks as it may indicate performance issues.
124        A note about the Ogre::FrameListener: Even though we don't use them,
125        they still get called. However, the delta times are not correct (except
126        for timeSinceLastFrame, which is the most important). A little research
127        as shown that there is probably only one FrameListener that doesn't even
128        need the time. So we shouldn't run into problems.
129    */
130    void GSGraphics::ticked(const Clock& time)
131    {
132        uint64_t timeBeforeTick = time.getRealMicroseconds();
133        float dt = time.getDeltaTime();
134
135        this->inputManager_->tick(dt);
136        // tick console
137        this->console_->tick(dt);
138        this->tickChild(time);
139       
140        uint64_t timeAfterTick = time.getRealMicroseconds();
141
142        tickTime_ += (unsigned int)(timeAfterTick - timeBeforeTick);
143        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
144        {
145            GraphicsEngine::getInstance().setAverageTickTime(
146                (float)tickTime_ * 0.001f / (frameCount_ - statisticsStartCount_));
147            float avgFPS = (float)(frameCount_ - statisticsStartCount_)
148                / (timeAfterTick - statisticsStartTime_) * 1000000.0;
149            GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
150
151            tickTime_ = 0;
152            statisticsStartCount_ = frameCount_;
153            statisticsStartTime_  = timeAfterTick;
154        }
155
156        // don't forget to call _fireFrameStarted in ogre to make sure
157        // everything goes smoothly
158        Ogre::FrameEvent evt;
159        evt.timeSinceLastFrame = dt;
160        evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
161        ogreRoot_->_fireFrameStarted(evt);
162
163        // Pump messages in all registered RenderWindows
164        // This calls the WindowEventListener objects.
165        Ogre::WindowEventUtilities::messagePump();
166        // make sure the window stays active even when not focused
167        // (probably only necessary on windows)
168        GraphicsEngine::getInstance().setWindowActivity(true);
169
170        // render
171        ogreRoot_->_updateAllRenderTargets();
172
173        // again, just to be sure ogre works fine
174        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
175
176        ++frameCount_;
177    }
178}
Note: See TracBrowser for help on using the repository browser.