Changeset 3243 for code/branches/core4/src/core/Game.h
- Timestamp:
- Jun 28, 2009, 6:22:40 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core4/src/core/Game.h
r3238 r3243 46 46 #include <boost/preprocessor/cat.hpp> 47 47 48 #include "util/Debug.h" 48 49 #include "OrxonoxClass.h" 49 50 … … 53 54 and every following paramter is a constructor argument (which is usually non existent) 54 55 */ 55 #define AddGameState(classname, ...) \56 static bool BOOST_PP_CAT(bGameStateDummy_##class name, __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) 57 58 58 59 // tolua_begin … … 91 92 void addTickTime(uint32_t length); 92 93 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); 95 96 static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } //tolua_export 96 97 … … 99 100 100 101 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 102 132 { 103 133 uint64_t tickTime; … … 110 140 void unloadState(GameState* state); 111 141 112 std::vector<GameState*> activeStates_; 142 std::map<std::string, GameState*> gameStates_; 143 std::vector<GameState*> activeStates_; 113 144 boost::shared_ptr<GameStateTreeNode> rootStateNode_; 114 145 boost::shared_ptr<GameStateTreeNode> activeStateNode_; … … 123 154 // variables for time statistics 124 155 uint64_t statisticsStartTime_; 125 std::list< statisticsTickInfo> statisticsTickTimes_;156 std::list<StatisticsTickInfo> statisticsTickTimes_; 126 157 uint32_t periodTime_; 127 158 uint32_t periodTickTime_; … … 134 165 std::string levelName_; 135 166 136 static std::map<std::string, GameState *> allStates_s;167 static std::map<std::string, GameStateInfo> gameStateDeclarations_s; 137 168 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 142 197 #endif /* _Game_H__ */
Note: See TracChangeset
for help on using the changeset viewer.