Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/gamestates/GSGraphics.cc @ 1968

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

Moved all Ogre related code from GSRoot to GSGraphics.
You should now be able to start the gui, goto ioConsole, then start the gui again and load the level.
gui —> level —> gui doesn't yet work. But I will not dig into that until our object hierarchy has been replaced.

  • Property svn:eol-style set to native
File size: 18.7 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
[1686]32#include <fstream>
33#include <OgreConfigFile.h>
[1661]34#include <OgreFrameListener.h>
35#include <OgreRoot.h>
[1891]36#include <OgreLogManager.h>
[1686]37#include <OgreException.h>
38#include <OgreRenderWindow.h>
[1828]39#include <OgreRenderSystem.h>
[1686]40#include <OgreTextureManager.h>
41#include <OgreViewport.h>
[1661]42#include <OgreWindowEventUtilities.h>
43
[1755]44#include "util/Debug.h"
[1764]45#include "util/Exception.h"
[1662]46#include "core/ConsoleCommand.h"
47#include "core/ConfigValueIncludes.h"
[1686]48#include "core/CoreIncludes.h"
[1661]49#include "core/input/InputManager.h"
[1788]50#include "core/input/KeyBinder.h"
51#include "core/input/ExtendedInputState.h"
[1661]52#include "overlays/console/InGameConsole.h"
53#include "gui/GUIManager.h"
[1686]54#include "tools/WindowEventListener.h"
55#include "Settings.h"
56
57// for compatibility
[1662]58#include "GraphicsEngine.h"
[1661]59
60namespace orxonox
61{
62    GSGraphics::GSGraphics()
[1689]63        : GameState<GSRoot>("graphics")
[1891]64        , renderWindow_(0)
65        , viewport_(0)
[1661]66        , inputManager_(0)
67        , console_(0)
68        , guiManager_(0)
[1891]69        , ogreRoot_(0)
70        , ogreLogger_(0)
71        , graphicsEngine_(0)
[1788]72        , masterKeyBinder_(0)
[1672]73        , frameCount_(0)
[1674]74        , statisticsRefreshCycle_(0)
75        , statisticsStartTime_(0)
76        , statisticsStartCount_(0)
77        , tickTime_(0)
[1661]78    {
[1686]79        RegisterRootObject(GSGraphics);
[1891]80        setConfigValues();
[1661]81    }
82
83    GSGraphics::~GSGraphics()
84    {
85    }
86
87    void GSGraphics::setConfigValues()
88    {
[1688]89        SetConfigValue(resourceFile_, "resources.cfg").description("Location of the resources file in the data path.");
[1891]90        SetConfigValue(ogreConfigFile_,  "ogre.cfg").description("Location of the Ogre config file");
91        SetConfigValue(ogrePluginsFile_, "plugins.cfg").description("Location of the Ogre plugins file");
92        SetConfigValue(ogreLogFile_,     "ogre.log").description("Logfile for messages from Ogre. \
93                                                                 Use \"\" to suppress log file creation.");
94        SetConfigValue(ogreLogLevelTrivial_ , 5).description("Corresponding orxonox debug level for ogre Trivial");
95        SetConfigValue(ogreLogLevelNormal_  , 4).description("Corresponding orxonox debug level for ogre Normal");
96        SetConfigValue(ogreLogLevelCritical_, 2).description("Corresponding orxonox debug level for ogre Critical");
97        SetConfigValue(statisticsRefreshCycle_, 200000).description("Sets the time in microseconds interval at \
98                                                                    which average fps, etc. get updated.");
[1661]99    }
100
101    void GSGraphics::enter()
102    {
[1696]103        Settings::_getInstance().bShowsGraphics_ = true;
104
[1891]105        // initialise graphics engine. Doesn't load the render window yet!
106        graphicsEngine_ = new GraphicsEngine();
[1674]107
[1891]108        // Ogre setup procedure
109        setupOgre();
[1686]110        this->declareResources();
111        this->loadRenderer();    // creates the render window
[1661]112        // TODO: Spread this so that this call only initialises things needed for the Console and GUI
[1686]113        this->initialiseResources();
[1661]114
[1686]115
116        // HACK: temporary:
[1891]117        graphicsEngine_->renderWindow_  = this->renderWindow_;
118        graphicsEngine_->root_          = this->ogreRoot_;
119        graphicsEngine_->viewport_      = this->viewport_;
[1686]120
121
[1661]122        // Calls the InputManager which sets up the input devices.
123        // The render window width and height are used to set up the mouse movement.
124        inputManager_ = new InputManager();
[1686]125        size_t windowHnd = 0;
126        this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
127        inputManager_->initialise(windowHnd, renderWindow_->getWidth(), renderWindow_->getHeight(), true);
[1788]128        // Configure master input state with a KeyBinder
129        //masterKeyBinder_ = new KeyBinder();
[1887]130        //masterKeyBinder_->loadBindings("master_keybindings.ini");
[1788]131        //inputManager_->getMasterInputState()->addKeyHandler(masterKeyBinder_);
[1661]132
133        // Load the InGameConsole
134        console_ = new InGameConsole();
135        console_->initialise();
136
137        // load the CEGUI interface
138        guiManager_ = new GUIManager();
[1686]139        guiManager_->initialise(this->renderWindow_);
[1674]140
141        // reset frame counter
142        this->frameCount_ = 0;
143        this->tickTime_ = 0;
144        statisticsStartTime_ = 0;
145        statisticsStartCount_ = 0;
[1686]146
147        // add console commands
148        FunctorMember<GSGraphics>* functor1 = createFunctor(&GSGraphics::printScreen);
149        functor1->setObject(this);
150        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "printScreen"));
[1661]151    }
152
153    void GSGraphics::leave()
154    {
[1824]155        using namespace Ogre;
156
[1891]157        // remove our WindowEventListener first to avoid bad calls after the window has been destroyed
[1878]158        Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this);
159
[1662]160        delete this->guiManager_;
[1661]161
[1662]162        delete this->console_;
[1661]163
[1788]164        //inputManager_->getMasterInputState()->removeKeyHandler(this->masterKeyBinder_);
165        //delete this->masterKeyBinder_;
[1662]166        delete this->inputManager_;
167
[1824]168        // destroy render window
169        RenderSystem* renderer = this->ogreRoot_->getRenderSystem();
170        renderer->destroyRenderWindow("Orxonox");
[1696]171
[1891]172        /*** CODE SNIPPET, UNUSED ***/
[1824]173        // Does the opposite of initialise()
[1891]174        //ogreRoot_->shutdown();
[1824]175        // Remove all resources and resource groups
[1825]176        //StringVector groups = ResourceGroupManager::getSingleton().getResourceGroups();
177        //for (StringVector::iterator it = groups.begin(); it != groups.end(); ++it)
178        //{
179        //    ResourceGroupManager::getSingleton().destroyResourceGroup(*it);
180        //}
[1824]181
[1825]182        //ParticleSystemManager::getSingleton().removeAllTemplates();
[1824]183
184        // Shutdown the render system
[1825]185        //this->ogreRoot_->setRenderSystem(0);
[1824]186
[1891]187        delete this->ogreRoot_;
188
189#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
190        // delete the ogre log and the logManager (since we have created it).
191        this->ogreLogger_->getDefaultLog()->removeListener(this);
192        this->ogreLogger_->destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
193        delete this->ogreLogger_;
194#endif
195
196        delete graphicsEngine_;
197
[1696]198        Settings::_getInstance().bShowsGraphics_ = false;
[1661]199    }
200
[1662]201    /**
202        Main loop of the orxonox game.
203        We use the Ogre::Timer to measure time since it uses the most precise
204        method an a platform (however the windows timer lacks time when under
205        heavy kernel load!).
206        There is a simple mechanism to measure the average time spent in our
207        ticks as it may indicate performance issues.
208        A note about the Ogre::FrameListener: Even though we don't use them,
209        they still get called. However, the delta times are not correct (except
210        for timeSinceLastFrame, which is the most important). A little research
211        as shown that there is probably only one FrameListener that doesn't even
212        need the time. So we shouldn't run into problems.
213    */
[1674]214    void GSGraphics::ticked(const Clock& time)
[1661]215    {
[1724]216        unsigned long long timeBeforeTick = time.getRealMicroseconds();
[1674]217        float dt = time.getDeltaTime();
218
[1672]219        this->inputManager_->tick(dt);
220        // tick console
221        this->console_->tick(dt);
[1674]222        this->tickChild(time);
223       
[1724]224        unsigned long long timeAfterTick = time.getRealMicroseconds();
[1661]225
[1674]226        tickTime_ += (unsigned int)(timeAfterTick - timeBeforeTick);
227        if (timeAfterTick > statisticsStartTime_ + statisticsRefreshCycle_)
228        {
229            GraphicsEngine::getInstance().setAverageTickTime(
230                (float)tickTime_ * 0.001f / (frameCount_ - statisticsStartCount_));
231            float avgFPS = (float)(frameCount_ - statisticsStartCount_)
232                / (timeAfterTick - statisticsStartTime_) * 1000000.0;
233            GraphicsEngine::getInstance().setAverageFramesPerSecond(avgFPS);
[1661]234
[1674]235            tickTime_ = 0;
236            statisticsStartCount_ = frameCount_;
237            statisticsStartTime_  = timeAfterTick;
238        }
[1661]239
[1672]240        // don't forget to call _fireFrameStarted in ogre to make sure
241        // everything goes smoothly
242        Ogre::FrameEvent evt;
243        evt.timeSinceLastFrame = dt;
244        evt.timeSinceLastEvent = dt; // note: same time, but shouldn't matter anyway
245        ogreRoot_->_fireFrameStarted(evt);
[1661]246
[1672]247        // Pump messages in all registered RenderWindows
248        // This calls the WindowEventListener objects.
249        Ogre::WindowEventUtilities::messagePump();
250        // make sure the window stays active even when not focused
251        // (probably only necessary on windows)
[1686]252        this->renderWindow_->setActive(true);
[1661]253
[1672]254        // render
255        ogreRoot_->_updateAllRenderTargets();
[1661]256
[1672]257        // again, just to be sure ogre works fine
258        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
[1661]259
[1672]260        ++frameCount_;
[1661]261    }
[1686]262
[1891]263    /**
264    @brief
265        Creates the Ogre Root object and sets up the ogre log.
266    */
267    void GSGraphics::setupOgre()
268    {
269        COUT(3) << "Setting up Ogre..." << std::endl;
270
271        // TODO: LogManager doesn't work on oli platform. The why is yet unknown.
272#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
273        // create a new logManager
274        ogreLogger_ = new Ogre::LogManager();
275        COUT(4) << "Ogre LogManager created" << std::endl;
276
277        // create our own log that we can listen to
278        Ogre::Log *myLog;
279        if (this->ogreLogFile_ == "")
280            myLog = ogreLogger_->createLog("ogre.log", true, false, true);
281        else
282            myLog = ogreLogger_->createLog(this->ogreLogFile_, true, false, false);
283        COUT(4) << "Ogre Log created" << std::endl;
284
285        myLog->setLogDetail(Ogre::LL_BOREME);
286        myLog->addListener(this);
287#endif
288
289        // Root will detect that we've already created a Log
290        COUT(4) << "Creating Ogre Root..." << std::endl;
291
292        if (ogrePluginsFile_ == "")
293        {
294            COUT(2) << "Warning: Ogre plugins file set to \"\". Defaulting to plugins.cfg" << std::endl;
295            ModifyConfigValue(ogrePluginsFile_, tset, "plugins.cfg");
296        }
297        if (ogreConfigFile_ == "")
298        {
299            COUT(2) << "Warning: Ogre config file set to \"\". Defaulting to config.cfg" << std::endl;
300            ModifyConfigValue(ogreConfigFile_, tset, "config.cfg");
301        }
302        if (ogreLogFile_ == "")
303        {
304            COUT(2) << "Warning: Ogre log file set to \"\". Defaulting to ogre.log" << std::endl;
305            ModifyConfigValue(ogreLogFile_, tset, "ogre.log");
306        }
307
308        // check for config file existence because Ogre displays (caught) exceptions if not
309        std::ifstream probe;
310        probe.open(ogreConfigFile_.c_str());
311        if (!probe)
312        {
313            // create a zero sized file
314            std::ofstream creator;
315            creator.open(ogreConfigFile_.c_str());
316            creator.close();
317        }
318        else
319            probe.close();
320
321        ogreRoot_ = new Ogre::Root(ogrePluginsFile_, ogreConfigFile_, ogreLogFile_);
322
323#if 0 // Ogre 1.4.3 doesn't yet support setDebugOutputEnabled(.)
324#if ORXONOX_PLATFORM != ORXONOX_PLATFORM_WIN32
325        // tame the ogre ouput so we don't get all the mess in the console
326        Ogre::Log* defaultLog = Ogre::LogManager::getSingleton().getDefaultLog();
327        defaultLog->setDebugOutputEnabled(false);
328        defaultLog->setLogDetail(Ogre::LL_BOREME);
329        defaultLog->addListener(this);
330#endif
331#endif
332
333        COUT(3) << "Ogre set up done." << std::endl;
334    }
335
[1686]336    void GSGraphics::declareResources()
337    {
338        CCOUT(4) << "Declaring Resources" << std::endl;
339        //TODO: Specify layout of data file and maybe use xml-loader
340        //TODO: Work with ressource groups (should be generated by a special loader)
341
342        if (resourceFile_ == "")
343        {
344            COUT(2) << "Warning: Ogre resource file set to \"\". Defaulting to resources.cfg" << std::endl;
345            ModifyConfigValue(resourceFile_, tset, "resources.cfg");
346        }
347
348        // Load resource paths from data file using configfile ressource type
349        Ogre::ConfigFile cf;
350        try
351        {
352            cf.load(Settings::getDataPath() + resourceFile_);
353        }
354        catch (...)
355        {
356            //COUT(1) << ex.getFullDescription() << std::endl;
357            COUT(0) << "Have you forgotten to set the data path in orxnox.ini?" << std::endl;
358            throw;
359        }
360
361        // Go through all sections & settings in the file
362        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
363
364        std::string secName, typeName, archName;
365        while (seci.hasMoreElements())
366        {
367            try
368            {
369                secName = seci.peekNextKey();
370                Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
371                Ogre::ConfigFile::SettingsMultiMap::iterator i;
372                for (i = settings->begin(); i != settings->end(); ++i)
373                {
374                    typeName = i->first; // for instance "FileSystem" or "Zip"
375                    archName = i->second; // name (and location) of archive
376
377                    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
378                        std::string(Settings::getDataPath() + archName), typeName, secName);
379                }
380            }
381            catch (Ogre::Exception& ex)
382            {
383                COUT(1) << ex.getFullDescription() << std::endl;
384            }
385        }
386    }
387
388    void GSGraphics::loadRenderer()
389    {
390        CCOUT(4) << "Configuring Renderer" << std::endl;
391
392        if (!ogreRoot_->restoreConfig())
393            if (!ogreRoot_->showConfigDialog())
394                ThrowException(InitialisationFailed, "Could not show Ogre configuration dialogue.");
395
396        CCOUT(4) << "Creating render window" << std::endl;
397
[1824]398        this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
[1686]399
400        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
401
[1820]402        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);
[1686]403
404        // create a full screen default viewport
405        this->viewport_ = this->renderWindow_->addViewport(0, 0);
406    }
407
408    void GSGraphics::initialiseResources()
409    {
410        CCOUT(4) << "Initialising resources" << std::endl;
411        //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
[1824]412        //try
413        //{
[1686]414            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
415            /*Ogre::StringVector str = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
416            for (unsigned int i = 0; i < str.size(); i++)
417            {
418            Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(str[i]);
419            }*/
[1824]420        //}
421        //catch (...)
422        //{
423        //    CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl;
424        //    throw;
425        //}
[1686]426    }
427
[1891]428    /**
429    @brief
430        Method called by the LogListener interface from Ogre.
431        We use it to capture Ogre log messages and handle it ourselves.
432    @param message
433        The message to be logged
434    @param lml
435        The message level the log is using
436    @param maskDebug
437        If we are printing to the console or not
438    @param logName
439        The name of this log (so you can have several listeners
440        for different logs, and identify them)
441    */
442    void GSGraphics::messageLogged(const std::string& message,
443        Ogre::LogMessageLevel lml, bool maskDebug, const std::string& logName)
444    {
445        int orxonoxLevel;
446        switch (lml)
447        {
448        case Ogre::LML_TRIVIAL:
449            orxonoxLevel = this->ogreLogLevelTrivial_;
450            break;
451        case Ogre::LML_NORMAL:
452            orxonoxLevel = this->ogreLogLevelNormal_;
453            break;
454        case Ogre::LML_CRITICAL:
455            orxonoxLevel = this->ogreLogLevelCritical_;
456            break;
457        default:
458            orxonoxLevel = 0;
459        }
460        OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
461            << "Ogre: " << message << std::endl;
462    }
[1686]463
464    /**
465    @brief
466        Window has moved.
467    @param rw
468        The render window it occured in
469    */
470    void GSGraphics::windowMoved(Ogre::RenderWindow *rw)
471    {
[1755]472        for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
[1686]473            it->windowMoved();
474    }
475
476    /**
477    @brief
478        Window has resized.
479    @param rw
480        The render window it occured in
481    @note
482        GraphicsEngine has a render window stored itself. This is the same
483        as rw. But we have to be careful when using multiple render windows!
484    */
485    void GSGraphics::windowResized(Ogre::RenderWindow *rw)
486    {
[1755]487        for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
[1686]488            it->windowResized(this->renderWindow_->getWidth(), this->renderWindow_->getHeight());
489    }
490
491    /**
492    @brief
493        Window focus has changed.
494    @param rw
495        The render window it occured in
496    */
[1878]497    void GSGraphics::windowFocusChange(Ogre::RenderWindow *rw)
[1686]498    {
[1755]499        for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
[1686]500            it->windowFocusChanged();
[1878]501
502        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
503        InputManager::getInstance().clearBuffers();
[1686]504    }
505
506    /**
507    @brief
508        Window was closed.
509    @param rw
510        The render window it occured in
511    */
512    void GSGraphics::windowClosed(Ogre::RenderWindow *rw)
513    {
514        // using CommandExecutor in order to avoid depending on Orxonox.h.
515        //CommandExecutor::execute("exit", false);
516        this->requestState("root");
517    }
518
519    void GSGraphics::printScreen()
520    {
521        if (this->renderWindow_)
522        {
523            this->renderWindow_->writeContentsToTimestampedFile("shot_", ".jpg");
524        }
525    }
[1661]526}
Note: See TracBrowser for help on using the repository browser.