Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 12, 2009, 11:58:01 PM (15 years ago)
Author:
rgrieder
Message:

Merged most of the core4 revisions back to the trunk except for:

  • orxonox_cast
  • all the radical changes in the input library
Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

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

    r3196 r3280  
    4646#include <boost/preprocessor/cat.hpp>
    4747
    48 #include "OrxonoxClass.h"
     48#include "util/Debug.h"
     49#include "util/StringUtils.h"
    4950
    5051/**
     
    5354    and every following paramter is a constructor argument (which is usually non existent)
    5455*/
    55 #define AddGameState(classname, ...) \
    56     static bool BOOST_PP_CAT(bGameStateDummy_##classname, __LINE__) = orxonox::Game::addGameState(new classname(__VA_ARGS__))
     56#define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
     57    static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
    5758
    58 // tolua_begin
    5959namespace orxonox
    6060{
     61    class GameConfiguration;
     62
    6163    /**
    6264    @brief
     
    6466    */
    6567    class _CoreExport Game
    66     // tolua_end
    67         : public OrxonoxClass
    68     // tolua_begin
    6968    {
    70     //tolua_end
    7169    public:
    7270        Game(int argc, char** argv);
    7371        ~Game();
    74         void setConfigValues();
    7572
    7673        void setStateHierarchy(const std::string& str);
     
    9188        void addTickTime(uint32_t length);
    9289
    93         static bool addGameState(GameState* state);
    94         static void destroyStates();
    95         static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } //tolua_export
    96 
    97         void setLevel(std::string levelName); //tolua_export
    98         std::string getLevel(); //tolua_export
     90        template <class T>
     91        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; }
    9993
    10094    private:
    101         struct statisticsTickInfo
     95        class _CoreExport GameStateFactory
     96        {
     97        public:
     98            virtual ~GameStateFactory() { }
     99            static GameState* fabricate(const std::string& className, const GameStateConstrParams& params);
     100            template <class T>
     101            static void createFactory(const std::string& className)
     102                { factories_s[className] = new TemplateGameStateFactory<T>(); }
     103            static void destroyFactories();
     104        private:
     105            virtual GameState* fabricate(const GameStateConstrParams& params) = 0;
     106            static std::map<std::string, GameStateFactory*> factories_s;
     107        };
     108        template <class T>
     109        class TemplateGameStateFactory : public GameStateFactory
     110        {
     111        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;
     122        };
     123
     124        struct StatisticsTickInfo
    102125        {
    103126            uint64_t    tickTime;
     
    110133        void unloadState(GameState* state);
    111134
    112         std::vector<GameState*>         activeStates_;
     135        std::map<std::string, GameState*>    gameStates_;
     136        std::vector<GameState*>              activeStates_;
    113137        boost::shared_ptr<GameStateTreeNode> rootStateNode_;
    114138        boost::shared_ptr<GameStateTreeNode> activeStateNode_;
     
    117141        Core*                           core_;
    118142        Clock*                          gameClock_;
     143        GameConfiguration*              configuration_;
    119144
    120         bool                            abort_;
     145        bool                            bChangingState_;
     146        bool                            bAbort_;
    121147
    122148        // variables for time statistics
    123149        uint64_t                        statisticsStartTime_;
    124         std::list<statisticsTickInfo>   statisticsTickTimes_;
     150        std::list<StatisticsTickInfo>   statisticsTickTimes_;
    125151        uint32_t                        periodTime_;
    126152        uint32_t                        periodTickTime_;
     
    128154        float                           avgTickTime_;
    129155
    130         // config values
    131         unsigned int                    statisticsRefreshCycle_;
    132         unsigned int                    statisticsAvgLength_;
    133         std::string                     levelName_;
     156        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
     157        static Game* singletonRef_s;        //!< Pointer to the Singleton
     158    };
    134159
    135         static std::map<std::string, GameState*> allStates_s;
    136         static Game* singletonRef_s;        //!< Pointer to the Singleton
    137         // tolua_begin
    138     };
     160    template <class T>
     161    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
     162    {
     163        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName));
     164        if (it == gameStateDeclarations_s.end())
     165        {
     166            GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)];
     167            info.stateName = stateName;
     168            info.className = className;
     169            info.bIgnoreTickTime = bIgnoreTickTime;
     170            info.bGraphicsMode = bGraphicsMode;
     171        }
     172        else
     173        {
     174            COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
     175            COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
     176        }
     177
     178        // Create a factory to delay GameState creation
     179        GameStateFactory::createFactory<T>(className);
     180
     181        // just a required dummy return value
     182        return true;
     183    }
    139184}
    140 //tolua_end
     185
    141186#endif /* _Game_H__ */
Note: See TracChangeset for help on using the changeset viewer.