Bridges-C++  3.4.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:
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 
183  virtual ~NonBlockingGame() =default;
184 
187  void start() {
188  timeOfLastFrame = localclock::now();
189  initialize();
190 
191  long framelimit = -1; //negative means no limit
192  {
193  char* str_limit = getenv("FORCE_BRIDGES_FRAMELIMIT");
194  if (str_limit != nullptr) {
195  std::stringstream ss;
196  ss << str_limit;
197  ss >> framelimit;
198  std::cerr << "Setting framelimit to " << framelimit << std::endl;
199  }
200  }
201  long frame = 0;
202  while (!gameover()) {
203  gameLoop();
204  render();
205  handleFrameRate();
206  frame++;
207  if (framelimit > 0 && frame > framelimit)
208  quit();
209  }
210  }
211 
212 
213  protected:
219  double getFrameRate() const {
220  return fps;
221  }
222 
226  bool keyLeft() {
227  return ih.keyLeft();
228  }
229 
233  bool keyRight() {
234  return ih.keyRight();
235  }
236 
240  bool keyUp() {
241  return ih.keyUp();
242  }
243 
247  bool keyDown() {
248  return ih.keyDown();
249  }
250 
254  bool keyW() {
255  return ih.keyW();
256  }
257 
261  bool keyA() {
262  return ih.keyA();
263  }
264 
268  bool keyS() {
269  return ih.keyS();
270  }
271 
275  bool keyD() {
276  return ih.keyD();
277  }
278 
282  bool keyQ() {
283  return ih.keyQ();
284  }
285 
289  bool keySpace() {
290  return ih.keySpace();
291  }
292 
293  };
294  }
295 }
296 
297 #endif
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase.h:85
void render()
Renders the game.
Definition: GameBase.h:93
bool debug
Definition: GameBase.h:39
bool keyA()
Is A currently pressed?
Definition: NonBlockingGame.h:261
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:187
bool keyDown()
Is Down currently pressed?
Definition: NonBlockingGame.h:247
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:219
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:118
bool keySpace()
Is Space currently pressed?
Definition: NonBlockingGame.h:289
bool keyQ()
Is Q currently pressed?
Definition: NonBlockingGame.h:282
bool keyLeft()
Is Left currently pressed?
Definition: NonBlockingGame.h:226
bool keyUp()
Is Up currently pressed?
Definition: NonBlockingGame.h:240
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:110
bool keyW()
Is W currently pressed?
Definition: NonBlockingGame.h:254
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:275
bool keyW() const
Definition: InputHelper.h:105
bool keyRight()
Is Right currently pressed?
Definition: NonBlockingGame.h:233
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:268
virtual ~NonBlockingGame()=default
bool keyLeft() const
Definition: InputHelper.h:97