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/core
Files:
7 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_;
Note: See TracChangeset for help on using the changeset viewer.