[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" |
---|
[3370] | 50 | #include "util/Singleton.h" |
---|
[2805] | 51 | |
---|
[3084] | 52 | /** |
---|
| 53 | @def |
---|
| 54 | Adds a new GameState to the Game. The second parameter is the name as string |
---|
| 55 | and every following paramter is a constructor argument (which is usually non existent) |
---|
| 56 | */ |
---|
[3280] | 57 | #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \ |
---|
| 58 | static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode) |
---|
[2844] | 59 | |
---|
[2805] | 60 | namespace orxonox |
---|
| 61 | { |
---|
[3280] | 62 | class GameConfiguration; |
---|
[3370] | 63 | using boost::scoped_ptr; |
---|
| 64 | using boost::shared_ptr; |
---|
[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 | */ |
---|
[3370] | 81 | class _CoreExport Game : public Singleton<Game> |
---|
[2805] | 82 | { |
---|
[3370] | 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; |
---|
[2805] | 87 | public: |
---|
[3323] | 88 | Game(const std::string& cmdLine); |
---|
[2805] | 89 | ~Game(); |
---|
| 90 | |
---|
[2844] | 91 | void setStateHierarchy(const std::string& str); |
---|
[3370] | 92 | shared_ptr<GameState> getState(const std::string& name); |
---|
[2844] | 93 | |
---|
[2805] | 94 | void run(); |
---|
| 95 | void stop(); |
---|
| 96 | |
---|
[2844] | 97 | void requestState(const std::string& name); |
---|
[2850] | 98 | void requestStates(const std::string& names); |
---|
[2844] | 99 | void popState(); |
---|
| 100 | |
---|
[2846] | 101 | const Clock& getGameClock() { return *this->gameClock_; } |
---|
| 102 | |
---|
[2817] | 103 | float getAvgTickTime() { return this->avgTickTime_; } |
---|
| 104 | float getAvgFPS() { return this->avgFPS_; } |
---|
| 105 | |
---|
[3370] | 106 | void subtractTickTime(int32_t length); |
---|
[2817] | 107 | |
---|
[3280] | 108 | template <class T> |
---|
| 109 | static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode); |
---|
[2805] | 110 | |
---|
| 111 | private: |
---|
[3280] | 112 | class _CoreExport GameStateFactory |
---|
[2817] | 113 | { |
---|
[3280] | 114 | public: |
---|
| 115 | virtual ~GameStateFactory() { } |
---|
[3370] | 116 | static shared_ptr<GameState> fabricate(const GameStateInfo& info); |
---|
[3280] | 117 | template <class T> |
---|
| 118 | static void createFactory(const std::string& className) |
---|
[3370] | 119 | { factories_s[className].reset(new TemplateGameStateFactory<T>()); } |
---|
[3280] | 120 | private: |
---|
[3370] | 121 | virtual shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0; |
---|
| 122 | static std::map<std::string, shared_ptr<GameStateFactory> > factories_s; |
---|
[3280] | 123 | }; |
---|
| 124 | template <class T> |
---|
| 125 | class TemplateGameStateFactory : public GameStateFactory |
---|
| 126 | { |
---|
| 127 | public: |
---|
[3370] | 128 | shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) |
---|
| 129 | { return shared_ptr<GameState>(new T(info)); } |
---|
[3280] | 130 | }; |
---|
| 131 | |
---|
| 132 | struct StatisticsTickInfo |
---|
| 133 | { |
---|
[2817] | 134 | uint64_t tickTime; |
---|
| 135 | uint32_t tickLength; |
---|
| 136 | }; |
---|
| 137 | |
---|
[2805] | 138 | Game(Game&); // don't mess with singletons |
---|
| 139 | |
---|
[3370] | 140 | void loadGraphics(); |
---|
| 141 | void unloadGraphics(); |
---|
[2805] | 142 | |
---|
[3370] | 143 | bool checkState(const std::string& name) const; |
---|
| 144 | void loadState(const std::string& name); |
---|
| 145 | void unloadState(const std::string& name); |
---|
[2805] | 146 | |
---|
[3370] | 147 | // Main loop structuring |
---|
| 148 | void updateGameStateStack(); |
---|
| 149 | void updateGameStates(); |
---|
| 150 | void updateStatistics(); |
---|
| 151 | void updateFPSLimiter(); |
---|
[2844] | 152 | |
---|
[3370] | 153 | // ScopeGuard helper function |
---|
| 154 | void resetChangingState() { this->bChangingState_ = false; } |
---|
[2844] | 155 | |
---|
[3370] | 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_; |
---|
| 168 | |
---|
[2817] | 169 | // variables for time statistics |
---|
[3370] | 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_; |
---|
[2817] | 178 | |
---|
[3280] | 179 | static std::map<std::string, GameStateInfo> gameStateDeclarations_s; |
---|
[3370] | 180 | static Game* singletonPtr_s; //!< Pointer to the Singleton |
---|
[2805] | 181 | }; |
---|
[3280] | 182 | |
---|
| 183 | template <class T> |
---|
| 184 | /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) |
---|
| 185 | { |
---|
[3370] | 186 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName); |
---|
[3280] | 187 | if (it == gameStateDeclarations_s.end()) |
---|
| 188 | { |
---|
[3370] | 189 | GameStateInfo& info = gameStateDeclarations_s[stateName]; |
---|
[3280] | 190 | info.stateName = stateName; |
---|
| 191 | info.className = className; |
---|
| 192 | info.bIgnoreTickTime = bIgnoreTickTime; |
---|
| 193 | info.bGraphicsMode = bGraphicsMode; |
---|
| 194 | } |
---|
| 195 | else |
---|
| 196 | { |
---|
| 197 | COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl; |
---|
| 198 | COUT(0) << " Ignoring second one ('" << stateName << "')." << std::endl; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | // Create a factory to delay GameState creation |
---|
| 202 | GameStateFactory::createFactory<T>(className); |
---|
| 203 | |
---|
| 204 | // just a required dummy return value |
---|
| 205 | return true; |
---|
| 206 | } |
---|
[2805] | 207 | } |
---|
[3280] | 208 | |
---|
[2805] | 209 | #endif /* _Game_H__ */ |
---|