-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshooter.cpp
More file actions
858 lines (728 loc) · 24.6 KB
/
shooter.cpp
File metadata and controls
858 lines (728 loc) · 24.6 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
#include <stdio.h>
#include <GL/gl.h> // Open Graphics Library (OpenGL) header
#include <GL/freeglut.h> // The GL Utility Toolkit (GLUT) Header
#include <stdlib.h>
#include <math.h>
// Math defines
#define M_PI 3.14159265358979323846
#define RAD2DEG 180.0/M_PI
#define DEG2RAD M_PI/180.0
// Keyboard defines
#define KEY_ONE 49
#define KEY_TWO 50
#define KEY_THREE 51
#define KEY_FOUR 52
#define KEY_m 109
#define KEY_r 114
// Keyboard arrows
#define KEY_LEFT 100
#define KEY_RIGHT 102
// 2D function defines, as 3D is not needed
#define myTranslate2D(x,y) glTranslated(x, y, 0.0)
#define myScale2D(x,y) glScalef(x, y, 1.0)
#define myRotate2D(angle) glRotatef(RAD2DEG*angle, 0.0, 0.0, 1.0)
// Various game defines
#define MAX_ACC_PLAYER 0.15
#define MAX_VELO_PLAYER 2
#define SPACEBAR 32
#define MAX_BULLET_ON_SCREEN 8
#define MAX_ENEMY_ON_SCREEN 4
#define MAX_VELO_BULLET 5
static int flag = 0;
static int shoot = 0;
static int score = 0;
static int countenemy = MAX_ENEMY_ON_SCREEN;
float mycolor[][3]={{1,0.5,1},{0,1,0},{0,0.5,1},{0.5,0.5,0},{0.5,0.5,0.5},{0.5,0,0},{0.5,0,0.5},{0,0.5,0.5},{1,0.5,1},{0,1,0},{0,0.5,1},{0.5,0.5,0},{0.5,0.5,0.5},{0.5,0.5,0},{0.5,0.5,0.5},{0.5,0.5,0},{0.5,0.5,0.5},{0.5,0.5,0},{0.5,0.5,0.5}};
int posx[MAX_ENEMY_ON_SCREEN];
int posy[MAX_ENEMY_ON_SCREEN];
double dt[MAX_ENEMY_ON_SCREEN];
/* -- type definitions ------------------------------------------------------ */
typedef struct {
int width;
int height;
char* title;
float field_of_view_angle;
float z_near;
float z_far;
} glutWindow;
typedef struct {
double x, y, phi, dx, vmax, vmax2, sizex,sizey;
bool Destroyed;
} Player;
typedef struct {
double x, y, phi, dx, dy, vmax, vmax2, sizex,sizey;
bool Destroyed;
} Enemy;
typedef struct {
int active;
double x, y, dx, dy, bullet_phi,bsizex,bsizey;
} Bullet;
/* -- function prototypes --------------------------------------------------- */
static void initialize ();
// Keyboard
void keyboard (unsigned char, int, int);
void keyPress (int, int, int);
void keyRelease (int, int, int);
void myTimer (int);
void Enemyupdate();
// Player
void drawPlayer ();
void drawEnemy(int);
void movePlayer ();
void moveEnemyBullet(int i);
void movePlayerBullet();
void checkMapBoundries ();
void DoCollision();
void updateScore(int);
// Display
void display ();
void myReshape (int, int);
void setWindowValues ();
void scoredisplay(int,int,int,int,int);
void mymenu();
void mymenu1();
void StartGame();
void GameOver();
void OnDestroy();
void OnEnemyDestroy(int i);
/* -- global variables ------------------------------------------------------ */
static glutWindow win;
// State of cursor keys
static int left = 0;
static int right = 0;
static int menu = 0;
static Player player;
static Enemy enemy[MAX_ENEMY_ON_SCREEN];
static Bullet bullets[MAX_BULLET_ON_SCREEN];
static Bullet ebullets[MAX_BULLET_ON_SCREEN];
/* -- functions ------------------------------------------------------------- */
int main (int argc, char **argv) {
setWindowValues();
// initialize and run program
glutInit(&argc, argv); // GLUT initialization
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); // Display Mode
glutInitWindowSize(win.width, win.height); // set window size
glutCreateWindow("Shoot out"); // create Window
glutDisplayFunc(display); // register Display Function
glutIdleFunc(display); // register Idle Function
// Keyboard
glutIgnoreKeyRepeat(1);
glutKeyboardFunc(keyboard);
glutSpecialFunc(keyPress);
glutSpecialUpFunc(keyRelease); // register Keyboard Handler
glutReshapeFunc(myReshape);
glutTimerFunc(33, myTimer, 0);
initialize();
glutMainLoop(); // run GLUT mainloop
return 0;
}
static void initialize () {
int space = (win.height-100)/MAX_ENEMY_ON_SCREEN;
for(int i=0;i< MAX_ENEMY_ON_SCREEN;i++)
{
if(space*i <win.height-80)
posy[i] = 90 + i*space;
dt[i] = 1+i*0.10;
if(i%2==0)
posx[i]=0;
else
posx[i] = win.width-10;
}
// Player & enemy -> set parameters including the maximum velocity of the player, the velocity of the rocket shots etc
player.Destroyed = false;
player.x = win.width/2;
player.y = 30.0;
player.sizex = 5;
player.sizey = 5;
player.dx = 0;
player.vmax = MAX_VELO_PLAYER;
player.vmax2 = MAX_VELO_PLAYER * MAX_VELO_PLAYER;
for(int i=0;i<MAX_ENEMY_ON_SCREEN;i++)
{
enemy[i].Destroyed = false;
enemy[i].x = posx[i] ;
enemy[i].y = posy[i];
enemy[i].sizex = 5;
enemy[i].sizey = 5;
enemy[i].phi = - DEG2RAD *180;
}
}
void keyboard ( unsigned char key, int x , int y) {
switch (key) {
// Quits game
case SPACEBAR:
shoot = 1;
break;
case KEY_ONE:
menu = 1;
break;
case KEY_TWO:
menu = 2;
break;
case KEY_THREE:
menu = 3;
break;
case KEY_FOUR:
menu = 5;
break;
case KEY_m:
menu = 4;
break;
case KEY_r:
menu = 6;
default:
break;
}
}
// This function is called when a special key is pressed;
void keyPress (int key, int x, int y) {
switch (key) {
case KEY_LEFT:
left = 1;
break;
case KEY_RIGHT:
right = 1;
break;
default:
break;
}
}
// This function is called when a special key is released
void keyRelease (int key, int x, int y) {
switch (key) {
case KEY_LEFT:
left = 0;
break;
case KEY_RIGHT:
right = 0;
break;
default:
break;
}
}
// Update
void myTimer (int value) {
if(player.Destroyed == false)
{
movePlayer();
movePlayerBullet();
}
if(countenemy <= MAX_ENEMY_ON_SCREEN ){
for(int i=0;i<MAX_ENEMY_ON_SCREEN;i++)
{
if(enemy[i].Destroyed == false)
moveEnemyBullet(i);
}
}
checkMapBoundries();
glutPostRedisplay();
glutTimerFunc(30, myTimer, value); // 30 frames per second
}
void movePlayer () {
if(left && right) {
//do nothing
}
// Player Movement
else if(left == 1) {
player.dx = player.dx - MAX_ACC_PLAYER;
}
else if(right == 1) {
player.dx = player.dx + MAX_ACC_PLAYER;
}
// Slows player down when up && down movement keys are not pressed
else if (left == 0 && right == 0) {
if (player.dx > 0) {
player.dx -= 0.2;
}
if (player.dx < 0 ) {
player.dx = 0;
}
}
double temp;
//If the player exceeds the max velocity (moving backwards), limit the velocity
if(right == 1 && (temp = (player.dx * player.dx)) > (player.vmax)) {
temp = player.vmax / sqrt(temp);
player.dx *= temp - 0.5;
}
//If the player exceeds the max velocity (moving forwards), limit the velocity
else if((temp = (player.dx * player.dx)) > player.vmax2) {
temp = player.vmax / sqrt(temp);
player.dx *= temp;
}
// Puts the math in motion
player.x = player.x + player.dx;
} // end movePlayer()
// Bullets
void moveEnemyBullet (int i) {
// Give the bullets velocity if shoot is true.
if(ebullets[i].active == 0) {
ebullets[i].active = 1;
ebullets[i].x = enemy[i].x;
ebullets[i].y = enemy[i].y;
ebullets[i].bullet_phi = enemy[i].phi;
ebullets[i].dx = -MAX_VELO_BULLET * sin(enemy[i].phi);
ebullets[i].dy = MAX_VELO_BULLET * cos(enemy[i].phi);
ebullets[i].bsizex = 3;
ebullets[i].bsizey = 3;
}
// Advance bullets and eliminating those that have gone pastthe window boundaries
if(ebullets[i].active == 1) {
ebullets[i].x = ebullets[i].x + ebullets[i].dx;
ebullets[i].y = ebullets[i].y + ebullets[i].dy;
}
//Bullet Boundries/ Destroy bullet outside boundries
if(ebullets[i].active == 1 && (ebullets[i].x > win.width || ebullets[i].x < 0 || ebullets[i].y > win.height || ebullets[i].y < 0)) {
ebullets[i].active = 0;
}
} // end moveBullet()
void movePlayerBullet () {
int i = 0;
// Give the bullets velocity if shoot is true.
if (shoot == 1) {
for(i = 0; i < MAX_BULLET_ON_SCREEN; i++) {
if(bullets[i].active == 0) {
bullets[i].active = 1;
bullets[i].x = player.x;
bullets[i].y = player.y;
bullets[i].bullet_phi = player.phi;
bullets[i].dx = -MAX_VELO_BULLET * sin(player.phi);
bullets[i].dy = MAX_VELO_BULLET * cos(player.phi);
bullets[i].bsizex = 3;
bullets[i].bsizey = 3;
break;
}
}
// Resets shoot key to prevent rapid fire
shoot = 0;
}
// Advance bullets and eliminating those that have gone pastthe window boundaries
for(i = 0; i < MAX_BULLET_ON_SCREEN; i++) {
if(bullets[i].active == 1) {
bullets[i].x = bullets[i].x + bullets[i].dx;
bullets[i].y = bullets[i].y + bullets[i].dy;
}
//Bullet Boundries/ Destroy bullet outside boundries
if(bullets[i].active == 1 && (bullets[i].x > win.width || bullets[i].x < 0 || bullets[i].y > win.height || bullets[i].y < 0)) {
bullets[i].active = 0;
}
}
} // end moveBullet()
void drawPlayerBullet (int i) {
glColor3f(0.9f, 0.0f, 0.0f);
glPushMatrix();
myTranslate2D(bullets[i].x, bullets[i].y);
myRotate2D(bullets[i].bullet_phi);
myScale2D(bullets[i].bsizex, bullets[i].bsizey);
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
}
void drawEnemyBullet(int i)
{
glColor3f(0.0f, 1.0f, 0.0f);
glPushMatrix();
myTranslate2D(ebullets[i].x, ebullets[i].y);
myRotate2D(ebullets[i].bullet_phi);
myScale2D(ebullets[i].bsizex, ebullets[i].bsizey);
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
}
// Map boundaries - prevents player from going out of designated map area
void checkMapBoundries () {
if(player.x > win.width) {
player.x = win.width;
}
if(player.x < 0) {
player.x = 0;
}
if(player.y > win.height) {
player.y = win.height;
}
if(player.y < 0) {
player.y = 0;
}
}
/**
* The display callback handles exposure events and is called whenever the display must be refreshed.
* Values can be passed to the display callback function only by means of global variables.
*/
void display () {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
glLoadIdentity();
glTranslatef(0.0f,0.0f,-3.0f);
mymenu();
glutSwapBuffers();
}
void myReshape (int w, int h) {
glMatrixMode(GL_PROJECTION); // select projection matrix
glViewport(0, 0, win.width, win.height); // set the viewport
glMatrixMode(GL_PROJECTION); // set matrix mode
glLoadIdentity(); // reset projection matrix
glOrtho(0.0, win.width, 0.0, win.height, win.z_near, win.z_far);
glMatrixMode(GL_MODELVIEW); // specify which matrix is the current matrix
glShadeModel( GL_SMOOTH );
glClearDepth( 1.0f ); // specify the clear value for the depth buffer
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LEQUAL );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST ); // specify implementation-specific hints
glClearColor(0.0, 0.0, 0.02, 1.0); // specify clear values for the color buffers
}
// Sets game window specifications
void setWindowValues () {
win.width = 640;
win.height = 480;
win.field_of_view_angle = 0;
win.z_near = 1.0f;
win.z_far = 100.0f;
}
/* -- drawing functions ----------------------------------------------------- */
void drawPlayer () {
glColor3f(0.2f, 0.9f, 1.0f);
glPushMatrix();
myTranslate2D(player.x, player.y);
myRotate2D(player.phi);
myScale2D(player.sizex,player.sizey);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
}
void drawEnemy (int i){
if(enemy[i].Destroyed == false){
glColor3fv(mycolor[i]);
glPushMatrix();
myTranslate2D(enemy[i].x-27 , enemy[i].y);
myScale2D(enemy[i].sizex,enemy[i].sizey);
/* Starting position */
glBegin(GL_POLYGON);
glVertex2f( 5.0f, 0.0f);// Top left
glVertex2f( 10.0f,0.0f);// Top Right
glVertex2f( 5.0f,-2.0f);// Bottom Right
glVertex2f( 0.0f, 0.0f);
glVertex2f( -5.0f, -2.0f);// Top left
glVertex2f( 0.0f,-2.0f);
glEnd();
glPopMatrix();
}
}
// Updates the motion of enemy
void Enemyupdate()
{
for (int i = 0; i < MAX_ENEMY_ON_SCREEN; ++i)
{
if(enemy[i].Destroyed == false){
if(i%2 == 0){
if(!flag)
{
enemy[i].x+=dt[i];
if(enemy[i].x>win.width)
flag=1;
}
if(flag)
{
enemy[i].x+=-(dt[i]);
if(enemy[i].x<5)
flag=0;
}
}
else
{
if(!flag)
{
enemy[i].x+=-(dt[i]);
if(enemy[i].x<5)
flag=1;
}
if(flag)
{
enemy[i].x+=dt[i];
if(enemy[i].x>win.width)
flag=0;
}
}
}
}
}
// Check collision between player & enemy or bullet & enemy
void DoCollision()
{
bool xt,yt,zt,ut,et,ft,playxt,playyt;
int i=0,j=0;
for ( i = 0,j=0 ;i<MAX_BULLET_ON_SCREEN && j<MAX_ENEMY_ON_SCREEN; i++,j++)
{
if(enemy[j].Destroyed == false && bullets[i].active == 1){
xt = (bullets[i].x + bullets[i].bsizex * 3 >= enemy[j].x && enemy[j].x + enemy[j].sizex *5 >= bullets[i].x);
yt = (bullets[i].y + bullets[i].bsizey * 3 >= enemy[j].y && enemy[j].y + enemy[j].sizey *5 >= bullets[i].y);
zt = (bullets[i].x + bullets[i].bsizex * 3 <= -(enemy[j].x) && -(enemy[j].x) + -(enemy[j].sizex) *5 <= bullets[i].x);
ut = (bullets[i].y + bullets[i].bsizey * 3 <= -(enemy[j].y) && -(enemy[j].y) + -(enemy[j].sizey) *5 <= bullets[i].y);
if(xt && yt || zt && ut)
{
bullets[i].active = 0;
ebullets[i].active = 0;
enemy[j].Destroyed = true;
score = score + 1;
countenemy = countenemy - 1;
}
}
playxt = player.x + player.sizex * 4 >= enemy[j].x && enemy[j].x + enemy[j].sizex *5 >= player.x;
playyt = player.y + player.sizey * 4 >= enemy[j].y && enemy[j].y + enemy[j].sizey *5 >= player.y;
et = (ebullets[i].x + ebullets[i].bsizex * 3 >= player.x && player.x + player.sizex * 3 >= ebullets[i].x);
ft = (ebullets[i].y + ebullets[i].bsizey * 3 >= player.y && player.y + player.sizey * 3 >= ebullets[i].y);
if(playxt && playyt || et && ft )
{
ebullets[i].active = 0;
bullets[i].active = 0;
player.Destroyed = true;
}
}
}
// Update score
void scoredisplay (int posx, int posy, int posz, int space_char, int scorevar)
{
int j=0,p,k;
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
p = scorevar;
j = 0;
k = 0;
glRasterPos2i(520, 440);
glColor3f(0.0f, 0.0f, 1.0f);
const unsigned char* t = reinterpret_cast<const unsigned char *>("Score: ");
glutBitmapString(font_style1, t);
while(p > 9)
{
k = p % 10;
glRasterPos3f ((posx-(j*space_char)),posy, posz);
glutBitmapCharacter(font_style1,48+k);
j++;
p /= 10;
}
glRasterPos3f ((posx-(j*space_char)), posy, posz);
glutBitmapCharacter(font_style1,48+p);
glFlush();
}
// Game over text
void GameOver()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos2f(270,240);
glColor3f(1.0f, 0.0f, 0.7f);
if(countenemy == 0)
{
const unsigned char* w = reinterpret_cast<const unsigned char *>("You win");
glutBitmapString(font_style1, w);
}
else
{
const unsigned char* t = reinterpret_cast<const unsigned char *>("Game Over !\n");
glutBitmapString(font_style1, t);
const unsigned char* v = reinterpret_cast<const unsigned char *>("\nHit r to restart!");
glutBitmapString(font_style1, v);
}
for(int i=0;i<MAX_BULLET_ON_SCREEN;i++)
bullets[i].active=0;
for(int i=0;i<MAX_ENEMY_ON_SCREEN;i++)
ebullets[i].active=0;
}
//Loads the game
void StartGame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
if(player.Destroyed == false)
drawPlayer();
for(int i=0;i<MAX_ENEMY_ON_SCREEN;i++)
drawEnemy(i);
Enemyupdate();
DoCollision();
if(player.Destroyed == true)
OnDestroy();
for(int i=0;i<MAX_ENEMY_ON_SCREEN;i++)
if(enemy[i].Destroyed == true)
OnEnemyDestroy(i);
scoredisplay(600,440,-1,1,score);
if(player.Destroyed == true || countenemy == 0)
GameOver();
// Draws the bullets on screen when they are active
for (int i = 0; i < MAX_ENEMY_ON_SCREEN; i++)
if (ebullets[i].active)
drawEnemyBullet(i);
for (int i = 0; i < MAX_BULLET_ON_SCREEN; i++)
if (bullets[i].active)
drawPlayerBullet(i);
}
//Loads the instructions
void Instructions()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos2f(270,400);
glColor3f(1.0f, 1.0f, 0.7f);
const unsigned char* w = reinterpret_cast<const unsigned char *>("Instructions\n");
glutBitmapString(font_style1, w);
glRasterPos2f(20,340);
glColor3f(1.0f, 0.0f, 0.7f);
const unsigned char* o = reinterpret_cast<const unsigned char *>("Objective:\nEvade and destroy all the enemies on screen \n");
glutBitmapString(font_style1, o);
glRasterPos2f(20,240);
glColor3f(1.0f, 0.3f, 0.7f);
const unsigned char* p = reinterpret_cast<const unsigned char *>("0. Left and Right arrow keys - move player\n");
const unsigned char* q = reinterpret_cast<const unsigned char *>("1. Space button - Fire a rocket\n");
const unsigned char* r = reinterpret_cast<const unsigned char *>("2. Esc -end game and exit as well\n");
glutBitmapString(font_style1, p);
glutBitmapString(font_style1, q);
glutBitmapString(font_style1, r);
}
//Loads the main menu
void mymenu()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos2f(240,360);
glColor3f(1.0f, 0.0f, 0.7f);
const unsigned char* s = reinterpret_cast<const unsigned char *>("Hit a number to select\n");
glutBitmapString(font_style1, s);
glRasterPos2f(240,260);
glColor3f(1.0f, 0.0f, 0.0f);
const unsigned char* t = reinterpret_cast<const unsigned char *>("1. Start Game\n");
glutBitmapString(font_style1, t);
const unsigned char* u = reinterpret_cast<const unsigned char *>("2. Instructions\n");
glutBitmapString(font_style1, u);
const unsigned char* v = reinterpret_cast<const unsigned char *>("3. Quit\n");
glutBitmapString(font_style1, v);
switch(menu)
{
case 1:
StartGame();
break;
case 2:
Instructions();
break;
case 3:
exit(0);
break;
case 4:
mymenu1();
break;
default:
break;
}
}
void mymenu1()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen and Depth Buffer
GLvoid *font_style1 = GLUT_BITMAP_TIMES_ROMAN_24;
glRasterPos2f(240,360);
glColor3f(1.0f, 0.0f, 0.7f);
const unsigned char* s = reinterpret_cast<const unsigned char *>("Hit a number to select\n");
glutBitmapString(font_style1, s);
glRasterPos2f(240,260);
glColor3f(1.0f, 0.0f, 0.0f);
const unsigned char* t = reinterpret_cast<const unsigned char *>("1. Resume Game\n");
glutBitmapString(font_style1, t);
const unsigned char* u = reinterpret_cast<const unsigned char *>("2. Instructions\n");
glutBitmapString(font_style1, u);
const unsigned char* v = reinterpret_cast<const unsigned char *>("3. Quit\n");
glutBitmapString(font_style1, v);
const unsigned char* w = reinterpret_cast<const unsigned char *>("4. Back to main menu\n");
glutBitmapString(font_style1, w);
}
void OnDestroy()
{
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
myTranslate2D(player.x-4, player.y-5);
myRotate2D(45);
myScale2D(player.sizex-2,player.sizey-2);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 1.0f , -1.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 0.0f , 2.0f ); // Bottom Right
glEnd();
glPopMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
myTranslate2D(player.x+4, player.y+1);
myRotate2D(60);
myScale2D(player.sizex-1.5,player.sizey-1.5);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
myTranslate2D(player.x, player.y);
myRotate2D(30);
myScale2D(player.sizex-2.5,player.sizey-2.5);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
}
void OnEnemyDestroy(int i)
{
glColor3fv(mycolor[i]);
glPushMatrix();
myTranslate2D(enemy[i].x-4, enemy[i].y-5);
myRotate2D(45);
myScale2D(enemy[i].sizex-2,enemy[i].sizey-2);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 1.0f , -1.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 0.0f , 2.0f ); // Bottom Right
glEnd();
glPopMatrix();
glColor3fv(mycolor[i]);
glPushMatrix();
myTranslate2D(enemy[i].x+14, enemy[i].y+11);
myRotate2D(60);
myScale2D(enemy[i].sizex-1.5,enemy[i].sizey-1.5);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
glColor3fv(mycolor[i]);
glPushMatrix();
myTranslate2D(enemy[i].x, enemy[i].y);
myRotate2D(30);
myScale2D(enemy[i].sizex-12.5,enemy[i].sizey-12.5);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
glColor3fv(mycolor[i]);
glPushMatrix();
myTranslate2D(enemy[i].x, enemy[i].y-15);
myRotate2D(70);
myScale2D(enemy[i].sizex-2.5,enemy[i].sizey-2.5);
/* Starting position */
glBegin(GL_TRIANGLES);
glVertex2f( 0.0f , 2.0f ); // Top
glVertex2f(-1.0f , -1.0f ); // Bottom Left
glVertex2f( 1.0f , -1.0f ); // Bottom Right
glEnd();
glPopMatrix();
}