Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 31, 2008, 2:37:00 PM (16 years ago)
Author:
rgrieder
Message:

Moved most of the GraphicsEngine code to GSRoot and GSGraphics.
GraphicsEngine is now more of a legacy object to ensure functionality until there is a new solution.

Location:
code/branches/gui/src/orxonox/gamestates
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • code/branches/gui/src/orxonox/gamestates/GSGUI.cc

    r1674 r1686  
    3030#include "GSGUI.h"
    3131
     32#include <OgreViewport.h>
    3233#include "GraphicsEngine.h"
    3334#include "core/input/InputManager.h"
  • code/branches/gui/src/orxonox/gamestates/GSGraphics.cc

    r1674 r1686  
    3030#include "GSGraphics.h"
    3131
     32#include <fstream>
     33#include <OgreConfigFile.h>
    3234#include <OgreFrameListener.h>
    3335#include <OgreRoot.h>
     36#include <OgreException.h>
     37#include <OgreRenderWindow.h>
     38#include <OgreTextureManager.h>
     39#include <OgreViewport.h>
    3440#include <OgreWindowEventUtilities.h>
    35 #include <OgreRenderWindow.h>
    36 
     41
     42#include "core/Debug.h"
    3743#include "core/ConsoleCommand.h"
    3844#include "core/ConfigValueIncludes.h"
     45#include "core/CoreIncludes.h"
    3946#include "core/input/InputManager.h"
     47#include "core/Exception.h"
    4048#include "overlays/console/InGameConsole.h"
    4149#include "gui/GUIManager.h"
     50#include "tools/WindowEventListener.h"
     51#include "Settings.h"
     52
     53// for compatibility
    4254#include "GraphicsEngine.h"
    4355
     
    4759        : GameState("graphics")
    4860        , ogreRoot_(0)
    49         , graphicsEngine_(0)
    5061        , inputManager_(0)
    5162        , console_(0)
     
    5768        , tickTime_(0)
    5869    {
     70        RegisterRootObject(GSGraphics);
    5971    }
    6072
     
    6577    void GSGraphics::setConfigValues()
    6678    {
     79        SetConfigValue(resourceFile_,    "resources.cfg").description("Location of the resources file in the data path.");
    6780        SetConfigValue(statisticsRefreshCycle_, 200000).description("Sets the time in microseconds interval at which average fps, etc. get updated.");
    6881    }
     
    7386
    7487        this->ogreRoot_ = Ogre::Root::getSingletonPtr();
    75         this->graphicsEngine_ = GraphicsEngine::getInstancePtr();
    76 
    77         graphicsEngine_->loadRenderer();    // creates the render window
    78 
     88
     89        this->declareResources();
     90        this->loadRenderer();    // creates the render window
    7991        // TODO: Spread this so that this call only initialises things needed for the Console and GUI
    80         graphicsEngine_->initialiseResources();
     92        this->initialiseResources();
     93
     94
     95        // HACK: temporary:
     96        GraphicsEngine& graphicsEngine = GraphicsEngine::getInstance();
     97        graphicsEngine.renderWindow_ = this->renderWindow_;
     98        graphicsEngine.root_ = this->ogreRoot_;
     99        graphicsEngine.viewport_ = this->viewport_;
     100
    81101
    82102        // Calls the InputManager which sets up the input devices.
    83103        // The render window width and height are used to set up the mouse movement.
    84104        inputManager_ = new InputManager();
    85         inputManager_->initialise(graphicsEngine_->getWindowHandle(),
    86             graphicsEngine_->getWindowWidth(), graphicsEngine_->getWindowHeight(), true);
     105        size_t windowHnd = 0;
     106        this->renderWindow_->getCustomAttribute("WINDOW", &windowHnd);
     107        inputManager_->initialise(windowHnd, renderWindow_->getWidth(), renderWindow_->getHeight(), true);
    87108
    88109        // Load the InGameConsole
     
    92113        // load the CEGUI interface
    93114        guiManager_ = new GUIManager();
    94         guiManager_->initialise();
     115        guiManager_->initialise(this->renderWindow_);
    95116
    96117        // reset frame counter
     
    99120        statisticsStartTime_ = 0;
    100121        statisticsStartCount_ = 0;
     122
     123        // add console commands
     124        FunctorMember<GSGraphics>* functor1 = createFunctor(&GSGraphics::printScreen);
     125        functor1->setObject(this);
     126        CommandExecutor::addConsoleCommandShortcut(createConsoleCommand(functor1, "printScreen"));
    101127    }
    102128
     
    109135        delete this->inputManager_;
    110136
    111         this->ogreRoot_->detachRenderTarget(GraphicsEngine::getInstance().getRenderWindow());
    112         delete GraphicsEngine::getInstance().getRenderWindow();
     137        this->ogreRoot_->detachRenderTarget(this->renderWindow_);
     138        delete this->renderWindow_;
    113139        //this->ogreRoot_->shutdown
    114140        // TODO: destroy render window
     
    166192        // make sure the window stays active even when not focused
    167193        // (probably only necessary on windows)
    168         GraphicsEngine::getInstance().setWindowActivity(true);
     194        this->renderWindow_->setActive(true);
    169195
    170196        // render
     
    176202        ++frameCount_;
    177203    }
     204
     205    void GSGraphics::declareResources()
     206    {
     207        CCOUT(4) << "Declaring Resources" << std::endl;
     208        //TODO: Specify layout of data file and maybe use xml-loader
     209        //TODO: Work with ressource groups (should be generated by a special loader)
     210
     211        if (resourceFile_ == "")
     212        {
     213            COUT(2) << "Warning: Ogre resource file set to \"\". Defaulting to resources.cfg" << std::endl;
     214            ModifyConfigValue(resourceFile_, tset, "resources.cfg");
     215        }
     216
     217        // Load resource paths from data file using configfile ressource type
     218        Ogre::ConfigFile cf;
     219        try
     220        {
     221            cf.load(Settings::getDataPath() + resourceFile_);
     222        }
     223        catch (...)
     224        {
     225            //COUT(1) << ex.getFullDescription() << std::endl;
     226            COUT(0) << "Have you forgotten to set the data path in orxnox.ini?" << std::endl;
     227            throw;
     228        }
     229
     230        // Go through all sections & settings in the file
     231        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
     232
     233        std::string secName, typeName, archName;
     234        while (seci.hasMoreElements())
     235        {
     236            try
     237            {
     238                secName = seci.peekNextKey();
     239                Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
     240                Ogre::ConfigFile::SettingsMultiMap::iterator i;
     241                for (i = settings->begin(); i != settings->end(); ++i)
     242                {
     243                    typeName = i->first; // for instance "FileSystem" or "Zip"
     244                    archName = i->second; // name (and location) of archive
     245
     246                    Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
     247                        std::string(Settings::getDataPath() + archName), typeName, secName);
     248                }
     249            }
     250            catch (Ogre::Exception& ex)
     251            {
     252                COUT(1) << ex.getFullDescription() << std::endl;
     253            }
     254        }
     255    }
     256
     257    void GSGraphics::loadRenderer()
     258    {
     259        CCOUT(4) << "Configuring Renderer" << std::endl;
     260
     261        if (!ogreRoot_->restoreConfig())
     262            if (!ogreRoot_->showConfigDialog())
     263                ThrowException(InitialisationFailed, "Could not show Ogre configuration dialogue.");
     264
     265        CCOUT(4) << "Creating render window" << std::endl;
     266
     267        this->renderWindow_ = ogreRoot_->initialise(true, "OrxonoxV2");
     268
     269        Ogre::WindowEventUtilities::addWindowEventListener(this->renderWindow_, this);
     270
     271        //Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
     272
     273        // create a full screen default viewport
     274        this->viewport_ = this->renderWindow_->addViewport(0, 0);
     275    }
     276
     277    void GSGraphics::initialiseResources()
     278    {
     279        CCOUT(4) << "Initialising resources" << std::endl;
     280        //TODO: Do NOT load all the groups, why are we doing that? And do we really do that? initialise != load...
     281        try
     282        {
     283            Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
     284            /*Ogre::StringVector str = Ogre::ResourceGroupManager::getSingleton().getResourceGroups();
     285            for (unsigned int i = 0; i < str.size(); i++)
     286            {
     287            Ogre::ResourceGroupManager::getSingleton().loadResourceGroup(str[i]);
     288            }*/
     289        }
     290        catch (...)
     291        {
     292            CCOUT(2) << "Error: There was a serious error when initialising the resources." << std::endl;
     293            throw;
     294        }
     295    }
     296
     297
     298    /**
     299    @brief
     300        Window has moved.
     301    @param rw
     302        The render window it occured in
     303    */
     304    void GSGraphics::windowMoved(Ogre::RenderWindow *rw)
     305    {
     306        for (Iterator<orxonox::WindowEventListener> it = ObjectList<orxonox::WindowEventListener>::start(); it; ++it)
     307            it->windowMoved();
     308    }
     309
     310    /**
     311    @brief
     312        Window has resized.
     313    @param rw
     314        The render window it occured in
     315    @note
     316        GraphicsEngine has a render window stored itself. This is the same
     317        as rw. But we have to be careful when using multiple render windows!
     318    */
     319    void GSGraphics::windowResized(Ogre::RenderWindow *rw)
     320    {
     321        for (Iterator<orxonox::WindowEventListener> it = ObjectList<orxonox::WindowEventListener>::start(); it; ++it)
     322            it->windowResized(this->renderWindow_->getWidth(), this->renderWindow_->getHeight());
     323    }
     324
     325    /**
     326    @brief
     327        Window focus has changed.
     328    @param rw
     329        The render window it occured in
     330    */
     331    void GSGraphics::windowFocusChanged(Ogre::RenderWindow *rw)
     332    {
     333        for (Iterator<orxonox::WindowEventListener> it = ObjectList<orxonox::WindowEventListener>::start(); it; ++it)
     334            it->windowFocusChanged();
     335    }
     336
     337    /**
     338    @brief
     339        Window was closed.
     340    @param rw
     341        The render window it occured in
     342    */
     343    void GSGraphics::windowClosed(Ogre::RenderWindow *rw)
     344    {
     345        // using CommandExecutor in order to avoid depending on Orxonox.h.
     346        //CommandExecutor::execute("exit", false);
     347        this->requestState("root");
     348    }
     349
     350    void GSGraphics::printScreen()
     351    {
     352        if (this->renderWindow_)
     353        {
     354            this->renderWindow_->writeContentsToTimestampedFile("shot_", ".jpg");
     355        }
     356    }
    178357}
  • code/branches/gui/src/orxonox/gamestates/GSGraphics.h

    r1674 r1686  
    3232#include "OrxonoxPrereqs.h"
    3333#include <OgrePrerequisites.h>
     34#include <OgreWindowEventUtilities.h>
    3435#include "core/GameState.h"
    3536#include "core/OrxonoxClass.h"
     
    3738namespace orxonox
    3839{
    39     class _OrxonoxExport GSGraphics : public GameState, public OrxonoxClass
     40    class _OrxonoxExport GSGraphics : public GameState, public OrxonoxClass, public Ogre::WindowEventListener
    4041    {
     42        friend class ClassIdentifier<GSGraphics>;
    4143    public:
    4244        GSGraphics();
    4345        ~GSGraphics();
    44 
    45         void setConfigValues();
    4646
    4747    private:
     
    5050        void ticked(const Clock& time);
    5151
     52        void setConfigValues();
     53
     54        void declareResources();
     55        void loadRenderer();
     56        void initialiseResources();
     57
     58        void printScreen();
     59
     60        // window events from Ogre::WindowEventListener
     61        void windowMoved       (Ogre::RenderWindow* rw);
     62        void windowResized     (Ogre::RenderWindow* rw);
     63        void windowFocusChanged(Ogre::RenderWindow* rw);
     64        void windowClosed      (Ogre::RenderWindow* rw);
     65
    5266        Ogre::Root*           ogreRoot_;
    53         GraphicsEngine*       graphicsEngine_;   //!< pointer to GraphicsEngine instance
     67        Ogre::RenderWindow*   renderWindow_;          //!< the current render window
     68        Ogre::Viewport*       viewport_;              //!< default full size viewport
     69
     70        // managed singletons
    5471        InputManager*         inputManager_;
    5572        InGameConsole*        console_;
     
    6279        unsigned long         statisticsStartCount_;
    6380        unsigned int          tickTime_;
     81
     82        // config values
     83        std::string           resourceFile_;          //!< resources file name
     84        unsigned int          detailLevelParticle_;   //!< Detail level of particle effects (0: off, 1: low, 2: normal, 3: high)
    6485    };
    6586}
  • code/branches/gui/src/orxonox/gamestates/GSLevel.cc

    r1674 r1686  
    3030#include "GSLevel.h"
    3131
     32#include <OgreSceneManager.h>
     33#include <OgreRoot.h>
    3234#include "core/input/InputManager.h"
    3335#include "core/input/SimpleInputState.h"
     
    4547        : GameState(name)
    4648        , timeFactor_(1.0f)
     49        , sceneManager_(0)
    4750        , keyBinder_(0)
    4851        , inputState_(0)
     
    6568
    6669        // create Ogre SceneManager for the level
    67         GraphicsEngine::getInstance().createNewScene();
     70        this->sceneManager_ = GraphicsEngine::getInstance().getOgreRoot()->
     71            createSceneManager(Ogre::ST_GENERIC, "LevelSceneManager");
     72        COUT(4) << "Created SceneManager: " << sceneManager_->getName() << std::endl;
     73        GraphicsEngine::getInstance().setLevelSceneManager(this->sceneManager_);
    6874
    6975        // Start the Radar
     
    9298        delete this->radar_;
    9399
    94         // TODO: delete SceneManager
     100        GraphicsEngine::getInstance().getOgreRoot()->destroySceneManager(this->sceneManager_);
    95101
    96102        inputState_->setHandler(0);
  • code/branches/gui/src/orxonox/gamestates/GSLevel.h

    r1674 r1686  
    3131
    3232#include "OrxonoxPrereqs.h"
     33#include <OgrePrerequisites.h>
    3334#include "core/GameState.h"
    3435
     
    5859        float timeFactor_;       //!< A factor to change the gamespeed
    5960
     61        Ogre::SceneManager*   sceneManager_;
    6062        KeyBinder*            keyBinder_;        //!< tool that loads and manages the input bindings
    6163        SimpleInputState*     inputState_;
  • code/branches/gui/src/orxonox/gamestates/GSRoot.cc

    r1674 r1686  
    3030#include "GSRoot.h"
    3131
     32#include <OgreLogManager.h>
     33#include <OgreRoot.h>
     34
    3235//#include "util/SubString.h"
    3336#include "core/Factory.h"
    3437#include "core/ConfigFileManager.h"
    3538#include "core/ConfigValueIncludes.h"
     39#include "core/CoreIncludes.h"
    3640#include "core/ConsoleCommand.h"
    3741#include "core/CommandLine.h"
     
    6569        : RootGameState("root")
    6670        , settings_(0)
     71        , ogreRoot_(0)
     72        , ogreLogger_(0)
    6773        , graphicsEngine_(0)
    6874    {
     75        RegisterRootObject(GSRoot);
    6976    }
    7077
    7178    GSRoot::~GSRoot()
    7279    {
     80    }
     81
     82    void GSRoot::setConfigValues()
     83    {
     84        SetConfigValue(ogreConfigFile_,  "ogre.cfg").description("Location of the Ogre config file");
     85        SetConfigValue(ogrePluginsFile_, "plugins.cfg").description("Location of the Ogre plugins file");
     86        SetConfigValue(ogreLogFile_,     "ogre.log").description("Logfile for messages from Ogre. \
     87                                                                 Use \"\" to suppress log file creation.");
     88        SetConfigValue(ogreLogLevelTrivial_ , 5).description("Corresponding orxonox debug level for ogre Trivial");
     89        SetConfigValue(ogreLogLevelNormal_  , 4).description("Corresponding orxonox debug level for ogre Normal");
     90        SetConfigValue(ogreLogLevelCritical_, 2).description("Corresponding orxonox debug level for ogre Critical");
    7391    }
    7492
     
    108126#endif
    109127
     128        // do this after the previous call..
     129        setConfigValues();
     130
    110131        // creates the class hierarchy for all classes with factories
    111132        Factory::createClassHierarchy();
     
    128149        TclThreadManager::getInstance();
    129150
     151        setupOgre();
     152
    130153        // initialise graphics engine. Doesn't load the render window yet!
    131154        graphicsEngine_ = new GraphicsEngine();
    132         graphicsEngine_->setup();       // creates ogre root and other essentials
    133155
    134156        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
     
    151173    {
    152174        delete graphicsEngine_;
     175
     176        delete this->ogreRoot_;
     177
     178#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
     179        // delete the ogre log and the logManager (since we have created it).
     180        this->ogreLogger_->getDefaultLog()->removeListener(this);
     181        this->ogreLogger_->destroyLog(Ogre::LogManager::getSingleton().getDefaultLog());
     182        delete this->ogreLogger_;
     183#endif
     184
    153185        delete settings_;
    154186
     
    199231#endif
    200232    }
     233
     234    /**
     235    @brief
     236        Creates the Ogre Root object and sets up the ogre log.
     237    */
     238    void GSRoot::setupOgre()
     239    {
     240        COUT(3) << "Setting up Ogre..." << std::endl;
     241
     242        // TODO: LogManager doesn't work on oli platform. The why is yet unknown.
     243#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
     244        // create a new logManager
     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        if (this->ogreLogFile_ == "")
     251            myLog = ogreLogger_->createLog("ogre.log", true, false, true);
     252        else
     253            myLog = ogreLogger_->createLog(this->ogreLogFile_, true, false, false);
     254        COUT(4) << "Ogre Log created" << std::endl;
     255
     256        myLog->setLogDetail(Ogre::LL_BOREME);
     257        myLog->addListener(this);
     258#endif
     259
     260        // Root will detect that we've already created a Log
     261        COUT(4) << "Creating Ogre Root..." << std::endl;
     262
     263        if (ogrePluginsFile_ == "")
     264        {
     265            COUT(2) << "Warning: Ogre plugins file set to \"\". Defaulting to plugins.cfg" << std::endl;
     266            ModifyConfigValue(ogrePluginsFile_, tset, "plugins.cfg");
     267        }
     268        if (ogreConfigFile_ == "")
     269        {
     270            COUT(2) << "Warning: Ogre config file set to \"\". Defaulting to config.cfg" << std::endl;
     271            ModifyConfigValue(ogreConfigFile_, tset, "config.cfg");
     272        }
     273        if (ogreLogFile_ == "")
     274        {
     275            COUT(2) << "Warning: Ogre log file set to \"\". Defaulting to ogre.log" << std::endl;
     276            ModifyConfigValue(ogreLogFile_, tset, "ogre.log");
     277        }
     278
     279        // check for config file existence because Ogre displays (caught) exceptions if not
     280        std::ifstream probe;
     281        probe.open(ogreConfigFile_.c_str());
     282        if (!probe)
     283        {
     284            // create a zero sized file
     285            std::ofstream creator;
     286            creator.open(ogreConfigFile_.c_str());
     287            creator.close();
     288        }
     289        else
     290            probe.close();
     291
     292        ogreRoot_ = new Ogre::Root(ogrePluginsFile_, ogreConfigFile_, ogreLogFile_);
     293
     294#if 0 // Ogre 1.4.3 doesn't yet support setDebugOutputEnabled(.)
     295#if ORXONOX_PLATFORM != ORXONOX_PLATFORM_WIN32
     296        // tame the ogre ouput so we don't get all the mess in the console
     297        Ogre::Log* defaultLog = Ogre::LogManager::getSingleton().getDefaultLog();
     298        defaultLog->setDebugOutputEnabled(false);
     299        defaultLog->setLogDetail(Ogre::LL_BOREME);
     300        defaultLog->addListener(this);
     301#endif
     302#endif
     303
     304        COUT(3) << "Ogre set up done." << std::endl;
     305    }
     306
     307    /**
     308    @brief
     309        Method called by the LogListener interface from Ogre.
     310        We use it to capture Ogre log messages and handle it ourselves.
     311    @param message
     312        The message to be logged
     313    @param lml
     314        The message level the log is using
     315    @param maskDebug
     316        If we are printing to the console or not
     317    @param logName
     318        The name of this log (so you can have several listeners
     319        for different logs, and identify them)
     320    */
     321    void GSRoot::messageLogged(const std::string& message,
     322        Ogre::LogMessageLevel lml, bool maskDebug, const std::string& logName)
     323    {
     324        int orxonoxLevel;
     325        switch (lml)
     326        {
     327        case Ogre::LML_TRIVIAL:
     328            orxonoxLevel = this->ogreLogLevelTrivial_;
     329            break;
     330        case Ogre::LML_NORMAL:
     331            orxonoxLevel = this->ogreLogLevelNormal_;
     332            break;
     333        case Ogre::LML_CRITICAL:
     334            orxonoxLevel = this->ogreLogLevelCritical_;
     335            break;
     336        default:
     337            orxonoxLevel = 0;
     338        }
     339        OutputHandler::getOutStream().setOutputLevel(orxonoxLevel)
     340            << "Ogre: " << message << std::endl;
     341    }
    201342}
  • code/branches/gui/src/orxonox/gamestates/GSRoot.h

    r1674 r1686  
    3131
    3232#include "OrxonoxPrereqs.h"
     33#include <OgreLog.h>
    3334#include "core/RootGameState.h"
     35#include "core/OrxonoxClass.h"
    3436
    3537namespace orxonox
    3638{
    37     class _OrxonoxExport GSRoot : public RootGameState
     39    class _OrxonoxExport GSRoot : public RootGameState, public Ogre::LogListener, public OrxonoxClass
    3840    {
     41        friend class ClassIdentifier<GSRoot>;
    3942    public:
    4043        GSRoot();
     
    5154        void ticked(const Clock& time);
    5255
     56        void setConfigValues();
     57        void messageLogged(const std::string& message, Ogre::LogMessageLevel lml,
     58            bool maskDebug, const std::string& logName);
    5359        void setThreadAffinity();
     60        void setupOgre();
    5461
    5562        Settings*             settings_;
     63        Ogre::Root*           ogreRoot_;                  //!< Ogre's root
     64        Ogre::LogManager*     ogreLogger_;
    5665        GraphicsEngine*       graphicsEngine_;   //!< Interface to Ogre
     66
     67        std::string           ogreConfigFile_;        //!< ogre config file name
     68        std::string           ogrePluginsFile_;       //!< ogre plugins file name
     69        std::string           ogreLogFile_;           //!< log file name for Ogre log messages
     70        int                   ogreLogLevelTrivial_;   //!< Corresponding Orxonx debug level for LL_TRIVIAL
     71        int                   ogreLogLevelNormal_;    //!< Corresponding Orxonx debug level for LL_NORMAL
     72        int                   ogreLogLevelCritical_;  //!< Corresponding Orxonx debug level for LL_CRITICAL
    5773    };
    5874}
Note: See TracChangeset for help on using the changeset viewer.