Changeset 3370 for code/trunk/src/core/Game.h
- Timestamp:
- Jul 30, 2009, 2:10:44 PM (15 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/resource (added) merged: 3328,3336-3340,3342-3350,3352-3366
- Property svn:mergeinfo changed
-
code/trunk/src/core/Game.h
r3323 r3370 44 44 #include <vector> 45 45 #include <boost/shared_ptr.hpp> 46 #include <boost/scoped_ptr.hpp> 46 47 #include <boost/preprocessor/cat.hpp> 47 48 48 49 #include "util/Debug.h" 49 #include "util/S tringUtils.h"50 #include "util/Singleton.h" 50 51 51 52 /** … … 60 61 { 61 62 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 }; 62 74 63 75 /** 64 76 @brief 65 77 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) 66 80 */ 67 class _CoreExport Game 81 class _CoreExport Game : public Singleton<Game> 68 82 { 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; 69 87 public: 70 88 Game(const std::string& cmdLine); … … 72 90 73 91 void setStateHierarchy(const std::string& str); 74 GameState*getState(const std::string& name);92 shared_ptr<GameState> getState(const std::string& name); 75 93 76 94 void run(); … … 86 104 float getAvgFPS() { return this->avgFPS_; } 87 105 88 void addTickTime(uint32_t length);106 void subtractTickTime(int32_t length); 89 107 90 108 template <class T> 91 109 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; }93 110 94 111 private: … … 97 114 public: 98 115 virtual ~GameStateFactory() { } 99 static GameState* fabricate(const std::string& className, const GameStateConstrParams& params);116 static shared_ptr<GameState> fabricate(const GameStateInfo& info); 100 117 template <class T> 101 118 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>()); } 104 120 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; 107 123 }; 108 124 template <class T> … … 110 126 { 111 127 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)); } 122 130 }; 123 131 … … 130 138 Game(Game&); // don't mess with singletons 131 139 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_; 147 168 148 169 // 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_; 155 178 156 179 static std::map<std::string, GameStateInfo> gameStateDeclarations_s; 157 static Game* singleton Ref_s; //!< Pointer to the Singleton180 static Game* singletonPtr_s; //!< Pointer to the Singleton 158 181 }; 159 182 … … 161 184 /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) 162 185 { 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); 164 187 if (it == gameStateDeclarations_s.end()) 165 188 { 166 GameStateInfo& info = gameStateDeclarations_s[ getLowercase(stateName)];189 GameStateInfo& info = gameStateDeclarations_s[stateName]; 167 190 info.stateName = stateName; 168 191 info.className = className;
Note: See TracChangeset
for help on using the changeset viewer.