/
dungarvanMinesweeper.cpp
executable file
·903 lines (728 loc) · 22.2 KB
/
dungarvanMinesweeper.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
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
#include <windows.h> // for timeGetTime()
#include <mmsystem.h> // ditto
#include <iostream> // I/O
#include <time.h>
#include <glut.h> // GLUT
//#include <gl/glu.h> // for gluPerspective & gluLookAt
#include "model3DS.h" // 3DS model support
#include "enemy.cpp"
#include "bullet.cpp"
#define PI 3.141592654f
#define NUM_LEVELS 3
#define BULLETS_LIM 50
model3DS *dungarvan;
void setupScene();
void updateScene();
void renderScene();
void exitScene();
void keypress(unsigned char key, int x, int y);
void keyup(unsigned char key, int x, int y);
void setViewport(int width, int height);
void toggleFullscreen();
void light();
void character(void);
void drawArm(GLUquadricObj *quadratic);
GLuint importTexture(std::string filename);
void importSkybox();
void drawEnemy(enemy e);
void initEnemies();
void drawEnemies();
void camera();
void checkLevelComplete();
void drawText();
void renderBitmapString(float x, float y, int spacing, void *font, std::string string);
void drawIntro();
void restart();
void keyboardMovement();
// Setup some global variables for tracking through iterations of the glut main loop
bool disco = false, fullscreen=false, lighting=true, thirdPerson=true, topDown=false, sound=true, intro=true, win=false, dead=false;
int armAngle=50;
bool armDirection = true;
int windowId;
float multiplier = 2;
float xRotation = 0, yRotation = 0;
float xPos=0, yPos=0, zPos=0;
int globalHeight = 22;
float prevX=NULL, prevY=NULL;
bool fire = false; // Is this the ideal way to do this?
bool wPressed=false, aPressed=false, sPressed=false, dPressed=false;
GLuint textureId;
DWORD lastTickCount;
GLfloat white_light[] = {1.0, 1.0, 1.0, 1.0};
GLfloat left_light_position[] = {1500,700, -600, 1.0};
GLfloat right_light_position[] = {-1500,700,+600, 1.0};
GLuint north, south, east, west, top, bottom;
// Enemies
enemy *enemies[100];
int numEnemies = 100;
int level = 0, lives=10;
int enemyCount[5];
int enemiesLeft = 6;
// Bullets
bullet *b;
bullet *bullets[BULLETS_LIM];
int bulletCounter=0;
GLUquadricObj *quad[BULLETS_LIM];
// Level
float lowerX = -800.0;
float upperX = 100.0;
float lowerZ = -400.0;
float upperZ = 400.0;
// Fog stuff
bool fog = false;
#define FOG_COLOR 0.4, 0.4, 0.4
float fog_thickness = 0.3;
float fog_start = 120;
float fog_end = 300;
void character (void) {
glPushMatrix();
glDisable(GL_LIGHTING);
glTranslatef(0, -4.5, -14);
glPushMatrix();
// Body
glColor3f(1.0, 0.7, 0.3);
GLUquadricObj *quadratic;
quadratic=gluNewQuadric();
gluSphere(quadratic,2.5f,32,32);
glPopMatrix();
glTranslatef(0, +3.5, 0);
glColor3f(1.0, 0.6, 0.0);
gluSphere(quadratic,1.0f,32,32);
glPushMatrix();
// Arm 1
drawArm(quadratic);
glPopMatrix();
glPushMatrix();
// Arm 2
glRotatef(180, 0.0, 1.0, 0.0);
drawArm(quadratic);
glPopMatrix();
glEnable(GL_LIGHTING);
glPopMatrix();
}
void drawArm(GLUquadricObj *quadratic){
glColor3f(0.9, 0.9, 0.9);
// Distance from body, height
glTranslatef(1.75, -2.5, 0);
glRotatef(90,1.0,0.0,0.0);
glRotatef(armAngle,0.0,1.0,0.0);
gluCylinder(quadratic, 0.5, 0.5, 3,32,32);
}
void fireBullets(){
glDisable(GL_LIGHTING);
for (int i=0; i<BULLETS_LIM; i++){
if (bullets[i]->isFiring()){
if (i>2){
printf("break");
}
glPushMatrix();
glTranslatef(bullets[i]->getX(), bullets[i]->getY(), bullets[i]->getZ());
//Draw bullets
glColor3f(0.5, 0.6, 0.0);
gluSphere(quad[i],bullets[i]->getRadius(),32,32);
if (bullets[i]->increment(lowerX, upperX, lowerZ, upperZ)) {
//NOP
}else{
bullets[i]->stop();
}
glPopMatrix();
}
}
glEnable(GL_LIGHTING);
for (int i=0; i<enemyCount[level]; i++) {
for (int j=0; j<BULLETS_LIM; j++){
if (!enemies[i]->isDead() && bullets[j]->checkCollission(enemies[i]->getX(), enemies[i]->getY(), enemies[i]->getZ(), enemies[i]->getRadius())){
enemies[i]->kill();
enemiesLeft--;
bullets[j]->stop();
if (sound) PlaySound(L"audio/explode.wav",NULL,SND_FILENAME|SND_ASYNC);
printf("\nBANG! There are %i enemies left. \n ", enemiesLeft);
checkLevelComplete();
}
}
}
}
void checkLevelComplete(){
bool allDead = true;
for (int i=0; i<enemyCount[level]; i++){
if (!enemies[i]->isDead()){
allDead = false;
}
}
if (allDead && level == NUM_LEVELS){
win = true;
}
if (allDead && level<NUM_LEVELS){
level++;
printf("\nlevel++ to %i with %i enemies\n", level, enemyCount[level]);
if (sound) PlaySound(L"audio/level.wav",NULL,SND_FILENAME|SND_ASYNC);
initEnemies();
}
if (level==1){
fog=true;
light();
}
}
void camera(){
if (topDown){
glTranslated(-xPos,0,-zPos); // Translate to current xPos, yPos, zPos
glRotatef(90,1.0,0.0,0.0); // X
glRotatef(yRotation,0.0,1.0,0.0); // Y
} else {
glRotatef(xRotation,1.0,0.0,0.0); // X
glRotatef(yRotation,0.0,1.0,0.0); // Y
yPos = globalHeight;
glTranslated(-xPos,yPos,-zPos); // Translate to current xPos, yPos, zPos
}
}
void drawIntro(){
if (yRotation<180) yRotation+=2;
if (xRotation>0) xRotation-=2;
if (xPos<=-241) xPos+=2; else intro=false;
if (yPos<=globalHeight) yPos++;
if (zPos>=-46) zPos--;
glRotatef(xRotation,1.0,0.0,0.0); // X
glRotatef(yRotation,0.0,1.0,0.0); // Y
glTranslated(-xPos,yPos,-zPos); // Translate to current xPos, yPos, zPos
}
void restart(){
dead = false;
level = 0;
lives=10;
enemiesLeft = 6;
initEnemies();
}
void skybox(){
// http://sidvind.com/wiki/Skybox_tutorial was used to aid the construction of this
glPushMatrix();
glRotatef(xRotation,1.0,0.0,0.0); // X
glRotatef(yRotation,0.0,1.0,0.0); // Y
// Reset and transform the matrix.
glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
// Colour our sky blue just incase
// glColor4f(0.1,0.3,0.8,1);
// Render the front quad
glBindTexture(GL_TEXTURE_2D, north);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 0.5f, -0.5f, -0.5f );
glTexCoord2f(1, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
glTexCoord2f(1, 1); glVertex3f( -0.5f, 0.5f, -0.5f );
glTexCoord2f(0, 1); glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
// Render the left quad
glBindTexture(GL_TEXTURE_2D, west);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( 0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, -0.5f, -0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, 0.5f, -0.5f );
glTexCoord2f(0, 1); glVertex3f( 0.5f, 0.5f, 0.5f );
glEnd();
// Render the back quad
glBindTexture(GL_TEXTURE_2D, south);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, 0.5f, 0.5f );
glTexCoord2f(0, 1); glVertex3f( -0.5f, 0.5f, 0.5f );
glEnd();
// Render the right quad
glBindTexture(GL_TEXTURE_2D, east);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
glTexCoord2f(1, 0); glVertex3f( -0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( -0.5f, 0.5f, 0.5f );
glTexCoord2f(0, 1); glVertex3f( -0.5f, 0.5f, -0.5f );
glEnd();
// Render the top quad
glBindTexture(GL_TEXTURE_2D, top);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3f( -0.5f, 0.5f, -0.5f );
glTexCoord2f(0, 0); glVertex3f( -0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, 0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, 0.5f, -0.5f );
glEnd();
// Render the bottom quad
glBindTexture(GL_TEXTURE_2D, bottom);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f( -0.5f, -0.5f, -0.5f );
glTexCoord2f(0, 1); glVertex3f( -0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 1); glVertex3f( 0.5f, -0.5f, 0.5f );
glTexCoord2f(1, 0); glVertex3f( 0.5f, -0.5f, -0.5f );
glEnd();
// Restore enable bits and matrix
glPopAttrib();
glPopMatrix();
}
void renderScene(){
// Set the colour behind our skybox
if (disco){
// Every 10 ticks
if (lastTickCount%10==0) glClearColor((float)1000/rand(),(float)1000/rand(),(float)1000/rand(),1.0);
}else{
glClearColor(0.0,0.0,0.0,1.0);
}
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
// Clear framebuffer & depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset Modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
skybox();
drawText();
if (thirdPerson) character();
keyboardMovement();
if (intro) {
drawIntro();
} else {
camera();
}
drawEnemies();
if (fire) fireBullets();
// Draw textured tetrahedron
glEnable(GL_TEXTURE_2D);
glColor3f(1.0,1.0,1.0);
glBindTexture(GL_TEXTURE_2D,textureId);
// Draw Dungarvan
glPushMatrix();
dungarvan->draw();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
// Swap double buffer for flicker-free animation
glutSwapBuffers();
}
void drawText(){
// The tutorial at http://www.lighthouse3d.com/opengl/glut/index.php?bmpfontortho helped with this
// Draw some text on the screen
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// 2D orthographic with window's res
gluOrtho2D(0, 1600, 1200, 0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Needed for itoa - convert each int to char. Seriously, C++!? :-(
char buffer [33];
_itoa_s(lives, buffer, 10);
if (win) {
renderBitmapString(700,50,0,GLUT_BITMAP_TIMES_ROMAN_24,"//You win!//");
}
if (dead){
renderBitmapString(700,50,0,GLUT_BITMAP_TIMES_ROMAN_24,"//Game over! Click to restart.//");
}
if (intro){
renderBitmapString(700,50,0,GLUT_BITMAP_TIMES_ROMAN_24,"//Minesweeper//");
renderBitmapString(600,80,0,GLUT_BITMAP_TIMES_ROMAN_24,"W A S D + mouse to move. Click to shoot. ");
}else{
std::string stringLives = "Lives: " + (std::string)buffer;
_itoa_s(enemiesLeft, buffer, 10);
std::string stringEnemiesLeft = "Enemies Left: " + (std::string)buffer;
_itoa_s(level+1, buffer, 10);
std::string stringLevel = "Level: " + (std::string)buffer;
// char *lives = "Lives: 3";
// char *enemiesLeft = "Enemies: 6" + (char)enemiesLeft;
// char *level = "Level: 1";
renderBitmapString(5,15,0,GLUT_BITMAP_HELVETICA_18,stringLives);
renderBitmapString(5,30,0,GLUT_BITMAP_HELVETICA_18,stringEnemiesLeft);
renderBitmapString(5,45,0,GLUT_BITMAP_HELVETICA_18,stringLevel);
}
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void renderBitmapString(float x, float y, int spacing, void *font, std::string string) {
// NB this function is from http://www.lighthouse3d.com/opengl/glut/index.php?bmpfont
//char *c;
int x1=x;
for (int i=0; i<(float)string.length(); i++){
char c = string.at(i);
glColor3f(0.0, 1.0, 0.0);
glRasterPos2f(x1,y);
glutBitmapCharacter(font, c);
x1 = x1 + glutBitmapWidth(font,c) + spacing;
}
}
void drawEnemy(enemy e){
glPushMatrix();
glDisable(GL_LIGHTING);
glTranslatef(e.getX(), e.getY(), e.getZ());
if (level==0) glColor3f(0.6,0.6,0.6);
if (level==1) glColor3f(1,0.0,0.0);
if (level==2) glColor3f(0.1,0.1,0.1);
GLUquadricObj *quadratic;
quadratic=gluNewQuadric();
float r=0, g=0, b=0;
gluSphere(quadratic,e.getRadius(),32,32);
glPushMatrix();
glTranslatef(0, e.getRadius(), 0);
glColor3f(r,g,b);
gluSphere(quadratic,e.getRadius()/4,32,32);
glPopMatrix();
glPushMatrix();
glTranslatef(0, -e.getRadius(), 0);
glColor3f(r,g,b);
gluSphere(quadratic,e.getRadius()/4,32,32);
glPopMatrix();
glPushMatrix();
glTranslatef(e.getRadius(), 0, 0);
glColor3f(r,g,b);
gluSphere(quadratic,e.getRadius()/4,32,32);
glPopMatrix();
glPushMatrix();
glTranslatef(-e.getRadius(), 0, 0);
glColor3f(r,g,b);
gluSphere(quadratic,e.getRadius()/4,32,32);
glPopMatrix();
glPushMatrix();
glTranslatef(0, 0, e.getRadius());
glColor3f(r,g,b);
gluSphere(quadratic,e.getRadius()/4,32,32);
glPopMatrix();
glPushMatrix();
glTranslatef(0, 0, -e.getRadius());
glColor3f(r,g,b);
gluSphere(quadratic,e.getRadius()/4,32,32);
glPopMatrix();
glEnable(GL_LIGHTING);
glPopMatrix();
}
void updateScene(){
// Wait until at least 16ms passed since start of last frame
// Effectively caps framerate at ~60fps
while(timeGetTime()-lastTickCount<16);
lastTickCount=timeGetTime();
// Increment angle for next frame
if (armAngle<=100&&armDirection){
armAngle+=2;
}
if (armAngle>=50&&!armDirection){
armAngle-=2;
}
if (armAngle==100){
armDirection=false;
}
if (armAngle==50){
armDirection=true;
}
// Do any other updates here
// Draw the next frame
glutPostRedisplay();
}
void keypress(unsigned char key, int x, int y){
int mod = glutGetModifiers();
int run = 1;
if (mod == GLUT_ACTIVE_SHIFT){
run = run * 2;
}
// Test if user pressed ESCAPE (ascii 27)
// If so, exit the program
if(key==27){ // ESC
exitScene();
}
if (key==32){ // Space bar
}
if (key=='w')
wPressed=true;
if (key=='a')
aPressed=true;
if (key=='s')
sPressed=true;
if (key=='d')
dPressed=true;
if (key== 'x'){
multiplier += 0.5;
}
if (key== 'c'){
multiplier -= 0.5;
}
// Change the view
if (key=='v'){
if (thirdPerson) thirdPerson=false; else thirdPerson=true;
}
if (key=='y'){
if (disco) { disco = false; } else {disco = true;}
}
if (key=='f'){
toggleFullscreen();
}
if (key=='l'){
// Cheat!
// for (int i=0; i<enemyCount[level]; i++){
// enemies[i]->kill();
//}
//checkLevelComplete();
/*
if (!lighting){
glEnable(GL_LIGHTING);
lighting = true;
}else{
glDisable(GL_LIGHTING);
lighting = false;
}
*/
}
}
void keyup(unsigned char key, int x, int y){
if (key=='w')
wPressed=false;
if (key=='a')
aPressed=false;
if (key=='s')
sPressed=false;
if (key=='d')
dPressed=false;
}
void keyboardMovement(){
int run = 1;
if (wPressed){
float xRotationrad, yRotationrad;
yRotationrad = (yRotation / 180 * PI);
xRotationrad = (xRotation / 180 * PI);
xPos += run * multiplier * float(sin(yRotationrad)) ;
yPos -= run * multiplier * float(sin(xRotationrad)) ;
zPos -= run * multiplier * float(cos(yRotationrad)) ;
}
if (sPressed){
float xRotationrad, yRotationrad;
yRotationrad = (yRotation / 180 * PI);
xRotationrad = (xRotation / 180 * PI);
xPos -= run * multiplier* float(sin(yRotationrad));
yPos += run * multiplier* float(sin(xRotationrad));
zPos += run * multiplier* float(cos(yRotationrad));
}
if (aPressed){
float yRotationrad;
yRotationrad = (yRotation / 180 * PI);
xPos -= run * multiplier * float(cos(yRotationrad));
zPos -= run * multiplier * float(sin(yRotationrad));
}
if (dPressed){
float yRotationrad;
yRotationrad = (yRotation / 180 * PI);
xPos += run * multiplier * float(cos(yRotationrad));
zPos += run * multiplier * float(sin(yRotationrad));
}
}
void mouseMove(int x, int y){
float movementX = 0;
float movementY = 0;
if (prevX!=NULL) movementX = (float)x - prevX;
if (prevY!=NULL) movementY = (float)y - prevY;
if (!intro){
xRotation += movementY;
yRotation += movementX;
}
prevX = (float)x;
prevY = (float)y;
}
void mouseClick(int button, int state, int x, int y){
if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
if (dead){
// Click to restart when dead
restart();
}else{
// Setup for firing a bullet
float rotation = yRotation;
bullets[bulletCounter] = new bullet(xPos, globalHeight-50, +zPos, rotation, 1.0, 10);
fire = true;
bulletCounter++;
// Reset our bullet array counter, it's at the limit
if (bulletCounter==10) bulletCounter=0;
printf("Creating bullet at xPos = %f, zPos=%f with r=%f\n", xPos, zPos, rotation);
if (sound) PlaySound(L"audio/shoot.wav",NULL,SND_FILENAME|SND_ASYNC);
}
}
}
void initEnemies(){
// Setup level based variables before init'ing
enemyCount[0] = 6;
enemyCount[1] = 11;
enemyCount[2] = 23;
enemyCount[3] = 30;
enemyCount[4] = 50;
enemiesLeft = enemyCount[level];
float enemySize = 30.0f;
float enemySpeed = 1.0f;
if (level==1){
enemySize = 15.0f;
enemySpeed = 1.5f;
}
if (level==2 || level==3){
enemySize = 13.0f;
enemySpeed = 2.0f;
}
int enemyHeight = -globalHeight + (enemySize/2);
srand ( lastTickCount );
// Now init every enemy required for this level
for (int i=0; i<enemyCount[level]; i++){
int randX = 0, randZ = 0;
int xBoundSize = abs(lowerX - upperX);
int zBoundSize = abs(lowerX - upperX);
randX = rand() % xBoundSize + lowerX;
randZ = rand() % zBoundSize + lowerZ;
enemies[i] = new enemy(randX, enemyHeight, randZ, enemySize, enemySpeed);
}
// Now override a few of our random enemies to be static
enemies[0] = new enemy(-204, enemyHeight, -248, enemySize, 0); // Town Square
enemies[10] = new enemy(145, enemyHeight, -128, enemySize, 0); // Supervalue carpark
enemies[12] = new enemy(153, enemyHeight, 21, enemySize, 0); // Supervalue carpark
for (int i=0; i<BULLETS_LIM; i++){
bullets[i] = new bullet(0, 0, 0, 0, 0, 0);
bullets[i]->stop();
quad[i]=gluNewQuadric();
}
}
void drawEnemies(){
for (int i=0; i<enemyCount[level]; i++){
if (!enemies[i]->isDead()){
drawEnemy(*enemies[i]);
// Ideally we should be able to call increment and pass a level1 object but C++ arrays are silly.
//printf("x, y, z %f,%f,%f - enemeypos %f, %f, %f\n", xPos, yPos, zPos, enemies[i]->getX(), enemies[i]->getY(), enemies[i]->getZ());
if (enemies[i]->checkCollission(xPos, yPos, zPos)){
if (sound) PlaySound(L"audio/killEnemy.wav",NULL,SND_FILENAME|SND_ASYNC);
printf("\nlives--\n");
lives--;
//enemies[i]->reverseDirection();
enemies[i]->kill();
enemiesLeft--;
if (lives<0){
dead = true;
}
}
enemies[i]->increment(lowerX, upperX, lowerZ, upperZ);
}
}
}
void light(){
//glTexEnvi(GL_MODULATE);
if (lighting) glEnable(GL_LIGHTING);
// Create a "sunlight"
GLfloat sun[] = {1.0, 1.0, 1.0, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, sun);
glLightfv(GL_LIGHT0, GL_SPECULAR, sun);
GLfloat lightPosition[] = {0.5f, 0.75f, 0.75f, 0.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
// Material Settings
GLfloat allLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
// turn on full specular and color by default in material
glMateriali(GL_FRONT, GL_SHININESS, 64);
glMaterialfv(GL_FRONT, GL_SPECULAR, allLight);
glMaterialfv(GL_FRONT, GL_DIFFUSE, allLight);
glMaterialfv(GL_FRONT, GL_AMBIENT, allLight);
glEnable(GL_LIGHT0);
if(fog){
// Enable and Set Fog
GLfloat fogColor[] = { FOG_COLOR, 1.0 };
glEnable(GL_FOG);
glFogi(GL_FOG_MODE, GL_LINEAR);
glFogf(GL_FOG_START, fog_start);
glFogf(GL_FOG_END, fog_end);
glFogf(GL_FOG_DENSITY, fog_thickness);
glFogfv(GL_FOG_COLOR, fogColor);
}else{
// No Fog
glDisable(GL_FOG);
}
}
void setupScene(){
std::cout<<"Initializing scene..."<<std::endl;
light();
initEnemies();
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
dungarvan = new model3DS("dungarvan\\dungarvan.3ds", 2);
if (intro) {
xPos = -575;
yPos = -50;
zPos = 267;
xRotation = 29;
yRotation = 29;
}
}
void exitScene(){
std::cout<<"Exiting scene..."<<std::endl;
// Close window
if (!fullscreen) glutDestroyWindow(windowId);
// Free any allocated memory
// Exit program
exit(0);
}
void setViewport(int width, int height) {
// Work out window ratio, avoid divide-by-zero
if(height==0)height=1;
float ratio = float(width)/float(height);
// Reset projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Fill screen with viewport
glViewport(0, 0, width, height);
if (topDown){
glOrtho(1000, -1000, globalHeight, -2500, -1000, 1000);
}else{
// Set a 45 degree perspective
gluPerspective(45, ratio, .1, 2000);
}
}
void toggleFullscreen(){
if (fullscreen){
glutLeaveGameMode();
glutInitWindowPosition(50,50);
glutInitWindowSize(1600,1200);
windowId = glutCreateWindow("Lab 4: Loading a textured model");
}else{
glutDestroyWindow(windowId);
glutGameModeString("1600x1200:24");
glutEnterGameMode();
}
}
GLuint importTexture(std::string filename){
std::transform(filename.begin(),filename.end(),filename.begin(),tolower);
GLuint newTextureId;
glGenTextures(1, &newTextureId);
textureBMP newTexture(filename, newTextureId);
return newTextureId;
}
void importSkybox(){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Make skybox textures global so only imported once
north = importTexture("skybox\\north.bmp");
south = importTexture("skybox\\south.bmp");
east = importTexture("skybox\\east.bmp");
west = importTexture("skybox\\west.bmp");
top = importTexture("skybox\\top.bmp");
bottom = importTexture("skybox\\bottom.bmp");
}
int main(int argc, char *argv[]){
// Initialise OpenGL
glutInit(&argc, argv);
// Set window position, size & create window
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
if (fullscreen){
glutGameModeString("1900x1080:24");
glutEnterGameMode();
}else{
glutInitWindowPosition(50,50);
glutInitWindowSize(1600,1200);
windowId = glutCreateWindow("Lab 4: Loading a textured model");
}
// Set GLUT callback functions
glutReshapeFunc(setViewport);
glutDisplayFunc(renderScene);
glutIdleFunc(updateScene);
glutKeyboardFunc(keypress);
glutKeyboardUpFunc(keyup);
glutPassiveMotionFunc(mouseMove);
glutMouseFunc(mouseClick);
// Setup OpenGL state & scene resources (models, textures etc)
setupScene();
importSkybox();
// Show window & start update loop
glutMainLoop();
return 0;
}