Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 30, 2009, 2:10:44 PM (15 years ago)
Author:
rgrieder
Message:

Merged resource branch back to the trunk. Changes:

  • Automated graphics loading by evaluating whether a GameState requires it
  • Using native Tcl library (x3n)

Windows users: Update your dependency package!

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/Game.h

    r3323 r3370  
    4444#include <vector>
    4545#include <boost/shared_ptr.hpp>
     46#include <boost/scoped_ptr.hpp>
    4647#include <boost/preprocessor/cat.hpp>
    4748
    4849#include "util/Debug.h"
    49 #include "util/StringUtils.h"
     50#include "util/Singleton.h"
    5051
    5152/**
     
    6061{
    6162    class GameConfiguration;
     63    using boost::scoped_ptr;
     64    using boost::shared_ptr;
     65
     66    //! Helper object required before GameStates are being constructed
     67    struct GameStateInfo
     68    {
     69        std::string stateName;
     70        std::string className;
     71        bool bIgnoreTickTime;
     72        bool bGraphicsMode;
     73    };
    6274
    6375    /**
    6476    @brief
    6577        Main class responsible for running the game.
     78    @remark
     79        You should only create this singleton once because it owns the Core class! (see remark there)
    6680    */
    67     class _CoreExport Game
     81    class _CoreExport Game : public Singleton<Game>
    6882    {
     83        friend class Singleton<Game>;
     84        typedef std::vector<shared_ptr<GameState> > GameStateVector;
     85        typedef std::map<std::string, shared_ptr<GameState> > GameStateMap;
     86        typedef boost::shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
    6987    public:
    7088        Game(const std::string& cmdLine);
     
    7290
    7391        void setStateHierarchy(const std::string& str);
    74         GameState* getState(const std::string& name);
     92        shared_ptr<GameState> getState(const std::string& name);
    7593
    7694        void run();
     
    86104        float getAvgFPS()      { return this->avgFPS_; }
    87105
    88         void addTickTime(uint32_t length);
     106        void subtractTickTime(int32_t length);
    89107
    90108        template <class T>
    91109        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
    92         static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; }
    93110
    94111    private:
     
    97114        public:
    98115            virtual ~GameStateFactory() { }
    99             static GameState* fabricate(const std::string& className, const GameStateConstrParams& params);
     116            static shared_ptr<GameState> fabricate(const GameStateInfo& info);
    100117            template <class T>
    101118            static void createFactory(const std::string& className)
    102                 { factories_s[className] = new TemplateGameStateFactory<T>(); }
    103             static void destroyFactories();
     119                { factories_s[className].reset(new TemplateGameStateFactory<T>()); }
    104120        private:
    105             virtual GameState* fabricate(const GameStateConstrParams& params) = 0;
    106             static std::map<std::string, GameStateFactory*> factories_s;
     121            virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
     122            static std::map<std::string, shared_ptr<GameStateFactory> > factories_s;
    107123        };
    108124        template <class T>
     
    110126        {
    111127        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;
     128            shared_ptr<GameState> fabricateInternal(const GameStateInfo& info)
     129                { return shared_ptr<GameState>(new T(info)); }
    122130        };
    123131
     
    130138        Game(Game&); // don't mess with singletons
    131139
    132         void loadState(GameState* state);
    133         void unloadState(GameState* state);
    134 
    135         std::map<std::string, GameState*>    gameStates_;
    136         std::vector<GameState*>              activeStates_;
    137         boost::shared_ptr<GameStateTreeNode> rootStateNode_;
    138         boost::shared_ptr<GameStateTreeNode> activeStateNode_;
    139         std::vector<boost::shared_ptr<GameStateTreeNode> > requestedStateNodes_;
    140 
    141         Core*                           core_;
    142         Clock*                          gameClock_;
    143         GameConfiguration*              configuration_;
    144 
    145         bool                            bChangingState_;
    146         bool                            bAbort_;
     140        void loadGraphics();
     141        void unloadGraphics();
     142
     143        bool checkState(const std::string& name) const;
     144        void loadState(const std::string& name);
     145        void unloadState(const std::string& name);
     146
     147        // Main loop structuring
     148        void updateGameStateStack();
     149        void updateGameStates();
     150        void updateStatistics();
     151        void updateFPSLimiter();
     152
     153        // ScopeGuard helper function
     154        void resetChangingState() { this->bChangingState_ = false; }
     155
     156        scoped_ptr<Clock>                  gameClock_;
     157        scoped_ptr<Core>                   core_;
     158        scoped_ptr<GameConfiguration>      configuration_;
     159
     160        GameStateMap                       constructedStates_;
     161        GameStateVector                    loadedStates_;
     162        GameStateTreeNodePtr               rootStateNode_;
     163        GameStateTreeNodePtr               loadedTopStateNode_;
     164        std::vector<GameStateTreeNodePtr>  requestedStateNodes_;
     165
     166        bool                               bChangingState_;
     167        bool                               bAbort_;
    147168
    148169        // variables for time statistics
    149         uint64_t                        statisticsStartTime_;
    150         std::list<StatisticsTickInfo>   statisticsTickTimes_;
    151         uint32_t                        periodTime_;
    152         uint32_t                        periodTickTime_;
    153         float                           avgFPS_;
    154         float                           avgTickTime_;
     170        uint64_t                           statisticsStartTime_;
     171        std::list<StatisticsTickInfo>      statisticsTickTimes_;
     172        uint32_t                           periodTime_;
     173        uint32_t                           periodTickTime_;
     174        float                              avgFPS_;
     175        float                              avgTickTime_;
     176        int                                excessSleepTime_;
     177        unsigned int                       minimumSleepTime_;
    155178
    156179        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
    157         static Game* singletonRef_s;        //!< Pointer to the Singleton
     180        static Game* singletonPtr_s;        //!< Pointer to the Singleton
    158181    };
    159182
     
    161184    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
    162185    {
    163         std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName));
     186        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
    164187        if (it == gameStateDeclarations_s.end())
    165188        {
    166             GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)];
     189            GameStateInfo& info = gameStateDeclarations_s[stateName];
    167190            info.stateName = stateName;
    168191            info.className = className;
Note: See TracChangeset for help on using the changeset viewer.