|
@@ -0,0 +1,550 @@
|
|
1
|
+//
|
|
2
|
+// Breakout.cpp
|
|
3
|
+// ogamalBreakout
|
|
4
|
+//
|
|
5
|
+// Created by Osama Attia on 9/21/14.
|
|
6
|
+// ogamal@iastate.edu
|
|
7
|
+//
|
|
8
|
+
|
|
9
|
+#include "Breakout.h"
|
|
10
|
+#include <stdio.h>
|
|
11
|
+
|
|
12
|
+using namespace std;
|
|
13
|
+
|
|
14
|
+void recomputeFrame(int value);
|
|
15
|
+
|
|
16
|
+Breakout::Breakout() {
|
|
17
|
+ init();
|
|
18
|
+}
|
|
19
|
+
|
|
20
|
+Breakout::~Breakout() {
|
|
21
|
+
|
|
22
|
+}
|
|
23
|
+
|
|
24
|
+void Breakout::display(void) {
|
|
25
|
+
|
|
26
|
+ // Clear buffer
|
|
27
|
+ glClear(GL_COLOR_BUFFER_BIT);
|
|
28
|
+
|
|
29
|
+ // Set OpenGL for 2D drawing
|
|
30
|
+ glMatrixMode(GL_PROJECTION);
|
|
31
|
+ glLoadIdentity();
|
|
32
|
+ glDisable(GL_LIGHTING);
|
|
33
|
+ glDisable(GL_DEPTH_TEST);
|
|
34
|
+ glDisable(GL_TEXTURE_2D);
|
|
35
|
+ glOrtho(0.0f, WINWIDTH, WINHEIGHT, 0.0f, 0.0f, 1.0f);
|
|
36
|
+ glMatrixMode(GL_MODELVIEW);
|
|
37
|
+ glLoadIdentity();
|
|
38
|
+
|
|
39
|
+ // Draw my cool gradient background
|
|
40
|
+ drawBackground();
|
|
41
|
+
|
|
42
|
+ // Select which state of the game to display
|
|
43
|
+ switch (gameState) {
|
|
44
|
+ case INIT:
|
|
45
|
+ // Init values
|
|
46
|
+ init();
|
|
47
|
+ break;
|
|
48
|
+
|
|
49
|
+ case Menus:
|
|
50
|
+ // TODO List menu
|
|
51
|
+ break;
|
|
52
|
+
|
|
53
|
+ case Gameplay:
|
|
54
|
+ // Draw the game
|
|
55
|
+ drawGame();
|
|
56
|
+ // If no balls, player loses the game
|
|
57
|
+ if (balls.size() <= 0 & lifesCount > 0) {
|
|
58
|
+ newBall(-1, -1);
|
|
59
|
+ lifesCount--;
|
|
60
|
+ reward = 100;
|
|
61
|
+ } else if (balls.size() <= 0) {
|
|
62
|
+ // TODO - GAME OVER
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ // If no bricks, player wins the level
|
|
66
|
+ if (bricks.size() <= 0 && level <= 2) {
|
|
67
|
+ level++;
|
|
68
|
+ initBricks();
|
|
69
|
+ } else if (bricks.size() <= 0) {
|
|
70
|
+ // TODO - PLAYER WON
|
|
71
|
+ }
|
|
72
|
+ break;
|
|
73
|
+
|
|
74
|
+ case Scoreboard:
|
|
75
|
+ // TODO
|
|
76
|
+ break;
|
|
77
|
+
|
|
78
|
+ default:
|
|
79
|
+ break;
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ glutTimerFunc(TIMER, recomputeFrame, 0);
|
|
83
|
+
|
|
84
|
+ glutSwapBuffers();
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+void recomputeFrame(int value) {
|
|
88
|
+ glutPostRedisplay();
|
|
89
|
+}
|
|
90
|
+
|
|
91
|
+void Breakout::init(void) {
|
|
92
|
+ // Reset game statistics
|
|
93
|
+ score = 0;
|
|
94
|
+ level = 1;
|
|
95
|
+ reward = 100;
|
|
96
|
+ lifesCount = 3;
|
|
97
|
+
|
|
98
|
+ // Remove all balls
|
|
99
|
+ balls.clear();
|
|
100
|
+
|
|
101
|
+ // Remove all bricks
|
|
102
|
+ bricks.clear();
|
|
103
|
+
|
|
104
|
+ // Init bricks
|
|
105
|
+ initBricks();
|
|
106
|
+
|
|
107
|
+ // Add ball and paddle
|
|
108
|
+ initPaddle();
|
|
109
|
+ newBall(-1, -1);
|
|
110
|
+
|
|
111
|
+ // Start game play
|
|
112
|
+ gameState = Breakout::Gameplay;
|
|
113
|
+}
|
|
114
|
+
|
|
115
|
+void Breakout::drawBackground(void) {
|
|
116
|
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
117
|
+ glBegin(GL_QUADS);
|
|
118
|
+ // Top color
|
|
119
|
+ glColor3f(0.3f, 0.3f, 0.3f);
|
|
120
|
+ glVertex2f(WINWIDTH, WINHEIGHT);
|
|
121
|
+ glVertex2f(-WINWIDTH, WINHEIGHT);
|
|
122
|
+ // Bottom color
|
|
123
|
+ glColor3f(0.0f, 0.0f, 0.0f);
|
|
124
|
+ glVertex2f(0.0f, 0.0f);
|
|
125
|
+ glVertex2f(0.0f, 0.0f);
|
|
126
|
+ glEnd();
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+void Breakout::drawGame(void) {
|
|
130
|
+ // // Draw coordinates for guidance
|
|
131
|
+ // drawCoordinate();
|
|
132
|
+
|
|
133
|
+ // Draw balls
|
|
134
|
+ drawBalls();
|
|
135
|
+
|
|
136
|
+ // Draw bricks
|
|
137
|
+ drawBricks();
|
|
138
|
+
|
|
139
|
+ // Draw paddle
|
|
140
|
+ drawPaddle();
|
|
141
|
+
|
|
142
|
+ // Draw game statistics (lifes, score)
|
|
143
|
+ drawGameStats();
|
|
144
|
+
|
|
145
|
+}
|
|
146
|
+
|
|
147
|
+void Breakout::newBall(float x = -1, float y = -1) {
|
|
148
|
+ Ball b1;
|
|
149
|
+ if (x < 0 || y < 0) {
|
|
150
|
+ b1.xpos = WINWIDTH / 2.0;
|
|
151
|
+ b1.ypos = WINHEIGHT - 30.0f;
|
|
152
|
+ } else {
|
|
153
|
+ b1.xpos = x;
|
|
154
|
+ b1.ypos = y;
|
|
155
|
+ }
|
|
156
|
+ if ((float) rand() / (RAND_MAX) < 0.5)
|
|
157
|
+ b1.xvel = 5.0f;
|
|
158
|
+ else
|
|
159
|
+ b1.xvel = -5.0f;
|
|
160
|
+ b1.yvel = -10.0f;
|
|
161
|
+ b1.radius = BALL_RADIUS;
|
|
162
|
+ b1.r = 0.4f + (float) rand() / (RAND_MAX);
|
|
163
|
+ b1.g = 0.25f + (float) rand() / (RAND_MAX);
|
|
164
|
+ b1.b = 0.4f + (float) rand() / (RAND_MAX);
|
|
165
|
+ balls.push_back(b1);
|
|
166
|
+}
|
|
167
|
+
|
|
168
|
+void Breakout::drawBalls(void) {
|
|
169
|
+ for (std::vector<Ball>::iterator it = balls.begin(); it != balls.end(); ) {
|
|
170
|
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // use GL_LINE if no fill
|
|
171
|
+ glBegin(GL_POLYGON);
|
|
172
|
+ glColor3f(it->r, it->g, it->b);
|
|
173
|
+ for(int j = 0; j < CIRCLE_SEGMENTS; j++) {
|
|
174
|
+ float const theta = 2.0f * 3.1415926f * (float)j / (float)CIRCLE_SEGMENTS;
|
|
175
|
+ float const x = it->radius * cosf(theta);
|
|
176
|
+ float const y = it->radius * sinf(theta);
|
|
177
|
+ glVertex2f(x + it->xpos, y + it->ypos);
|
|
178
|
+ }
|
|
179
|
+ glEnd();
|
|
180
|
+
|
|
181
|
+ // Set new position
|
|
182
|
+ it->xpos += it->xvel;
|
|
183
|
+ it->ypos += it->yvel;
|
|
184
|
+
|
|
185
|
+ // Collision with left/right/top window sides
|
|
186
|
+ if ( (it->xpos <= (2 * it->radius)) || (it ->xpos >= (WINWIDTH - 2 * it->radius)) ) {
|
|
187
|
+ it->xvel *= -1;
|
|
188
|
+ }
|
|
189
|
+ if ( (it->ypos <= (2 * it->radius)) ) {
|
|
190
|
+ it->yvel *= -1;
|
|
191
|
+ }
|
|
192
|
+ if (it->ypos >= (WINHEIGHT - 2 * it->radius)) {
|
|
193
|
+ it = balls.erase(it);
|
|
194
|
+ continue;
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ // Collission with the bricks
|
|
198
|
+ for (std::vector<Brick>::iterator br = bricks.begin(); br != bricks.end(); ) {
|
|
199
|
+ // Check collission between circle and vertical brick sides
|
|
200
|
+ if (it->ypos >= br->ypos && it->ypos <= br->ypos + br->height) {
|
|
201
|
+ // brick right edge and left point on circle
|
|
202
|
+ if ((it->xpos - it->radius - br->xpos - br->width) <= 5 && (it->xpos - it->radius - br->xpos - br->width) >= 0) {
|
|
203
|
+ it->xvel *= -1;
|
|
204
|
+ br = hitBrick(br);
|
|
205
|
+ continue;
|
|
206
|
+ }
|
|
207
|
+
|
|
208
|
+ // brick left edge and right point on circle
|
|
209
|
+ if ((it->xpos + it->radius - br->xpos) >= -5 && (it->xpos + it->radius - br->xpos) <= 0) {
|
|
210
|
+ it->xvel *= -1;
|
|
211
|
+ br = hitBrick(br);
|
|
212
|
+ continue;
|
|
213
|
+ }
|
|
214
|
+ }
|
|
215
|
+
|
|
216
|
+ // Check collission between circle and horizontal brick sides
|
|
217
|
+ if (it->xpos >= br->xpos && it->xpos <= br->xpos + br->width) {
|
|
218
|
+ // brick bottom edge and top point on circle
|
|
219
|
+ if ((it->ypos - it->radius - br->ypos - br->height) <= 10 && (it->ypos - it->radius - br->ypos - br->height) >= 0) {
|
|
220
|
+ it->yvel *= -1;
|
|
221
|
+ br = hitBrick(br);
|
|
222
|
+ continue;
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+ // brick top edge and bottom point on circle
|
|
226
|
+ if ((it->ypos + it->radius - br->ypos) >= -10 && (it->ypos + it->radius - br->ypos) <= 0) {
|
|
227
|
+ it->yvel *= -1;
|
|
228
|
+ br = hitBrick(br);
|
|
229
|
+ continue;
|
|
230
|
+ }
|
|
231
|
+ }
|
|
232
|
+
|
|
233
|
+ GLfloat d;
|
|
234
|
+ // Check collission with top left corner
|
|
235
|
+ d = pow((it->xpos - br->xpos), 2.0) + pow((it->ypos - br->ypos), 2.0);
|
|
236
|
+ if (d < it->radius + 5.0) {
|
|
237
|
+ it->xvel *= -1;
|
|
238
|
+ it->yvel *= -1;
|
|
239
|
+ br = hitBrick(br);
|
|
240
|
+ continue;
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ // Check collission with top right corner
|
|
244
|
+ d = pow((it->xpos - br->xpos - br->width), 2.0) + pow((it->ypos - br->ypos), 2.0);
|
|
245
|
+ if (d < it->radius + 5.0) {
|
|
246
|
+ it->xvel *= -1;
|
|
247
|
+ it->yvel *= -1;
|
|
248
|
+ br = hitBrick(br);
|
|
249
|
+ continue;
|
|
250
|
+ }
|
|
251
|
+
|
|
252
|
+ // Check collission with bottom left corner
|
|
253
|
+ d = pow((it->xpos - br->xpos), 2.0) + pow((it->ypos - br->ypos - br->height), 2.0);
|
|
254
|
+ if (d < it->radius + 5.0) {
|
|
255
|
+ it->xvel *= -1;
|
|
256
|
+ it->yvel *= -1;
|
|
257
|
+ br = hitBrick(br);
|
|
258
|
+ continue;
|
|
259
|
+ }
|
|
260
|
+
|
|
261
|
+ // Check collission with bottom right corner
|
|
262
|
+ d = pow((it->xpos - br->xpos - br->width), 2.0) + pow((it->ypos - br->ypos - br->height), 2.0);
|
|
263
|
+ if (d < it->radius + 5.0) {
|
|
264
|
+ it->xvel *= -1;
|
|
265
|
+ it->yvel *= -1;
|
|
266
|
+ br = hitBrick(br);
|
|
267
|
+ continue;
|
|
268
|
+ }
|
|
269
|
+
|
|
270
|
+ ++br; // next brick
|
|
271
|
+ }
|
|
272
|
+
|
|
273
|
+ // Check collission between paddle's top edge and bottom point on circle
|
|
274
|
+ if (it->xpos >= paddle.xpos && it->xpos <= paddle.xpos + paddle.width) {
|
|
275
|
+ if ((it->ypos + it->radius - paddle.ypos) >= -10 && (it->ypos + it->radius - paddle.ypos) <= 0) {
|
|
276
|
+ it->yvel *= -1;
|
|
277
|
+ reward = 100;
|
|
278
|
+ score += reward;
|
|
279
|
+ continue;
|
|
280
|
+ }
|
|
281
|
+ }
|
|
282
|
+
|
|
283
|
+ ++it; // next ball
|
|
284
|
+ }
|
|
285
|
+}
|
|
286
|
+
|
|
287
|
+void Breakout::initPaddle(void) {
|
|
288
|
+ paddle.r = 0.2f;
|
|
289
|
+ paddle.g = 0.5f;
|
|
290
|
+ paddle.b = 1.0f;
|
|
291
|
+ paddle.width = 150.0f;
|
|
292
|
+ paddle.height = 12.0f;
|
|
293
|
+ paddle.xpos = WINWIDTH / 2.0f - paddle.width / 2.0f;
|
|
294
|
+ paddle.ypos = WINHEIGHT - 20.0f;
|
|
295
|
+}
|
|
296
|
+
|
|
297
|
+void Breakout::drawPaddle() {
|
|
298
|
+ // Make sure paddle is larger than 25px
|
|
299
|
+ if (paddle.width < 25) {
|
|
300
|
+ paddle.width = 25;
|
|
301
|
+ }
|
|
302
|
+
|
|
303
|
+ glColor3f(paddle.r, paddle.g, paddle.b);
|
|
304
|
+ glRectf(paddle.xpos, paddle.ypos, paddle.xpos + 5.0f, paddle.ypos + paddle.height);
|
|
305
|
+ glRectf(paddle.xpos + 10.0f, paddle.ypos, paddle.xpos + paddle.width - 10.0f, paddle.ypos + paddle.height);
|
|
306
|
+ glRectf(paddle.xpos + paddle.width - 5.0f, paddle.ypos, paddle.xpos + paddle.width, paddle.ypos + paddle.height);
|
|
307
|
+}
|
|
308
|
+
|
|
309
|
+void Breakout::drawBricks(void) {
|
|
310
|
+ for (std::vector<Brick>::iterator it = bricks.begin(); it != bricks.end(); ++it) {
|
|
311
|
+ glColor3f(it->r, it->g, it->b);
|
|
312
|
+ glRectf(it->xpos, it->ypos, it->xpos + it->width, it->ypos + it->height);
|
|
313
|
+
|
|
314
|
+ // Top cool triangle (kind of texture)
|
|
315
|
+ glBegin(GL_QUADS);
|
|
316
|
+ glColor3f(it->r-0.2f, it->g-0.2f, it->b-0.2f);
|
|
317
|
+ glVertex2f(it->xpos, it->ypos);
|
|
318
|
+ glColor3f(it->r-0.05f, it->g-0.05f, it->b-0.05f);
|
|
319
|
+ glVertex2f(it->xpos + it->width, it->ypos);
|
|
320
|
+ glColor3f(it->r-0.15f, it->g-0.15f, it->b-0.15f);
|
|
321
|
+ glVertex2f(it->xpos + it->width, it->ypos + it->height);
|
|
322
|
+ glVertex2f(it->xpos, it->ypos);
|
|
323
|
+ glEnd();
|
|
324
|
+ }
|
|
325
|
+}
|
|
326
|
+
|
|
327
|
+template <typename Iterator>
|
|
328
|
+Iterator Breakout::hitBrick(Iterator brick) {
|
|
329
|
+ score += reward;
|
|
330
|
+ reward += 25;
|
|
331
|
+// system("afpqlay ../../cartoon008.wav");
|
|
332
|
+
|
|
333
|
+ // Decrease brick health
|
|
334
|
+ if (brick->health > 1) {
|
|
335
|
+ brick->r = 0.95f;
|
|
336
|
+ brick->g = 0.95f;
|
|
337
|
+ brick->b = 0.95f;
|
|
338
|
+ brick->health -= 1;
|
|
339
|
+ return ++brick;
|
|
340
|
+ } else {
|
|
341
|
+ return bricks.erase(brick);
|
|
342
|
+ }
|
|
343
|
+}
|
|
344
|
+
|
|
345
|
+void Breakout::initBricks(void) {
|
|
346
|
+ if (level == 1)
|
|
347
|
+ bricksLevel1();
|
|
348
|
+ else if (level == 2)
|
|
349
|
+ bricksLevel2();
|
|
350
|
+}
|
|
351
|
+
|
|
352
|
+void Breakout::bricksLevel1(void) {
|
|
353
|
+ Brick newBrick;
|
|
354
|
+ newBrick.r = 0.95f;
|
|
355
|
+ newBrick.g = 0.95f;
|
|
356
|
+ newBrick.b = 0.95f;
|
|
357
|
+ newBrick.health = 1;
|
|
358
|
+ newBrick.width = (WALLWIDTH - (WALLCOLS - 2) * WALLSPACE) / WALLCOLS;
|
|
359
|
+ newBrick.height = (WALLHEIGHT - (WALLROWS - 2) * WALLSPACE) / WALLROWS;
|
|
360
|
+
|
|
361
|
+ for (int i = 0; i < WALLROWS; ++i) {
|
|
362
|
+ for (int j = 0; j < WALLCOLS; ++j) {
|
|
363
|
+ // Set stronger bricks
|
|
364
|
+ if (i+1 > ceil(WALLROWS / 2.0) - 2 && i < ceil(WALLROWS / 2.0) + 2 && j+2 > ceil(WALLCOLS / 2.0) - 3 && j < ceil(WALLCOLS / 2.0) + 3) {
|
|
365
|
+ newBrick.r = 1.0f;
|
|
366
|
+ newBrick.g = 0.5f;
|
|
367
|
+ newBrick.b = 0.5f;
|
|
368
|
+ newBrick.health = 2;
|
|
369
|
+ } else {
|
|
370
|
+ newBrick.r = 0.95f;
|
|
371
|
+ newBrick.g = 0.95f;
|
|
372
|
+ newBrick.b = 0.95f;
|
|
373
|
+ newBrick.health = 1;
|
|
374
|
+ }
|
|
375
|
+
|
|
376
|
+ newBrick.xpos = WALLX + j * newBrick.width + j * WALLSPACE;
|
|
377
|
+ newBrick.ypos = WALLY + i * newBrick.height + i * WALLSPACE;
|
|
378
|
+ bricks.push_back(newBrick);
|
|
379
|
+ }
|
|
380
|
+ }
|
|
381
|
+}
|
|
382
|
+
|
|
383
|
+void Breakout::bricksLevel2(void) {
|
|
384
|
+ Brick newBrick;
|
|
385
|
+ newBrick.width = (WALLWIDTH - (WALLCOLS - 2) * WALLSPACE) / WALLCOLS;
|
|
386
|
+ newBrick.height = (WALLHEIGHT - (WALLROWS - 2) * WALLSPACE) / WALLROWS;
|
|
387
|
+
|
|
388
|
+ for (int i = 0; i < WALLROWS; i++) {
|
|
389
|
+ for (int j = 0; j < WALLCOLS; j++) {
|
|
390
|
+ // Set stronger bricks
|
|
391
|
+ if (i == 1 || i == WALLROWS - 2 || j == 1 || j == WALLCOLS - 2) {
|
|
392
|
+ newBrick.r = 1.0f;
|
|
393
|
+ newBrick.g = 0.5f;
|
|
394
|
+ newBrick.b = 0.5f;
|
|
395
|
+ newBrick.health = 2;
|
|
396
|
+ } else {
|
|
397
|
+ newBrick.r = 0.95f;
|
|
398
|
+ newBrick.g = 0.95f;
|
|
399
|
+ newBrick.b = 0.95f;
|
|
400
|
+ newBrick.health = 1;
|
|
401
|
+ }
|
|
402
|
+
|
|
403
|
+ newBrick.xpos = WALLX + j * newBrick.width + j * WALLSPACE;
|
|
404
|
+ newBrick.ypos = WALLY + i * newBrick.height + i * WALLSPACE;
|
|
405
|
+ bricks.push_back(newBrick);
|
|
406
|
+ }
|
|
407
|
+ }
|
|
408
|
+}
|
|
409
|
+
|
|
410
|
+void Breakout::drawGameStats(void) {
|
|
411
|
+ glBegin(GL_LINES);
|
|
412
|
+ // Bottom right (red)
|
|
413
|
+ glColor3f(1.0f, 0.0f, 0.0f);
|
|
414
|
+ glVertex2f(20.0f, 30.0f);
|
|
415
|
+ glVertex2f(WINWIDTH - 20.0f, 30.0f);
|
|
416
|
+ glEnd();
|
|
417
|
+
|
|
418
|
+ float offset = 25.0f;
|
|
419
|
+ for (int i = 0; i < lifesCount & i < 10; ++i) {
|
|
420
|
+ drawLife(35 + offset * i, 15);
|
|
421
|
+ }
|
|
422
|
+
|
|
423
|
+ drawScore();
|
|
424
|
+}
|
|
425
|
+
|
|
426
|
+void Breakout::drawLife(float x, float y) {
|
|
427
|
+ // Scale the heart symbol
|
|
428
|
+ float const scale = 0.5f;
|
|
429
|
+
|
|
430
|
+ // Heart symbol equations from Walfram Mathworld: http://mathworld.wolfram.com/HeartCurve.html
|
|
431
|
+ glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
432
|
+ glBegin(GL_POLYGON);
|
|
433
|
+ glColor3f(1.0f, 0.2f, 0.2f);
|
|
434
|
+ for(int j = 0; j < CIRCLE_SEGMENTS; j++) {
|
|
435
|
+ float const theta = 2.0f * 3.1415926f * (float)j / (float)CIRCLE_SEGMENTS;
|
|
436
|
+ float const xx = scale * 16.0f * sinf(theta) * sinf(theta) * sinf(theta);
|
|
437
|
+ float const yy = -1 * scale * (13.0f * cosf(theta) - 5.0f * cosf(2.0f * theta) - 2 * cosf(3.0f * theta) - cosf(4.0f * theta));
|
|
438
|
+ glVertex2f(x + xx, y + yy);
|
|
439
|
+ }
|
|
440
|
+ glEnd();
|
|
441
|
+}
|
|
442
|
+
|
|
443
|
+void Breakout::drawScore(void) {
|
|
444
|
+ glPushMatrix();
|
|
445
|
+ // Write score word
|
|
446
|
+ glColor3f(1.0f, 0.7f, 0.7f);
|
|
447
|
+ glRasterPos2f(WINWIDTH - 120, 20);
|
|
448
|
+ char buf[300], *p;
|
|
449
|
+ p = buf;
|
|
450
|
+ sprintf(buf, "Score: ");
|
|
451
|
+ do glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *p); while(*(++p));
|
|
452
|
+ // Print the score
|
|
453
|
+ p = buf;
|
|
454
|
+ sprintf(buf, " %d", score);
|
|
455
|
+ glColor3f(1.0f, 0.2f, 0.2f);
|
|
456
|
+ glRasterPos2f(WINWIDTH - 120, 20);
|
|
457
|
+ do glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *p); while(*(++p));
|
|
458
|
+ glPopMatrix();
|
|
459
|
+}
|
|
460
|
+
|
|
461
|
+void Breakout::drawCoordinate(void) {
|
|
462
|
+ glBegin(GL_LINES);
|
|
463
|
+ // Top left (white)
|
|
464
|
+ glColor3f(1.0f, 1.0f, 1.0f);
|
|
465
|
+ glVertex2f(20.0f, 10.0f);
|
|
466
|
+ glVertex2f(20.0f, 30.0f);
|
|
467
|
+ glVertex2f(10.0f, 20.0f);
|
|
468
|
+ glVertex2f(30.0f, 20.0f);
|
|
469
|
+
|
|
470
|
+ // Bottom right (red)
|
|
471
|
+ glColor3f(1.0f, 0.0f, 0.0f);
|
|
472
|
+ glVertex2f(WINWIDTH - 20.0f, WINHEIGHT - 10.0f);
|
|
473
|
+ glVertex2f(WINWIDTH - 20.0f, WINHEIGHT - 30.0f);
|
|
474
|
+ glVertex2f(WINWIDTH - 10.0f, WINHEIGHT - 20.0f);
|
|
475
|
+ glVertex2f(WINWIDTH - 30.0f, WINHEIGHT - 20.0f);
|
|
476
|
+ glEnd();
|
|
477
|
+}
|
|
478
|
+
|
|
479
|
+void Breakout::reshape(int width, int height) {
|
|
480
|
+ if (width != WINWIDTH || height != WINHEIGHT)
|
|
481
|
+ glutReshapeWindow(WINWIDTH, WINHEIGHT);
|
|
482
|
+}
|
|
483
|
+
|
|
484
|
+void Breakout::mouseClick(int button, int state, int x, int y) {
|
|
485
|
+ if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
|
|
486
|
+ newBall(x, y);
|
|
487
|
+ }
|
|
488
|
+
|
|
489
|
+ // Force redraw
|
|
490
|
+ glutPostRedisplay();
|
|
491
|
+}
|
|
492
|
+
|
|
493
|
+void Breakout::mouseMove(int x, int y) {
|
|
494
|
+ y = WINHEIGHT - y;
|
|
495
|
+ if (x - paddle.width / 2.0f >= 0 && x + paddle.width / 2.0f <= WINWIDTH) {
|
|
496
|
+ paddle.xpos = x - paddle.width / 2.0f;
|
|
497
|
+ } else if (x - paddle.width / 2.0f <= 0) {
|
|
498
|
+ paddle.xpos = 0;
|
|
499
|
+ } else if (x + paddle.width / 2.0f >= WINWIDTH) {
|
|
500
|
+ paddle.xpos = WINWIDTH - paddle.width;
|
|
501
|
+ }
|
|
502
|
+ glutPostRedisplay();
|
|
503
|
+}
|
|
504
|
+
|
|
505
|
+void Breakout::keyStroke(unsigned char key, int x, int y) {
|
|
506
|
+ switch (key) {
|
|
507
|
+ case 'q': // Exit
|
|
508
|
+ exit(0);
|
|
509
|
+ break;
|
|
510
|
+ case 'n': // New game
|
|
511
|
+ init();
|
|
512
|
+ break;
|
|
513
|
+ case 'h':
|
|
514
|
+ lifesCount++;
|
|
515
|
+ break;
|
|
516
|
+ case 27: // Esc button
|
|
517
|
+ exit(0);
|
|
518
|
+ break;
|
|
519
|
+ default:
|
|
520
|
+ break;
|
|
521
|
+ }
|
|
522
|
+}
|
|
523
|
+
|
|
524
|
+void Breakout::specialKeyPos(int key, int x, int y) {
|
|
525
|
+ switch(key)
|
|
526
|
+ {
|
|
527
|
+ case GLUT_KEY_LEFT:
|
|
528
|
+ if (paddle.xpos > 0) {
|
|
529
|
+ paddle.xpos -= 5.0f;
|
|
530
|
+ paddle.xpos -= 5.0f;
|
|
531
|
+ glutPostRedisplay();
|
|
532
|
+ paddle.xpos -= 5.0f;
|
|
533
|
+ paddle.xpos -= 5.0f;
|
|
534
|
+ glutPostRedisplay();
|
|
535
|
+ }
|
|
536
|
+ break;
|
|
537
|
+ case GLUT_KEY_RIGHT:
|
|
538
|
+ if (paddle.xpos + paddle.width < WINWIDTH) {
|
|
539
|
+ paddle.xpos += 5.0f;
|
|
540
|
+ paddle.xpos += 5.0f;
|
|
541
|
+ glutPostRedisplay();
|
|
542
|
+ paddle.xpos += 5.0f;
|
|
543
|
+ paddle.xpos += 5.0f;
|
|
544
|
+ glutPostRedisplay();
|
|
545
|
+ }
|
|
546
|
+ break;
|
|
547
|
+ default:
|
|
548
|
+ break;
|
|
549
|
+ }
|
|
550
|
+}
|