-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyOpenGLView.cpp
More file actions
3311 lines (3089 loc) · 129 KB
/
MyOpenGLView.cpp
File metadata and controls
3311 lines (3089 loc) · 129 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
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
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// MyOpenGLView.cpp : implementation file
//
#include "stdafx.h"
#include "OpenGLDoc.h"
#include "MyOpenGLView.h"
#include <math.h>
#define PI 3.14159
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BUFFER_LENGTH 64
/////////////////////////////////////////////////////////////////////////////
// CMyOpenGLView
IMPLEMENT_DYNCREATE(CMyOpenGLView, COpenGLView)
BEGIN_MESSAGE_MAP(CMyOpenGLView, COpenGLView)
//{{AFX_MSG_MAP(CMyOpenGLView)
ON_WM_RBUTTONDOWN()
ON_WM_KEYDOWN()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_COLLISION_ON, OnCollisionOn)
ON_COMMAND(ID_COLLISION_OFF, OnCollisionOff)
ON_COMMAND(ID_HOST, OnHost)
ON_COMMAND(ID_JOIN, OnJoin)
ON_COMMAND(ID_DISCONNECT, OnDisconnect)
ON_COMMAND(ID_SINGLE, OnSingle)
ON_COMMAND(ID_MULTI, OnMulti)
ON_COMMAND(ID_QUIT, OnQuit)
ON_MESSAGE(MM_MCINOTIFY, OnMCINotify)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
CMyOpenGLView::CMyOpenGLView(): azimuth(0.0), elevation(0.0), rotation(0.0)
{
COpenGLView::COpenGLView( );
}
CMyOpenGLView::~CMyOpenGLView()
{
for(int py = 0; py < (int) pyramid.size(); py++){
delete pyramid[py];
}
for(int f = 0; f < (int) friendly.size(); f++){
delete friendly[f];
}
for(int e = 0; e < (int) enemy.size(); e++){
delete enemy[e];
}
for(int h = 0; h < (int) house.size(); h++){
delete house[h];
}
for(int s = 0; s < (int) silo.size(); s++){
delete silo[s];
}
for(int i = 0; i < (int) pine.size(); i++){
delete pine[i];
}
for(int j = 0; j < (int) smallPine.size(); j++){
delete smallPine[j];
}
for (i = 0; i < (int) shells.size(); i++) {
delete shells[i];
}
for (i = 0; i < (int) bullets.size(); i++) {
delete bullets[i];
}
for (i = 0; i < (int) oppShells.size(); i++) {
delete oppShells[i];
}
for (i = 0; i < (int) oppBullets.size(); i++) {
delete oppBullets[i];
}
}
GLuint textNames[8] = {0};
BOOL CMyOpenGLView::OpenGLInit() {
COpenGLView::OpenGLInit();
m_iPort = 12345;
multiPlayer = false;
server = false;
m_listenSocket.SetParent(this);
m_connectSocket.SetParent(this);
xDist = yDist = zDist = minDist = 0.0f;
collisionDetection = true;
bulletCollision = false;
shellCollision = false;
previousShotTime = 0.0;
elapsedShotTime = 0.0;
shotFired = false;
mgLatch = false;
score = 0;
startGame = false;
gameOver = false;
playedFinalSound = false;
backgroundMusic = "metalGear.mid";
setUpLighting();
setUpTextures();
ourTank.init(textNames[6]);
mg.init();
opponent.init(textNames[6]);
oppMG.init();
spawnSites[0][0] = -290.0;
spawnSites[0][1] = -360.0;
spawnSites[0][2] = 315.0;
spawnSites[1][0] = 200.0;
spawnSites[1][1] = -450.0;
spawnSites[1][2] = 25.0;
spawnSites[2][0] = 200.0;
spawnSites[2][1] = 350.0;
spawnSites[2][2] = 145.0;
spawnSites[3][0] = -350.0;
spawnSites[3][1] = 150.0;
spawnSites[3][2] = 260.0;
spawnSiteActive[0] = spawnSiteActive[1] = spawnSiteActive[2] = spawnSiteActive[3] = false;
srand(timeGetTime());
for(int py = 0; py < NUMBER_OF_PYRAMIDS; py++){
pyramid.push_back(new Pyramid());
}
pyramid[0]->init(50, 100, 0, 900);
pyramid[1]->init(50, 100, -200, 900);
pyramid[2]->init(50, 100, -500, 900);
pyramid[3]->init(50, 100, -600, 900);
pyramid[4]->init(80, 120, -800, 900);
pyramid[5]->init(50, 100, -900, 700);
pyramid[6]->init(80, 120, -900, 500);
pyramid[7]->init(80, 120, -900, 200);
pyramid[8]->init(80, 120, -900, 0);
pyramid[9]->init(80, 120, -900, -350);
pyramid[10]->init(80, 120, -900, -500);
pyramid[11]->init(80, 120, 300, 900);
pyramid[12]->init(50, 100, 500, 900);
pyramid[13]->init(80, 120, 900, 900);
pyramid[14]->init(50, 100, 900, 700);
pyramid[15]->init(50, 120, 900, 500);
pyramid[16]->init(80, 120, 800, 100);
pyramid[17]->init(80, 120, 800, 0);
pyramid[18]->init(50, 120, 900, -300);
pyramid[19]->init(80, 120, 900, -500);
for(int f = 0; f < NUMBER_OF_FRIENDLIES; f++){
friendly.push_back(new Good());
}
friendly[0]->init(-530, 620, textNames[6]);
friendly[1]->init(160, 710, textNames[6]);
friendly[2]->init(230, 280, textNames[6]);
friendly[3]->init(-175, -30, textNames[6]);
friendly[4]->init(25, -115, textNames[6]);
for(int e = 0; e < NUMBER_OF_ENEMIES; e++){
enemy.push_back(new Foe());
}
enemy[0]->init(0, -30, textNames[7]);
enemy[0]->init(-220, -100, textNames[7]);
enemy[1]->init(-490, 575, textNames[7]);
enemy[2]->init(-180, -130, textNames[7]);
enemy[3]->init(-120, -40, textNames[7]);
enemy[4]->init(-30, -90, textNames[7]);
enemy[5]->init(25, -200, textNames[7]);
enemy[6]->init(80, -40, textNames[7]);
enemy[7]->init(120, -85, textNames[7]);
enemy[8]->init(170, -30, textNames[7]);
enemy[9]->init(185, -200, textNames[7]);
enemy[10]->init(160, 350, textNames[7]);
enemy[11]->init(200, 380, textNames[7]);
enemy[12]->init(270, 310, textNames[7]);
enemy[13]->init(90, 600, textNames[7]);
enemy[14]->init(130, 700, textNames[7]);
enemy[15]->init(200, 650, textNames[7]);
enemy[16]->init(-400, 200, textNames[7]);
enemy[17]->init(-300, 10, textNames[7]);
enemy[18]->init(-50, 100, textNames[7]);
enemy[19]->init(100, 225, textNames[7]);
for(int h = 0; h < NUMBER_OF_HOUSES; h++){
house.push_back(new House());
}
for(int street1 = 0; street1 < 10; street1++){
int x1 = street1 * 50;
house[street1]->init(-250.0f + x1, 3.75f, -15.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
}
for(int street2 = 10; street2 < 20; street2++){
int x2 = (street2 - 10) * 50;
house[street2]->init(-250.0f + x2, 3.75f, -65.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
}
for(int street3 = 20; street3 < 30; street3++){
int x3 = (street3 - 20) * 50;
house[street3]->init(-250.0f + x3, 3.75f, -115.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
}
for(int street4 = 30; street4 < 40; street4++){
int x4 = (street4 - 30) * 50;
house[street4]->init(-250.0f + x4, 3.75f, -165.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
}
for(int street5 = 40; street5 < 50; street5++){
int x5 = (street5 - 40) * 50;
house[street5]->init(-250.0f + x5, 3.75f, -215.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
}
//BARN
house[50]->init(200.0f , 5.0f, 355.0f, 20.0f, 30.0f, 10.0f, 10.0f,
textNames[1], textNames[5]);
//FARMHOUSES
house[51]->init(250.0f , 3.75f, 300.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
house[52]->init(260.0f , 3.75f, 320.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
//SHED
house[53]->init(180.0f , 1.5f, 360.0f, 5.0f, 4.0f, 3.0f, 2.0f,
textNames[1], textNames[5]);
//Second Farm
//BARN
house[54]->init(-500.0f , 5.0f, 600.0f, 20.0f, 30.0f, 10.0f, 10.0f,
textNames[1], textNames[5]);
//FARMHOUSES
house[55]->init(-520.0f , 3.75f, 650.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
house[56]->init(-480.0f , 3.75f, 650.0f, 12.0f, 10.0f, 7.5f, 5.0f,
textNames[1], textNames[5]);
//SHED
house[57]->init(-520.0f , 1.5f, 600.0f, 5.0f, 4.0f, 3.0f, 2.0f,
textNames[1], textNames[5]);
for(int s = 0; s < NUMBER_OF_SILOS; s++){
silo.push_back(new Silo());
}
silo[0]->init(180.0f, 10.0f, 375.0f, 2.0f, 20.0f, textNames[0], textNames[5]);
silo[1]->init(220.0f, 10.0f, 380.0f, 2.0f, 20.0f, textNames[0], textNames[5]);
//Second Farm
silo[2]->init(-480.0f, 10.0f, 600.0f, 2.0f, 20.0f, textNames[0], textNames[5]);
silo[3]->init(-480.0f, 10.0f, 590.0f, 2.0f, 20.0f, textNames[0], textNames[5]);
silo[4]->init(-480.0f, 10.0f, 580.0f, 2.0f, 20.0f, textNames[0], textNames[5]);
//888888888888888888888888888888888888888888888888888888888888888
for(int i = 0; i < NUMBER_OF_PINES; i++){
pine.push_back(new Pine());
}
pine[0]->init(-250, 110);
pine[1]->init(-200, -8);
pine[2]->init(-175, 65);
pine[3]->init(-155, 26);
pine[4]->init(-134, 10);
pine[5]->init(-122, 80);
pine[6]->init(-100, -5);
pine[7]->init(-85, 80);
pine[8]->init(-75, -3);
pine[9]->init(-40, 55);
pine[10]->init(-20, 20);
pine[11]->init(10, 65);
pine[12]->init(55, 14);
pine[13]->init(75, 0);
pine[14]->init(100, 126);
pine[15]->init(125, 70);
pine[16]->init(150, 130);
pine[17]->init(175, -4);
pine[18]->init(200, 50);
pine[19]->init(250, 65);
pine[20]->init(-230, -40);
pine[21]->init(-175, -185);
pine[22]->init(-130, -80);
pine[23]->init(-70, -140);
pine[24]->init(-30, -145);
pine[25]->init(20, -80);
pine[26]->init(75, -180);
pine[27]->init(130, -40);
pine[28]->init(120, -130);
//FARM TREES
pine[29]->init(320, 290);
pine[30]->init(325, 325);
pine[31]->init(320, 350);
pine[32]->init(325, 375);
pine[33]->init(320, 400);
pine[34]->init(290, 420);
pine[35]->init(255, 425);
pine[36]->init(230, 420);
pine[37]->init(190, 425);
pine[38]->init(150, 420);
pine[39]->init(135, 390);
pine[40]->init(140, 370);
pine[41]->init(135, 350);
pine[42]->init(140, 320);
pine[43]->init(135, 300);
for(int j = 0; j < NUMBER_OF_SMALL_PINES; j++){
smallPine.push_back(new SmallPine());
}
smallPine[0]->init(-250, 10);
smallPine[1]->init(-200, 38);
smallPine[2]->init(-165, 55);
smallPine[3]->init(-125, 86);
smallPine[4]->init(-104, 25);
smallPine[5]->init(-82, 65);
smallPine[6]->init(-70, -5);
smallPine[7]->init(-55, 38);
smallPine[8]->init(-35, 15);
smallPine[9]->init(-15, 58);
smallPine[10]->init(-35, 0);
smallPine[11]->init(18, 80);
smallPine[12]->init(43, 110);
smallPine[13]->init(75, 48);
smallPine[14]->init(80, 75);
smallPine[15]->init(112, 40);
smallPine[16]->init(130, 98);
smallPine[17]->init(145, 34);
smallPine[18]->init(185, -3);
smallPine[19]->init(210, 50);
smallPine[20]->init(-225, -145);
smallPine[21]->init(-75, -185);
smallPine[22]->init(-30, -35);
smallPine[23]->init(115, -130);
smallPine[24]->init(130, -180);
//TREE GROVE
smallPine[25]->init(100, 500);
smallPine[26]->init(100, 520);
smallPine[27]->init(100, 540);
smallPine[28]->init(100, 560);
smallPine[29]->init(100, 580);
smallPine[30]->init(100, 600);
smallPine[31]->init(100, 620);
smallPine[32]->init(100, 640);
smallPine[33]->init(100, 660);
smallPine[34]->init(100, 680);
smallPine[35]->init(120, 500);
smallPine[36]->init(120, 520);
smallPine[37]->init(120, 540);
smallPine[38]->init(120, 560);
smallPine[39]->init(120, 580);
smallPine[40]->init(120, 600);
smallPine[41]->init(120, 620);
smallPine[42]->init(120, 640);
smallPine[43]->init(120, 660);
smallPine[44]->init(120, 680);
smallPine[45]->init(140, 500);
smallPine[46]->init(140, 520);
smallPine[47]->init(140, 540);
smallPine[48]->init(140, 560);
smallPine[49]->init(140, 580);
smallPine[50]->init(140, 600);
smallPine[51]->init(140, 620);
smallPine[52]->init(140, 640);
smallPine[53]->init(140, 660);
smallPine[54]->init(140, 680);
smallPine[55]->init(160, 500);
smallPine[56]->init(160, 520);
smallPine[57]->init(160, 540);
smallPine[58]->init(160, 560);
smallPine[59]->init(160, 580);
smallPine[60]->init(160, 600);
smallPine[61]->init(160, 620);
smallPine[62]->init(160, 640);
smallPine[63]->init(160, 660);
smallPine[64]->init(160, 680);
smallPine[65]->init(180, 500);
smallPine[66]->init(180, 520);
smallPine[67]->init(180, 540);
smallPine[68]->init(180, 560);
smallPine[69]->init(180, 580);
smallPine[70]->init(180, 600);
smallPine[71]->init(180, 620);
smallPine[72]->init(180, 640);
smallPine[73]->init(180, 660);
smallPine[74]->init(180, 680);
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// COpenGLView drawing
void CMyOpenGLView::OnDraw(CDC* pDC)
{
static GLfloat light0_pos[] = {100.0, 100.0, 100.0, 0.0};
static GLfloat light1_pos[] = {95.0, 95.0, 95.0, 1.0};
static GLfloat light2_pos[] = {0.0, 0.0, 0.0, 1.0};
static GLfloat spot_direction[] = {0.0, 0.0, -1.0};
// TODO: add draw code for native data here
m_ElapsedTime = timeGetTime();
// Keep the paint messages coming
if ( m_bAnimationRunning ) {
InvalidateRect( 0, FALSE );
GetParent()->PostMessage(WM_PAINT);
}
/* if (ElapsedTimeinMSSinceLastRender() >= 100) {
updateScene();
} // end if
*/
// Limit frame rate to 25 frames per second
if ( ElapsedTimeinMSSinceLastRender() >= 35 || (!m_bAnimationRunning) ) {
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode (GL_MODELVIEW); // Modelview matrix stack
glLoadIdentity();
// Positon Light 2
glLightfv(GL_LIGHT2, GL_POSITION, light2_pos);
glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, spot_direction);
glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
// Execute Viewing transformations
setViewPoint();
// Draw the scene objects
drawScene();
if (startGame && !gameOver) {
PlayMusic(backgroundMusic);
} // end if
// Position Light 0
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glFlush();
SwapBuffers( m_pDC->GetSafeHdc() );
if ( m_bAnimationRunning ) {
updateScene();
} // end if
displayFrameRate();
// save the elapsed time, this is used with the next elapsed time to calculate the
// elapsed time since a render and control the frame rate
m_previousElapsedTime = m_ElapsedTime;
}
}
void CMyOpenGLView::setViewPoint()
{
if (startGame && !gameOver) {
GLUquadricObj *sun;
sun = gluNewQuadric();
/* // Execute Viewing transformations
if (viewPointMode == FLY) {
computeFlyView();
}*/
// rotate view for eye positioning via the mouse
glRotatef( rotationX, 1, 0, 0 );
glRotatef( rotationY, 0, 1, 0 );
glRotatef(azimuth, 0, 1, 0);
glPushMatrix();
glRotatef(-azimuth, 0, 1, 0);
// translate the tank away from where the eye position will be,
// which is behind the machine gun as a "tank commander"
glTranslatef(-1.0f, -4.25f, -1.5f);
// compensation rotation for how model was created
glRotatef(90.0f, 0, 1, 0);
ourTank.draw();
glPopMatrix();
// set up lighting emissitivity property for the sun and for
// putting emissitivity back to normal
GLfloat sun_emissive[3] = {1.0, 1.0, 0.0};
GLfloat no_emissive[3] = {0.0, 0.0, 0.0};
// color of the sun = yellow
glColor4f(1.0, 1.0, 0.65f, 0.0);
glPushMatrix();
// always place the sun behind the player's right shoulder if
// the player is looking straight out in front of the tank
glTranslatef(120, 120, 120);
// make the sun emissive so it looks like it glows
glMaterialfv(GL_FRONT, GL_EMISSION, sun_emissive);
gluSphere(sun, 5, 20, 20);
// glutSolidSphere(5, 20, 20);
glPopMatrix();
// turn off the emissivity so the rest of the world doesn't glow
glMaterialfv(GL_FRONT, GL_EMISSION, no_emissive);
// translate to the eye position to set the view point
// remember, viewPoint{X,Y,Z} are all initialized to zero (0)
// and the movement of the tank occurs by adjusting the "viewPoint"
// eventhough it is no longer really a viewPoint, just a reference
// point for location in the world. The real "viewPoint" is
// one unit to the right, five units up, and one unit back from
// this reference point.
glTranslatef(viewPointX - 1, viewPointY - 5, viewPointZ - 1);
} else if (!startGame && !gameOver) {
// basic view point setup, looking down the -z axis
gluLookAt(0.0, 0.0, 0.0,
0.0, 0.0, -1.0,
0.0, 1.0, 0.0);
} else if (startGame && gameOver) {
// create a view point looking down on the environment when the game
// ends
gluLookAt(350.0, 150.0, -400.0,
-75.0, 0.0, 100.0,
0.0, 1.0, 0.0);
} // end if-else
} // end SetViewPoint
// Draw scene objects
void CMyOpenGLView::drawScene()
{
//******** Draw Your Objects Here *********
if (startGame) {
if (multiPlayer) {
// do network stuff here
// NEED TO DECOUPLE NETWORK FUNCTIONALITY
// FROM THE DRAWING FUNCTIONALITY
Packet *packet = new Packet;
packet->packetType = 0;
packet->x = viewPointX - 1;
packet->y = viewPointY - 5;
packet->z = viewPointZ - 1;
packet->azimuth = azimuth;
packet->rotationX = rotationX;
packet->rotationY = rotationY;
packet->speed = ourTank.getSpeed();
// packet->isDestroyed = ourTank.isDestroyed();
packet->mgLatch = mgLatch;
int len = sizeof(*packet);
if (m_connectSocket.Send((void*)packet, len) == SOCKET_ERROR) {
CheckError(m_connectSocket.GetLastError());
OnDisconnect();
} // end if
delete packet;
/* if (server) {
if (m_connectSocket.Send((void*)packet, len) == SOCKET_ERROR)
{
CheckError(m_connectSocket.GetLastError());
m_connectSocket.Close();
multiPlayer = false;
}
delete packet;
} else {
if (m_connectSocket.Send((void*)packet, len) == SOCKET_ERROR)
{
CheckError(m_connectSocket.GetLastError());
m_connectSocket.Close();
multiPlayer = false;
}
delete packet;
} // end if-else*/
} // end if
// If in a multi-player game, have to draw the opponent and his
// machine gun seperately, so it is articulated
if (multiPlayer) {
// Draw opponent
glPushMatrix();
glTranslatef(-opponent.getX(), -opponent.getY(), -opponent.getZ());
glRotatef(90 - opponent.getAzimuth(), 0, 1, 0);
glTranslatef(1.5, -4.25, -1.0);
if (!opponent.isDestroyed() ||
(opponent.isDestroyed() && opponent.getExplosion()->isGrowing())) {
opponent.draw();
} // end if
if (opponent.isDestroyed()) {
opponent.getExplosion()->draw();
if (opponent.getExplosion()->isExplosionDone()) {
opponent.regen();
} // end if
} // end if
glPopMatrix();
// Draw opponent's machine gun
glPushMatrix();
glTranslatef(-opponent.getX(), -opponent.getY(), -opponent.getZ());
glRotatef(-opponent.getAzimuth(), 0, 1, 0);
glTranslatef(0.0f, -0.55f, -1.5f);
if (opponent.isMGLatched()) {
glRotatef(-opponent.getRotationX(), 1, 0, 0);
glRotatef(-opponent.getRotationY(), 0, 1, 0);
} // end if
if (!opponent.isDestroyed() ||
(opponent.isDestroyed() && opponent.getExplosion()->isGrowing())) {
oppMG.draw();
} // end if
glPopMatrix();
if (ourTank.isRespawning()) {
float xDist = fabsf(spawnSites[spawnSite][0] - ourTank.getX());
float zDist = fabsf(spawnSites[spawnSite][1] - ourTank.getZ());
float dist = (GLfloat) sqrt(xDist * xDist + zDist * zDist);
if (dist > 50) {
spawnSiteActive[spawnSite] = false;
ourTank.setRespawnFlag(false);
Packet *packet = new Packet;
packet->packetType = 10;
packet->id = spawnSite;
packet->spawnSiteActive = false;
int len = sizeof(*packet);
if (m_connectSocket.Send((void*)packet, len) == SOCKET_ERROR) {
CheckError(m_connectSocket.GetLastError());
OnDisconnect();
} // end if
delete packet;
} // end if
} // end if
} // end if
//88888888888888888888888888888888888888888888888888888888
// NOTE: CHECK TO MOVE THIS TO INIT METHOD
char* explode = "explobig.wav";
// Draw the sky, which is a texture plastered on a sphere
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textNames[3]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
// create a quadric for the sky
GLUquadricObj *sobj;
sobj = gluNewQuadric();
gluQuadricOrientation(sobj, GLU_INSIDE);
gluQuadricDrawStyle(sobj, GLU_FILL);
gluQuadricNormals(sobj, GLU_SMOOTH);
gluQuadricTexture(sobj, GL_TRUE);
// draw the sky and delete the quadric object
glColor3f(0.0, 0.65f, 1.0);
gluSphere(sobj, 1100, 100, 100);
gluDeleteQuadric(sobj);
// Draw ground
// Use a marble texture to make the ground look like grass
// Hey, it works...
glBindTexture(GL_TEXTURE_2D, textNames[2]);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND);
glColor3f(0.0, 0.5, 0.0);
for (int x = -1000; x < 1000; x += 100) {
for (int z = -1000; z < 1000; z += 100) {
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3f((GLfloat)x, 0.0, (GLfloat)z);
glTexCoord2f(10.0, 0.0);
glVertex3f((GLfloat)x, 0.0, (GLfloat)z + 100.0f);
glTexCoord2f(10.0, 10.0);
glVertex3f((GLfloat)x + 100.0f, 0.0, (GLfloat)z + 100.0f);
glTexCoord2f(0.0, 10.0);
glVertex3f((GLfloat)x + 100.0f, 0.0, (GLfloat)z);
glEnd();
} // end for
} // end for
// Turn off texturing
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_2D);
// Draw our machine gun
glPushMatrix();
// first, to create a rotation without a moment arm
// translate back to the reference point for the tank,
// which is defined by the "viewPoints"
glTranslatef(-viewPointX + 1, viewPointY + 5, -viewPointZ + 1);
// rotate about the azimuth
glRotatef(-azimuth, 0, 1, 0);
// translate back to where the gun should be
glTranslatef(0, -0.55f, -1.5f);
// if the tank commander is steering the machine gun,
// then rotate it to match the view points rotations done
// with the mouse
if (mgLatch) {
glRotatef(-rotationX, 1, 0, 0);
glRotatef(-rotationY, 0, 1, 0);
} // end if
// draw the machine gun
mg.draw();
glPopMatrix();
// Display tank info on screen
// This info is the HUD for the tank and the game.
// ** Would like to be able to create this with a translucent look
// to it. **
char buffer[50];
int i;
i = sprintf(buffer, "Speed: ");
if (ourTank.getSpeed() < 0.05 && ourTank.getSpeed() > -0.05) {
i += sprintf(buffer + i, "0");
} else {
i += sprintf(buffer + i, "%.0f", ourTank.getSpeed() * 10);
} // end if-else
if (ourTank.getSpeed() >= -0.05) {
// Color for speed when going forward is green
glColor3f(0.0, 1.0, 0.0);
} else {
// Color for speed when going back is red
glColor3f(1.0, 0.0, 0.0);
} // end if-else
// draw speed in the lower left corner of the screen
flatFont.screenTextOutput(10, 10, winSizeX, winSizeY, buffer);
// for the azimuth, buffer enough leading zeros so it always
// displays three digits
if (azimuth < 0.5 && azimuth > -0.5) {
i = sprintf(buffer, "000");
} else if (azimuth < 10) {
i = sprintf(buffer, "00");
i += sprintf(buffer + i, "%.0f", azimuth);
} else if (azimuth < 100) {
i = sprintf(buffer, "0");
i += sprintf(buffer + i, "%.0f", azimuth);
} else {
i = sprintf(buffer, "%.0f", azimuth);
} // end if-else
// Azimuth is drawn in red
glColor3f(1.0, 0.0, 0.0);
// Draw azimuth in the top center of the screen
flatFont.screenTextOutput(winSizeX/2 - 25, winSizeY - 20, winSizeX, winSizeY, buffer);
i = sprintf(buffer, "Shells: ");
i += sprintf(buffer + i, "%i", ourTank.getShells());
// Display the number of shells using red in the lower right corner of the screen
flatFont.screenTextOutput(winSizeX - 200, 30, winSizeX, winSizeY, buffer);
// If 10 seconds have not passed since the last main gun shot, display "Reloading"
// right above the shell count
if (ElapsedTimeInMSSinceLastShot() > 250 && ElapsedTimeInMSSinceLastShot() < 10000) {
i = sprintf(buffer, " Reloading");
flatFont.screenTextOutput(winSizeX - 170, 50, winSizeX, winSizeY, buffer);
} // end if
i = sprintf(buffer, ".50 Rounds: ");
i += sprintf(buffer + i, "%i", ourTank.getBullets());
// Display the number of machine gun bullets using red in the bottom right corner
// of the screen, below the number of main gun shells
flatFont.screenTextOutput(winSizeX - 200, 10, winSizeX, winSizeY, buffer);
i = sprintf(buffer, "Score: ");
i += sprintf(buffer + i, "%i", score);
// The score is drawn in blue
glColor3f(0.0, 0.0, 1.0);
// Display the score in the upper left corner of the screen
flatFont.screenTextOutput(10, winSizeY - 20, winSizeX, winSizeY, buffer);
i = sprintf(buffer, "Hull: ");
i += sprintf(buffer + i, "%.0f", ourTank.getDamageCapacity());
if (ourTank.getDamageCapacity() > 20) {
// If damage capacity if above 2/3, the color it green
glColor3f(0.0, 1.0, 0.0);
} else if (ourTank.getDamageCapacity() > 10) {
// Damage capacity between 1/3 and 2/3 is yellow
glColor3f(1.0, 1.0, 0.0);
} else {
// Damage capacity below 1/3 is red
glColor3f(1.0, 0.0, 0.0);
} // end if-else
// Draw damage capacity in the upper right corner of the screen
flatFont.screenTextOutput(winSizeX - 100, winSizeY - 20, winSizeX, winSizeY, buffer);
// Color for time is blue
glColor3f(0.0, 0.0, 1.0);
i = sprintf(buffer, "Time Remaining");
// Draw time remaining in the bottom center of the screen
flatFont.screenTextOutput(winSizeX/2 - 100, 30, winSizeX, winSizeY, buffer);
double elapsedTime = timeGetTime() - gameStartTime;
double timeRemainingInSec = (600000 - elapsedTime) / 1000;
int timeRemainingMin = (int) (timeRemainingInSec / 60.0);
int timeRemainingSec = (int) (timeRemainingInSec - timeRemainingMin * 60);
// As long as the game is not over, draw the time
if (!gameOver) {
i = sprintf(buffer, "%d:", timeRemainingMin);
if (timeRemainingSec < 10) {
i += sprintf(buffer + i, "0%d", timeRemainingSec);
} else {
i += sprintf(buffer + i, "%d", timeRemainingSec);
} // end if
flatFont.screenTextOutput(winSizeX/2 - 35, 10, winSizeX, winSizeY, buffer);
} // end if
// When you get killed in a multi-player game, display a message in the center of
// the screen
if (multiPlayer && ourTank.getDamageCapacity() <= 0) {
i = sprintf(buffer, "You've been fragged!");
ourTank.stop();
glColor3f(1.0, 0.0, 0.0);
flatFont.screenTextOutput(winSizeX/2 - 100, winSizeY/2, winSizeX, winSizeY, buffer);
respawn();
// For multi-player, game ends when the time runs out
// When the game is over, display "Game Over" in the center of the screen
} else if (multiPlayer && timeRemainingInSec <= 0) {
i = sprintf(buffer, "Game Over");
ourTank.stop();
glColor3f(1.0, 0.0, 0.0);
flatFont.screenTextOutput(winSizeX/2 - 50, winSizeY/2, winSizeX, winSizeY, buffer);
gameOver = true;
// Play "Game over man..." clip from Aliens at the end of the game
if (!playedFinalSound) {
mciSendCommand(wDeviceID, MCI_STOP, 0, NULL);
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
sndPlaySound("gameover.wav", SND_ASYNC | SND_FILENAME);
playedFinalSound = true;
} // end if
} // end if-else
// For single player, the game ends when the player runs out of both bullets and
// shells, or time, or the tank is destroyed
if (!multiPlayer && (ourTank.getDamageCapacity() <= 0 || timeRemainingInSec <= 0 ||
(ourTank.getBullets() == 0 && ourTank.getShells() == 0) ||
(enemy.size() == 0))) {
i = sprintf(buffer, "Game Over");
ourTank.stop();
glColor3f(1.0, 0.0, 0.0);
flatFont.screenTextOutput(winSizeX/2 - 50, winSizeY/2, winSizeX, winSizeY, buffer);
gameOver = true;
// Play "Game over man..." clip from Aliens at the end of the game
if (!playedFinalSound) {
mciSendCommand(wDeviceID, MCI_STOP, 0, NULL);
mciSendCommand(wDeviceID, MCI_CLOSE, 0, NULL);
sndPlaySound("gameover.wav", SND_ASYNC | SND_FILENAME);
playedFinalSound = true;
} // end if
} // end if
// Draw the "mountains" using tetrahedrons from MV4202 labs
for(int p = 0; p < (int) pyramid.size(); p++){
glPushMatrix();
glTranslatef(pyramid[p]->getX(), 0.0, pyramid[p]->getZ());
pyramid[p]->draw();
glPopMatrix();
} // end for
// Draw the "friendly" tanks that look like ours
for(int t = 0; t < (int) friendly.size(); t++){
glPushMatrix();
glTranslatef(friendly[t]->getX(), 0.9f, friendly[t]->getZ());
if (!friendly[t]->isDestroyed()) {
friendly[t]->draw();
} else if (friendly[t]->isDestroyed() && friendly[t]->getExplosion()->isGrowing()) {
friendly[t]->draw();
} // end if-else
if (friendly[t]->isDestroyed()) {
friendly[t]->getExplosion()->draw();
if (friendly[t]->getExplosion()->isExplosionDone()) {
delete friendly[t];
friendly.erase(&friendly[t]);
} // end if
} // end if
glPopMatrix();
} // end for
// Draw enemy tanks
for (i = 0; i < (int) enemy.size(); i++) {
glPushMatrix();
glTranslatef(enemy[i]->getX(), 0.5, enemy[i]->getZ());
if (!enemy[i]->isDestroyed()) {
enemy[i]->draw();
} else if (enemy[i]->isDestroyed() && enemy[i]->getExplosion()->isGrowing()) {
enemy[i]->draw();
} // end if-else
if (enemy[i]->isDestroyed()) {
enemy[i]->getExplosion()->draw();
if (enemy[i]->getExplosion()->isExplosionDone()) {
delete enemy[i];
enemy.erase(&enemy[i]);
} // end if
} // end if
glPopMatrix();
} // end for
// Draw houses
for (i = 0; i < (int) house.size(); i++) {
if (!house[i]->isDestroyed()) {
house[i]->draw();
} else if (house[i]->isDestroyed() && house[i]->getExplosion()->isGrowing()) {
house[i]->draw();
} // end if-else
if (house[i]->isDestroyed()) {
glPushMatrix();
glTranslatef(house[i]->getX(), 0, house[i]->getZ());
house[i]->getExplosion()->draw();
glPopMatrix();
if (house[i]->getExplosion()->isExplosionDone()) {
delete house[i];
house.erase(&house[i]);
} // end if
} // end if
} // end for
// Draw pine trees
for(int a = 0; a < (int) pine.size(); a++){
glPushMatrix();
glTranslatef(pine[a]->getX(), 0, pine[a]->getZ());
if (!pine[a]->isDestroyed()) {
pine[a]->draw();
} else if (pine[a]->isDestroyed() && pine[a]->getExplosion()->isGrowing()) {
pine[a]->draw();
} // end if-else
if (pine[a]->isDestroyed()) {
pine[a]->getExplosion()->draw();
if (pine[a]->getExplosion()->isExplosionDone()) {
delete pine[a];
pine.erase(&pine[a]);
} // end if
} // end if
glPopMatrix();
} // end for
// Draw small pine trees
for(int b = 0; b < (int) smallPine.size(); b++){
glPushMatrix();
glTranslatef(smallPine[b]->getX(), 0, smallPine[b]->getZ());
if (!smallPine[b]->isDestroyed()) {
smallPine[b]->draw();
} else if (smallPine[b]->isDestroyed() &&
smallPine[b]->getExplosion()->isGrowing()) {
smallPine[b]->draw();
} // end if-else
if (smallPine[b]->isDestroyed()) {
smallPine[b]->getExplosion()->draw();
if (smallPine[b]->getExplosion()->isExplosionDone()) {
delete smallPine[b];
smallPine.erase(&smallPine[b]);
} // end if
} // end if
glPopMatrix();
} // end for
// Draw silos
for(int sh = 0; sh < (int) silo.size(); sh++){
glPushMatrix();
glTranslatef(silo[sh]->getX(), 0, silo[sh]->getZ());
if (!silo[sh]->isDestroyed()) {
silo[sh]->draw();
} else if (silo[sh]->isDestroyed() && silo[sh]->getExplosion()->isGrowing()) {
silo[sh]->draw();
} // end if-else
if (silo[sh]->isDestroyed()) {
silo[sh]->getExplosion()->draw();
if (silo[sh]->getExplosion()->isExplosionDone()) {
delete silo[sh];
silo.erase(&silo[sh]);
} // end if
} // end if
glPopMatrix();
} // end for
// Draw the shells from the main gun
for (i = 0; i < (int) shells.size(); i++) {
shellCollision = false;
glPushMatrix();
glTranslatef(shells[i]->getX(), shells[i]->getY(), shells[i]->getZ());
glRotatef(-(shells[i]->getAzimuth()), 0, 1, 0);
glTranslatef(-1.0f, -1.75f, -7.5f - shells[i]->getRange());
shells[i]->draw();
glPopMatrix();
// Check for shell collisions with the opponent
// NOTE: PROBABLY NEED TO CHANGE THIS FOR MULTI-PLAYER SO
// THAT THE OPPONENT IS CHECKING FOR COLLISION WITH OUR
// SHELLS
if (multiPlayer && !shellCollision) {
xDist = fabsf(shells[i]->getRealX() + opponent.getX());
zDist = fabsf(shells[i]->getRealZ() + opponent.getZ());
dist = (GLfloat) sqrt(xDist * xDist + zDist * zDist);
if (dist < opponent.getRadius()) {
shellCollision = true;
if (opponent.getDamageCapacity() > 0) {
opponent.takeDamage(shells[i]->getDamage());
} // end if
if (multiPlayer && opponent.getDamageCapacity() > 0) {
Packet *packet = new Packet;
packet->packetType = 3;
packet->damage = shells[i]->getDamage();
int len = sizeof(*packet);
if (m_connectSocket.Send((void*)packet, len) == SOCKET_ERROR) {
CheckError(m_connectSocket.GetLastError());
OnDisconnect();
} // end if
delete packet;