[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 |
---|
[7401] | 31 | @ingroup Management CoreGame |
---|
[2805] | 32 | @brief |
---|
[7401] | 33 | Declaration of Game Singleton which is responsible for running the game. |
---|
[2805] | 34 | */ |
---|
| 35 | |
---|
| 36 | #ifndef _Game_H__ |
---|
| 37 | #define _Game_H__ |
---|
| 38 | |
---|
[2844] | 39 | #include "CorePrereqs.h" |
---|
[3196] | 40 | |
---|
[2805] | 41 | #include <cassert> |
---|
[2817] | 42 | #include <list> |
---|
[2844] | 43 | #include <map> |
---|
[3196] | 44 | #include <string> |
---|
[2844] | 45 | #include <vector> |
---|
[3196] | 46 | #include <boost/shared_ptr.hpp> |
---|
| 47 | #include <boost/preprocessor/cat.hpp> |
---|
| 48 | |
---|
[8858] | 49 | #include "util/Output.h" |
---|
[8423] | 50 | #include "util/DestructionHelper.h" |
---|
[3370] | 51 | #include "util/Singleton.h" |
---|
[2805] | 52 | |
---|
[3084] | 53 | /** |
---|
[7401] | 54 | @brief |
---|
[3084] | 55 | Adds a new GameState to the Game. The second parameter is the name as string |
---|
[8423] | 56 | and every following parameter is a constructor argument (which is usually non existent) |
---|
[3084] | 57 | */ |
---|
[3280] | 58 | #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \ |
---|
[8418] | 59 | static bool BOOST_PP_CAT(bGameStateDummy_##className, __UNIQUE_NUMBER__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode) |
---|
[6417] | 60 | // tolua_begin |
---|
[2805] | 61 | namespace orxonox |
---|
| 62 | { |
---|
[6417] | 63 | // tolua_end |
---|
[3280] | 64 | |
---|
[3370] | 65 | //! Helper object required before GameStates are being constructed |
---|
| 66 | struct GameStateInfo |
---|
| 67 | { |
---|
| 68 | std::string stateName; |
---|
| 69 | std::string className; |
---|
| 70 | bool bIgnoreTickTime; |
---|
| 71 | bool bGraphicsMode; |
---|
| 72 | }; |
---|
| 73 | |
---|
[2805] | 74 | /** |
---|
| 75 | @brief |
---|
| 76 | Main class responsible for running the game. |
---|
[3370] | 77 | @remark |
---|
| 78 | You should only create this singleton once because it owns the Core class! (see remark there) |
---|
[2805] | 79 | */ |
---|
[6417] | 80 | // tolua_begin |
---|
| 81 | class _CoreExport Game |
---|
| 82 | // tolua_end |
---|
[10624] | 83 | : public Singleton<Game> |
---|
[6417] | 84 | { // tolua_export |
---|
[3370] | 85 | friend class Singleton<Game>; |
---|
| 86 | typedef std::vector<shared_ptr<GameState> > GameStateVector; |
---|
| 87 | typedef std::map<std::string, shared_ptr<GameState> > GameStateMap; |
---|
[5695] | 88 | typedef shared_ptr<GameStateTreeNode> GameStateTreeNodePtr; |
---|
[5693] | 89 | |
---|
[2805] | 90 | public: |
---|
[3323] | 91 | Game(const std::string& cmdLine); |
---|
[2805] | 92 | |
---|
[8423] | 93 | //! Leave empty and use cleanup() instead |
---|
| 94 | ~Game() {} |
---|
| 95 | /// Destructor that also executes when object fails to construct |
---|
| 96 | void destroy(); |
---|
| 97 | |
---|
[2844] | 98 | void setStateHierarchy(const std::string& str); |
---|
[3370] | 99 | shared_ptr<GameState> getState(const std::string& name); |
---|
[2844] | 100 | |
---|
[2805] | 101 | void run(); |
---|
| 102 | void stop(); |
---|
| 103 | |
---|
[6417] | 104 | static Game& getInstance(){ return Singleton<Game>::getInstance(); } // tolua_export |
---|
[2844] | 105 | |
---|
[6417] | 106 | void requestState(const std::string& name); //tolua_export |
---|
| 107 | void requestStates(const std::string& names); //tolua_export |
---|
| 108 | void popState(); //tolua_export |
---|
| 109 | |
---|
[2846] | 110 | const Clock& getGameClock() { return *this->gameClock_; } |
---|
| 111 | |
---|
[2817] | 112 | float getAvgTickTime() { return this->avgTickTime_; } |
---|
| 113 | float getAvgFPS() { return this->avgFPS_; } |
---|
| 114 | |
---|
[3370] | 115 | void subtractTickTime(int32_t length); |
---|
[2817] | 116 | |
---|
[3280] | 117 | template <class T> |
---|
| 118 | static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode); |
---|
[2805] | 119 | |
---|
| 120 | private: |
---|
[3280] | 121 | class _CoreExport GameStateFactory |
---|
[2817] | 122 | { |
---|
[3280] | 123 | public: |
---|
| 124 | virtual ~GameStateFactory() { } |
---|
[3370] | 125 | static shared_ptr<GameState> fabricate(const GameStateInfo& info); |
---|
[3280] | 126 | template <class T> |
---|
| 127 | static void createFactory(const std::string& className) |
---|
[5929] | 128 | { getFactories()[className].reset(new TemplateGameStateFactory<T>()); } |
---|
[5693] | 129 | |
---|
[3370] | 130 | virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0; |
---|
[5929] | 131 | static std::map<std::string, shared_ptr<GameStateFactory> >& getFactories(); |
---|
[3280] | 132 | }; |
---|
| 133 | template <class T> |
---|
| 134 | class TemplateGameStateFactory : public GameStateFactory |
---|
| 135 | { |
---|
| 136 | public: |
---|
[3370] | 137 | shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) |
---|
| 138 | { return shared_ptr<GameState>(new T(info)); } |
---|
[3280] | 139 | }; |
---|
| 140 | |
---|
| 141 | struct StatisticsTickInfo |
---|
| 142 | { |
---|
[2817] | 143 | uint64_t tickTime; |
---|
| 144 | uint32_t tickLength; |
---|
| 145 | }; |
---|
| 146 | |
---|
[2805] | 147 | Game(Game&); // don't mess with singletons |
---|
| 148 | |
---|
[3370] | 149 | void loadGraphics(); |
---|
[10624] | 150 | void unloadGraphics(bool loadGraphicsManagerWithoutRenderer = true); |
---|
[2805] | 151 | |
---|
[5929] | 152 | void parseStates(std::vector<std::pair<std::string, int> >::const_iterator& it, shared_ptr<GameStateTreeNode> currentNode); |
---|
[3370] | 153 | bool checkState(const std::string& name) const; |
---|
| 154 | void loadState(const std::string& name); |
---|
| 155 | void unloadState(const std::string& name); |
---|
[2805] | 156 | |
---|
[3370] | 157 | // Main loop structuring |
---|
| 158 | void updateGameStateStack(); |
---|
| 159 | void updateGameStates(); |
---|
| 160 | void updateStatistics(); |
---|
| 161 | void updateFPSLimiter(); |
---|
[2844] | 162 | |
---|
[3370] | 163 | // ScopeGuard helper function |
---|
| 164 | void resetChangingState() { this->bChangingState_ = false; } |
---|
[2844] | 165 | |
---|
[8423] | 166 | Clock* gameClock_; |
---|
| 167 | Core* core_; |
---|
[3370] | 168 | |
---|
| 169 | GameStateMap constructedStates_; |
---|
| 170 | GameStateVector loadedStates_; |
---|
| 171 | GameStateTreeNodePtr rootStateNode_; |
---|
| 172 | GameStateTreeNodePtr loadedTopStateNode_; |
---|
| 173 | std::vector<GameStateTreeNodePtr> requestedStateNodes_; |
---|
| 174 | |
---|
| 175 | bool bChangingState_; |
---|
| 176 | bool bAbort_; |
---|
| 177 | |
---|
[2817] | 178 | // variables for time statistics |
---|
[3370] | 179 | uint64_t statisticsStartTime_; |
---|
| 180 | std::list<StatisticsTickInfo> statisticsTickTimes_; |
---|
| 181 | uint32_t periodTime_; |
---|
| 182 | uint32_t periodTickTime_; |
---|
| 183 | float avgFPS_; |
---|
| 184 | float avgTickTime_; |
---|
| 185 | int excessSleepTime_; |
---|
| 186 | unsigned int minimumSleepTime_; |
---|
[2817] | 187 | |
---|
[10624] | 188 | /// Helper object that stores the config values |
---|
| 189 | GameConfig* config_; |
---|
[6417] | 190 | |
---|
[8423] | 191 | /// Helper object that executes the surrogate destructor destroy() |
---|
| 192 | DestructionHelper<Game> destructionHelper_; |
---|
| 193 | |
---|
[3280] | 194 | static std::map<std::string, GameStateInfo> gameStateDeclarations_s; |
---|
[3370] | 195 | static Game* singletonPtr_s; //!< Pointer to the Singleton |
---|
[6417] | 196 | }; //tolua_export |
---|
[3280] | 197 | |
---|
| 198 | template <class T> |
---|
| 199 | /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) |
---|
| 200 | { |
---|
[3370] | 201 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName); |
---|
[3280] | 202 | if (it == gameStateDeclarations_s.end()) |
---|
| 203 | { |
---|
[3370] | 204 | GameStateInfo& info = gameStateDeclarations_s[stateName]; |
---|
[3280] | 205 | info.stateName = stateName; |
---|
| 206 | info.className = className; |
---|
| 207 | info.bIgnoreTickTime = bIgnoreTickTime; |
---|
| 208 | info.bGraphicsMode = bGraphicsMode; |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | { |
---|
[8858] | 212 | orxout(internal_warning) << "Cannot declare two GameStates with the same name." << endl; |
---|
| 213 | orxout(internal_warning) << "Ignoring second one ('" << stateName << "')." << endl; |
---|
[3280] | 214 | } |
---|
| 215 | |
---|
| 216 | // Create a factory to delay GameState creation |
---|
| 217 | GameStateFactory::createFactory<T>(className); |
---|
| 218 | |
---|
| 219 | // just a required dummy return value |
---|
| 220 | return true; |
---|
| 221 | } |
---|
[6417] | 222 | } //tolua_export |
---|
[3280] | 223 | |
---|
[2805] | 224 | #endif /* _Game_H__ */ |
---|