Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged r2101 (objecthierarchy) to trunk.

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