Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3338


Ignore:
Timestamp:
Jul 23, 2009, 2:20:51 PM (15 years ago)
Author:
rgrieder
Message:

Improved exception-safety in the GUIManager.

Location:
code/branches/resource/src/orxonox
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/resource/src/orxonox/GraphicsManager.cc

    r3336 r3338  
    3737
    3838#include <fstream>
     39#include <memory>
    3940#include <boost/filesystem.hpp>
    4041#include <boost/shared_ptr.hpp>
  • code/branches/resource/src/orxonox/gamestates/GSGraphics.cc

    r3327 r3338  
    125125
    126126        // load the CEGUI interface
    127         guiManager_ = new GUIManager();
    128         guiManager_->initialise(renderWindow);
     127        guiManager_ = new GUIManager(renderWindow);
    129128
    130129        // add console command to toggle GUI
  • code/branches/resource/src/orxonox/gui/GUIManager.cc

    r3337 r3338  
    3636#include "GUIManager.h"
    3737
     38#include <memory>
    3839extern "C" {
    3940#include <lua.h>
     
    8990    GUIManager* GUIManager::singletonRef_s = 0;
    9091
    91     GUIManager::GUIManager()
    92         : renderWindow_(0)
    93         , guiRenderer_(0)
    94         , resourceProvider_(0)
    95         , scriptModule_(0)
    96         , guiSystem_(0)
    97         , state_(Uninitialised)
    98     {
    99         assert(singletonRef_s == 0);
    100         singletonRef_s = this;
    101     }
    102 
    103     /**
    104     @brief
    105         Deconstructor of the GUIManager
    106 
    107         Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine.
    108     */
    109     GUIManager::~GUIManager()
    110     {
    111         if (guiSystem_)
    112             delete guiSystem_;
    113 
    114         if (scriptModule_)
    115         {
    116             // destroy our own tolua interfaces
    117             lua_pushnil(luaState_);
    118             lua_setglobal(luaState_, "Orxonox");
    119             lua_pushnil(luaState_);
    120             lua_setglobal(luaState_, "Core");
    121             delete scriptModule_;
    122         }
    123 
    124         if (guiRenderer_)
    125             delete guiRenderer_;
    126 
    127         singletonRef_s = 0;
    128     }
    129 
    130     /**
    131     @brief
    132         Initialises the GUIManager by starting up CEGUI
    133     @param renderWindow
    134         Ogre's render window. Without this, the GUI cannot be displayed.
    135     @return true if success, otherwise false
    136 
    137         Before this call the GUIManager won't do anything, but can be accessed.
     92    /**
     93    @brief
     94        Constructs the GUIManager by starting up CEGUI
    13895
    13996        Creates the interface to Ogre, sets up the CEGUI renderer and the Lua script module together with the Lua engine.
     
    14198        After Lua setup tolua++-elements are linked to Lua-state to give Lua access to C++-code.
    14299        Finally initial Lua code is executed (maybe we can do this with the CEGUI startup script automatically).
    143     */
    144     bool GUIManager::initialise(Ogre::RenderWindow* renderWindow)
    145     {
     100    @param renderWindow
     101        Ogre's render window. Without this, the GUI cannot be displayed.
     102    @return true if success, otherwise false
     103    */
     104    GUIManager::GUIManager(Ogre::RenderWindow* renderWindow)
     105        : renderWindow_(renderWindow)
     106        , resourceProvider_(0)
     107    {
     108        assert(singletonRef_s == 0);
     109        singletonRef_s = this;
     110
    146111        using namespace CEGUI;
    147         if (state_ == Uninitialised)
    148         {
    149             COUT(3) << "Initialising CEGUI." << std::endl;
    150 
    151             try
    152             {
    153                 // save the render window
    154                 renderWindow_ = renderWindow;
    155 
    156                 // Note: No SceneManager specified yet
    157                 this->guiRenderer_ = new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000);
    158                 this->resourceProvider_ = guiRenderer_->createResourceProvider();
    159                 this->resourceProvider_->setDefaultResourceGroup("GUI");
    160 
    161                 // setup scripting
    162                 this->scriptModule_ = new LuaScriptModule();
    163                 this->luaState_ = this->scriptModule_->getLuaState();
    164 
    165                 // Create our own logger to specify the filepath
    166                 this->ceguiLogger_ = new CEGUILogger();
    167                 this->ceguiLogger_->setLogFilename(Core::getLogPathString() + "cegui.log");
    168                 // set the log level according to ours (translate by subtracting 1)
    169                 this->ceguiLogger_->setLoggingLevel(
    170                     (LoggingLevel)(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
    171 
    172                 // create the CEGUI system singleton
    173                 this->guiSystem_ = new System(this->guiRenderer_, this->resourceProvider_, 0, this->scriptModule_);
    174 
    175                 // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
    176                 tolua_Core_open(this->scriptModule_->getLuaState());
    177                 tolua_Orxonox_open(this->scriptModule_->getLuaState());
    178 
    179                 // initialise the basic lua code
    180                 loadLuaCode();
    181             }
    182             catch (CEGUI::Exception& ex)
    183             {
     112
     113        COUT(3) << "Initialising CEGUI." << std::endl;
     114
     115        try
     116        {
     117            // Note: No SceneManager specified yet
     118            guiRenderer_.reset(new OgreCEGUIRenderer(renderWindow_, Ogre::RENDER_QUEUE_OVERLAY, false, 3000));
     119            resourceProvider_ = guiRenderer_->createResourceProvider();
     120            resourceProvider_->setDefaultResourceGroup("GUI");
     121
     122            // setup scripting
     123            scriptModule_.reset(new LuaScriptModule());
     124            luaState_ = scriptModule_->getLuaState();
     125
     126            // Create our own logger to specify the filepath
     127            std::auto_ptr<CEGUILogger> ceguiLogger(new CEGUILogger());
     128            ceguiLogger->setLogFilename(Core::getLogPathString() + "cegui.log");
     129            // set the log level according to ours (translate by subtracting 1)
     130            ceguiLogger->setLoggingLevel(
     131                static_cast<LoggingLevel>(Core::getSoftDebugLevel(OutputHandler::LD_Logfile) - 1));
     132            this->ceguiLogger_ = ceguiLogger.release();
     133
     134            // create the CEGUI system singleton
     135            guiSystem_.reset(new System(guiRenderer_.get(), resourceProvider_, 0, scriptModule_.get()));
     136
     137            // do this after 'new CEGUI::Sytem' because that creates the lua state in the first place
     138            tolua_Core_open(this->scriptModule_->getLuaState());
     139            tolua_Orxonox_open(this->scriptModule_->getLuaState());
     140
     141            // initialise the basic lua code
     142            this->loadLuaCode();
     143        }
     144        catch (CEGUI::Exception& ex)
     145        {
    184146#if CEGUI_VERSION_MAJOR == 0 && CEGUI_VERSION_MINOR < 6
    185                 throw GeneralException(ex.getMessage().c_str());
     147            throw GeneralException(ex.getMessage().c_str());
    186148#else
    187                 throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
    188                     ex.getFileName().c_str(), ex.getName().c_str());
     149            throw GeneralException(ex.getMessage().c_str(), ex.getLine(),
     150                ex.getFileName().c_str(), ex.getName().c_str());
    189151#endif
    190             }
    191 
    192             state_ = Ready;
    193         }
    194 
    195         return true;
     152        }
     153    }
     154
     155    /**
     156    @brief
     157        Destructor of the GUIManager
     158
     159        Basically shuts down CEGUI and destroys the Lua engine and afterwards the interface to the Ogre engine.
     160    */
     161    GUIManager::~GUIManager()
     162    {
     163        // destroy our own tolua interfaces
     164        lua_pushnil(luaState_);
     165        lua_setglobal(luaState_, "Orxonox");
     166        lua_pushnil(luaState_);
     167        lua_setglobal(luaState_, "Core");
     168
     169        singletonRef_s = 0;
    196170    }
    197171
     
    245219
    246220    /**
    247     @brief
    248         Executes Lua code
    249     @param str
    250         reference to string object holding the Lua code which is to be executed
    251 
    252         This function gives total access to the GUI. You can execute ANY Lua code here.
    253     */
    254     void GUIManager::executeCode(const std::string& str)
    255     {
    256         try
    257         {
    258             this->scriptModule_->executeString(str);
    259         }
    260         catch (CEGUI::Exception& ex)
    261         {
    262             COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
    263         }
    264     }
    265 
    266     /**
    267221
    268222    */
     
    305259    /**
    306260    @brief
     261        Executes Lua code
     262    @param str
     263        reference to string object holding the Lua code which is to be executed
     264
     265        This function gives total access to the GUI. You can execute ANY Lua code here.
     266    */
     267    void GUIManager::executeCode(const std::string& str)
     268    {
     269        try
     270        {
     271            this->scriptModule_->executeString(str);
     272        }
     273        catch (const CEGUI::Exception& ex)
     274        {
     275            COUT(2) << "CEGUI Error: \"" << ex.getMessage() << "\" while executing code \"" << str << "\"" << std::endl;
     276        }
     277        catch (...)
     278        {
     279            COUT(2) << "Couldn't execute GUI related Lua code due to unknown reasons." << std::endl;
     280        }
     281    }
     282
     283    /**
     284    @brief
    307285        Displays specified GUI on screen
    308286    @param name
     
    314292    void GUIManager::showGUI(const std::string& name)
    315293    {
    316         if (state_ != Uninitialised)
    317         {
    318             //COUT(3) << "Loading GUI " << name << std::endl;
    319             try
    320             {
    321                 this->scriptModule_->executeString(std::string("showGUI(\"") + name + "\")");
    322             }
    323             catch (CEGUI::Exception& ex)
    324             {
    325                 COUT(2) << "Error while executing lua script. Message:\n" << ex.getMessage() << std::endl;
    326             }
    327             catch (...)
    328             {
    329                 COUT(2) << "Could show a menu due to unknown reasons." << std::endl;
    330             }
    331         }
    332         else
    333         {
    334             COUT(2) << "Warning: GUI Manager not yet initialised, cannot load a GUI" << std::endl;
    335         }
     294        this->executeCode(std::string("showGUI(\"") + name + "\")");
    336295    }
    337296
  • code/branches/resource/src/orxonox/gui/GUIManager.h

    r3337 r3338  
    4242#include <string>
    4343#include <CEGUIForwardRefs.h>
     44#include <boost/scoped_ptr.hpp>
    4445
    4546#include "util/OgreForwardRefs.h"
     
    6768// tolua_end
    6869    public:
    69         /**
    70         @enum State
    71             The current state of the GUIManager. There should maybe be more (or we can omit this totally).
    72         */
    73         enum State
    74         {
    75             Uninitialised,  //!< Initial state of the GUIManager
    76             Ready,          //!< State after initialisation if ready
    77             OnDisplay       //!< State if GUI is displayed
    78         };
    79 
    80         GUIManager();
     70        GUIManager(Ogre::RenderWindow* renderWindow);
    8171        ~GUIManager();
    82 
    83         bool initialise(Ogre::RenderWindow* renderWindow);
    8472
    8573        void update(const Clock& time);
     
    11098        void mouseScrolled (int abs, int rel);
    11199
    112         Ogre::RenderWindow*         renderWindow_;      //!< Ogre's render window to give CEGUI access to it
    113         CEGUI::OgreCEGUIRenderer*   guiRenderer_;       //!< CEGUI's interface to the Ogre Engine
    114         CEGUI::ResourceProvider*    resourceProvider_;  //!< CEGUI's resource provider
    115         CEGUI::LuaScriptModule*     scriptModule_;      //!< CEGUI's script module to use Lua
    116         CEGUI::Logger*              ceguiLogger_;       //!< CEGUI's logger to be able to log CEGUI errors in our log
    117         CEGUI::System*              guiSystem_;         //!< CEGUI's main system
    118         lua_State*                  luaState_;          //!< Lua state, access point to the Lua engine
     100        boost::scoped_ptr<CEGUI::OgreCEGUIRenderer> guiRenderer_;  //!< CEGUI's interface to the Ogre Engine
     101        boost::scoped_ptr<CEGUI::LuaScriptModule>   scriptModule_; //!< CEGUI's script module to use Lua
     102        boost::scoped_ptr<CEGUI::System>            guiSystem_;    //!< CEGUI's main system
     103        Ogre::RenderWindow*      renderWindow_;     //!< Ogre's render window to give CEGUI access to it
     104        CEGUI::ResourceProvider* resourceProvider_; //!< CEGUI's resource provider
     105        CEGUI::Logger*           ceguiLogger_;      //!< CEGUI's logger to be able to log CEGUI errors in our log
     106        lua_State*               luaState_;         //!< Lua state, access point to the Lua engine
    119107
    120         State                       state_;             //!< reflects state of the GUIManager
    121 
    122         static GUIManager*          singletonRef_s;     //!< Singleton reference to GUIManager
     108        static GUIManager*       singletonRef_s;    //!< Singleton reference to GUIManager
    123109
    124110    }; // tolua_export
Note: See TracChangeset for help on using the changeset viewer.