| [2805] | 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Reto Grieder |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | /** |
|---|
| 30 | @file |
|---|
| 31 | @brief |
|---|
| 32 | Declaration of Game Singleton. |
|---|
| 33 | */ |
|---|
| 34 | |
|---|
| 35 | #ifndef _Game_H__ |
|---|
| 36 | #define _Game_H__ |
|---|
| 37 | |
|---|
| [2844] | 38 | #include "CorePrereqs.h" |
|---|
| [3196] | 39 | |
|---|
| [2805] | 40 | #include <cassert> |
|---|
| [2817] | 41 | #include <list> |
|---|
| [2844] | 42 | #include <map> |
|---|
| [3196] | 43 | #include <string> |
|---|
| [2844] | 44 | #include <vector> |
|---|
| [3196] | 45 | #include <boost/shared_ptr.hpp> |
|---|
| [3370] | 46 | #include <boost/scoped_ptr.hpp> |
|---|
| [3196] | 47 | #include <boost/preprocessor/cat.hpp> |
|---|
| 48 | |
|---|
| [3280] | 49 | #include "util/Debug.h" |
|---|
| [5637] | 50 | #include "util/ScopeGuard.h" |
|---|
| [3370] | 51 | #include "util/Singleton.h" |
|---|
| [2805] | 52 | |
|---|
| [3084] | 53 | /** |
|---|
| 54 | @def |
|---|
| 55 | Adds a new GameState to the Game. The second parameter is the name as string |
|---|
| 56 | and every following paramter is a constructor argument (which is usually non existent) |
|---|
| 57 | */ |
|---|
| [3280] | 58 | #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \ |
|---|
| 59 | static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode) |
|---|
| [2844] | 60 | |
|---|
| [2805] | 61 | namespace orxonox |
|---|
| 62 | { |
|---|
| [3280] | 63 | class GameConfiguration; |
|---|
| [3370] | 64 | using boost::scoped_ptr; |
|---|
| 65 | using boost::shared_ptr; |
|---|
| [3280] | 66 | |
|---|
| [3370] | 67 | //! Helper object required before GameStates are being constructed |
|---|
| 68 | struct GameStateInfo |
|---|
| 69 | { |
|---|
| 70 | std::string stateName; |
|---|
| 71 | std::string className; |
|---|
| 72 | bool bIgnoreTickTime; |
|---|
| 73 | bool bGraphicsMode; |
|---|
| 74 | }; |
|---|
| 75 | |
|---|
| [2805] | 76 | /** |
|---|
| 77 | @brief |
|---|
| 78 | Main class responsible for running the game. |
|---|
| [3370] | 79 | @remark |
|---|
| 80 | You should only create this singleton once because it owns the Core class! (see remark there) |
|---|
| [2805] | 81 | */ |
|---|
| [3370] | 82 | class _CoreExport Game : public Singleton<Game> |
|---|
| [2805] | 83 | { |
|---|
| [3370] | 84 | friend class Singleton<Game>; |
|---|
| 85 | typedef std::vector<shared_ptr<GameState> > GameStateVector; |
|---|
| 86 | typedef std::map<std::string, shared_ptr<GameState> > GameStateMap; |
|---|
| 87 | typedef boost::shared_ptr<GameStateTreeNode> GameStateTreeNodePtr; |
|---|
| [5637] | 88 | |
|---|
| [2805] | 89 | public: |
|---|
| [3323] | 90 | Game(const std::string& cmdLine); |
|---|
| [2805] | 91 | ~Game(); |
|---|
| 92 | |
|---|
| [2844] | 93 | void setStateHierarchy(const std::string& str); |
|---|
| [3370] | 94 | shared_ptr<GameState> getState(const std::string& name); |
|---|
| [2844] | 95 | |
|---|
| [2805] | 96 | void run(); |
|---|
| 97 | void stop(); |
|---|
| 98 | |
|---|
| [2844] | 99 | void requestState(const std::string& name); |
|---|
| [2850] | 100 | void requestStates(const std::string& names); |
|---|
| [2844] | 101 | void popState(); |
|---|
| 102 | |
|---|
| [2846] | 103 | const Clock& getGameClock() { return *this->gameClock_; } |
|---|
| 104 | |
|---|
| [2817] | 105 | float getAvgTickTime() { return this->avgTickTime_; } |
|---|
| 106 | float getAvgFPS() { return this->avgFPS_; } |
|---|
| 107 | |
|---|
| [3370] | 108 | void subtractTickTime(int32_t length); |
|---|
| [2817] | 109 | |
|---|
| [3280] | 110 | template <class T> |
|---|
| 111 | static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode); |
|---|
| [2805] | 112 | |
|---|
| 113 | private: |
|---|
| [3280] | 114 | class _CoreExport GameStateFactory |
|---|
| [2817] | 115 | { |
|---|
| [3280] | 116 | public: |
|---|
| 117 | virtual ~GameStateFactory() { } |
|---|
| [3370] | 118 | static shared_ptr<GameState> fabricate(const GameStateInfo& info); |
|---|
| [3280] | 119 | template <class T> |
|---|
| 120 | static void createFactory(const std::string& className) |
|---|
| [3370] | 121 | { factories_s[className].reset(new TemplateGameStateFactory<T>()); } |
|---|
| [5637] | 122 | |
|---|
| [3370] | 123 | virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0; |
|---|
| 124 | static std::map<std::string, shared_ptr<GameStateFactory> > factories_s; |
|---|
| [3280] | 125 | }; |
|---|
| 126 | template <class T> |
|---|
| 127 | class TemplateGameStateFactory : public GameStateFactory |
|---|
| 128 | { |
|---|
| 129 | public: |
|---|
| [3370] | 130 | shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) |
|---|
| 131 | { return shared_ptr<GameState>(new T(info)); } |
|---|
| [3280] | 132 | }; |
|---|
| [5637] | 133 | // For the factory destruction |
|---|
| 134 | typedef Loki::ObjScopeGuardImpl0<std::map<std::string, shared_ptr<GameStateFactory> >, void (std::map<std::string, shared_ptr<GameStateFactory> >::*)()> ObjScopeGuard; |
|---|
| [3280] | 135 | |
|---|
| 136 | struct StatisticsTickInfo |
|---|
| 137 | { |
|---|
| [2817] | 138 | uint64_t tickTime; |
|---|
| 139 | uint32_t tickLength; |
|---|
| 140 | }; |
|---|
| 141 | |
|---|
| [2805] | 142 | Game(Game&); // don't mess with singletons |
|---|
| 143 | |
|---|
| [3370] | 144 | void loadGraphics(); |
|---|
| 145 | void unloadGraphics(); |
|---|
| [2805] | 146 | |
|---|
| [3370] | 147 | bool checkState(const std::string& name) const; |
|---|
| 148 | void loadState(const std::string& name); |
|---|
| 149 | void unloadState(const std::string& name); |
|---|
| [2805] | 150 | |
|---|
| [3370] | 151 | // Main loop structuring |
|---|
| 152 | void updateGameStateStack(); |
|---|
| 153 | void updateGameStates(); |
|---|
| 154 | void updateStatistics(); |
|---|
| 155 | void updateFPSLimiter(); |
|---|
| [2844] | 156 | |
|---|
| [3370] | 157 | // ScopeGuard helper function |
|---|
| 158 | void resetChangingState() { this->bChangingState_ = false; } |
|---|
| [2844] | 159 | |
|---|
| [3370] | 160 | scoped_ptr<Clock> gameClock_; |
|---|
| 161 | scoped_ptr<Core> core_; |
|---|
| [5637] | 162 | ObjScopeGuard gsFactoryDestroyer_; |
|---|
| [3370] | 163 | scoped_ptr<GameConfiguration> configuration_; |
|---|
| 164 | |
|---|
| 165 | GameStateMap constructedStates_; |
|---|
| 166 | GameStateVector loadedStates_; |
|---|
| 167 | GameStateTreeNodePtr rootStateNode_; |
|---|
| 168 | GameStateTreeNodePtr loadedTopStateNode_; |
|---|
| 169 | std::vector<GameStateTreeNodePtr> requestedStateNodes_; |
|---|
| 170 | |
|---|
| 171 | bool bChangingState_; |
|---|
| 172 | bool bAbort_; |
|---|
| 173 | |
|---|
| [2817] | 174 | // variables for time statistics |
|---|
| [3370] | 175 | uint64_t statisticsStartTime_; |
|---|
| 176 | std::list<StatisticsTickInfo> statisticsTickTimes_; |
|---|
| 177 | uint32_t periodTime_; |
|---|
| 178 | uint32_t periodTickTime_; |
|---|
| 179 | float avgFPS_; |
|---|
| 180 | float avgTickTime_; |
|---|
| 181 | int excessSleepTime_; |
|---|
| 182 | unsigned int minimumSleepTime_; |
|---|
| [2817] | 183 | |
|---|
| [3280] | 184 | static std::map<std::string, GameStateInfo> gameStateDeclarations_s; |
|---|
| [3370] | 185 | static Game* singletonPtr_s; //!< Pointer to the Singleton |
|---|
| [2805] | 186 | }; |
|---|
| [3280] | 187 | |
|---|
| 188 | template <class T> |
|---|
| 189 | /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) |
|---|
| 190 | { |
|---|
| [3370] | 191 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName); |
|---|
| [3280] | 192 | if (it == gameStateDeclarations_s.end()) |
|---|
| 193 | { |
|---|
| [3370] | 194 | GameStateInfo& info = gameStateDeclarations_s[stateName]; |
|---|
| [3280] | 195 | info.stateName = stateName; |
|---|
| 196 | info.className = className; |
|---|
| 197 | info.bIgnoreTickTime = bIgnoreTickTime; |
|---|
| 198 | info.bGraphicsMode = bGraphicsMode; |
|---|
| 199 | } |
|---|
| 200 | else |
|---|
| 201 | { |
|---|
| 202 | COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl; |
|---|
| 203 | COUT(0) << " Ignoring second one ('" << stateName << "')." << std::endl; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | // Create a factory to delay GameState creation |
|---|
| 207 | GameStateFactory::createFactory<T>(className); |
|---|
| 208 | |
|---|
| 209 | // just a required dummy return value |
|---|
| 210 | return true; |
|---|
| 211 | } |
|---|
| [2805] | 212 | } |
|---|
| [3280] | 213 | |
|---|
| [2805] | 214 | #endif /* _Game_H__ */ |
|---|