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 | |
---|
38 | #include "CorePrereqs.h" |
---|
39 | |
---|
40 | #include <cassert> |
---|
41 | #include <list> |
---|
42 | #include <map> |
---|
43 | #include <string> |
---|
44 | #include <vector> |
---|
45 | #include <boost/shared_ptr.hpp> |
---|
46 | #include <boost/preprocessor/cat.hpp> |
---|
47 | |
---|
48 | #include "util/Debug.h" |
---|
49 | #include "OrxonoxClass.h" |
---|
50 | |
---|
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 | */ |
---|
56 | #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \ |
---|
57 | static bool BOOST_PP_CAT(bGameStateDummy_##className, __LINE__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode) |
---|
58 | |
---|
59 | // tolua_begin |
---|
60 | namespace orxonox |
---|
61 | { |
---|
62 | /** |
---|
63 | @brief |
---|
64 | Main class responsible for running the game. |
---|
65 | */ |
---|
66 | class _CoreExport Game : public OrxonoxClass |
---|
67 | { |
---|
68 | public: |
---|
69 | Game(int argc, char** argv); |
---|
70 | ~Game(); |
---|
71 | void setConfigValues(); |
---|
72 | |
---|
73 | void setStateHierarchy(const std::string& str); |
---|
74 | GameState* getState(const std::string& name); |
---|
75 | |
---|
76 | void run(); |
---|
77 | void stop(); |
---|
78 | |
---|
79 | void requestState(const std::string& name); |
---|
80 | void requestStates(const std::string& names); |
---|
81 | void popState(); |
---|
82 | |
---|
83 | const Clock& getGameClock() { return *this->gameClock_; } |
---|
84 | |
---|
85 | float getAvgTickTime() { return this->avgTickTime_; } |
---|
86 | float getAvgFPS() { return this->avgFPS_; } |
---|
87 | |
---|
88 | void addTickTime(uint32_t length); |
---|
89 | |
---|
90 | template <class T> |
---|
91 | static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode); |
---|
92 | static Game& getInstance() { assert(singletonRef_s); return *singletonRef_s; } |
---|
93 | |
---|
94 | private: |
---|
95 | class _CoreExport GameStateFactory |
---|
96 | { |
---|
97 | public: |
---|
98 | virtual ~GameStateFactory() { } |
---|
99 | static GameState* fabricate(const std::string& className, const GameStateConstrParams& params); |
---|
100 | template <class T> |
---|
101 | static void createFactory(const std::string& className) |
---|
102 | { factories_s[className] = new TemplateGameStateFactory<T>(); } |
---|
103 | static void destroyFactories(); |
---|
104 | private: |
---|
105 | virtual GameState* fabricate(const GameStateConstrParams& params) = 0; |
---|
106 | static std::map<std::string, GameStateFactory*> factories_s; |
---|
107 | }; |
---|
108 | template <class T> |
---|
109 | class TemplateGameStateFactory : public GameStateFactory |
---|
110 | { |
---|
111 | public: |
---|
112 | GameState* fabricate(const GameStateConstrParams& params) |
---|
113 | { return new T(params); } |
---|
114 | }; |
---|
115 | |
---|
116 | struct GameStateInfo |
---|
117 | { |
---|
118 | std::string stateName; |
---|
119 | std::string className; |
---|
120 | bool bIgnoreTickTime; |
---|
121 | bool bGraphicsMode; |
---|
122 | }; |
---|
123 | |
---|
124 | struct StatisticsTickInfo |
---|
125 | { |
---|
126 | uint64_t tickTime; |
---|
127 | uint32_t tickLength; |
---|
128 | }; |
---|
129 | |
---|
130 | Game(Game&); // don't mess with singletons |
---|
131 | |
---|
132 | void loadState(GameState* state); |
---|
133 | void unloadState(GameState* state); |
---|
134 | |
---|
135 | std::map<std::string, GameState*> gameStates_; |
---|
136 | std::vector<GameState*> activeStates_; |
---|
137 | boost::shared_ptr<GameStateTreeNode> rootStateNode_; |
---|
138 | boost::shared_ptr<GameStateTreeNode> activeStateNode_; |
---|
139 | std::vector<boost::shared_ptr<GameStateTreeNode> > requestedStateNodes_; |
---|
140 | |
---|
141 | Core* core_; |
---|
142 | Clock* gameClock_; |
---|
143 | |
---|
144 | bool bChangingState_; |
---|
145 | bool bAbort_; |
---|
146 | |
---|
147 | // variables for time statistics |
---|
148 | uint64_t statisticsStartTime_; |
---|
149 | std::list<StatisticsTickInfo> statisticsTickTimes_; |
---|
150 | uint32_t periodTime_; |
---|
151 | uint32_t periodTickTime_; |
---|
152 | float avgFPS_; |
---|
153 | float avgTickTime_; |
---|
154 | |
---|
155 | // config values |
---|
156 | unsigned int statisticsRefreshCycle_; |
---|
157 | unsigned int statisticsAvgLength_; |
---|
158 | |
---|
159 | static std::map<std::string, GameStateInfo> gameStateDeclarations_s; |
---|
160 | static Game* singletonRef_s; //!< Pointer to the Singleton |
---|
161 | }; |
---|
162 | |
---|
163 | template <class T> |
---|
164 | /*static*/ bool Game::declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bGraphicsMode) |
---|
165 | { |
---|
166 | std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(getLowercase(stateName)); |
---|
167 | if (it == gameStateDeclarations_s.end()) |
---|
168 | { |
---|
169 | GameStateInfo& info = gameStateDeclarations_s[getLowercase(stateName)]; |
---|
170 | info.stateName = stateName; |
---|
171 | info.className = className; |
---|
172 | info.bIgnoreTickTime = bIgnoreTickTime; |
---|
173 | info.bGraphicsMode = bGraphicsMode; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | COUT(0) << "Error: Cannot declare two GameStates with the same name." << std::endl; |
---|
178 | COUT(0) << " Ignoring second one ('" << stateName << "')." << std::endl; |
---|
179 | } |
---|
180 | |
---|
181 | // Create a factory to delay GameState creation |
---|
182 | GameStateFactory::createFactory<T>(className); |
---|
183 | |
---|
184 | // just a required dummy return value |
---|
185 | return true; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | #endif /* _Game_H__ */ |
---|