Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/kicklib/src/libraries/core/GraphicsManager.cc @ 8112

Last change on this file since 8112 was 8073, checked in by rgrieder, 14 years ago

Merged mac_osx branch (after renaming it from ois_update) into kicklib branch.

  • Property svn:eol-style set to native
File size: 15.1 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#include "GraphicsManager.h"
31
32#include <fstream>
33#include <sstream>
34#include <boost/filesystem.hpp>
35#include <boost/shared_array.hpp>
36
37#include <OgreFrameListener.h>
38#include <OgreRoot.h>
39#include <OgreLogManager.h>
40#include <OgreRenderWindow.h>
41#include <OgreRenderSystem.h>
42#include <OgreResourceGroupManager.h>
43#include <OgreTextureManager.h>
44#include <OgreViewport.h>
45#include <OgreWindowEventUtilities.h>
46
47#include "SpecialConfig.h"
48#include "util/Clock.h"
49#include "util/Exception.h"
50#include "util/StringUtils.h"
51#include "util/SubString.h"
52#include "ConfigValueIncludes.h"
53#include "CoreIncludes.h"
54#include "Core.h"
55#include "Game.h"
56#include "GameMode.h"
57#include "Loader.h"
58#include "PathConfig.h"
59#include "WindowEventListener.h"
60#include "XMLFile.h"
61#include "command/ConsoleCommand.h"
62
63namespace orxonox
64{
65    static const std::string __CC_printScreen_name = "printScreen";
66    DeclareConsoleCommand(__CC_printScreen_name, &prototype::void__void);
67
68    class OgreWindowEventListener : public Ogre::WindowEventListener
69    {
70    public:
71        void windowResized     (Ogre::RenderWindow* rw)
72            { orxonox::WindowEventListener::resizeWindow(rw->getWidth(), rw->getHeight()); }
73        void windowFocusChange (Ogre::RenderWindow* rw)
74            { orxonox::WindowEventListener::changeWindowFocus(rw->isActive()); }
75        void windowClosed      (Ogre::RenderWindow* rw)
76            { orxonox::Game::getInstance().stop(); }
77        void windowMoved       (Ogre::RenderWindow* rw)
78            { orxonox::WindowEventListener::moveWindow(); }
79    };
80
81    GraphicsManager* GraphicsManager::singletonPtr_s = 0;
82
83    /**
84    @brief
85        Non-initialising constructor.
86    */
87    GraphicsManager::GraphicsManager(bool bLoadRenderer)
88        : ogreWindowEventListener_(new OgreWindowEventListener())
89        , renderWindow_(0)
90        , viewport_(0)
91    {
92        RegisterObject(GraphicsManager);
93
94        this->setConfigValues();
95
96        // Ogre setup procedure (creating Ogre::Root)
97        this->loadOgreRoot();
98
99        // At first, add the root paths of the data directories as resource locations
100        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(PathConfig::getDataPathString(), "FileSystem");
101        // Load resources
102        resources_.reset(new XMLFile("DefaultResources.oxr"));
103        resources_->setLuaSupport(false);
104        Loader::open(resources_.get());
105
106        // Only for development runs
107        if (PathConfig::isDevelopmentRun())
108        {
109            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(PathConfig::getExternalDataPathString(), "FileSystem");
110            extResources_.reset(new XMLFile("resources.oxr"));
111            extResources_->setLuaSupport(false);
112            Loader::open(extResources_.get());
113        }
114
115        if (bLoadRenderer)
116        {
117            // Reads the ogre config and creates the render window
118            this->upgradeToGraphics();
119        }
120    }
121
122    /**
123    @brief
124        Destruction is done by the member scoped_ptrs.
125    */
126    GraphicsManager::~GraphicsManager()
127    {
128        Loader::unload(debugOverlay_.get());
129
130        Ogre::WindowEventUtilities::removeWindowEventListener(renderWindow_, ogreWindowEventListener_.get());
131        ModifyConsoleCommand(__CC_printScreen_name).resetFunction();
132
133        // Undeclare the resources
134        Loader::unload(resources_.get());
135        if (PathConfig::isDevelopmentRun())
136            Loader::unload(extResources_.get());
137    }
138
139    void GraphicsManager::setConfigValues()
140    {
141        SetConfigValue(ogreConfigFile_,  "ogre.cfg")
142            .description("Location of the Ogre config file");
143        SetConfigValue(ogrePluginsDirectory_, specialConfig::ogrePluginsDirectory)
144            .description("Folder where the Ogre plugins are located.");
145        SetConfigValue(ogrePlugins_, specialConfig::ogrePlugins)
146            .description("Comma separated list of all plugins to load.");
147        SetConfigValue(ogreLogFile_,     "ogre.log")
148            .description("Logfile for messages from Ogre. Use \"\" to suppress log file creation.");
149        SetConfigValue(ogreLogLevelTrivial_ , 5)
150            .description("Corresponding orxonox debug level for ogre Trivial");
151        SetConfigValue(ogreLogLevelNormal_  , 4)
152            .description("Corresponding orxonox debug level for ogre Normal");
153        SetConfigValue(ogreLogLevelCritical_, 2)
154            .description("Corresponding orxonox debug level for ogre Critical");
155    }
156
157    /**
158    @brief
159        Loads the renderer and creates the render window if not yet done so.
160    @remarks
161        This operation is irreversible without recreating the GraphicsManager!
162        So if it throws you HAVE to recreate the GraphicsManager!!!
163        It therefore offers almost no exception safety.
164    */
165    void GraphicsManager::upgradeToGraphics()
166    {
167        if (renderWindow_ != NULL)
168            return;
169
170        // load all the required plugins for Ogre
171        this->loadOgrePlugins();
172
173        this->loadRenderer();
174
175        // Initialise all resources (do this AFTER the renderer has been loaded!)
176        // Note: You can only do this once! Ogre will check whether a resource group has
177        // already been initialised. If you need to load resources later, you will have to
178        // choose another resource group.
179        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
180    }
181
182    /**
183    @brief
184        Creates the Ogre Root object and sets up the ogre log.
185    */
186    void GraphicsManager::loadOgreRoot()
187    {
188        COUT(3) << "Setting up Ogre..." << std::endl;
189
190        if (ogreConfigFile_.empty())
191        {
192            COUT(2) << "Warning: Ogre config file set to \"\". Defaulting to config.cfg" << std::endl;
193            ModifyConfigValue(ogreConfigFile_, tset, "config.cfg");
194        }
195        if (ogreLogFile_.empty())
196        {
197            COUT(2) << "Warning: Ogre log file set to \"\". Defaulting to ogre.log" << std::endl;
198            ModifyConfigValue(ogreLogFile_, tset, "ogre.log");
199        }
200
201        boost::filesystem::path ogreConfigFilepath(PathConfig::getConfigPath() / this->ogreConfigFile_);
202        boost::filesystem::path ogreLogFilepath(PathConfig::getLogPath() / this->ogreLogFile_);
203
204        // create a new logManager
205        // Ogre::Root will detect that we've already created a Log
206        ogreLogger_.reset(new Ogre::LogManager());
207        COUT(4) << "Ogre LogManager created" << std::endl;
208
209        // create our own log that we can listen to
210        Ogre::Log *myLog;
211        myLog = ogreLogger_->createLog(ogreLogFilepath.string(), true, false, false);
212        COUT(4) << "Ogre Log created" << std::endl;
213
214        myLog->setLogDetail(Ogre::LL_BOREME);
215        myLog->addListener(this);
216
217        COUT(4) << "Creating Ogre Root..." << std::endl;
218
219        // check for config file existence because Ogre displays (caught) exceptions if not
220        if (!boost::filesystem::exists(ogreConfigFilepath))
221        {
222            // create a zero sized file
223            std::ofstream creator;
224            creator.open(ogreConfigFilepath.string().c_str());
225            creator.close();
226        }
227
228        // Leave plugins file empty. We're going to do that part manually later
229        ogreRoot_.reset(new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string()));
230
231        COUT(3) << "Ogre set up done." << std::endl;
232    }
233
234    void GraphicsManager::loadOgrePlugins()
235    {
236        // just to make sure the next statement doesn't segfault
237        if (ogrePluginsDirectory_.empty())
238            ogrePluginsDirectory_ = '.';
239
240        boost::filesystem::path folder(ogrePluginsDirectory_);
241        // Do some SubString magic to get the comma separated list of plugins
242        SubString plugins(ogrePlugins_, ",", " ", false, '\\', false, '"', false, '{', '}', false, '\0');
243        // Use backslash paths on Windows! file_string() already does that though.
244        for (unsigned int i = 0; i < plugins.size(); ++i)
245#if BOOST_FILESYSTEM_VERSION < 3
246            ogreRoot_->loadPlugin((folder / plugins[i]).file_string());
247#else
248            ogreRoot_->loadPlugin((folder / plugins[i]).string());
249#endif
250    }
251
252    void GraphicsManager::loadRenderer()
253    {
254        CCOUT(4) << "Configuring Renderer" << std::endl;
255
256        if (!ogreRoot_->restoreConfig() || Core::getInstance().getOgreConfigTimestamp() > Core::getInstance().getLastLevelTimestamp())
257        {
258            if (!ogreRoot_->showConfigDialog())
259                ThrowException(InitialisationFailed, "OGRE graphics configuration dialogue canceled.");
260            else
261                Core::getInstance().updateOgreConfigTimestamp();
262        }
263
264        CCOUT(4) << "Creating render window" << std::endl;
265
266        this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
267        // Propagate the size of the new winodw
268        this->ogreWindowEventListener_->windowResized(renderWindow_);
269
270        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_.get());
271
272// HACK
273#ifdef ORXONOX_PLATFORM_APPLE
274        //INFO: This will give our window focus, and not lock it to the terminal
275        ProcessSerialNumber psn = {0, kCurrentProcess};
276        TransformProcessType(&psn, kProcessTransformToForegroundApplication);
277        SetFrontProcess(&psn);
278#endif
279// End of HACK
280
281        // create a full screen default viewport
282        // Note: This may throw when adding a viewport with an existing z-order!
283        //       But in our case we only have one viewport for now anyway, therefore
284        //       no ScopeGuards or anything to handle exceptions.
285        this->viewport_ = this->renderWindow_->addViewport(0, 0);
286
287        Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(Ogre::MIP_UNLIMITED);
288
289        // add console commands
290        ModifyConsoleCommand(__CC_printScreen_name).setFunction(&GraphicsManager::printScreen, this);
291    }
292
293    void GraphicsManager::loadDebugOverlay()
294    {
295        // Load debug overlay to show info about fps and tick time
296        COUT(4) << "Loading Debug Overlay..." << std::endl;
297        debugOverlay_.reset(new XMLFile("debug.oxo"));
298        Loader::open(debugOverlay_.get());
299    }
300
301    /**
302    @note
303        A note about the Ogre::FrameListener: Even though we don't use them,
304        they still get called. However, the delta times are not correct (except
305        for timeSinceLastFrame, which is the most important). A little research
306        as shown that there is probably only one FrameListener that doesn't even
307        need the time. So we shouldn't run into problems.
308    */
309    void GraphicsManager::postUpdate(const Clock& time)
310    {
311        Ogre::FrameEvent evt;
312        evt.timeSinceLastFrame = time.getDeltaTime();
313        evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
314
315        // don't forget to call _fireFrameStarted to OGRE to make sure
316        // everything goes smoothly
317        ogreRoot_->_fireFrameStarted(evt);
318
319        // Pump messages in all registered RenderWindows
320        // This calls the WindowEventListener objects.
321        Ogre::WindowEventUtilities::messagePump();
322        // make sure the window stays active even when not focused
323        // (probably only necessary on windows)
324        this->renderWindow_->setActive(true);
325
326        // Time before rendering
327        uint64_t timeBeforeTick = time.getRealMicroseconds();
328
329        // Render frame
330        ogreRoot_->_updateAllRenderTargets();
331
332        uint64_t timeAfterTick = time.getRealMicroseconds();
333        // Subtract the time used for rendering from the tick time counter
334        Game::getInstance().subtractTickTime((int32_t)(timeAfterTick - timeBeforeTick));
335
336        // again, just to be sure OGRE works fine
337        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
338    }
339
340    void GraphicsManager::setCamera(Ogre::Camera* camera)
341    {
342        this->viewport_->setCamera(camera);
343    }
344
345    /**
346    @brief
347        Method called by the LogListener interface from Ogre.
348        We use it to capture Ogre log messages and handle it ourselves.
349    @param message
350        The message to be logged
351    @param lml
352        The message level the log is using
353    @param maskDebug
354        If we are printing to the console or not
355    @param logName
356        The name of this log (so you can have several listeners
357        for different logs, and identify them)
358    */
359    void GraphicsManager::messageLogged(const std::string& message,
360        Ogre::LogMessageLevel lml, bool maskDebug, const std::string& logName)
361    {
362        int orxonoxLevel;
363        std::string introduction;
364        // Do not show caught OGRE exceptions in front
365        if (message.find("EXCEPTION") != std::string::npos)
366        {
367            orxonoxLevel = OutputLevel::Debug;
368            introduction = "Ogre, caught exception: ";
369        }
370        else
371        {
372            switch (lml)
373            {
374            case Ogre::LML_TRIVIAL:
375                orxonoxLevel = this->ogreLogLevelTrivial_;
376                break;
377            case Ogre::LML_NORMAL:
378                orxonoxLevel = this->ogreLogLevelNormal_;
379                break;
380            case Ogre::LML_CRITICAL:
381                orxonoxLevel = this->ogreLogLevelCritical_;
382                break;
383            default:
384                orxonoxLevel = 0;
385            }
386            introduction = "Ogre: ";
387        }
388        OutputHandler::getOutStream(orxonoxLevel)
389            << introduction << message << std::endl;
390    }
391
392    size_t GraphicsManager::getRenderWindowHandle()
393    {
394        size_t windowHnd = 0;
395        renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
396        return windowHnd;
397    }
398
399    bool GraphicsManager::isFullScreen() const
400    {
401        Ogre::ConfigOptionMap& options = ogreRoot_->getRenderSystem()->getConfigOptions();
402        if (options.find("Full Screen") != options.end())
403        {
404            if (options["Full Screen"].currentValue == "Yes")
405                return true;
406            else
407                return false;
408        }
409        else
410        {
411            COUT(0) << "Could not find 'Full Screen' render system option. Fix This!!!" << std::endl;
412            return false;
413        }
414    }
415
416    void GraphicsManager::printScreen()
417    {
418        assert(this->renderWindow_);
419        this->renderWindow_->writeContentsToTimestampedFile(PathConfig::getLogPathString() + "screenShot_", ".png");
420    }
421}
Note: See TracBrowser for help on using the repository browser.