Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/GraphicsManager.cc @ 3156

Last change on this file since 3156 was 3131, checked in by rgrieder, 15 years ago

Clean up in files in src/orxonox and src/orxonox/tools.

  • Property svn:eol-style set to native
File size: 15.4 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 *      Benjamin Knecht <beni_at_orxonox.net>, (C) 2007
25 *   Co-authors:
26 *      Felix Schulthess
27 *
28 */
29
30/**
31@file
32@brief
33    Implementation of an partial interface to Ogre.
34*/
35
36#include "GraphicsManager.h"
37
38#include <fstream>
39#include <boost/filesystem.hpp>
40
41#include <OgreCompositorManager.h>
42#include <OgreConfigFile.h>
43#include <OgreFrameListener.h>
44#include <OgreRoot.h>
45#include <OgreLogManager.h>
46#include <OgreException.h>
47#include <OgreRenderWindow.h>
48#include <OgreRenderSystem.h>
49#include <OgreTextureManager.h>
50#include <OgreViewport.h>
51#include <OgreWindowEventUtilities.h>
52
53#include "SpecialConfig.h"
54#include "util/Debug.h"
55#include "util/Exception.h"
56#include "util/String.h"
57#include "util/SubString.h"
58#include "core/Clock.h"
59#include "core/ConsoleCommand.h"
60#include "core/ConfigValueIncludes.h"
61#include "core/CoreIncludes.h"
62#include "core/Core.h"
63#include "core/Game.h"
64#include "core/GameMode.h"
65#include "tools/WindowEventListener.h"
66#include "tools/ParticleInterface.h"
67
68namespace orxonox
69{
70    class _OrxonoxExport OgreWindowEventListener : public Ogre::WindowEventListener
71    {
72        void windowResized     (Ogre::RenderWindow* rw);
73        void windowFocusChange (Ogre::RenderWindow* rw);
74        void windowClosed      (Ogre::RenderWindow* rw);
75        //void windowMoved       (Ogre::RenderWindow* rw);
76    };
77
78    GraphicsManager* GraphicsManager::singletonRef_s = 0;
79
80    /**
81    @brief
82        Non-initialising constructor.
83    */
84    GraphicsManager::GraphicsManager()
85        : ogreRoot_(0)
86        , ogreLogger_(0)
87        , renderWindow_(0)
88        , viewport_(0)
89        , ogreWindowEventListener_(0)
90    {
91        RegisterObject(GraphicsManager);
92
93        assert(singletonRef_s == 0);
94        singletonRef_s = this;
95
96        this->loaded_ = false;
97
98        this->setConfigValues();
99    }
100
101    void GraphicsManager::initialise()
102    {
103        // Ogre setup procedure
104        setupOgre();
105        // load all the required plugins for Ogre
106        loadOgrePlugins();
107        // read resource declaration file
108        this->declareResources();
109        // Reads ogre config and creates the render window
110        this->loadRenderer();
111
112        // TODO: Spread this
113        this->initialiseResources();
114
115        // add console commands
116        FunctorMember<GraphicsManager>* functor1 = createFunctor(&GraphicsManager::printScreen);
117        functor1->setObject(this);
118        ccPrintScreen_ = createConsoleCommand(functor1, "printScreen");
119        CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
120
121        this->loaded_ = true;
122    }
123
124    /**
125    @brief
126        Destroys all the Ogre related objects
127    */
128    GraphicsManager::~GraphicsManager()
129    {
130        if (this->loaded_)
131        {
132            delete this->ccPrintScreen_;
133
134            if (this->ogreWindowEventListener_)
135            {
136                // remove our WindowEventListener first to avoid bad calls after the window has been destroyed
137                Ogre::WindowEventUtilities::removeWindowEventListener(this->renderWindow_, this->ogreWindowEventListener_);
138                delete this->ogreWindowEventListener_;
139            }
140
141            // destroy render window
142//            Ogre::RenderSystem* renderer = this->ogreRoot_->getRenderSystem();
143//            renderer->destroyRenderWindow("Orxonox");
144
145            // unload all compositors
146            Ogre::CompositorManager::getSingleton().removeAll();
147
148            // Delete OGRE main control organ
149            delete this->ogreRoot_;
150
151            // delete the ogre log and the logManager (since we have created it in the first place).
152            this->ogreLogger_->getDefaultLog()->removeListener(this);
153            this->ogreLogger_->destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
154            delete this->ogreLogger_;
155        }
156
157        assert(singletonRef_s);
158        singletonRef_s = 0;
159    }
160
161    void GraphicsManager::setConfigValues()
162    {
163        SetConfigValue(resourceFile_,    "resources.cfg")
164            .description("Location of the resources file in the data path.");
165        SetConfigValue(ogreConfigFile_,  "ogre.cfg")
166            .description("Location of the Ogre config file");
167        SetConfigValue(ogrePluginsFolder_, ORXONOX_OGRE_PLUGINS_FOLDER)
168            .description("Folder where the Ogre plugins are located.");
169        SetConfigValue(ogrePlugins_, ORXONOX_OGRE_PLUGINS)
170            .description("Comma separated list of all plugins to load.");
171        SetConfigValue(ogreLogFile_,     "ogre.log")
172            .description("Logfile for messages from Ogre. Use \"\" to suppress log file creation.");
173        SetConfigValue(ogreLogLevelTrivial_ , 5)
174            .description("Corresponding orxonox debug level for ogre Trivial");
175        SetConfigValue(ogreLogLevelNormal_  , 4)
176            .description("Corresponding orxonox debug level for ogre Normal");
177        SetConfigValue(ogreLogLevelCritical_, 2)
178            .description("Corresponding orxonox debug level for ogre Critical");
179        SetConfigValue(detailLevelParticle_, 2)
180            .description("O: off, 1: low, 2: normal, 3: high").callback(this, &GraphicsManager::detailLevelParticleChanged);
181    }
182
183    void GraphicsManager::detailLevelParticleChanged()
184    {
185        for (ObjectList<ParticleInterface>::iterator it = ObjectList<ParticleInterface>::begin(); it; ++it)
186            it->detailLevelChanged(this->detailLevelParticle_);
187    }
188
189    void GraphicsManager::update(const Clock& time)
190    {
191        if (this->loaded_)
192        {
193            Ogre::FrameEvent evt;
194            evt.timeSinceLastFrame = time.getDeltaTime();
195            evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
196
197            // don't forget to call _fireFrameStarted to OGRE to make sure
198            // everything goes smoothly
199            ogreRoot_->_fireFrameStarted(evt);
200
201            // Pump messages in all registered RenderWindows
202            // This calls the WindowEventListener objects.
203            Ogre::WindowEventUtilities::messagePump();
204            // make sure the window stays active even when not focused
205            // (probably only necessary on windows)
206            this->renderWindow_->setActive(true);
207
208            // render
209            ogreRoot_->_updateAllRenderTargets();
210
211            // again, just to be sure OGRE works fine
212            ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
213        }
214    }
215
216    void GraphicsManager::setCamera(Ogre::Camera* camera)
217    {
218        this->viewport_->setCamera(camera);
219    }
220
221    /**
222    @brief
223        Creates the Ogre Root object and sets up the ogre log.
224    */
225    void GraphicsManager::setupOgre()
226    {
227        COUT(3) << "Setting up Ogre..." << std::endl;
228
229        if (ogreConfigFile_ == "")
230        {
231            COUT(2) << "Warning: Ogre config file set to \"\". Defaulting to config.cfg" << std::endl;
232            ModifyConfigValue(ogreConfigFile_, tset, "config.cfg");
233        }
234        if (ogreLogFile_ == "")
235        {
236            COUT(2) << "Warning: Ogre log file set to \"\". Defaulting to ogre.log" << std::endl;
237            ModifyConfigValue(ogreLogFile_, tset, "ogre.log");
238        }
239
240        boost::filesystem::path ogreConfigFilepath(Core::getConfigPath() / this->ogreConfigFile_);
241        boost::filesystem::path ogreLogFilepath(Core::getLogPath() / this->ogreLogFile_);
242
243        // create a new logManager
244        // Ogre::Root will detect that we've already created a Log
245        ogreLogger_ = new Ogre::LogManager();
246        COUT(4) << "Ogre LogManager created" << std::endl;
247
248        // create our own log that we can listen to
249        Ogre::Log *myLog;
250        myLog = ogreLogger_->createLog(ogreLogFilepath.string(), true, false, false);
251        COUT(4) << "Ogre Log created" << std::endl;
252
253        myLog->setLogDetail(Ogre::LL_BOREME);
254        myLog->addListener(this);
255
256        COUT(4) << "Creating Ogre Root..." << std::endl;
257
258        // check for config file existence because Ogre displays (caught) exceptions if not
259        if (!boost::filesystem::exists(ogreConfigFilepath))
260        {
261            // create a zero sized file
262            std::ofstream creator;
263            creator.open(ogreConfigFilepath.string().c_str());
264            creator.close();
265        }
266
267        // Leave plugins file empty. We're going to do that part manually later
268        ogreRoot_ = new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string());
269
270        COUT(3) << "Ogre set up done." << std::endl;
271    }
272
273    void GraphicsManager::loadOgrePlugins()
274    {
275        // just to make sure the next statement doesn't segfault
276        if (ogrePluginsFolder_ == "")
277            ogrePluginsFolder_ = ".";
278
279        boost::filesystem::path folder(ogrePluginsFolder_);
280        // Do some SubString magic to get the comma separated list of plugins
281        SubString plugins(ogrePlugins_, ",", " ", false, 92, false, 34, false, 40, 41, false, '\0');
282        // Use backslash paths on Windows! file_string() already does that though.
283        for (unsigned int i = 0; i < plugins.size(); ++i)
284            ogreRoot_->loadPlugin((folder / plugins[i]).file_string());
285    }
286
287    void GraphicsManager::declareResources()
288    {
289        CCOUT(4) << "Declaring Resources" << std::endl;
290        //TODO: Specify layout of data file and maybe use xml-loader
291        //TODO: Work with ressource groups (should be generated by a special loader)
292
293        if (resourceFile_ == "")
294        {
295            COUT(2) << "Warning: Ogre resource file set to \"\". Defaulting to resources.cfg" << std::endl;
296            ModifyConfigValue(resourceFile_, tset, "resources.cfg");
297        }
298
299        // Load resource paths from data file using configfile ressource type
300        Ogre::ConfigFile cf;
301        try
302        {
303            cf.load((Core::getMediaPath() / resourceFile_).string());
304        }
305        catch (...)
306        {
307            //COUT(1) << ex.getFullDescription() << std::endl;
308            COUT(0) << "Have you forgotten to set the data path in orxnox.ini?" << std::endl;
309            throw;
310        }
311
312        // Go through all sections & settings in the file
313        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
314
315        std::string secName, typeName, archName;
316        while (seci.hasMoreElements())
317        {
318            try
319            {
320                secName = seci.peekNextKey();
321                Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
322                Ogre::ConfigFile::SettingsMultiMap::iterator i;
323                for (i = settings->begin(); i != settings->end(); ++i)
324                {
325                    typeName = i->first; // for instance "FileSystem" or "Zip"
326                    archName = i->second; // name (and location) of archive
327
328                    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
329                        (Core::getMediaPath() / archName).string(), typeName, secName);
330                }
331            }
332            catch (Ogre::Exception& ex)
333            {
334                COUT(1) << ex.getFullDescription() << std::endl;
335            }
336        }
337    }
338
339    void GraphicsManager::loadRenderer()
340    {
341        CCOUT(4) << "Configuring Renderer" << std::endl;
342
343        if (!ogreRoot_->restoreConfig())
344            if (!ogreRoot_->showConfigDialog())
345                ThrowException(InitialisationFailed, "Could not show Ogre configuration dialogue.");
346
347        CCOUT(4) << "Creating render window" << std::endl;
348
349        this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
350
351        this->ogreWindowEventListener_ = new OgreWindowEventListener();
352        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_);
353
354        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);
355
356        // create a full screen default viewport
357        this->viewport_ = this->renderWindow_->addViewport(0, 0);
358    }
359
360    void GraphicsManager::initialiseResources()
361    {
362        CCOUT(4) << "Initialising resources" << std::endl;
363        //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
364        //try
365        //{
366            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
367            /*Ogre::StringVector str = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
368            for (unsigned int i = 0; i < str.size(); i++)
369            {
370            Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(str[i]);
371            }*/
372        //}
373        //catch (...)
374        //{
375        //    CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl;
376        //    throw;
377        //}
378    }
379
380    /**
381    @brief
382        Method called by the LogListener interface from Ogre.
383        We use it to capture Ogre log messages and handle it ourselves.
384    @param message
385        The message to be logged
386    @param lml
387        The message level the log is using
388    @param maskDebug
389        If we are printing to the console or not
390    @param logName
391        The name of this log (so you can have several listeners
392        for different logs, and identify them)
393    */
394    void GraphicsManager::messageLogged(const std::string& message,
395        Ogre::LogMessageLevel lml, bool maskDebug, const std::string& logName)
396    {
397        int orxonoxLevel;
398        switch (lml)
399        {
400        case Ogre::LML_TRIVIAL:
401            orxonoxLevel = this->ogreLogLevelTrivial_;
402            break;
403        case Ogre::LML_NORMAL:
404            orxonoxLevel = this->ogreLogLevelNormal_;
405            break;
406        case Ogre::LML_CRITICAL:
407            orxonoxLevel = this->ogreLogLevelCritical_;
408            break;
409        default:
410            orxonoxLevel = 0;
411        }
412        OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
413            << "Ogre: " << message << std::endl;
414    }
415
416    void GraphicsManager::printScreen()
417    {
418        assert(this->renderWindow_);
419       
420        this->renderWindow_->writeContentsToTimestampedFile(Core::getLogPathString() + "screenShot_", ".jpg");
421    }
422
423
424    /****** OgreWindowEventListener ******/
425
426    void OgreWindowEventListener::windowResized(Ogre::RenderWindow* rw)
427    {
428        for (ObjectList<orxonox::WindowEventListener>::iterator it
429            = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
430            it->windowResized(rw->getWidth(), rw->getHeight());
431    }
432    void OgreWindowEventListener::windowFocusChange(Ogre::RenderWindow* rw)
433    {
434        for (ObjectList<orxonox::WindowEventListener>::iterator it
435            = ObjectList<orxonox::WindowEventListener>::begin(); it; ++it)
436            it->windowFocusChanged();
437    }
438    void OgreWindowEventListener::windowClosed(Ogre::RenderWindow* rw)
439    {
440        Game::getInstance().stop();
441    }
442}
Note: See TracBrowser for help on using the repository browser.