1 #ifndef NONBLOCKING_GAME_H
2 #define NONBLOCKING_GAME_H
184 typedef std::chrono::steady_clock localclock;
202 localclock::time_point timeOfLastFrame;
204 void handleFrameRate() {
205 using std::chrono::seconds;
206 using std::chrono::microseconds;
207 using std::chrono::duration_cast;
209 microseconds frametime = duration_cast<microseconds>(seconds(1l)) / (int)fps;
211 localclock::time_point theoretical_next_frame = timeOfLastFrame + frametime;
213 auto wait_time = theoretical_next_frame - localclock::now();
215 if (wait_time.count() > 0) {
216 microseconds wait_time_in_us = duration_cast<microseconds>(wait_time);
217 usleep(wait_time_in_us.count());
220 timeOfLastFrame = localclock::now();
223 void updateInputState() {
246 std::string apikey,
int nbRow = 10,
int nbCol = 10)
247 :
GameBase(assignmentID, username, apikey, nbRow, nbCol),
249 upSM([this]() ->bool {return this->ih.
keyUp();}),
250 downSM([
this]() ->bool {
return this->ih.
keyDown();}),
251 leftSM([
this]() ->bool {
return this->ih.
keyLeft();}),
252 rightSM([
this]() ->bool {
return this->ih.
keyRight();}),
254 qSM([
this]() ->bool {
return this->ih.
keyQ();}),
255 spaceSM([
this]() ->bool {
return this->ih.
keySpace();}),
257 wSM([
this]() ->bool {
return this->ih.
keyW();}),
258 aSM([
this]() ->bool {
return this->ih.
keyA();}),
259 sSM([
this]() ->bool {
return this->ih.
keyS();}),
260 dSM([
this]() ->bool {
return this->ih.
keyD();}) {
262 std::cerr <<
"nbRow: " << nbRow <<
" nbCol: " <<
265 if (nbRow * nbCol > 32 * 32) {
266 throw "NonBlockingGame can not have a grid of more than 32x32 (or a combination(so 16x64 is ok; 16x128 is not)";
269 registerKeyListener(&ih);
278 timeOfLastFrame = localclock::now();
281 long framelimit = -1;
283 char* str_limit = getenv(
"FORCE_BRIDGES_FRAMELIMIT");
284 if (str_limit !=
nullptr) {
285 std::stringstream ss;
288 std::cerr <<
"Setting framelimit to " << framelimit << std::endl;
292 while (!gameover()) {
298 if (framelimit > 0 && frame > framelimit)
340 return leftSM.
fire();
373 return rightSM.
fire();
447 return downSM.
fire();
671 return spaceSM.
fire();
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 class provides the features necessary to implement simple non blocking games.
Definition: NonBlockingGame.h:179
bool keyRightStillNotPressed()
indicates whether the Right key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:368
void keyDownSetupFire(int f)
configures how many frames between two fire events for the Down key.
Definition: NonBlockingGame.h:453
bool keyUp()
Is Up currently pressed?
Definition: NonBlockingGame.h:384
bool keyRightJustPressed()
indicates whether the right key has just been pressed this current frame.
Definition: NonBlockingGame.h:356
void keyASetupFire(int f)
configures how many frames between two fire events for the A key.
Definition: NonBlockingGame.h:529
bool keySpaceJustPressed()
indicates whether the Space key has just been pressed this current frame.
Definition: NonBlockingGame.h:651
bool keyDownJustPressed()
indicates whether the Down key has just been pressed this current frame.
Definition: NonBlockingGame.h:428
bool keyDownFire()
indicates whether the current frame is a fire frame for the Down key.
Definition: NonBlockingGame.h:446
bool keyUpStillPressed()
indicates whether the Up key is still being pressed this current frame.
Definition: NonBlockingGame.h:394
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:594
bool keyAFire()
indicates whether the current frame is a fire frame for the A key.
Definition: NonBlockingGame.h:522
bool keyLeftStillNotPressed()
indicates whether the Left key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:334
void keyQSetupFire(int f)
configures how many frames between two fire events for the Q key.
Definition: NonBlockingGame.h:641
bool keySJustNotPressed()
indicates whether the S key has just been released this current frame.
Definition: NonBlockingGame.h:552
bool keyLeftFire()
indicates whether the current frame is a fire frame for the Left key.
Definition: NonBlockingGame.h:339
NonBlockingGame(int assignmentID, std::string username, std::string apikey, int nbRow=10, int nbCol=10)
Definition: NonBlockingGame.h:245
bool keySpaceJustNotPressed()
indicates whether the Space key has just been released this current frame.
Definition: NonBlockingGame.h:661
void keySpaceSetupFire(int f)
configures how many frames between two fire events for the Space key.
Definition: NonBlockingGame.h:677
void keyDSetupFire(int f)
configures how many frames between two fire events for the D key.
Definition: NonBlockingGame.h:605
bool keySpaceStillPressed()
indicates whether the Space key is still being pressed this current frame.
Definition: NonBlockingGame.h:656
bool keyLeftStillPressed()
indicates whether the Left key is still being pressed this current frame.
Definition: NonBlockingGame.h:326
bool keyDown()
Is Down currently pressed?
Definition: NonBlockingGame.h:423
bool keySpace()
Is Space currently pressed?
Definition: NonBlockingGame.h:647
bool keyAJustNotPressed()
indicates whether the A key has just been released this current frame.
Definition: NonBlockingGame.h:513
bool keyLeftJustNotPressed()
indicates whether the Left key has just been released this current frame.
Definition: NonBlockingGame.h:330
bool keyQStillNotPressed()
indicates whether the Q key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:630
bool keySJustPressed()
indicates whether the S key has just been pressed this current frame.
Definition: NonBlockingGame.h:541
bool keyQStillPressed()
indicates whether the Q key is still being pressed this current frame.
Definition: NonBlockingGame.h:620
bool keySStillNotPressed()
indicates whether the S key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:557
void keyRightSetupFire(int f)
configures how many frames between two fire events for the Right key.
Definition: NonBlockingGame.h:378
bool keyS()
Is S currently pressed?
Definition: NonBlockingGame.h:536
bool keySpaceFire()
indicates whether the current frame is a fire frame for the Space key.
Definition: NonBlockingGame.h:670
bool keyQJustPressed()
indicates whether the Q key has just been pressed this current frame.
Definition: NonBlockingGame.h:615
bool keyW()
Is W currently pressed?
Definition: NonBlockingGame.h:460
bool keyUpFire()
indicates whether the current frame is a fire frame for the Up key.
Definition: NonBlockingGame.h:409
void keyUpSetupFire(int f)
configures how many frames between two fire events for the Up key.
Definition: NonBlockingGame.h:416
void keyWSetupFire(int f)
configures how many frames between two fire events for the W key.
Definition: NonBlockingGame.h:491
bool keyWFire()
indicates whether the current frame is a fire frame for the W key.
Definition: NonBlockingGame.h:484
void keyLeftSetupFire(int f)
configures how many frames between two fire events for the Left key.
Definition: NonBlockingGame.h:346
bool keyWJustNotPressed()
indicates whether the W key has just been released this current frame.
Definition: NonBlockingGame.h:475
bool keyD()
Is D currently pressed?
Definition: NonBlockingGame.h:575
bool keyLeft()
Is Left currently pressed?
Definition: NonBlockingGame.h:316
bool keyRightFire()
indicates whether the current frame is a fire frame for the Right key.
Definition: NonBlockingGame.h:372
void keySSetupFire(int f)
configures how many frames between two fire events for the S key.
Definition: NonBlockingGame.h:568
bool keyDownStillNotPressed()
indicates whether the Down key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:442
bool keyDJustPressed()
indicates whether the D key has just been pressed this current frame.
Definition: NonBlockingGame.h:579
double getFrameRate() const
What frame rate is the game running at?
Definition: NonBlockingGame.h:309
void start()
Call this function from main to start the game. Runs exactly once.
Definition: NonBlockingGame.h:277
bool keyDJustNotPressed()
indicates whether the D key has just been released this current frame.
Definition: NonBlockingGame.h:589
bool keyUpJustNotPressed()
indicates whether the Up key has just been released this current frame.
Definition: NonBlockingGame.h:399
bool keyUpJustPressed()
indicates whether the Up key has just been pressed this current frame.
Definition: NonBlockingGame.h:389
bool keySStillPressed()
indicates whether the S key is still being pressed this current frame.
Definition: NonBlockingGame.h:547
bool keyQJustNotPressed()
indicates whether the Q key has just been released this current frame.
Definition: NonBlockingGame.h:625
bool keyDFire()
indicates whether the current frame is a fire frame for the D key.
Definition: NonBlockingGame.h:598
bool keyUpStillNotPressed()
indicates whether the Up key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:404
bool keyAStillNotPressed()
indicates whether the A key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:518
bool keyLeftJustPressed()
indicates whether the Left key has just been pressed this current frame.
Definition: NonBlockingGame.h:321
bool keyDStillPressed()
indicates whether the D key is still being pressed this current frame.
Definition: NonBlockingGame.h:584
bool keyQFire()
indicates whether the current frame is a fire frame for the Q key.
Definition: NonBlockingGame.h:634
bool keyAStillPressed()
indicates whether the A key is still being pressed this current frame.
Definition: NonBlockingGame.h:508
bool keyWJustPressed()
indicates whether the W key has just been pressed this current frame.
Definition: NonBlockingGame.h:465
bool keyRightJustNotPressed()
indicates whether the Right key has just been released this current frame.
Definition: NonBlockingGame.h:364
bool keyDownStillPressed()
indicates whether the Down key is still being pressed this current frame.
Definition: NonBlockingGame.h:433
bool keyWStillPressed()
indicates whether the W key is still being pressed this current frame.
Definition: NonBlockingGame.h:470
bool keyQ()
Is Q currently pressed?
Definition: NonBlockingGame.h:611
bool keyDownJustNotPressed()
indicates whether the Down key has just been released this current frame.
Definition: NonBlockingGame.h:438
bool keySFire()
indicates whether the current frame is a fire frame for the S key.
Definition: NonBlockingGame.h:561
bool keyAJustPressed()
indicates whether the A key has just been pressed this current frame.
Definition: NonBlockingGame.h:503
bool keyRightStillPressed()
indicates whether the Right key is still being pressed this current frame.
Definition: NonBlockingGame.h:360
bool keySpaceStillNotPressed()
indicates whether the Space key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:666
bool keyA()
Is A currently pressed?
Definition: NonBlockingGame.h:498
bool keyWStillNotPressed()
indicates whether the W key is not pressed and has not been pressed for more than a frame.
Definition: NonBlockingGame.h:480
bool keyRight()
Is Right currently pressed?
Definition: NonBlockingGame.h:352
Support for drawing Bar charts.
Definition: alltypes.h:4