Bridges-C++ 3.5.1
Bridges(C++ API)
Loading...
Searching...
No Matches
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#include <InputStateMachine.h>
7
8namespace bridges {
9 namespace game {
10
178 class NonBlockingGame : public GameBase {
179 private:
180 using GameBase::render;
182
183 typedef std::chrono::steady_clock localclock;
184
185 InputHelper ih;
187 InputStateMachine downSM;
188 InputStateMachine leftSM;
189 InputStateMachine rightSM;
190
192 InputStateMachine spaceSM;
193
198
199 double fps = 30.;
200
201 localclock::time_point timeOfLastFrame;
202
203 void handleFrameRate() {
204 using std::chrono::seconds;
205 using std::chrono::microseconds;
206 using std::chrono::duration_cast;
207
208 microseconds frametime = duration_cast<microseconds>(seconds(1l)) / (int)fps;
209
210 localclock::time_point theoretical_next_frame = timeOfLastFrame + frametime;
211
212 auto wait_time = theoretical_next_frame - localclock::now();
213
214 if (wait_time.count() > 0) {
215 microseconds wait_time_in_us = duration_cast<microseconds>(wait_time);
216 usleep(wait_time_in_us.count());
217 }
218
219 timeOfLastFrame = localclock::now();
220 }
221
222 void updateInputState() {
223 upSM.update();
224 downSM.update();
225 leftSM.update();
226 rightSM.update();
227
228 qSM.update();
229 spaceSM.update();
230
231 wSM.update();
232 aSM.update();
233 sSM.update();
234 dSM.update();
235 }
236
237 public:
244 NonBlockingGame(int assignmentID, std::string username,
245 std::string apikey, int nbRow = 10, int nbCol = 10)
246 : GameBase(assignmentID, username, apikey, nbRow, nbCol),
247 ih(),
248 upSM([this]() ->bool {return this->ih.keyUp();}),
249 downSM([this]() ->bool {return this->ih.keyDown();}),
250 leftSM([this]() ->bool {return this->ih.keyLeft();}),
251 rightSM([this]() ->bool {return this->ih.keyRight();}),
252
253 qSM([this]() ->bool {return this->ih.keyQ();}),
254 spaceSM([this]() ->bool {return this->ih.keySpace();}),
255
256 wSM([this]() ->bool {return this->ih.keyW();}),
257 aSM([this]() ->bool {return this->ih.keyA();}),
258 sSM([this]() ->bool {return this->ih.keyS();}),
259 dSM([this]() ->bool {return this->ih.keyD();}) {
260 if (debug)
261 std::cerr << "nbRow: " << nbRow << " nbCol: " <<
262 nbCol << std::endl;
263
264 if (nbRow * nbCol > 48 * 48) {
265 throw "NonBlockingGame can not have a grid of more than 48x48 (or a combination(so 24x96 is ok; 32x96 is not)";
266 }
267
268 registerKeyListener(&ih);
269
270 }
271
272 virtual ~NonBlockingGame() = default;
273
276 void start() {
277 timeOfLastFrame = localclock::now();
278 initialize();
279
280 long framelimit = -1; //negative means no limit
281 {
282 char* str_limit = getenv("FORCE_BRIDGES_FRAMELIMIT");
283 if (str_limit != nullptr) {
284 std::stringstream ss;
285 ss << str_limit;
286 ss >> framelimit;
287 std::cerr << "Setting framelimit to " << framelimit << std::endl;
288 }
289 }
290 long frame = 0;
291 while (!gameover()) {
292 updateInputState();
293 gameLoop();
294 render();
295 handleFrameRate();
296 frame++;
297 if (framelimit > 0 && frame > framelimit)
298 quit();
299 }
300 }
301
302 protected:
308 double getFrameRate() const {
309 return fps;
310 }
311 void setFrameRate(int fps) {
312 if (fps > 60) {
313 throw "fps limited to 60";
314 }
315 if (fps <= 0) {
316 throw "fps should be positive";
317 }
318 this->fps = fps;
319 }
320
324 bool keyLeft() {
325 return ih.keyLeft();
326 }
327
330 return leftSM.justPressed();
331 }
332
335 return leftSM.stillPressed();
336 }
339 return leftSM.justNotPressed();
340 }
343 return leftSM.stillNotPressed();
344 }
345
347 bool keyLeftFire() {
348 return leftSM.fire();
349 }
350
354 void keyLeftSetupFire(int f) {
355 leftSM.setFireCooldown(f);
356 }
360 bool keyRight() {
361 return ih.keyRight();
362 }
365 return rightSM.justPressed();
366 }
369 return rightSM.stillPressed();
370 }
373 return rightSM.justNotPressed();
374 }
377 return rightSM.stillNotPressed();
378 }
381 return rightSM.fire();
382 }
387 rightSM.setFireCooldown(f);
388 }
392 bool keyUp() {
393 return ih.keyUp();
394 }
395
398 return upSM.justPressed();
399 }
401
403 return upSM.stillPressed();
404 }
406
408 return upSM.justNotPressed();
409 }
411
413 return upSM.stillNotPressed();
414 }
416
417 bool keyUpFire() {
418 return upSM.fire();
419 }
423
424 void keyUpSetupFire(int f) {
425 upSM.setFireCooldown(f);
426 }
427
431 bool keyDown() {
432 return ih.keyDown();
433 }
434
437 return downSM.justPressed();
438 }
440
442 return downSM.stillPressed();
443 }
445
447 return downSM.justNotPressed();
448 }
451 return downSM.stillNotPressed();
452 }
454 bool keyDownFire() {
455 return downSM.fire();
456 }
460
461 void keyDownSetupFire(int f) {
462 downSM.setFireCooldown(f);
463 }
464
468 bool keyW() {
469 return ih.keyW();
470 }
471
474 return wSM.justPressed();
475 }
477
479 return wSM.stillPressed();
480 }
482
484 return wSM.justNotPressed();
485 }
487
489 return wSM.stillNotPressed();
490 }
492 bool keyWFire() {
493 return wSM.fire();
494 }
498
499 void keyWSetupFire(int f) {
500 wSM.setFireCooldown(f);
501 }
502
506 bool keyA() {
507 return ih.keyA();
508 }
509
512 return aSM.justPressed();
513 }
515
517 return aSM.stillPressed();
518 }
520
522 return aSM.justNotPressed();
523 }
525
527 return aSM.stillNotPressed();
528 }
530 bool keyAFire() {
531 return aSM.fire();
532 }
536
537 void keyASetupFire(int f) {
538 aSM.setFireCooldown(f);
539 }
540
544 bool keyS() {
545 return ih.keyS();
546 }
547
550 return sSM.justPressed();
551 }
552
554
556 return sSM.stillPressed();
557 }
559
561 return sSM.justNotPressed();
562 }
564
566 return sSM.stillNotPressed();
567 }
569 bool keySFire() {
570 return sSM.fire();
571 }
575
576 void keySSetupFire(int f) {
577 sSM.setFireCooldown(f);
578 }
579
583 bool keyD() {
584 return ih.keyD();
585 }
588 return dSM.justPressed();
589 }
591
593 return dSM.stillPressed();
594 }
596
598 return dSM.justNotPressed();
599 }
601
603 return dSM.stillNotPressed();
604 }
606 bool keyDFire() {
607 return dSM.fire();
608 }
612
613 void keyDSetupFire(int f) {
614 dSM.setFireCooldown(f);
615 }
619 bool keyQ() {
620 return ih.keyQ();
621 }
624 return qSM.justPressed();
625 }
627
629 return qSM.stillPressed();
630 }
632
634 return qSM.justNotPressed();
635 }
637
639 return qSM.stillNotPressed();
640 }
642 bool keyQFire() {
643 return qSM.fire();
644 }
648
649 void keyQSetupFire(int f) {
650 qSM.setFireCooldown(f);
651 }
655 bool keySpace() {
656 return ih.keySpace();
657 }
660 return spaceSM.justPressed();
661 }
663
665 return spaceSM.stillPressed();
666 }
668
670 return spaceSM.justNotPressed();
671 }
673
675 return spaceSM.stillNotPressed();
676 }
679 return spaceSM.fire();
680 }
684
686 spaceSM.setFireCooldown(f);
687 }
688 };
689 }
690}
691
692#endif
The base class building and using the BRIDGES based games.
Definition GameBase.h:23
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition GameBase.h:79
void render()
Renders the game.
Definition GameBase.h:87
This is meant to be an internal class, not something that the library user will use.
Definition InputHelper.h:21
bool keyA() const
Definition InputHelper.h:105
bool keySpace() const
Definition InputHelper.h:117
bool keyRight() const
Definition InputHelper.h:98
bool keyDown() const
Definition InputHelper.h:90
bool keyW() const
Definition InputHelper.h:102
bool keyLeft() const
Definition InputHelper.h:94
bool keyS() const
Definition InputHelper.h:108
bool keyUp() const
Definition InputHelper.h:86
bool keyQ() const
Definition InputHelper.h:114
bool keyD() const
Definition InputHelper.h:111
Definition InputStateMachine.h:9
void update()
Definition InputStateMachine.h:38
void setFireCooldown(int c)
Definition InputStateMachine.h:123
bool justPressed() const
Definition InputStateMachine.h:110
bool fire() const
Definition InputStateMachine.h:130
bool justNotPressed() const
Definition InputStateMachine.h:116
bool stillNotPressed() const
Definition InputStateMachine.h:119
bool stillPressed() const
Definition InputStateMachine.h:113
This class provides the features necessary to implement simple non blocking games.
Definition NonBlockingGame.h:178
bool keyRightStillNotPressed()
indicates whether the Right key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:376
void keyDownSetupFire(int f)
configures how many frames between two fire events for the Down key.
Definition NonBlockingGame.h:461
bool keyUp()
Is Up currently pressed?
Definition NonBlockingGame.h:392
bool keyRightJustPressed()
indicates whether the right key has just been pressed this current frame.
Definition NonBlockingGame.h:364
void keyASetupFire(int f)
configures how many frames between two fire events for the A key.
Definition NonBlockingGame.h:537
bool keySpaceJustPressed()
indicates whether the Space key has just been pressed this current frame.
Definition NonBlockingGame.h:659
bool keyDownJustPressed()
indicates whether the Down key has just been pressed this current frame.
Definition NonBlockingGame.h:436
bool keyDownFire()
indicates whether the current frame is a fire frame for the Down key.
Definition NonBlockingGame.h:454
bool keyUpStillPressed()
indicates whether the Up key is still being pressed this current frame.
Definition NonBlockingGame.h:402
virtual ~NonBlockingGame()=default
bool keyDStillNotPressed()
indicates whether the D key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:602
bool keyAFire()
indicates whether the current frame is a fire frame for the A key.
Definition NonBlockingGame.h:530
bool keyLeftStillNotPressed()
indicates whether the Left key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:342
void keyQSetupFire(int f)
configures how many frames between two fire events for the Q key.
Definition NonBlockingGame.h:649
bool keySJustNotPressed()
indicates whether the S key has just been released this current frame.
Definition NonBlockingGame.h:560
bool keyLeftFire()
indicates whether the current frame is a fire frame for the Left key.
Definition NonBlockingGame.h:347
NonBlockingGame(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbCol=10)
Definition NonBlockingGame.h:244
bool keySpaceJustNotPressed()
indicates whether the Space key has just been released this current frame.
Definition NonBlockingGame.h:669
void keySpaceSetupFire(int f)
configures how many frames between two fire events for the Space key.
Definition NonBlockingGame.h:685
void keyDSetupFire(int f)
configures how many frames between two fire events for the D key.
Definition NonBlockingGame.h:613
bool keySpaceStillPressed()
indicates whether the Space key is still being pressed this current frame.
Definition NonBlockingGame.h:664
void setFrameRate(int fps)
Definition NonBlockingGame.h:311
bool keyLeftStillPressed()
indicates whether the Left key is still being pressed this current frame.
Definition NonBlockingGame.h:334
bool keyDown()
Is Down currently pressed?
Definition NonBlockingGame.h:431
bool keySpace()
Is Space currently pressed?
Definition NonBlockingGame.h:655
bool keyAJustNotPressed()
indicates whether the A key has just been released this current frame.
Definition NonBlockingGame.h:521
bool keyLeftJustNotPressed()
indicates whether the Left key has just been released this current frame.
Definition NonBlockingGame.h:338
bool keyQStillNotPressed()
indicates whether the Q key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:638
bool keySJustPressed()
indicates whether the S key has just been pressed this current frame.
Definition NonBlockingGame.h:549
bool keyQStillPressed()
indicates whether the Q key is still being pressed this current frame.
Definition NonBlockingGame.h:628
bool keySStillNotPressed()
indicates whether the S key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:565
void keyRightSetupFire(int f)
configures how many frames between two fire events for the Right key.
Definition NonBlockingGame.h:386
bool keyS()
Is S currently pressed?
Definition NonBlockingGame.h:544
bool keySpaceFire()
indicates whether the current frame is a fire frame for the Space key.
Definition NonBlockingGame.h:678
bool keyQJustPressed()
indicates whether the Q key has just been pressed this current frame.
Definition NonBlockingGame.h:623
bool keyW()
Is W currently pressed?
Definition NonBlockingGame.h:468
bool keyUpFire()
indicates whether the current frame is a fire frame for the Up key.
Definition NonBlockingGame.h:417
void keyUpSetupFire(int f)
configures how many frames between two fire events for the Up key.
Definition NonBlockingGame.h:424
void keyWSetupFire(int f)
configures how many frames between two fire events for the W key.
Definition NonBlockingGame.h:499
bool keyWFire()
indicates whether the current frame is a fire frame for the W key.
Definition NonBlockingGame.h:492
void keyLeftSetupFire(int f)
configures how many frames between two fire events for the Left key.
Definition NonBlockingGame.h:354
bool keyWJustNotPressed()
indicates whether the W key has just been released this current frame.
Definition NonBlockingGame.h:483
bool keyD()
Is D currently pressed?
Definition NonBlockingGame.h:583
bool keyLeft()
Is Left currently pressed?
Definition NonBlockingGame.h:324
bool keyRightFire()
indicates whether the current frame is a fire frame for the Right key.
Definition NonBlockingGame.h:380
void keySSetupFire(int f)
configures how many frames between two fire events for the S key.
Definition NonBlockingGame.h:576
bool keyDownStillNotPressed()
indicates whether the Down key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:450
bool keyDJustPressed()
indicates whether the D key has just been pressed this current frame.
Definition NonBlockingGame.h:587
double getFrameRate() const
What frame rate is the game running at?
Definition NonBlockingGame.h:308
void start()
Call this function from main to start the game. Runs exactly once.
Definition NonBlockingGame.h:276
bool keyDJustNotPressed()
indicates whether the D key has just been released this current frame.
Definition NonBlockingGame.h:597
bool keyUpJustNotPressed()
indicates whether the Up key has just been released this current frame.
Definition NonBlockingGame.h:407
bool keyUpJustPressed()
indicates whether the Up key has just been pressed this current frame.
Definition NonBlockingGame.h:397
bool keySStillPressed()
indicates whether the S key is still being pressed this current frame.
Definition NonBlockingGame.h:555
bool keyQJustNotPressed()
indicates whether the Q key has just been released this current frame.
Definition NonBlockingGame.h:633
bool keyDFire()
indicates whether the current frame is a fire frame for the D key.
Definition NonBlockingGame.h:606
bool keyUpStillNotPressed()
indicates whether the Up key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:412
bool keyAStillNotPressed()
indicates whether the A key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:526
bool keyLeftJustPressed()
indicates whether the Left key has just been pressed this current frame.
Definition NonBlockingGame.h:329
bool keyDStillPressed()
indicates whether the D key is still being pressed this current frame.
Definition NonBlockingGame.h:592
bool keyQFire()
indicates whether the current frame is a fire frame for the Q key.
Definition NonBlockingGame.h:642
bool keyAStillPressed()
indicates whether the A key is still being pressed this current frame.
Definition NonBlockingGame.h:516
bool keyWJustPressed()
indicates whether the W key has just been pressed this current frame.
Definition NonBlockingGame.h:473
bool keyRightJustNotPressed()
indicates whether the Right key has just been released this current frame.
Definition NonBlockingGame.h:372
bool keyDownStillPressed()
indicates whether the Down key is still being pressed this current frame.
Definition NonBlockingGame.h:441
bool keyWStillPressed()
indicates whether the W key is still being pressed this current frame.
Definition NonBlockingGame.h:478
bool keyQ()
Is Q currently pressed?
Definition NonBlockingGame.h:619
bool keyDownJustNotPressed()
indicates whether the Down key has just been released this current frame.
Definition NonBlockingGame.h:446
bool keySFire()
indicates whether the current frame is a fire frame for the S key.
Definition NonBlockingGame.h:569
bool keyAJustPressed()
indicates whether the A key has just been pressed this current frame.
Definition NonBlockingGame.h:511
bool keyRightStillPressed()
indicates whether the Right key is still being pressed this current frame.
Definition NonBlockingGame.h:368
bool keySpaceStillNotPressed()
indicates whether the Space key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:674
bool keyA()
Is A currently pressed?
Definition NonBlockingGame.h:506
bool keyWStillNotPressed()
indicates whether the W key is not pressed and has not been pressed for more than a frame.
Definition NonBlockingGame.h:488
bool keyRight()
Is Right currently pressed?
Definition NonBlockingGame.h:360
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition alltypes.h:4