Bridges-C++  3.2.0
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:
168  NonBlockingGame(int assignmentID, std::string username,
169  std::string apikey, int nbRow = 10, int nbCol = 10)
170  : GameBase(assignmentID, username, apikey, nbRow, nbCol) {
171  if (debug)
172  std::cerr << "nbRow: " << nbRow << " nbCol: " <<
173  nbCol << std::endl;
174 
175  if (nbRow * nbCol > 32 * 32) {
176  throw "NonBlockingGame can not have a grid of more than 32x32 (or a combination(so 16x64 is ok; 16x128 is not)";
177  }
178 
179  registerKeyListener(&ih);
180 
181  }
182 
185  void start() {
186  timeOfLastFrame = localclock::now();
187  initialize();
188 
189  long framelimit = -1; //negative means no limit
190  {
191  char* str_limit = getenv("FORCE_BRIDGES_FRAMELIMIT");
192  if (str_limit != nullptr) {
193  std::stringstream ss;
194  ss << str_limit;
195  ss >> framelimit;
196  std::cerr << "Setting framelimit to " << framelimit << std::endl;
197  }
198  }
199  long frame = 0;
200  while (!gameover()) {
201  gameLoop();
202  render();
203  handleFrameRate();
204  frame++;
205  if (framelimit > 0 && frame > framelimit)
206  quit();
207  }
208  }
209 
210 
211  protected:
217  double getFrameRate() const {
218  return fps;
219  }
220 
224  bool keyLeft() {
225  return ih.keyLeft();
226  }
227 
231  bool keyRight() {
232  return ih.keyRight();
233  }
234 
238  bool keyUp() {
239  return ih.keyUp();
240  }
241 
245  bool keyDown() {
246  return ih.keyDown();
247  }
248 
252  bool keyW() {
253  return ih.keyW();
254  }
255 
259  bool keyA() {
260  return ih.keyA();
261  }
262 
266  bool keyS() {
267  return ih.keyS();
268  }
269 
273  bool keyD() {
274  return ih.keyD();
275  }
276 
280  bool keyQ() {
281  return ih.keyQ();
282  }
283 
287  bool keySpace() {
288  return ih.keySpace();
289  }
290 
291  };
292  }
293 }
294 
295 #endif
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:83
void render()
Renders the game.
Definition: GameBase.h:91
bool debug
Definition: GameBase.h:39
bool keyA()
Is A currently pressed?
Definition: NonBlockingGame.h:259
This class provides the features necessary to implement simple non blocking games.
Definition: NonBlockingGame.h:128
bool keyD() const
Definition: InputHelper.h:114
void start()
Call this function from main to start the game. Runs exactly once.
Definition: NonBlockingGame.h:185
bool keyDown()
Is Down currently pressed?
Definition: NonBlockingGame.h:245
The base class building and using the BRIDGES based games.
Definition: GameBase.h:25
bool keySpace() const
Definition: InputHelper.h:120
NonBlockingGame(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbCol=10)
Definition: NonBlockingGame.h:168
double getFrameRate() const
What frame rate is the game running at?
Definition: NonBlockingGame.h:217
bool keyUp() const
Definition: InputHelper.h:89
bool keyA() const
Definition: InputHelper.h:108
void quit()
calling this function causes the game to end.
Definition: GameBase.h:116
bool keySpace()
Is Space currently pressed?
Definition: NonBlockingGame.h:287
bool keyQ()
Is Q currently pressed?
Definition: NonBlockingGame.h:280
bool keyLeft()
Is Left currently pressed?
Definition: NonBlockingGame.h:224
bool keyUp()
Is Up currently pressed?
Definition: NonBlockingGame.h:238
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:117
bool keyDown() const
Definition: InputHelper.h:93
bool keyS() const
Definition: InputHelper.h:111
bool gameover() const
Definition: GameBase.h:108
bool keyW()
Is W currently pressed?
Definition: NonBlockingGame.h:252
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:21
bool keyD()
Is D currently pressed?
Definition: NonBlockingGame.h:273
bool keyW() const
Definition: InputHelper.h:105
bool keyRight()
Is Right currently pressed?
Definition: NonBlockingGame.h:231
bool keyRight() const
Definition: InputHelper.h:101
virtual void gameLoop()=0
This function is called once per frame of the game.
bool keyS()
Is S currently pressed?
Definition: NonBlockingGame.h:266
bool keyLeft() const
Definition: InputHelper.h:97