1 #ifndef NONBLOCKING_GAME_H 2 #define NONBLOCKING_GAME_H 133 typedef std::chrono::steady_clock localclock;
139 localclock::time_point timeOfLastFrame;
141 void handleFrameRate() {
142 using std::chrono::seconds;
143 using std::chrono::microseconds;
144 using std::chrono::duration_cast;
146 microseconds frametime = duration_cast<microseconds>(seconds(1
l)) / (
int)fps;
148 localclock::time_point theoretical_next_frame = timeOfLastFrame + frametime;
150 auto wait_time = theoretical_next_frame - localclock::now();
152 if (wait_time.count() > 0) {
153 microseconds wait_time_in_us = duration_cast<microseconds>(wait_time);
154 usleep(wait_time_in_us.count());
157 timeOfLastFrame = localclock::now();
169 std::string apikey,
int nbRow = 10,
int nbCol = 10)
170 :
GameBase(assignmentID, username, apikey, nbRow, nbCol) {
172 std::cerr <<
"nbRow: " << nbRow <<
" nbCol: " <<
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)";
186 timeOfLastFrame = localclock::now();
189 long framelimit = -1;
191 char* str_limit = getenv(
"FORCE_BRIDGES_FRAMELIMIT");
192 if (str_limit !=
nullptr) {
193 std::stringstream ss;
196 std::cerr <<
"Setting framelimit to " << framelimit << std::endl;
205 if (framelimit > 0 && frame > framelimit)
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
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
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
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 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.
bool keyD()
Is D currently pressed?
Definition: NonBlockingGame.h:273
bool keyRight()
Is Right currently pressed?
Definition: NonBlockingGame.h:231
virtual void gameLoop()=0
This function is called once per frame of the game.
bool keyS()
Is S currently pressed?
Definition: NonBlockingGame.h:266