1#ifndef NONBLOCKING_GAME_H
2#define NONBLOCKING_GAME_H
183 typedef std::chrono::steady_clock localclock;
201 localclock::time_point timeOfLastFrame;
203 void handleFrameRate() {
204 using std::chrono::seconds;
205 using std::chrono::microseconds;
206 using std::chrono::duration_cast;
208 microseconds frametime = duration_cast<microseconds>(seconds(1l)) / (int)fps;
210 localclock::time_point theoretical_next_frame = timeOfLastFrame + frametime;
212 auto wait_time = theoretical_next_frame - localclock::now();
214 if (wait_time.count() > 0) {
215 microseconds wait_time_in_us = duration_cast<microseconds>(wait_time);
216 usleep(wait_time_in_us.count());
219 timeOfLastFrame = localclock::now();
222 void updateInputState() {
245 std::string apikey,
int nbRow = 10,
int nbCol = 10)
246 :
GameBase(assignmentID, username, apikey, nbRow, nbCol),
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();}),
253 qSM([
this]() ->bool {
return this->ih.
keyQ();}),
254 spaceSM([
this]() ->bool {
return this->ih.
keySpace();}),
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();}) {
261 std::cerr <<
"nbRow: " << nbRow <<
" nbCol: " <<
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)";
268 registerKeyListener(&ih);
277 timeOfLastFrame = localclock::now();
280 long framelimit = -1;
282 char* str_limit = getenv(
"FORCE_BRIDGES_FRAMELIMIT");
283 if (str_limit !=
nullptr) {
284 std::stringstream ss;
287 std::cerr <<
"Setting framelimit to " << framelimit << std::endl;
291 while (!gameover()) {
297 if (framelimit > 0 && frame > framelimit)
313 throw "fps limited to 60";
316 throw "fps should be positive";
348 return leftSM.
fire();
381 return rightSM.
fire();
455 return downSM.
fire();
679 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: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