Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Game.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_GAME
19 #define INCLUDED_GAME
20 
21 #include "ps/Errors.h"
22 #include <vector>
23 
25 
26 class CWorld;
27 class CSimulation2;
28 class CGameView;
29 class CNetTurnManager;
30 class IReplayLogger;
31 struct CColor;
32 
33 /**
34  * The container that holds the rules, resources and attributes of the game.
35  * The CGame object is responsible for creating a game that is defined by
36  * a set of attributes provided. The CGame object is also responsible for
37  * maintaining the relations between CPlayer and CWorld, CSimulation and CWorld.
38  **/
39 class CGame
40 {
42  /**
43  * pointer to the CWorld object representing the game world.
44  **/
46  /**
47  * pointer to the CSimulation2 object operating on the game world.
48  **/
50  /**
51  * pointer to the CGameView object representing the view into the game world.
52  **/
54  /**
55  * the game has been initialized and ready for use if true.
56  **/
58  /**
59  * Timescale multiplier for simulation rate.
60  **/
61  float m_SimRate;
62 
64 
66 
67 public:
68  CGame(bool disableGraphics = false);
69  ~CGame();
70 
71  /**
72  * the game is paused and no updates will be performed if true.
73  **/
74  bool m_Paused;
75 
76  void StartGame(const CScriptValRooted& attribs, const std::string& savedState);
78 
79  /**
80  * Periodic heartbeat that controls the process. performs all per-frame updates.
81  * Simulation update is called and game status update is called.
82  *
83  * @param deltaRealTime Elapsed real time since last beat/frame, in seconds.
84  * @param doInterpolate Perform graphics interpolation if true.
85  * @return bool false if it can't keep up with the desired simulation rate
86  * indicating that you might want to render less frequently.
87  */
88  bool Update(const double deltaRealTime, bool doInterpolate = true);
89 
90  void Interpolate(float simFrameLength, float realFrameLength);
91 
92  int GetPlayerID();
93  void SetPlayerID(int playerID);
94 
95  /**
96  * Retrieving player colours from scripts is slow, so this updates an
97  * internal cache of all players' colours.
98  * Call this just before rendering, so it will always have the latest
99  * colours.
100  */
101  void CachePlayerColours();
102 
103  CColor GetPlayerColour(int player) const;
104 
105  /**
106  * Get m_GameStarted.
107  *
108  * @return bool the value of m_GameStarted.
109  **/
110  inline bool IsGameStarted() const
111  {
112  return m_GameStarted;
113  }
114 
115  /**
116  * Get the pointer to the game world object.
117  *
118  * @return CWorld * the value of m_World.
119  **/
120  inline CWorld *GetWorld()
121  { return m_World; }
122 
123  /**
124  * Get the pointer to the game view object.
125  *
126  * @return CGameView * the value of m_GameView.
127  **/
128  inline CGameView *GetView()
129  { return m_GameView; }
130 
131  /**
132  * Get the pointer to the simulation2 object.
133  *
134  * @return CSimulation2 * the value of m_Simulation2.
135  **/
137  { return m_Simulation2; }
138 
139  /**
140  * Set the simulation scale multiplier.
141  *
142  * @param simRate Float value to set m_SimRate to.
143  * Because m_SimRate is also used to
144  * scale TimeSinceLastFrame it must be
145  * clamped to 0.0f.
146  **/
147  inline void SetSimRate(float simRate)
148  { if (isfinite(simRate)) m_SimRate = std::max(simRate, 0.0f); }
149 
150  inline float GetSimRate()
151  { return m_SimRate; }
152 
153  /**
154  * Replace the current turn manager.
155  * This class will take ownership of the pointer.
156  */
157  void SetTurnManager(CNetTurnManager* turnManager);
158 
160  { return m_TurnManager; }
161 
163  { return *m_ReplayLogger; }
164 
165 private:
166  void RegisterInit(const CScriptValRooted& attribs, const std::string& savedState);
168 
169  std::vector<CColor> m_PlayerColours;
170 
171  int LoadInitialState();
172  std::string m_InitialSavedState; // valid between RegisterInit and LoadInitialState
173  bool m_IsSavedGame; // true if loading a saved game; false for a new game
174 };
175 
176 extern CGame *g_Game;
177 
178 #endif
The container that holds the rules, resources and attributes of the game.
Definition: Game.h:39
float GetSimRate()
Definition: Game.h:150
void CachePlayerColours()
Retrieving player colours from scripts is slow, so this updates an internal cache of all players&#39; col...
Definition: Game.cpp:332
Definition: Overlay.h:34
#define isfinite
Definition: posix.h:127
int LoadInitialState()
Definition: Game.cpp:173
CWorld * m_World
pointer to the CWorld object representing the game world.
Definition: Game.h:45
bool m_IsSavedGame
Definition: Game.h:173
Replay log recorder interface.
Definition: Replay.h:29
~CGame()
Destructor.
Definition: Game.cpp:89
CGameView * m_GameView
pointer to the CGameView object representing the view into the game world.
Definition: Game.h:53
Public API for simulation system.
Definition: Simulation2.h:46
int GetPlayerID()
Definition: Game.cpp:249
void Interpolate(float simFrameLength, float realFrameLength)
Definition: Game.cpp:321
CWorld is a general data class containing whatever is needed to accurately represent the world...
Definition: World.h:47
void SetPlayerID(int playerID)
Definition: Game.cpp:254
bool Update(const double deltaRealTime, bool doInterpolate=true)
Periodic heartbeat that controls the process.
Definition: Game.cpp:272
u32 PSRETURN
Definition: Errors.h:75
void SetTurnManager(CNetTurnManager *turnManager)
Replace the current turn manager.
Definition: Game.cpp:102
void StartGame(const CScriptValRooted &attribs, const std::string &savedState)
Definition: Game.cpp:261
CNetTurnManager * GetTurnManager() const
Definition: Game.h:159
bool m_Paused
the game is paused and no updates will be performed if true.
Definition: Game.h:74
std::string m_InitialSavedState
Definition: Game.h:172
Common network turn system (used by clients and offline games).
CGame * g_Game
Globally accessible pointer to the CGame object.
Definition: Game.cpp:56
void SetSimRate(float simRate)
Set the simulation scale multiplier.
Definition: Game.h:147
void RegisterInit(const CScriptValRooted &attribs, const std::string &savedState)
Initializes the game with the set of attributes provided.
Definition: Game.cpp:119
CNetTurnManager * m_TurnManager
Definition: Game.h:65
CSimulation2 * GetSimulation2()
Get the pointer to the simulation2 object.
Definition: Game.h:136
std::vector< CColor > m_PlayerColours
Definition: Game.h:169
CGameView * GetView()
Get the pointer to the game view object.
Definition: Game.h:128
int m_PlayerID
Definition: Game.h:63
CGame(bool disableGraphics=false)
Constructor.
Definition: Game.cpp:62
bool IsGameStarted() const
Get m_GameStarted.
Definition: Game.h:110
CColor GetPlayerColour(int player) const
Definition: Game.cpp:354
CWorld * GetWorld()
Get the pointer to the game world object.
Definition: Game.h:120
float m_SimRate
Timescale multiplier for simulation rate.
Definition: Game.h:61
PSRETURN ReallyStartGame()
Game initialization has been completed.
Definition: Game.cpp:198
IReplayLogger * m_ReplayLogger
Definition: Game.h:167
bool m_GameStarted
the game has been initialized and ready for use if true.
Definition: Game.h:57
IReplayLogger & GetReplayLogger() const
Definition: Game.h:162
NONCOPYABLE(CGame)
CSimulation2 * m_Simulation2
pointer to the CSimulation2 object operating on the game world.
Definition: Game.h:49