[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> |
---|
| 46 | #include <boost/preprocessor/cat.hpp> |
---|
| 47 | |
---|
[3280] | 48 | #include "util/Debug.h" |
---|
| 49 | #include "util/StringUtils.h" |
---|
[2805] | 50 | |
---|
[3084] | 51 | /** |
---|
| 52 | @def |
---|
| 53 | Adds a new GameState to the Game. The second parameter is the name as string |
---|
| 54 | and every following paramter is a constructor argument (which is usually non existent) |
---|
| 55 | */ |
---|
[3280] | 56 | #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \ |
---|
| 57 | static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode) |
---|
[2844] | 58 | |
---|
[2805] | 59 | namespace orxonox |
---|
| 60 | { |
---|
[3280] | 61 | class GameConfiguration; |
---|
| 62 | |
---|
[3355] | 63 | //! Helper object required before GameStates are being constructed |
---|
| 64 | struct GameStateInfo |
---|
| 65 | { |
---|
| 66 | std::string stateName; |
---|
| 67 | std::string className; |
---|
| 68 | bool bIgnoreTickTime; |
---|
| 69 | bool bGraphicsMode; |
---|
| 70 | }; |
---|
| 71 | |
---|
[2805] | 72 | /** |
---|
| 73 | @brief |
---|
| 74 | Main class responsible for running the game. |
---|
| 75 | */ |
---|
[3036] | 76 | class _CoreExport Game |
---|
[2805] | 77 | { |
---|
| 78 | public: |
---|
[3323] | 79 | Game(const std::string& cmdLine); |
---|
[2805] | 80 | ~Game(); |
---|
| 81 | |
---|
[2844] | 82 | void setStateHierarchy(const std::string& str); |
---|
| 83 | GameState* getState(const std::string& name); |
---|
| 84 | |
---|
[2805] | 85 | void run(); |
---|
| 86 | void stop(); |
---|
| 87 | |
---|
[2844] | 88 | void requestState(const std::string& name); |
---|
[2850] | 89 | void requestStates(const std::string& names); |
---|
[2844] | 90 | void popState(); |
---|
| 91 | |
---|
[2846] | 92 | const Clock& getGameClock() { return *this->gameClock_; } |
---|
| 93 | |
---|
[2817] | 94 | float getAvgTickTime() { return this->avgTickTime_; } |
---|
| 95 | float getAvgFPS() { return this->avgFPS_; } |
---|
| 96 | |
---|
[3349] | 97 | void subtractTickTime(int32_t length); |
---|
[2817] | 98 | |
---|
[3280] | 99 | template <class T> |
---|
| 100 | static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode); |
---|
| 101 | static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
[2805] | 102 | |
---|
| 103 | private: |
---|
[3280] | 104 | class _CoreExport GameStateFactory |
---|
[2817] | 105 | { |
---|
[3280] | 106 | public: |
---|
| 107 | virtual ~GameStateFactory() { } |
---|
[3355] | 108 | static GameState* fabricate(const GameStateInfo& info); |
---|
[3280] | 109 | template <class T> |
---|
| 110 | static void createFactory(const std::string& className) |
---|
| 111 | { factories_s[className] = new TemplateGameStateFactory<T>(); } |
---|
| 112 | static void destroyFactories(); |
---|
| 113 | private: |
---|
[3355] | 114 | virtual GameState* fabricateInternal(const GameStateInfo& info) = 0; |
---|
[3280] | 115 | static std::map<std::string, GameStateFactory*> factories_s; |
---|
| 116 | }; |
---|
| 117 | template <class T> |
---|
| 118 | class TemplateGameStateFactory : public GameStateFactory |
---|
| 119 | { |
---|
| 120 | public: |
---|
[3355] | 121 | GameState* fabricateInternal(const GameStateInfo& info) |
---|
| 122 | { return new T(info); } |
---|
[3280] | 123 | }; |
---|
| 124 | |
---|
| 125 | struct StatisticsTickInfo |
---|
| 126 | { |
---|
[2817] | 127 | uint64_t tickTime; |
---|
| 128 | uint32_t tickLength; |
---|
| 129 | }; |
---|
| 130 | |
---|
[2805] | 131 | Game(Game&); // don't mess with singletons |
---|
| 132 | |
---|
[3355] | 133 | void loadGraphics(); |
---|
| 134 | void unloadGraphics(); |
---|
| 135 | |
---|
[2844] | 136 | void loadState(GameState* state); |
---|
| 137 | void unloadState(GameState* state); |
---|
[2805] | 138 | |
---|
[3352] | 139 | // Main loop structuring |
---|
| 140 | void updateGameStateStack(); |
---|
| 141 | void updateGameStates(); |
---|
| 142 | void updateStatistics(); |
---|
| 143 | void updateFPSLimiter(); |
---|
| 144 | |
---|
[3280] | 145 | std::map<std::string, GameState*> gameStates_; |
---|
| 146 | std::vector<GameState*> activeStates_; |
---|
[3196] | 147 | boost::shared_ptr<GameStateTreeNode> rootStateNode_; |
---|
| 148 | boost::shared_ptr<GameStateTreeNode> activeStateNode_; |
---|
| 149 | std::vector<boost::shared_ptr<GameStateTreeNode> > requestedStateNodes_; |
---|
[2805] | 150 | |
---|
[2844] | 151 | Core* core_; |
---|
| 152 | Clock* gameClock_; |
---|
[3280] | 153 | GameConfiguration* configuration_; |
---|
[2844] | 154 | |
---|
[3280] | 155 | bool bChangingState_; |
---|
| 156 | bool bAbort_; |
---|
[2844] | 157 | |
---|
[2817] | 158 | // variables for time statistics |
---|
[2844] | 159 | uint64_t statisticsStartTime_; |
---|
[3280] | 160 | std::list<StatisticsTickInfo> statisticsTickTimes_; |
---|
[2844] | 161 | uint32_t periodTime_; |
---|
| 162 | uint32_t periodTickTime_; |
---|
| 163 | float avgFPS_; |
---|
| 164 | float avgTickTime_; |
---|
[3352] | 165 | int excessSleepTime_; |
---|
| 166 | unsigned int minimumSleepTime_; |
---|
[2817] | 167 | |
---|
[3280] | 168 | static std::map<std::string, GameStateInfo> gameStateDeclarations_s; |
---|
[2805] | 169 | static Game* singletonRef_s; //!< Pointer to the Singleton |
---|
| 170 | }; |
---|
[3280] | 171 | |
---|
| 172 | template <class T> |
---|
| 173 | /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) |
---|
| 174 | { |
---|
| 175 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName)); |
---|
| 176 | if (it == gameStateDeclarations_s.end()) |
---|
| 177 | { |
---|
| 178 | GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)]; |
---|
| 179 | info.stateName = stateName; |
---|
| 180 | info.className = className; |
---|
| 181 | info.bIgnoreTickTime = bIgnoreTickTime; |
---|
| 182 | info.bGraphicsMode = bGraphicsMode; |
---|
| 183 | } |
---|
| 184 | else |
---|
| 185 | { |
---|
| 186 | COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl; |
---|
| 187 | COUT(0) << " Ignoring second one ('" << stateName << "')." << std::endl; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | // Create a factory to delay GameState creation |
---|
| 191 | GameStateFactory::createFactory<T>(className); |
---|
| 192 | |
---|
| 193 | // just a required dummy return value |
---|
| 194 | return true; |
---|
| 195 | } |
---|
[2805] | 196 | } |
---|
[3280] | 197 | |
---|
[2805] | 198 | #endif /* _Game_H__ */ |
---|