Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 28, 2009, 6:22:40 PM (15 years ago)
Author:
rgrieder
Message:

Moved GameState construction from pre-main() to after Core creation. That makes it possible to use Core features (like configValues) in the GameState constructor.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/core4/src/core/Game.h

    r3238 r3243  
    4646#include <boost/preprocessor/cat.hpp>
    4747
     48#include "util/Debug.h"
    4849#include "OrxonoxClass.h"
    4950
     
    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
    5859// tolua_begin
     
    9192        void addTickTime(uint32_t length);
    9293
    93         static bool addGameState(GameState* state);
    94         static void destroyStates();
     94        template <class T>
     95        static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
    9596        static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } //tolua_export
    9697
     
    99100
    100101    private:
    101         struct statisticsTickInfo
     102        class _CoreExport GameStateFactory
     103        {
     104        public:
     105            virtual ~GameStateFactory() { }
     106            static GameState* fabricate(const std::string& className, const GameStateConstrParams& params);
     107            template <class T>
     108            static void createFactory(const std::string& className)
     109                { factories_s[className] = new TemplateGameStateFactory<T>(); }
     110            static void destroyFactories();
     111        private:
     112            virtual GameState* fabricate(const GameStateConstrParams& params) = 0;
     113            static std::map<std::string, GameStateFactory*> factories_s;
     114        };
     115        template <class T>
     116        class TemplateGameStateFactory : public GameStateFactory
     117        {
     118        public:
     119            GameState* fabricate(const GameStateConstrParams& params)
     120                { return new T(params); }
     121        };
     122
     123        struct GameStateInfo
     124        {
     125            std::string stateName;
     126            std::string className;
     127            bool bIgnoreTickTime;
     128            bool bGraphicsMode;
     129        };
     130
     131        struct StatisticsTickInfo
    102132        {
    103133            uint64_t    tickTime;
     
    110140        void unloadState(GameState* state);
    111141
    112         std::vector<GameState*>         activeStates_;
     142        std::map<std::string, GameState*>    gameStates_;
     143        std::vector<GameState*>              activeStates_;
    113144        boost::shared_ptr<GameStateTreeNode> rootStateNode_;
    114145        boost::shared_ptr<GameStateTreeNode> activeStateNode_;
     
    123154        // variables for time statistics
    124155        uint64_t                        statisticsStartTime_;
    125         std::list<statisticsTickInfo>   statisticsTickTimes_;
     156        std::list<StatisticsTickInfo>   statisticsTickTimes_;
    126157        uint32_t                        periodTime_;
    127158        uint32_t                        periodTickTime_;
     
    134165        std::string                     levelName_;
    135166
    136         static std::map<std::string, GameState*> allStates_s;
     167        static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
    137168        static Game* singletonRef_s;        //!< Pointer to the Singleton
    138         // tolua_begin
    139     };
    140 }
    141 //tolua_end
     169    }; // tolua_export
     170
     171    template <class T>
     172    /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode)
     173    {
     174        std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName));
     175        if (it == gameStateDeclarations_s.end())
     176        {
     177            GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)];
     178            info.stateName = stateName;
     179            info.className = className;
     180            info.bIgnoreTickTime = bIgnoreTickTime;
     181            info.bGraphicsMode = bGraphicsMode;
     182        }
     183        else
     184        {
     185            COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl;
     186            COUT(0) << "       Ignoring second one ('" << stateName << "')." << std::endl;
     187        }
     188
     189        // Create a factory to delay GameState creation
     190        GameStateFactory::createFactory<T>(className);
     191
     192        // just a required dummy return value
     193        return true;
     194    }
     195} // tolua_export
     196
    142197#endif /* _Game_H__ */
Note: See TracChangeset for help on using the changeset viewer.