Bridges-C++  3.1.1
Bridges(C++API)
NonBlockingGame.h
Go to the documentation of this file.
1 #ifndef NONBLOCKING_GAME_H
2 #define NONBLOCKING_GAME_H
3 
4 #include <GameBase.h>
5 #include <InputHelper.h>
6 
7 namespace bridges {
8  namespace game {
9 
128  class NonBlockingGame : public GameBase {
129  private:
130  using GameBase::render;
132 
133  typedef std::chrono::steady_clock localclock;
134 
135  InputHelper ih;
136 
137  double fps = 30.;
138 
139  localclock::time_point timeOfLastFrame;
140 
141  void handleFrameRate() {
142  using std::chrono::seconds;
143  using std::chrono::microseconds;
144  using std::chrono::duration_cast;
145 
146  microseconds frametime = duration_cast<microseconds>(seconds(1l)) / (int)fps;
147 
148  localclock::time_point theoretical_next_frame = timeOfLastFrame + frametime;
149 
150  auto wait_time = theoretical_next_frame - localclock::now();
151 
152  if (wait_time.count() > 0) {
153  microseconds wait_time_in_us = duration_cast<microseconds>(wait_time);
154  usleep(wait_time_in_us.count());
155  }
156 
157  timeOfLastFrame = localclock::now();
158  }
159 
160 
161  public:
162  NonBlockingGame(int assignmentID, std::string username, std::string apikey, int nbRow = 10, int nbCol = 10)
163  : GameBase(assignmentID, username, apikey, nbRow, nbCol) {
164  if (debug)
165  std::cerr << "nbRow: " << nbRow << " nbCol: " << nbCol << std::endl;
166 
167  if (nbRow * nbCol > 32 * 32) {
168  throw "NonBlockingGame can not have a grid of more than 32x32 (or a combination(so 16x64 is ok; 16x128 is not)";
169  }
170 
171  registerKeyListener(&ih);
172 
173  }
174 
176  void start() {
177  timeOfLastFrame = localclock::now();
178  initialize();
179 
180  long framelimit = -1; //negative means no limit
181  {
182  char* str_limit = getenv("FORCE_BRIDGES_FRAMELIMIT");
183  if (str_limit != nullptr) {
184  std::stringstream ss;
185  ss << str_limit;
186  ss >> framelimit;
187  std::cerr << "Setting framelimit to " << framelimit << std::endl;
188  }
189  }
190  long frame = 0;
191  while (!gameover()) {
192  gameLoop();
193  render();
194  handleFrameRate();
195  frame++;
196  if (framelimit > 0 && frame > framelimit)
197  quit();
198  }
199  }
200 
201 
202  protected:
208  double getFrameRate() const {
209  return fps;
210  }
211 
215  bool keyLeft() {
216  return ih.keyLeft();
217  }
218 
222  bool keyRight() {
223  return ih.keyRight();
224  }
225 
229  bool keyUp() {
230  return ih.keyUp();
231  }
232 
236  bool keyDown() {
237  return ih.keyDown();
238  }
239 
243  bool keyW() {
244  return ih.keyW();
245  }
246 
250  bool keyA() {
251  return ih.keyA();
252  }
253 
257  bool keyS() {
258  return ih.keyS();
259  }
260 
264  bool keyD() {
265  return ih.keyD();
266  }
267 
271  bool keyQ() {
272  return ih.keyQ();
273  }
274 
278  bool keySpace() {
279  return ih.keySpace();
280  }
281 
282  };
283  }
284 }
285 
286 #endif
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:61
void render()
Renders the game.
Definition: GameBase.h:69
bool debug
Definition: GameBase.h:24
bool keyA()
Is A currently pressed?
Definition: NonBlockingGame.h:250
This class provides the features necessary to implement simple non blocking games.
Definition: NonBlockingGame.h:128
bool keyD() const
Definition: InputHelper.h:103
void start()
Call this function from main to start the game.
Definition: NonBlockingGame.h:176
bool keyDown()
Is Down currently pressed?
Definition: NonBlockingGame.h:236
Definition: GameBase.h:10
bool keySpace() const
Definition: InputHelper.h:109
NonBlockingGame(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbCol=10)
Definition: NonBlockingGame.h:162
double getFrameRate() const
What frame rate is the game running at?
Definition: NonBlockingGame.h:208
bool keyUp() const
Definition: InputHelper.h:78
bool keyA() const
Definition: InputHelper.h:97
void quit()
calling this function causes the game to end.
Definition: GameBase.h:94
bool keySpace()
Is Space currently pressed?
Definition: NonBlockingGame.h:278
bool keyQ()
Is Q currently pressed?
Definition: NonBlockingGame.h:271
bool keyLeft()
Is Left currently pressed?
Definition: NonBlockingGame.h:215
bool keyUp()
Is Up currently pressed?
Definition: NonBlockingGame.h:229
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
bool keyQ() const
Definition: InputHelper.h:106
bool keyDown() const
Definition: InputHelper.h:82
bool keyS() const
Definition: InputHelper.h:100
bool gameover() const
Definition: GameBase.h:86
bool keyW()
Is W currently pressed?
Definition: NonBlockingGame.h:243
virtual void initialize()=0
This function is called once when the game starts.
this is meant to be an internal class, not something that the library user will use ...
Definition: InputHelper.h:10
bool keyD()
Is D currently pressed?
Definition: NonBlockingGame.h:264
bool keyW() const
Definition: InputHelper.h:94
bool keyRight()
Is Right currently pressed?
Definition: NonBlockingGame.h:222
bool keyRight() const
Definition: InputHelper.h:90
virtual void gameLoop()=0
This function is called once per frame of the game.
bool keyS()
Is S currently pressed?
Definition: NonBlockingGame.h:257
bool keyLeft() const
Definition: InputHelper.h:86