/
game.cpp
305 lines (254 loc) · 7.66 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* game.cpp
*
* Implementation of Game class.
*
*/
#include "game.h"
// static initializer
Game* Game::callbackInstance(NULL);
Game::Game() {
}
Game::Game(int* argcp, char** argv) {
ball = new Ball ();
paddle = new Paddle ();
init(argcp, argv);
}
Game::~Game() {
delete ball;
delete paddle;
}
void Game::init(int* argcp, char** argvp) {
glutInit(argcp, argvp);
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize (480, 480);
glutInitContextVersion( 3, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow ("Ablockalypse");
glutSetWindowTitle("Ablockalypse");
glutSetIconTitle("Ablockalypse");
glewExperimental = GL_TRUE;
glewInit ();
// do shader stuff here...
GLuint program = InitShader( "vshader.glsl", "fshader.glsl" );
glUseProgram( program );
vPosition = glGetAttribLocation( program, "vPosition" );
vColor = glGetAttribLocation( program, "vColor" );
xlate = glGetUniformLocation( program, "xlate" );
initGeometry (paddle);
initGeometry (ball);
initBricks ();
glEnable( GL_DEPTH_TEST );
glClearColor(0, 0, 0, 0);
}
void Game::initGeometry (Geometry* g)
{
glGenVertexArrays( 1, &g->vao );
glBindVertexArray( g->vao );
GLuint buffer;
glGenBuffers (1, &buffer);
glBindBuffer (GL_ARRAY_BUFFER, buffer);
glBufferData (GL_ARRAY_BUFFER, (g->vertices.size()*sizeof(vec4))+(g->colors.size()*sizeof(vec4)),
NULL, GL_STATIC_DRAW);
glBufferSubData
(GL_ARRAY_BUFFER, 0, g->vertices.size()*sizeof(vec4), &g->vertices[0]);
glBufferSubData
(GL_ARRAY_BUFFER, g->vertices.size()*sizeof(vec4),
g->colors.size()*sizeof(vec4), &g->colors[0]);
glEnableVertexAttribArray ( vPosition );
glVertexAttribPointer ( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0));
glEnableVertexAttribArray( vColor );
glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(g->vertices.size()*sizeof(vec4)));
}
void Game::initGame() {
if (callbackInstance == NULL){
callbackInstance = this;
}
// game init code here...
score = 0;
numBricks = 48;
lives = 3;
paused = false;
initBricks ();
}
void Game::initBricks ()
{
bricks.clear ();
bricks.resize (numBricks);
vec3 start (-1.0, 0.75, 0);
float xInc = .125;
float yInc = .125;
for (unsigned i = 0; i < bricks.size (); ++i){
bricks[i] = Brick (start);
initGeometry (&bricks[i]);
start.x += xInc;
if (start.x > (1.0 - bricks[i].size)) {
start.x = -1;
start.y -= yInc;
}
}
}
void Game::registerCallbacks() {
glutTimerFunc (150, timerWrapper, 0);
glutDisplayFunc(displayWrapper);
//glutReshapeFunc(reshapeWrapper);
glutKeyboardFunc(keyboardWrapper);
}
void Game::run() {
initGame();
registerCallbacks();
glutMainLoop();
}
void Game::updateBall (Ball* b)
{
vec3 newPos ( (b->origin.x+b->velocity.x), (b->origin.y+b->velocity.y), 0);
// check for collision with paddle
if ((newPos.x >= paddle->origin.x+paddle->vertices[0].x) &&
(newPos.x <= paddle->origin.x+paddle->vertices[1].x) &&
(newPos.y <= paddle->origin.y+paddle->vertices[0].y) &&
(newPos.y >= paddle->origin.y+paddle->vertices[2].y)){
b->velocity.y = -b->velocity.y;
// impart a slight x velocity component change based on
// where the ball strikes the paddle
float modifier = newPos.x - (paddle->origin.x+(paddle->width/2) );
modifier /= paddle->width;
modifier /= 30.0;
b->velocity.x += modifier;
//b->velocity = normalize (b->velocity)/10.0;
}
// check for collision with bricks
for (unsigned i=0; i < bricks.size(); ){
COLLISION_STATUS status = getCollisionStatus (b, newPos, &bricks[i]);
if (status != NO_COLLISION){
bricks.erase (bricks.begin()+i);
score += 50;
}
else{
++i;
}
}
// check for collision with walls
if (newPos.x >= 1){
newPos.x = 1;
b->velocity.x = -b->velocity.x;
}
else if (newPos.x <= -1){
newPos.x = -1;
b->velocity.x = -b->velocity.x;
}
else if (newPos.y >= 1){
newPos.y = 1;
b->velocity.y = -b->velocity.y;
}
else if (newPos.y <= -1){
if (lives > 0){
lives--;
b->origin = vec3 (0, 0, 0);
return;
}
}
b->origin = newPos;
}
COLLISION_STATUS
Game::getCollisionStatus (Ball* b, vec3 &pos, const Brick* r){
if (b->velocity.x >= 0){
if (b->velocity.y >= 0){
if (pointInRegion(pos, r)){
b->velocity.y = -b->velocity.y;
pos.y = r->origin.y;
return COLLIDES_BOTTOM;
}
}
else{
if (pointInRegion(pos, r)){
b->velocity.y = -b->velocity.y;
pos.y = r->origin.y+r->size;
return COLLIDES_TOP;
}
}
}
else{
if (b->velocity.y >= 0){
if (pointInRegion(pos, r)){
b->velocity.y = -b->velocity.y;
pos.y = r->origin.y;
return COLLIDES_BOTTOM;
}
}
else{
if (pointInRegion(pos, r)){
b->velocity.y = -b->velocity.y;
pos.y = r->origin.y+r->size;
return COLLIDES_TOP;
}
}
}
return NO_COLLISION;
}
bool Game::pointInRegion (const vec3 &pos, const Brick* r)
{
if (pos.x >= (r->origin.x + r->vertices[0].x) &&
pos.x <= (r->origin.x + r->vertices[2].x) &&
pos.y >= (r->origin.y + r->vertices[0].y) &&
pos.y <= (r->origin.y + r->vertices[1].y))
{
return true;
}
return false;
}
void Game::display() {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
displayGeometry(ball, GL_TRIANGLE_FAN);
displayGeometry(paddle, GL_LINE_LOOP);
for (unsigned i = 0; i < bricks.size (); ++i){
displayGeometry (&bricks[i], GL_TRIANGLE_FAN);
}
glutSwapBuffers();
}
void Game::displayGeometry(Geometry* g, GLenum mode) {
glUniform3fv (xlate, 1, g->origin);
glBindVertexArray (g->vao);
glDrawArrays (mode, 0, 4);
}
void Game::displayWrapper() {
callbackInstance->display();
}
void Game::reshape(int width, int height) {
// reshape code here...
}
void Game::reshapeWrapper(int width, int height) {
callbackInstance->reshape(width, height);
}
void Game::keyboard(unsigned char keycode, int x, int y) {
switch (keycode){
case 'a':
if (!paused){paddle->translate (-1.0);}
break;
case 'd':
if (!paused) {paddle->translate (1.0);}
break;
case 'p':
paused = !paused;
break;
}
}
void Game::keyboardWrapper(unsigned char keycode, int x, int y) {
callbackInstance->keyboard(keycode, x, y);
}
void Game::timer (int val)
{
//cout << "val: " << val << endl;
stringstream ss("");
ss << "Ablockalypse v0.1 Score: " << score << " Balls Left: " << lives;
glutSetWindowTitle(ss.str().c_str());
if (!paused){
updateBall(ball);
}
glutPostRedisplay();
glutTimerFunc (40, timerWrapper, 0);
}
void Game::timerWrapper(int val)
{
callbackInstance->timer(val);
}