Bridges-C++ 3.5.0-dev2-1-ge3e57bf
Bridges(C++ API)
NonBlockingGame3D.h
Go to the documentation of this file.
1#ifndef NONBLOCKING_GAME_3D_H
2#define NONBLOCKING_GAME_3D_H
3
4#include <GameBase3D.h>
5#include <InputHelper.h>
6
7namespace bridges {
8 namespace game {
9
22 private:
25
26 typedef std::chrono::steady_clock localclock;
27
28 InputHelper ih;
29
30 double fps = 30.;
31
32 localclock::time_point timeOfLastFrame;
33
34 void handleFrameRate() {
35 using std::chrono::seconds;
36 using std::chrono::microseconds;
37 using std::chrono::duration_cast;
38
39 microseconds frametime = duration_cast<microseconds>(seconds(1l)) / (int)fps;
40
41 localclock::time_point theoretical_next_frame = timeOfLastFrame + frametime;
42
43 auto wait_time = theoretical_next_frame - localclock::now();
44
45 if (wait_time.count() > 0) {
46 microseconds wait_time_in_us = duration_cast<microseconds>(wait_time);
47 usleep(wait_time_in_us.count());
48 }
49
50 timeOfLastFrame = localclock::now();
51 }
52
53 public:
60 NonBlockingGame3D(int assignmentID, std::string username,
61 std::string apikey)
62 : GameBase3D(assignmentID, username, apikey) {
63
64 registerKeyListener(&ih);
65 }
66
67 virtual ~NonBlockingGame3D() = default;
68
71 void start() {
72 timeOfLastFrame = localclock::now();
73 initialize();
74
75 render();
76
77 exit(0);
78 long framelimit = -1; //negative means no limit
79 {
80 char* str_limit = getenv("FORCE_BRIDGES_FRAMELIMIT");
81 if (str_limit != nullptr) {
82 std::stringstream ss;
83 ss << str_limit;
84 ss >> framelimit;
85 std::cerr << "Setting framelimit to " << framelimit << std::endl;
86 }
87 }
88 long frame = 0;
89 while (!gameover()) {
90 gameLoop();
91 render();
92 handleFrameRate();
93 frame++;
94 if (framelimit > 0 && frame > framelimit)
95 quit();
96 }
97 }
98
99 protected:
105 double getFrameRate() const {
106 return fps;
107 }
108
112 bool keyLeft() {
113 return ih.keyLeft();
114 }
115
119 bool keyRight() {
120 return ih.keyRight();
121 }
122
126 bool keyUp() {
127 return ih.keyUp();
128 }
129
133 bool keyDown() {
134 return ih.keyDown();
135 }
136
140 bool keyW() {
141 return ih.keyW();
142 }
143
147 bool keyA() {
148 return ih.keyA();
149 }
150
154 bool keyS() {
155 return ih.keyS();
156 }
157
161 bool keyD() {
162 return ih.keyD();
163 }
164
168 bool keyQ() {
169 return ih.keyQ();
170 }
171
175 bool keySpace() {
176 return ih.keySpace();
177 }
178
179 };
180 }
181}
182
183#endif
This class extends the GameBase class to 3D environments.
Definition: GameBase3D.h:22
void render()
Renders the game.
Definition: GameBase3D.h:90
void registerKeyListener(KeypressListener *p)
register a new KeypressListener
Definition: GameBase3D.h:82
void quit()
calling this function causes the game to end.
Definition: GameBase3D.h:107
bool gameover() const
Definition: GameBase3D.h:138
virtual void gameLoop()=0
This function is called once per frame of the game.
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 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
This class is an extension of the NonBlockingGame class to 3D game environments.
Definition: NonBlockingGame3D.h:21
double getFrameRate() const
What frame rate is the game running at?
Definition: NonBlockingGame3D.h:105
bool keyS()
Is S currently pressed?
Definition: NonBlockingGame3D.h:154
bool keyLeft()
Is Left currently pressed?
Definition: NonBlockingGame3D.h:112
NonBlockingGame3D(int assignmentID, std::string username, std::string apikey)
Definition: NonBlockingGame3D.h:60
bool keyD()
Is D currently pressed?
Definition: NonBlockingGame3D.h:161
bool keySpace()
Is Space currently pressed?
Definition: NonBlockingGame3D.h:175
void start()
Call this function from main to start the game. Runs exactly once.
Definition: NonBlockingGame3D.h:71
bool keyRight()
Is Right currently pressed?
Definition: NonBlockingGame3D.h:119
bool keyW()
Is W currently pressed?
Definition: NonBlockingGame3D.h:140
bool keyQ()
Is Q currently pressed?
Definition: NonBlockingGame3D.h:168
bool keyA()
Is A currently pressed?
Definition: NonBlockingGame3D.h:147
virtual ~NonBlockingGame3D()=default
bool keyDown()
Is Down currently pressed?
Definition: NonBlockingGame3D.h:133
bool keyUp()
Is Up currently pressed?
Definition: NonBlockingGame3D.h:126
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4