Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1672 was 1672, checked in by rgrieder, 16 years ago
  • Changed GameState so that the new RootGameState can override 2 virtual methods
  • added RootGameState that takes care of state transitions (can only happen between ticks)
  • moved main loop to GSRoot instead of GSGraphics
  • network GameStates not yet finished
  • GraphicsEngine not yet merged into GSGraphics
  • Property svn:eol-style set to native
File size: 5.8 KB
RevLine 
[1661]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>
[1672]36#include <OgreRenderWindow.h>
[1661]37
[1662]38#include "core/ConsoleCommand.h"
39#include "core/ConfigValueIncludes.h"
[1661]40#include "core/input/InputManager.h"
[1670]41//#include "core/Core.h"
[1661]42#include "overlays/console/InGameConsole.h"
43#include "gui/GUIManager.h"
[1662]44#include "GraphicsEngine.h"
[1661]45
46namespace orxonox
47{
48    GSGraphics::GSGraphics()
49        : GameState("graphics")
50        , debugRefreshTime_(0.0f)
51        , inputManager_(0)
52        , console_(0)
53        , guiManager_(0)
[1672]54        , frameCount_(0)
[1661]55    {
56    }
57
58    GSGraphics::~GSGraphics()
59    {
60    }
61
62    void GSGraphics::setConfigValues()
63    {
64        SetConfigValue(debugRefreshTime_, 0.2).description("Sets the time interval at which average fps, etc. get updated.");
65    }
66
67    void GSGraphics::enter()
68    {
[1672]69        this->ogreRoot_ = Ogre::Root::getSingletonPtr();
[1661]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    {
[1662]94        delete this->guiManager_;
[1661]95
[1662]96        delete this->console_;
[1661]97
[1662]98        delete this->inputManager_;
99
[1672]100        this->ogreRoot_->detachRenderTarget(GraphicsEngine::getInstance().getRenderWindow());
101        delete GraphicsEngine::getInstance().getRenderWindow();
102        this->ogreRoot_->shutdown
[1662]103        // TODO: destroy render window
[1661]104    }
105
[1662]106    /**
107        Main loop of the orxonox game.
108        We use the Ogre::Timer to measure time since it uses the most precise
109        method an a platform (however the windows timer lacks time when under
110        heavy kernel load!).
111        There is a simple mechanism to measure the average time spent in our
112        ticks as it may indicate performance issues.
113        A note about the Ogre::FrameListener: Even though we don't use them,
114        they still get called. However, the delta times are not correct (except
115        for timeSinceLastFrame, which is the most important). A little research
116        as shown that there is probably only one FrameListener that doesn't even
117        need the time. So we shouldn't run into problems.
118    */
[1672]119    void GSGraphics::ticked(float dt, uint64_t time)
[1661]120    {
[1672]121        this->inputManager_->tick(dt);
[1662]122
[1672]123        this->tickChild(dt, time);
[1661]124
[1672]125        // tick console
126        this->console_->tick(dt);
[1661]127
[1672]128        //// get current time once again
129        //timeAfterTick = timer_->getMicroseconds();
[1661]130
[1672]131        //tickTime += timeAfterTick - timeBeforeTick;
132        //if (timeAfterTick > refreshStartTime + refreshTime)
133        //{
134        //    GraphicsEngine::getInstance().setAverageTickTime(
135        //        (float)tickTime * 0.001 / (frameCount - oldFrameCount));
136        //    float avgFPS = (float)(frameCount - oldFrameCount) / (timeAfterTick - refreshStartTime) * 1000000.0;
137        //    GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
[1661]138
[1672]139        //    oldFrameCount = frameCount;
140        //    tickTime = 0;
141        //    refreshStartTime = timeAfterTick;
142        //}
[1661]143
[1672]144        // don't forget to call _fireFrameStarted in ogre to make sure
145        // everything goes smoothly
146        Ogre::FrameEvent evt;
147        evt.timeSinceLastFrame = dt;
148        evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
149        ogreRoot_->_fireFrameStarted(evt);
[1661]150
[1672]151        // Pump messages in all registered RenderWindows
152        // This calls the WindowEventListener objects.
153        Ogre::WindowEventUtilities::messagePump();
154        // make sure the window stays active even when not focused
155        // (probably only necessary on windows)
156        GraphicsEngine::getInstance().setWindowActivity(true);
[1661]157
[1672]158        // render
159        ogreRoot_->_updateAllRenderTargets();
[1661]160
[1672]161        // again, just to be sure ogre works fine
162        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
[1661]163
[1672]164        ++frameCount_;
[1662]165
[1672]166        //}
167        //catch (std::exception& ex)
168        //{
169        //    // something went wrong.
170        //    COUT(1) << ex.what() << std::endl;
171        //    COUT(1) << "Main loop was stopped by an unhandled exception. Shutting down." << std::endl;
172        //}
[1661]173    }
174}
Note: See TracBrowser for help on using the repository browser.