File: StateManager.h

package info (click to toggle)
xmoto 0.5.11+dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 80,908 kB
  • sloc: cpp: 96,757; ansic: 22,196; sh: 4,940; makefile: 1,073; yacc: 289; sed: 16
file content (181 lines) | stat: -rw-r--r-- 4,843 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*=============================================================================
XMOTO

This file is part of XMOTO.

XMOTO is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

XMOTO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with XMOTO; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
=============================================================================*/

#ifndef __STATEMANAGER_H__
#define __STATEMANAGER_H__

#include "../helpers/Singleton.h"
#include "../VCommon.h"
#include "../XMKey.h"
#include "../helpers/RenderSurface.h"
#include "../include/xm_SDL.h"
#include <string>
#include <vector>
#include <map>

class VideoRecorder;
class GameState;
class Texture;
class RenderSurface;
class XMThreadStats;
class DownloadReplaysThread;

class StateManager : public Singleton<StateManager> {
  friend class Singleton<StateManager>;
private:
  StateManager();
  ~StateManager();

public:

  std::string getUniqueId();

  void pushState(GameState* pNewState);
  /* replace only the last state with id = i_parentId */
  void replaceState(GameState* pNewState, const std::string& i_parentId);

  // after some events, a state can requestForEnd
  // -> return NULL or the state which requested to be ended
  void flush();

  void update();
  void render();
  // input
  void xmKey(InputEventType i_type, const XMKey& i_xmkey);

  void changeFocus(bool i_hasFocus);
  void changeVisibility(bool i_visible);

  bool needUpdateOrRender();

  // to display on the screen
  int getCurrentUpdateFPS();
  int getCurrentRenderFPS();

  // in order to receive a message, you have to first register
  // yourself as an observer of this message
  void registerAsObserver(const std::string& message,   GameState* self);
  void unregisterAsObserver(const std::string& message, GameState* self);
  // register as emitter only for debug information
  void registerAsEmitter(const std::string& message);

  // send the message to registered states
  void sendSynchronousMessage(const std::string& message, const std::string& args="", const std::string& i_parentId = "");
  void sendAsynchronousMessage(const std::string& message, const std::string& args="", const std::string& i_parentId = "");

  bool isTopOfTheStates(GameState* i_state);
  int numberOfStates();

  int getMaxFps(){
    return m_maxFps;
  }

  // video recorder
  VideoRecorder* getVideoRecorder();

  // ask to states to clean themself
  static void cleanStates();
  static void refreshStaticCaptions();

  bool isThereASuchState(const std::string& i_name);
  bool isThereASuchStateType(const std::string& i_type);

  // thread to externalize db update in other thread to reduce freezes
  XMThreadStats* getDbStatsThread();

  DownloadReplaysThread* getReplayDownloaderThread();

private:
  GameState* popState();

  void calculateWhichStateIsRendered();
  void calculateFps();
  bool doRender();
  void drawFps();
  void drawStack();
  void drawTexturesLoading();
  void drawGeomsLoading();
  void drawCursor();

  void renderOverAll();

  void deleteToDeleteState();

  void setNewStateScreen(GameState* pNewState);

  bool m_isVisible;
  bool m_hasFocus;

  std::vector<GameState*> m_statesStack;
  std::vector<GameState*> m_toDeleteStates;

  // last time the m_current*Fps have been filled
  int m_lastFpsTime;

  // contains the number of frame during the last second
  int m_currentRenderFps;
  int m_currentUpdateFps;

  // counting the number of frame for the current second
  int m_renderFpsNbFrame;
  int m_updateFpsNbFrame;

  // the hz at which the state manager runs
  // (the highest from the states)
  int m_maxUpdateFps;
  int m_maxRenderFps;
  int m_maxFps;

  // to render depending on the max Hz
  float m_renderCounter;
  // the desired rendering fps
  int m_curRenderFps;
  // how many max fps beat for one render
  float m_renderPeriod;

  // cursor
  Texture* m_cursor;
  bool m_isCursorVisible;
  int m_lastMouseMoveTime;
  int m_lastMouseMoveTimeInZone;
  int m_previousMouseOverPlayer;
  int m_previousMouseX;
  int m_previousMouseY;

  // video
  VideoRecorder* m_videoRecorder;

  // messages and associate observer states
  std::map<std::string, std::vector<GameState*> > m_registeredStates;

  // db stats thread
  XMThreadStats *m_xmtstas;

  // replays downloader
  DownloadReplaysThread* m_drt;

  //
  int m_currentUniqueId;

  RenderSurface m_screen;

};

#endif