Orxonox  0.0.5 Codename: Arcturus
Game.h
Go to the documentation of this file.
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 
36 #ifndef _Game_H__
37 #define _Game_H__
38 
39 #include "CorePrereqs.h"
40 
41 #include <cassert>
42 #include <list>
43 #include <map>
44 #include <string>
45 #include <vector>
46 #include <memory>
47 #include <boost/preprocessor/cat.hpp>
48 
49 #include "util/Output.h"
50 #include "util/DestructionHelper.h"
51 #include "util/Singleton.h"
52 
58 #define DeclareGameState(className, stateName, bIgnoreTickTime, bGraphicsMode) \
59  static bool BOOST_PP_CAT(bGameStateDummy_##className, __UNIQUE_NUMBER__) = orxonox::Game::declareGameState<className>(#className, stateName, bIgnoreTickTime, bGraphicsMode)
60 // tolua_begin
61 namespace orxonox
62 {
63 // tolua_end
64 
67  {
72  };
73 
80 // tolua_begin
82 // tolua_end
83  : public Singleton<Game>
84  { // tolua_export
85  friend class Singleton<Game>;
86  typedef std::vector<std::shared_ptr<GameState>> GameStateVector;
87  typedef std::map<std::string, std::shared_ptr<GameState>> GameStateMap;
88  typedef std::shared_ptr<GameStateTreeNode> GameStateTreeNodePtr;
89 
90  public:
91  Game(const std::string& cmdLine);
92 
94  ~Game() = default;
96  void destroy();
97 
98  void setStateHierarchy(const std::string& str);
99  std::shared_ptr<GameState> getState(const std::string& name);
100 
101  void run();
102  void stop();
103 
104  static Game& getInstance(){ return Singleton<Game>::getInstance(); } // tolua_export
105 
106  void requestState(const std::string& name); //tolua_export
107  void requestStates(const std::string& names); //tolua_export
108  void popState(); //tolua_export
109 
110  const Clock& getGameClock() { return *this->gameClock_; }
111 
112  float getAvgTickTime() { return this->avgTickTime_; }
113  float getAvgFPS() { return this->avgFPS_; }
114 
115  void subtractTickTime(int32_t length);
116 
117  template <class T>
118  static bool declareGameState(const std::string& className, const std::string& stateName, bool bIgnoreTickTime, bool bConsoleMode);
119 
120  private:
122  {
123  public:
124  virtual ~GameStateFactory() = default;
125  static std::shared_ptr<GameState> fabricate(const GameStateInfo& info);
126  template <class T>
127  static void createFactory(const std::string& className)
128  { getFactories()[className].reset(new TemplateGameStateFactory<T>()); }
129 
130  virtual std::shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) = 0;
131  static std::map<std::string, std::shared_ptr<GameStateFactory>>& getFactories();
132  };
133  template <class T>
135  {
136  public:
137  virtual std::shared_ptr<GameState> fabricateInternal(const GameStateInfo& info) override
138  { return std::shared_ptr<GameState>(std::make_shared<T>(info)); }
139  };
140 
142  {
143  uint64_t tickTime;
144  uint32_t tickLength;
145  };
146 
147  // non-copyable:
148  Game(const Game&) = delete;
149  Game& operator=(const Game&) = delete;
150 
151  void loadGraphics();
152  void unloadGraphics(bool loadGraphicsManagerWithoutRenderer = true);
153 
154  void parseStates(std::vector<std::pair<std::string, int>>::const_iterator& it, std::shared_ptr<GameStateTreeNode> currentNode);
155  bool checkState(const std::string& name) const;
156  void loadState(const std::string& name);
157  void unloadState(const std::string& name);
158 
159  // Main loop structuring
160  void updateGameStateStack();
161  void updateGameStates();
162  void updateStatistics();
163  void updateFPSLimiter();
164 
165  // ScopeGuard helper function
166  void resetChangingState() { this->bChangingState_ = false; }
167 
170 
171  GameStateMap constructedStates_;
172  GameStateVector loadedStates_;
173  GameStateTreeNodePtr rootStateNode_;
174  GameStateTreeNodePtr loadedTopStateNode_;
175  std::vector<GameStateTreeNodePtr> requestedStateNodes_;
176 
178  bool bAbort_;
179 
180  // variables for time statistics
182  std::list<StatisticsTickInfo> statisticsTickTimes_;
183  uint32_t periodTime_;
184  uint32_t periodTickTime_;
185  float avgFPS_;
188  unsigned int minimumSleepTime_;
189 
192 
195 
196  static std::map<std::string, GameStateInfo> gameStateDeclarations_s;
198  }; //tolua_export
199 
200  template <class T>
202  {
203  std::map<std::string, GameStateInfo>::const_iterator it = gameStateDeclarations_s.find(stateName);
204  if (it == gameStateDeclarations_s.end())
205  {
206  GameStateInfo& info = gameStateDeclarations_s[stateName];
207  info.stateName = stateName;
208  info.className = className;
211  }
212  else
213  {
214  orxout(internal_warning) << "Cannot declare two GameStates with the same name." << endl;
215  orxout(internal_warning) << "Ignoring second one ('" << stateName << "')." << endl;
216  }
217 
218  // Create a factory to delay GameState creation
219  GameStateFactory::createFactory<T>(className);
220 
221  // just a required dummy return value
222  return true;
223  }
224 } //tolua_export
225 
226 #endif /* _Game_H__ */
std::vector< std::shared_ptr< GameState > > GameStateVector
Definition: Game.h:86
int excessSleepTime_
Definition: Game.h:187
bool bAbort_
Definition: Game.h:178
float getAvgFPS()
Definition: Game.h:113
std::shared_ptr< GameStateTreeNode > GameStateTreeNodePtr
Definition: Game.h:88
Definition: Game.h:121
static Game * singletonPtr_s
Pointer to the Singleton.
Definition: Game.h:197
uint32_t tickLength
Definition: Game.h:144
static Game & getInstance()
Definition: Game.h:104
std::list< StatisticsTickInfo > statisticsTickTimes_
Definition: Game.h:182
GameStateTreeNodePtr loadedTopStateNode_
Definition: Game.h:174
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
void resetChangingState()
Definition: Game.h:166
Definition: GameConfig.h:38
bool bChangingState_
Definition: Game.h:177
Output level, used for warnings which are important for developers.
Definition: OutputDefinitions.h:96
float avgFPS_
Definition: Game.h:185
static std::map< std::string, GameStateInfo > gameStateDeclarations_s
Definition: Game.h:196
GameStateMap constructedStates_
Definition: Game.h:171
The MetaPickup destroys all the PickupCarriers&#39; Pickupables.
Helper object required before GameStates are being constructed.
Definition: Game.h:66
uint32_t periodTickTime_
Definition: Game.h:184
float avgTickTime_
Definition: Game.h:186
GameStateVector loadedStates_
Definition: Game.h:172
static bool declareGameState(const std::string &className, const std::string &stateName, bool bIgnoreTickTime, bool bConsoleMode)
Definition: Game.h:201
GameConfig * config_
Helper object that stores the config values.
Definition: Game.h:191
bool bGraphicsMode
Definition: Game.h:71
The Core class is a singleton used to configure the program basics.
Definition: Core.h:60
static void createFactory(const std::string &className)
Definition: Game.h:127
Base for singleton classes.
Definition: Singleton.h:114
Definition: Game.h:141
OutputStream & orxout(OutputLevel level=level::debug_output, const OutputContextContainer &context=context::undefined())
This helper function returns a reference to a commonly used instance of OutputStream.
Definition: Output.h:81
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
virtual std::shared_ptr< GameState > fabricateInternal(const GameStateInfo &info) override
Definition: Game.h:137
#define _CoreExport
Definition: CorePrereqs.h:61
Utility class that helps to create a special kind of destructor that also executes if the destruction...
Definition: DestructionHelper.h:75
Defines the helper function orxout() and includes all necessary headers to use the output system...
float getAvgTickTime()
Definition: Game.h:112
Main class responsible for running the game.
Definition: Game.h:81
const Clock & getGameClock()
Definition: Game.h:110
std::string className
Definition: Game.h:69
std::string stateName
Definition: Game.h:68
Definition of the Singleton template that is used as base class for classes that allow only one insta...
Simple real time clock based on Ogre::Timer.
Definition: Clock.h:57
unsigned int minimumSleepTime_
Definition: Game.h:188
bool bIgnoreTickTime
Definition: Game.h:70
GameStateTreeNodePtr rootStateNode_
Definition: Game.h:173
uint64_t tickTime
Definition: Game.h:143
std::vector< GameStateTreeNodePtr > requestedStateNodes_
Definition: Game.h:175
uint64_t statisticsStartTime_
Definition: Game.h:181
static T & getInstance()
Returns a reference to the singleton instance.
Definition: Singleton.h:118
uint32_t periodTime_
Definition: Game.h:183
Clock * gameClock_
Definition: Game.h:168
Core * core_
Definition: Game.h:169
DestructionHelper< Game > destructionHelper_
Helper object that executes the surrogate destructor destroy()
Definition: Game.h:194
std::map< std::string, std::shared_ptr< GameState > > GameStateMap
Definition: Game.h:87