Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem2/src/orxonox/gamestates/GSGraphics.cc @ 2650

Last change on this file since 2650 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
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 <fstream>
33#include <OgreConfigFile.h>
34#include <OgreFrameListener.h>
35#include <OgreRoot.h>
36#include <OgreLogManager.h>
37#include <OgreException.h>
38#include <OgreRenderWindow.h>
39#include <OgreRenderSystem.h>
40#include <OgreTextureManager.h>
41#include <OgreViewport.h>
42#include <OgreWindowEventUtilities.h>
43
44#include "util/Debug.h"
45#include "util/Exception.h"
46#include "core/ConsoleCommand.h"
47#include "core/ConfigValueIncludes.h"
48#include "core/CoreIncludes.h"
49#include "core/Core.h"
50#include "core/input/InputManager.h"
51#include "core/input/KeyBinder.h"
52#include "core/input/ExtendedInputState.h"
53#include "core/Loader.h"
54#include "core/XMLFile.h"
55#include "overlays/console/InGameConsole.h"
56#include "gui/GUIManager.h"
57#include "tools/WindowEventListener.h"
58#include "objects/Tickable.h"
59#include "Settings.h"
60
61// for compatibility
62#include "GraphicsEngine.h"
63
64namespace orxonox
65{
66    GSGraphics::GSGraphics()
67        : GameState<GSRoot>("graphics")
68        , renderWindow_(0)
69        , viewport_(0)
70        , bWindowEventListenerUpdateRequired_(false)
71        , inputManager_(0)
72        , console_(0)
73        , guiManager_(0)
74        , ogreRoot_(0)
75        , ogreLogger_(0)
76        , graphicsEngine_(0)
77        , masterKeyBinder_(0)
78        , frameCount_(0)
79        , statisticsRefreshCycle_(0)
80        , statisticsStartTime_(0)
81        , statisticsStartCount_(0)
82        , tickTime_(0)
83        , debugOverlay_(0)
84    {
85        RegisterRootObject(GSGraphics);
86        setConfigValues();
87    }
88
89    GSGraphics::~GSGraphics()
90    {
91    }
92
93    void GSGraphics::setConfigValues()
94    {
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.");
113    }
114
115    void GSGraphics::enter()
116    {
117        Core::setShowsGraphics(true);
118
119        // initialise graphics engine. Doesn't load the render window yet!
120        graphicsEngine_ = new GraphicsEngine();
121
122        // Ogre setup procedure
123        setupOgre();
124        this->declareResources();
125        this->loadRenderer();    // creates the render window
126        // TODO: Spread this so that this call only initialises things needed for the Console and GUI
127        this->initialiseResources();
128
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);
133
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_);
138
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();
142        size_t windowHnd = 0;
143        this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
144        inputManager_->initialise(windowHnd, renderWindow_->getWidth(), renderWindow_->getHeight(), true);
145        // Configure master input state with a KeyBinder
146        masterKeyBinder_ = new KeyBinder();
147        masterKeyBinder_->loadBindings("masterKeybindings.ini", defaultMasterKeybindings_);
148        inputManager_->getMasterInputState()->addKeyHandler(masterKeyBinder_);
149
150        // Load the InGameConsole
151        console_ = new InGameConsole();
152        console_->initialise(this->renderWindow_->getWidth(), this->renderWindow_->getHeight());
153
154        // load the CEGUI interface
155        guiManager_ = new GUIManager();
156        guiManager_->initialise(this->renderWindow_);
157
158        // reset frame counter
159        this->frameCount_ = 0;
160        this->tickTime_ = 0;
161        statisticsStartTime_ = 0;
162        statisticsStartCount_ = 0;
163
164        // add console commands
165        FunctorMember<GSGraphics>* functor1 = createFunctor(&GSGraphics::printScreen);
166        functor1->setObject(this);
167        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "printScreen"));
168    }
169
170    void GSGraphics::leave()
171    {
172        using namespace Ogre;
173
174        // remove our WindowEventListener first to avoid bad calls after the window has been destroyed
175        Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this);
176
177        delete this->guiManager_;
178
179        delete this->console_;
180
181        //inputManager_->getMasterInputState()->removeKeyHandler(this->masterKeyBinder_);
182        delete this->masterKeyBinder_;
183        delete this->inputManager_;
184
185        Loader::unload(this->debugOverlay_);
186        delete this->debugOverlay_;
187
188        // destroy render window
189        RenderSystem* renderer = this->ogreRoot_->getRenderSystem();
190        renderer->destroyRenderWindow("Orxonox");
191
192        /*** CODE SNIPPET, UNUSED ***/
193        // Does the opposite of initialise()
194        //ogreRoot_->shutdown();
195        // Remove all resources and resource groups
196        //StringVector groups = ResourceGroupManager::getSingleton().getResourceGroups();
197        //for (StringVector::iterator it = groups.begin(); it != groups.end(); ++it)
198        //{
199        //    ResourceGroupManager::getSingleton().destroyResourceGroup(*it);
200        //}
201
202        //ParticleSystemManager::getSingleton().removeAllTemplates();
203
204        // Shutdown the render system
205        //this->ogreRoot_->setRenderSystem(0);
206
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
218        Core::setShowsGraphics(false);
219    }
220
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    */
234    void GSGraphics::ticked(const Clock& time)
235    {
236        unsigned long long timeBeforeTick = time.getRealMicroseconds();
237        float dt = time.getDeltaTime();
238
239        this->inputManager_->tick(dt);
240        // tick console
241        this->console_->tick(dt);
242        this->tickChild(time);
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
257        unsigned long long timeAfterTick = time.getRealMicroseconds();
258
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);
267
268            tickTime_ = 0;
269            statisticsStartCount_ = frameCount_;
270            statisticsStartTime_  = timeAfterTick;
271        }
272
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);
279
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)
285        this->renderWindow_->setActive(true);
286
287        // render
288        ogreRoot_->_updateAllRenderTargets();
289
290        // again, just to be sure ogre works fine
291        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
292
293        ++frameCount_;
294    }
295
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
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
431        this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
432
433        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
434
435        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);
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...
445        //try
446        //{
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            }*/
453        //}
454        //catch (...)
455        //{
456        //    CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl;
457        //    throw;
458        //}
459    }
460
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    }
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    {
505        for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
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    {
520        for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
521            it->windowResized(this->renderWindow_->getWidth(), this->renderWindow_->getHeight());
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());
526    }
527
528    /**
529    @brief
530        Window focus has changed.
531    @param rw
532        The render window it occured in
533    */
534    void GSGraphics::windowFocusChange(Ogre::RenderWindow *rw)
535    {
536        for (ObjectList<orxonox::WindowEventListener>::iterator it = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
537            it->windowFocusChanged();
538
539        // instruct InputManager to clear the buffers (core library so we cannot use the interface)
540        if (this->inputManager_)
541            this->inputManager_->clearBuffers();
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    }
562}
Note: See TracBrowser for help on using the repository browser.