Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 26, 2009, 2:15:08 PM (15 years ago)
Author:
rgrieder
Message:

Loading and unloading graphics automatically: As soon as a GameState requires graphics (defined at the GameState declaration with a bool) it gets loaded. And vice versa.

Location:
code/branches/resource/src
Files:
25 edited

Legend:

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

    r3349 r3355  
    364364        guiManager_ = new GUIManager(renderWindow);
    365365
    366         GameMode::setShowsGraphics(true);
    367366        bGraphicsLoaded_ = true;
    368367    }
     
    378377
    379378        bGraphicsLoaded_ = false;
    380         GameMode::setShowsGraphics(false);
    381379    }
    382380
  • code/branches/resource/src/core/CorePrereqs.h

    r3346 r3355  
    170170    // game states
    171171    class Game;
    172     struct GameStateConstrParams;
    173172    class GameState;
     173    struct GameStateInfo;
    174174    struct GameStateTreeNode;
    175175
  • code/branches/resource/src/core/Game.cc

    r3352 r3355  
    4848#include "CoreIncludes.h"
    4949#include "ConfigValueIncludes.h"
     50#include "GameMode.h"
    5051#include "GameState.h"
    5152
     
    5960    SetConsoleCommandShortcutExternAlias(stop_game, "exit");
    6061
    61     std::map<std::string, Game::GameStateInfo> Game::gameStateDeclarations_s;
     62    std::map<std::string, GameStateInfo> Game::gameStateDeclarations_s;
    6263    Game* Game::singletonRef_s = 0;
    6364
     
    143144            // Only create the states appropriate for the game mode
    144145            //if (GameMode::showsGraphics || !it->second.bGraphicsMode)
    145             GameStateConstrParams params = { it->second.stateName, it->second.bIgnoreTickTime };
    146             gameStates_[getLowercase(it->second.stateName)] = GameStateFactory::fabricate(it->second.className, params);
     146            gameStates_[getLowercase(it->second.stateName)] = GameStateFactory::fabricate(it->second);
    147147        }
    148148
     
    292292                // Add tick time for most of the states
    293293                uint64_t timeBeforeTick;
    294                 if ((*it)->ignoreTickTime())
     294                if ((*it)->getInfo().bIgnoreTickTime)
    295295                    timeBeforeTick = this->gameClock_->getRealMicroseconds();
    296296                (*it)->update(*this->gameClock_);
    297                 if ((*it)->ignoreTickTime())
     297                if ((*it)->getInfo().bIgnoreTickTime)
    298298                    this->subtractTickTime(static_cast<int32_t>(this->gameClock_->getRealMicroseconds() - timeBeforeTick));
    299299            }
     
    517517    /*** Internal ***/
    518518
     519    void Game::loadGraphics()
     520    {
     521        if (!GameMode::bShowsGraphics_s)
     522        {
     523            core_->loadGraphics();
     524            GameMode::bShowsGraphics_s = true;
     525        }
     526    }
     527
     528    void Game::unloadGraphics()
     529    {
     530        if (GameMode::bShowsGraphics_s)
     531        {
     532            core_->unloadGraphics();
     533            GameMode::bShowsGraphics_s = false;
     534        }
     535    }
     536
    519537    void Game::loadState(GameState* state)
    520538    {
    521539        this->bChangingState_ = true;
     540        // If state requires graphics, load it
     541        if (state->getInfo().bGraphicsMode)
     542            this->loadGraphics();
    522543        state->activate();
    523544        if (!this->activeStates_.empty())
     
    538559        {
    539560            state->deactivate();
     561            // Check if graphis is still required
     562            bool graphicsRequired = false;
     563            for (unsigned i = 0; i < activeStates_.size(); ++i)
     564                graphicsRequired |= activeStates_[i]->getInfo().bGraphicsMode;
     565            if (!graphicsRequired)
     566                this->unloadGraphics();
    540567        }
    541568        catch (const std::exception& ex)
     
    549576    std::map<std::string, Game::GameStateFactory*> Game::GameStateFactory::factories_s;
    550577
    551     /*static*/ GameState* Game::GameStateFactory::fabricate(const std::string& className, const GameStateConstrParams& params)
    552     {
    553         std::map<std::string, GameStateFactory*>::const_iterator it = factories_s.find(className);
     578    /*static*/ GameState* Game::GameStateFactory::fabricate(const GameStateInfo& info)
     579    {
     580        std::map<std::string, GameStateFactory*>::const_iterator it = factories_s.find(info.className);
    554581        assert(it != factories_s.end());
    555         return it->second->fabricate(params);
     582        return it->second->fabricateInternal(info);
    556583    }
    557584
  • code/branches/resource/src/core/Game.h

    r3352 r3355  
    6161    class GameConfiguration;
    6262
     63    //! Helper object required before GameStates are being constructed
     64    struct GameStateInfo
     65    {
     66        std::string stateName;
     67        std::string className;
     68        bool bIgnoreTickTime;
     69        bool bGraphicsMode;
     70    };
     71
    6372    /**
    6473    @brief
     
    97106        public:
    98107            virtual ~GameStateFactory() { }
    99             static GameState* fabricate(const std::string& className, const GameStateConstrParams& params);
     108            static GameState* fabricate(const GameStateInfo& info);
    100109            template <class T>
    101110            static void createFactory(const std::string& className)
     
    103112            static void destroyFactories();
    104113        private:
    105             virtual GameState* fabricate(const GameStateConstrParams& params) = 0;
     114            virtual GameState* fabricateInternal(const GameStateInfo& info) = 0;
    106115            static std::map<std::string, GameStateFactory*> factories_s;
    107116        };
     
    110119        {
    111120        public:
    112             GameState* fabricate(const GameStateConstrParams& params)
    113                 { return new T(params); }
    114         };
    115 
    116         struct GameStateInfo
    117         {
    118             std::string stateName;
    119             std::string className;
    120             bool bIgnoreTickTime;
    121             bool bGraphicsMode;
     121            GameState* fabricateInternal(const GameStateInfo& info)
     122                { return new T(info); }
    122123        };
    123124
     
    129130
    130131        Game(Game&); // don't mess with singletons
     132
     133        void loadGraphics();
     134        void unloadGraphics();
    131135
    132136        void loadState(GameState* state);
  • code/branches/resource/src/core/GameMode.h

    r3343 r3355  
    4141    class _CoreExport GameMode
    4242    {
    43         friend class Core;
     43        friend class Game;
    4444
    4545        public:
     
    5959            ~GameMode();
    6060
    61             static void setShowsGraphics(bool val) { bShowsGraphics_s = val; updateIsMaster(); }
    62             static void updateIsMaster  ()         { bIsMaster_s      = (bHasServer_s || bIsStandalone_s); }
     61            static void updateIsMaster()
     62            {
     63                bIsMaster_s = (bHasServer_s || bIsStandalone_s);
     64            }
    6365
    6466            static bool bShowsGraphics_s;                   //!< global variable that tells whether to show graphics
  • code/branches/resource/src/core/GameState.cc

    r3280 r3355  
    3838#include "util/Exception.h"
    3939#include "util/OrxAssert.h"
     40#include "Game.h"
    4041
    4142namespace orxonox
     
    4546        Constructor only initialises variables and sets the name permanently.
    4647    */
    47     GameState::GameState(const GameStateConstrParams& params)
    48         : name_(params.name)
    49         , bIgnoreTickTime_(params.bIgnoreTickTime)
     48    GameState::GameState(const GameStateInfo& info)
     49        : info_(info)
    5050        , parent_(0)
    5151    {
     
    6565    {
    6666        OrxAssert(this->activity_.active == false, "Deleting an active GameState is a very bad idea..");
     67    }
     68
     69    const std::string& GameState::getName() const
     70    {
     71        return info_.stateName;
    6772    }
    6873
     
    107112        else
    108113        {
    109             ThrowException(GameState, "Game state '" + name_ + "' doesn't have a child named '"
     114            ThrowException(GameState, "Game state '" + this->getName() + "' doesn't have a child named '"
    110115                + state->getName() + "'.");
    111116        }
  • code/branches/resource/src/core/GameState.h

    r3280 r3355  
    4545    /**
    4646    @brief
    47         Helper class to group construction parameters for better genericity.
    48     */
    49     struct GameStateConstrParams
    50     {
    51         std::string name;
    52         bool bIgnoreTickTime;
    53     };
    54 
    55     /**
    56     @brief
    5747        An implementation of a tree to manage game states.
    5848        This leads to a certain hierarchy that is created at runtime.
     
    8777
    8878    public:
    89         GameState(const GameStateConstrParams& params);
     79        GameState(const GameStateInfo& info);
    9080        virtual ~GameState();
    9181
    92         const std::string& getName() const { return name_; }
    93         State getActivity()          const { return this->activity_; }
    94         GameState* getParent()       const { return this->parent_; }
    95 
    96         bool ignoreTickTime()        const { return this->bIgnoreTickTime_; }
     82        const std::string& getName()   const;
     83        State getActivity()            const { return activity_; }
     84        GameState* getParent()         const { return parent_; }
     85        const GameStateInfo& getInfo() const { return info_; }
    9786
    9887        void addChild(GameState* state);
     
    111100        void updateInternal(const Clock& time);
    112101
    113         const std::string                        name_;
     102        const GameStateInfo&                     info_;
    114103        State                                    activity_;
    115         const bool                               bIgnoreTickTime_;
    116104        GameState*                               parent_;
    117105        std::map<std::string, GameState*>        children_;
  • code/branches/resource/src/orxonox/gamestates/GSClient.cc

    r3280 r3355  
    4242    SetCommandLineArgument(ip, "127.0.0.1").information("Sever IP as strin in the form #.#.#.#");
    4343
    44     GSClient::GSClient(const GameStateConstrParams& params)
    45         : GameState(params)
     44    GSClient::GSClient(const GameStateInfo& info)
     45        : GameState(info)
    4646        , client_(0)
    4747    {
  • code/branches/resource/src/orxonox/gamestates/GSClient.h

    r3280 r3355  
    4040    {
    4141    public:
    42         GSClient(const GameStateConstrParams& params);
     42        GSClient(const GameStateInfo& info);
    4343        ~GSClient();
    4444
  • code/branches/resource/src/orxonox/gamestates/GSDedicated.cc

    r3304 r3355  
    5555    termios* GSDedicated::originalTerminalSettings_;
    5656
    57     GSDedicated::GSDedicated(const GameStateConstrParams& params)
    58         : GameState(params)
     57    GSDedicated::GSDedicated(const GameStateInfo& info)
     58        : GameState(info)
    5959        , server_(0)
    6060        , closeThread_(false)
  • code/branches/resource/src/orxonox/gamestates/GSDedicated.h

    r3304 r3355  
    4848    {
    4949    public:
    50         GSDedicated(const GameStateConstrParams& params);
     50        GSDedicated(const GameStateInfo& info);
    5151        ~GSDedicated();
    5252
  • code/branches/resource/src/orxonox/gamestates/GSGraphics.cc

    r3349 r3355  
    5757    DeclareGameState(GSGraphics, "graphics", false, true);
    5858
    59     GSGraphics::GSGraphics(const GameStateConstrParams& params)
    60         : GameState(params)
     59    GSGraphics::GSGraphics(const GameStateInfo& info)
     60        : GameState(info)
    6161        , console_(0)
    6262        , soundManager_(0)
     
    8888    void GSGraphics::activate()
    8989    {
    90         // Load OGRE, CEGUI and OIS
    91         Core::getInstance().loadGraphics();
    92 
    9390        // load debug overlay
    9491        COUT(3) << "Loading Debug Overlay..." << std::endl;
     
    147144        // HACK: (destroys a resource smart pointer)
    148145        Map::hackDestroyMap();
    149 
    150         // Unload OGRE, CEGUI and OIS
    151         Core::getInstance().loadGraphics();
    152146    }
    153147
  • code/branches/resource/src/orxonox/gamestates/GSGraphics.h

    r3349 r3355  
    5050    {
    5151    public:
    52         GSGraphics(const GameStateConstrParams& params);
     52        GSGraphics(const GameStateInfo& info);
    5353        ~GSGraphics();
    5454
  • code/branches/resource/src/orxonox/gamestates/GSIOConsole.cc

    r3280 r3355  
    3838    DeclareGameState(GSIOConsole, "ioConsole", false, false);
    3939
    40     GSIOConsole::GSIOConsole(const GameStateConstrParams& params)
    41         : GameState(params)
     40    GSIOConsole::GSIOConsole(const GameStateInfo& info)
     41        : GameState(info)
    4242    {
    4343    }
  • code/branches/resource/src/orxonox/gamestates/GSIOConsole.h

    r3280 r3355  
    3838    {
    3939    public:
    40         GSIOConsole(const GameStateConstrParams& params);
     40        GSIOConsole(const GameStateInfo& info);
    4141        ~GSIOConsole();
    4242
  • code/branches/resource/src/orxonox/gamestates/GSLevel.cc

    r3346 r3355  
    6060    XMLFile* GSLevel::startFile_s = NULL;
    6161
    62     GSLevel::GSLevel(const GameStateConstrParams& params)
    63         : GameState(params)
     62    GSLevel::GSLevel(const GameStateInfo& info)
     63        : GameState(info)
    6464        , keyBinder_(0)
    6565        , gameInputState_(0)
  • code/branches/resource/src/orxonox/gamestates/GSLevel.h

    r3327 r3355  
    4141    {
    4242    public:
    43         GSLevel(const GameStateConstrParams& params);
     43        GSLevel(const GameStateInfo& info);
    4444        ~GSLevel();
    4545        void setConfigValues();
  • code/branches/resource/src/orxonox/gamestates/GSMainMenu.cc

    r3346 r3355  
    4545    DeclareGameState(GSMainMenu, "mainMenu", false, true);
    4646
    47     GSMainMenu::GSMainMenu(const GameStateConstrParams& params)
    48         : GameState(params)
     47    GSMainMenu::GSMainMenu(const GameStateInfo& info)
     48        : GameState(info)
    4949        , inputState_(0)
    5050    {
  • code/branches/resource/src/orxonox/gamestates/GSMainMenu.h

    r3327 r3355  
    4040    {
    4141    public:
    42         GSMainMenu(const GameStateConstrParams& params);
     42        GSMainMenu(const GameStateInfo& info);
    4343        ~GSMainMenu();
    4444
  • code/branches/resource/src/orxonox/gamestates/GSRoot.cc

    r3349 r3355  
    5353    SetCommandLineSwitch(standalone).information("Start in standalone mode");
    5454
    55     GSRoot::GSRoot(const GameStateConstrParams& params)
    56         : GameState(params)
     55    GSRoot::GSRoot(const GameStateInfo& info)
     56        : GameState(info)
    5757        , timeFactor_(1.0f)
    5858        , bPaused_(false)
     
    174174    @brief
    175175        Changes the speed of Orxonox
     176    @remark
     177        This function is a hack when placed here!
     178        Timefactor should be related to the scene (level or so), not the game
    176179    */
    177180    void GSRoot::setTimeFactor(float factor)
  • code/branches/resource/src/orxonox/gamestates/GSRoot.h

    r3280 r3355  
    3838    {
    3939    public:
    40         GSRoot(const GameStateConstrParams& params);
     40        GSRoot(const GameStateInfo& info);
    4141        ~GSRoot();
    4242
  • code/branches/resource/src/orxonox/gamestates/GSServer.cc

    r3280 r3355  
    4141    SetCommandLineArgument(port, 55556).shortcut("p").information("Network communication port to be used 0-65535 (default: 55556)");
    4242
    43     GSServer::GSServer(const GameStateConstrParams& params)
    44         : GameState(params)
     43    GSServer::GSServer(const GameStateInfo& info)
     44        : GameState(info)
    4545        , server_(0)
    4646    {
  • code/branches/resource/src/orxonox/gamestates/GSServer.h

    r3280 r3355  
    4040    {
    4141    public:
    42         GSServer(const GameStateConstrParams& params);
     42        GSServer(const GameStateInfo& info);
    4343        ~GSServer();
    4444
  • code/branches/resource/src/orxonox/gamestates/GSStandalone.cc

    r3280 r3355  
    3636    DeclareGameState(GSStandalone, "standalone", false, true);
    3737
    38     GSStandalone::GSStandalone(const GameStateConstrParams& params)
    39         : GameState(params)
     38    GSStandalone::GSStandalone(const GameStateInfo& info)
     39        : GameState(info)
    4040    {
    4141    }
  • code/branches/resource/src/orxonox/gamestates/GSStandalone.h

    r3280 r3355  
    3838    {
    3939    public:
    40         GSStandalone(const GameStateConstrParams& params);
     40        GSStandalone(const GameStateInfo& info);
    4141        ~GSStandalone();
    4242
Note: See TracChangeset for help on using the changeset viewer.