Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5677


Ignore:
Timestamp:
Aug 24, 2009, 3:03:44 PM (15 years ago)
Author:
rgrieder
Message:

Merged most of revision 5614 and its fixes from revisions 5628, 5658, 5662 and 5670:

  • Creating Ogre::Root in non graphics mode as well. This allows to always make use of the ResourceGroupManager.
  • Switched to smart pointers for the destruction code in GraphicsManager
Location:
code/branches/resource3/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource3/src/core/Core.cc

    r3370 r5677  
    313313        // creates the class hierarchy for all classes with factories
    314314        Factory::createClassHierarchy();
     315
     316        // Load OGRE excluding the renderer and the render window
     317        this->graphicsManager_.reset(new GraphicsManager(false));
    315318    }
    316319
     
    325328    void Core::loadGraphics()
    326329    {
    327         if (bGraphicsLoaded_)
    328             return;
    329 
    330         // Load OGRE including the render window
    331         scoped_ptr<GraphicsManager> graphicsManager(new GraphicsManager());
     330        // Any exception should trigger this, even in upgradeToGraphics (see its remarks)
     331        Loki::ScopeGuard unloader = Loki::MakeObjGuard(*this, &Core::unloadGraphics);
     332
     333        // Upgrade OGRE to receive a render window
     334        graphicsManager_->upgradeToGraphics();
    332335
    333336        // The render window width and height are used to set up the mouse movement.
    334337        size_t windowHnd = 0;
    335         graphicsManager->getRenderWindow()->getCustomAttribute("WINDOW", &windowHnd);
     338        graphicsManager_->getRenderWindow()->getCustomAttribute("WINDOW", &windowHnd);
    336339
    337340        // Calls the InputManager which sets up the input devices.
    338         scoped_ptr<InputManager> inputManager(new InputManager(windowHnd));
     341        inputManager_.reset(new InputManager(windowHnd));
    339342
    340343        // load the CEGUI interface
    341         guiManager_.reset(new GUIManager(graphicsManager->getRenderWindow()));
    342 
    343         // Dismiss scoped pointers
    344         graphicsManager_.swap(graphicsManager);
    345         inputManager_.swap(inputManager);
     344        guiManager_.reset(new GUIManager(graphicsManager_->getRenderWindow()));
     345
     346        unloader.Dismiss();
    346347
    347348        bGraphicsLoaded_ = true;
     
    350351    void Core::unloadGraphics()
    351352    {
    352         if (!bGraphicsLoaded_)
    353             return;
    354 
    355353        this->guiManager_.reset();;
    356354        this->inputManager_.reset();;
    357355        this->graphicsManager_.reset();
     356
     357        // Load Ogre::Root again, but without the render system
     358        try
     359            { this->graphicsManager_.reset(new GraphicsManager(false)); }
     360        catch (...)
     361        {
     362            COUT(0) << "An exception occurred during 'new GraphicsManager' while "
     363                    << "another exception was being handled. This will lead to undefined behaviour!" << std::endl
     364                    << "Terminating the program." << std::endl;
     365            abort();
     366        }
    358367
    359368        bGraphicsLoaded_ = false;
  • code/branches/resource3/src/core/Core.h

    r3370 r5677  
    6666        typedef Loki::ScopeGuardImpl0<void (*)()> SimpleScopeGuard;
    6767        friend class Singleton<Core>;
     68        friend class Game;
    6869
    6970        public:
     
    8283            bool preUpdate(const Clock& time) throw();
    8384            bool postUpdate(const Clock& time) throw();
    84 
    85             void loadGraphics();
    86             void unloadGraphics();
    8785
    8886            static int   getSoftDebugLevel(OutputHandler::OutputDevice device = OutputHandler::LD_All);
     
    113111        private:
    114112            Core(const Core&); //!< Don't use (undefined symbol)
     113
     114            void loadGraphics();
     115            void unloadGraphics();
    115116
    116117            void checkDevBuild();
  • code/branches/resource3/src/core/GraphicsManager.cc

    r3370 r5677  
    3737
    3838#include <fstream>
    39 #include <memory>
    4039#include <boost/filesystem.hpp>
    41 #include <boost/shared_ptr.hpp>
    42 
    43 #include <OgreCompositorManager.h>
     40
    4441#include <OgreConfigFile.h>
    4542#include <OgreFrameListener.h>
     
    4946#include <OgreRenderWindow.h>
    5047#include <OgreRenderSystem.h>
    51 #include <OgreTextureManager.h>
    5248#include <OgreViewport.h>
    5349#include <OgreWindowEventUtilities.h>
     
    6864namespace orxonox
    6965{
    70     using boost::shared_ptr;
    71 
    7266    class OgreWindowEventListener : public Ogre::WindowEventListener
    7367    {
     
    8983        Non-initialising constructor.
    9084    */
    91     GraphicsManager::GraphicsManager()
    92         : ogreRoot_(0)
    93         , ogreLogger_(0)
     85    GraphicsManager::GraphicsManager(bool bLoadRenderer)
     86        : ogreWindowEventListener_(new OgreWindowEventListener())
    9487        , renderWindow_(0)
    9588        , viewport_(0)
    96         , ogreWindowEventListener_(new OgreWindowEventListener())
    9789    {
    9890        RegisterObject(GraphicsManager);
     
    10092        this->setConfigValues();
    10193
    102         // Ogre setup procedure
    103         setupOgre();
    104 
    105         try
    106         {
    107             // load all the required plugins for Ogre
    108             loadOgrePlugins();
    109             // read resource declaration file
    110             this->declareResources();
    111             // Reads ogre config and creates the render window
    112             this->loadRenderer();
    113 
    114             // TODO: Spread this
    115             this->initialiseResources();
    116 
    117             // add console commands
    118             FunctorMember<GraphicsManager>* functor1 = createFunctor(&GraphicsManager::printScreen);
    119             functor1->setObject(this);
    120             ccPrintScreen_ = createConsoleCommand(functor1, "printScreen");
    121             CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
    122         }
    123         catch (...)
    124         {
    125             // clean up
    126             delete this->ogreRoot_;
    127             delete this->ogreLogger_;
    128             delete this->ogreWindowEventListener_;
    129             throw;
    130         }
    131     }
    132 
    133     /**
    134     @brief
    135         Destroys all the Ogre related objects
     94        // Ogre setup procedure (creating Ogre::Root)
     95        this->loadOgreRoot();
     96        // load all the required plugins for Ogre
     97        this->loadOgrePlugins();
     98        // read resource declaration file
     99        this->declareResources();
     100
     101        if (bLoadRenderer)
     102            this->upgradeToGraphics();
     103    }
     104
     105    /**
     106    @brief
     107        Destruction is done by the member scoped_ptrs.
    136108    */
    137109    GraphicsManager::~GraphicsManager()
    138110    {
    139 /*
    140         delete this->ccPrintScreen_;
    141 */
    142 
    143         // unload all compositors (this is only necessary because we don't yet destroy all resources!)
    144         Ogre::CompositorManager::getSingleton().removeAll();
    145 
    146         // Delete OGRE main control organ
    147         delete this->ogreRoot_;
    148 
    149         // delete the logManager (since we have created it in the first place).
    150         delete this->ogreLogger_;
    151 
    152         delete this->ogreWindowEventListener_;
    153111    }
    154112
     
    173131    }
    174132
    175     void GraphicsManager::update(const Clock& time)
    176     {
    177         Ogre::FrameEvent evt;
    178         evt.timeSinceLastFrame = time.getDeltaTime();
    179         evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
    180 
    181         // don't forget to call _fireFrameStarted to OGRE to make sure
    182         // everything goes smoothly
    183         ogreRoot_->_fireFrameStarted(evt);
    184 
    185         // Pump messages in all registered RenderWindows
    186         // This calls the WindowEventListener objects.
    187         Ogre::WindowEventUtilities::messagePump();
    188         // make sure the window stays active even when not focused
    189         // (probably only necessary on windows)
    190         this->renderWindow_->setActive(true);
    191 
    192         // Time before rendering
    193         uint64_t timeBeforeTick = time.getRealMicroseconds();
    194 
    195         // Render frame
    196         ogreRoot_->_updateAllRenderTargets();
    197 
    198         uint64_t timeAfterTick = time.getRealMicroseconds();
    199         // Subtract the time used for rendering from the tick time counter
    200         Game::getInstance().subtractTickTime(timeAfterTick - timeBeforeTick);
    201 
    202         // again, just to be sure OGRE works fine
    203         ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
    204     }
    205 
    206     void GraphicsManager::setCamera(Ogre::Camera* camera)
    207     {
    208         this->viewport_->setCamera(camera);
     133    /**
     134    @brief
     135        Loads the renderer and creates the render window if not yet done so.
     136    @remarks
     137        This operation is irreversible without recreating the GraphicsManager!
     138        So if it throws you HAVE to recreate the GraphicsManager!!!
     139        It therefore offers almost no exception safety.
     140    */
     141    void GraphicsManager::upgradeToGraphics()
     142    {
     143        if (renderWindow_ == NULL)
     144        {
     145            // Reads the ogre config and creates the render window
     146            this->loadRenderer();
     147            this->initialiseResources();
     148        }
    209149    }
    210150
     
    213153        Creates the Ogre Root object and sets up the ogre log.
    214154    */
    215     void GraphicsManager::setupOgre()
     155    void GraphicsManager::loadOgreRoot()
    216156    {
    217157        COUT(3) << "Setting up Ogre..." << std::endl;
     
    233173        // create a new logManager
    234174        // Ogre::Root will detect that we've already created a Log
    235         std::auto_ptr<Ogre::LogManager> logger(new Ogre::LogManager());
     175        ogreLogger_.reset(new Ogre::LogManager());
    236176        COUT(4) << "Ogre LogManager created" << std::endl;
    237177
    238178        // create our own log that we can listen to
    239179        Ogre::Log *myLog;
    240         myLog = logger->createLog(ogreLogFilepath.string(), true, false, false);
     180        myLog = ogreLogger_->createLog(ogreLogFilepath.string(), true, false, false);
    241181        COUT(4) << "Ogre Log created" << std::endl;
    242182
     
    256196
    257197        // Leave plugins file empty. We're going to do that part manually later
    258         ogreRoot_ = new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string());
    259         // In case that new Root failed the logger gets destroyed because of the std::auto_ptr
    260         ogreLogger_ = logger.release();
     198        ogreRoot_.reset(new Ogre::Root("", ogreConfigFilepath.string(), ogreLogFilepath.string()));
    261199
    262200        COUT(3) << "Ogre set up done." << std::endl;
     
    340278
    341279        this->renderWindow_ = ogreRoot_->initialise(true, "Orxonox");
     280        // Propagate the size of the new winodw
    342281        this->ogreWindowEventListener_->windowResized(renderWindow_);
    343282
    344         Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_);
    345 
    346         Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(0);
     283        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, ogreWindowEventListener_.get());
    347284
    348285        // create a full screen default viewport
     286        // Note: This may throw when adding a viewport with an existing z-order!
     287        //       But in our case we only have one viewport for now anyway, therefore
     288        //       no ScopeGuards or anything to handle exceptions.
    349289        this->viewport_ = this->renderWindow_->addViewport(0, 0);
     290
     291        // add console commands
     292        FunctorMember<GraphicsManager>* functor1 = createFunctor(&GraphicsManager::printScreen);
     293        ccPrintScreen_ = createConsoleCommand(functor1->setObject(this), "printScreen");
     294        CommandExecutor::addConsoleCommandShortcut(ccPrintScreen_);
    350295    }
    351296
     
    370315    }
    371316
     317    void GraphicsManager::update(const Clock& time)
     318    {
     319        Ogre::FrameEvent evt;
     320        evt.timeSinceLastFrame = time.getDeltaTime();
     321        evt.timeSinceLastEvent = time.getDeltaTime(); // note: same time, but shouldn't matter anyway
     322
     323        // don't forget to call _fireFrameStarted to OGRE to make sure
     324        // everything goes smoothly
     325        ogreRoot_->_fireFrameStarted(evt);
     326
     327        // Pump messages in all registered RenderWindows
     328        // This calls the WindowEventListener objects.
     329        Ogre::WindowEventUtilities::messagePump();
     330        // make sure the window stays active even when not focused
     331        // (probably only necessary on windows)
     332        this->renderWindow_->setActive(true);
     333
     334        // Time before rendering
     335        uint64_t timeBeforeTick = time.getRealMicroseconds();
     336
     337        // Render frame
     338        ogreRoot_->_updateAllRenderTargets();
     339
     340        uint64_t timeAfterTick = time.getRealMicroseconds();
     341        // Subtract the time used for rendering from the tick time counter
     342        Game::getInstance().subtractTickTime(timeAfterTick - timeBeforeTick);
     343
     344        // again, just to be sure OGRE works fine
     345        ogreRoot_->_fireFrameEnded(evt); // note: uses the same time as _fireFrameStarted
     346    }
     347
     348    void GraphicsManager::setCamera(Ogre::Camera* camera)
     349    {
     350        this->viewport_->setCamera(camera);
     351    }
     352
    372353    /**
    373354    @brief
  • code/branches/resource3/src/core/GraphicsManager.h

    r3370 r5677  
    4242#include <string>
    4343#include <OgreLog.h>
     44#include <boost/scoped_ptr.hpp>
     45
    4446#include "util/Singleton.h"
    4547#include "OrxonoxClass.h"
     
    4749namespace orxonox
    4850{
     51    using boost::scoped_ptr;
     52
    4953    /**
    5054    @brief
     
    5559        friend class Singleton<GraphicsManager>;
    5660    public:
    57         GraphicsManager();
     61        GraphicsManager(bool bLoadRenderer = true);
    5862        ~GraphicsManager();
    5963
     
    6266        void update(const Clock& time);
    6367
    64         inline Ogre::Viewport* getViewport()
    65             { return this->viewport_; }
    66         inline Ogre::RenderWindow* getRenderWindow()
    67             { return this->renderWindow_; }
     68        Ogre::Viewport* getViewport()         { return this->viewport_; }
     69        Ogre::RenderWindow* getRenderWindow() { return this->renderWindow_; }
     70
     71        void upgradeToGraphics();
     72        bool rendererLoaded() const { return renderWindow_ != NULL; }
    6873
    6974        void setCamera(Ogre::Camera* camera);
     
    7378
    7479        // OGRE initialisation
    75         void setupOgre();
     80        void loadOgreRoot();
    7681        void loadOgrePlugins();
    7782        void declareResources();
     
    8792
    8893    private:
    89         Ogre::Root*         ogreRoot_;                 //!< Ogre's root
    90         Ogre::LogManager*   ogreLogger_;
     94        scoped_ptr<OgreWindowEventListener> ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
     95        scoped_ptr<Ogre::LogManager>        ogreLogger_;
     96        scoped_ptr<Ogre::Root>              ogreRoot_;                //!< Ogre's root
    9197        Ogre::RenderWindow* renderWindow_;             //!< the one and only render window
    9298        Ogre::Viewport*     viewport_;                 //!< default full size viewport
    93         OgreWindowEventListener* ogreWindowEventListener_; //!< Pimpl to hide OgreWindowUtilities.h
    9499
    95100        // config values
  • code/branches/resource3/src/orxonox/gamestates/GSLevel.cc

    r3370 r5677  
    2929
    3030#include "GSLevel.h"
     31
     32#include <OgreCompositorManager.h>
    3133
    3234#include "core/input/InputManager.h"
     
    170172*/
    171173
     174        if (GameMode::showsGraphics())
     175        {
     176            // unload all compositors (this is only necessary because we don't yet destroy all resources!)
     177            Ogre::CompositorManager::getSingleton().removeAll();
     178        }
    172179
    173180        // this call will delete every BaseObject!
Note: See TracChangeset for help on using the changeset viewer.