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