-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
992 lines (906 loc) · 37.7 KB
/
main.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
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
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <cstdio>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <fstream>
#include <string>
#include <time.h>
#include <tetromino.h>
#include <board.h>
#include <texture.h>
#include <utility>
using namespace std;
bool LoadTGA(Texture *, char *); // For TGA Textures
// game variables
Board TetrisBoard;
Tetromino currentTetromino;
int CameraAngle = 0;
int CameraPitch = 0;
int CameraDistance = -30;
int BoardDistance = -20;
int level = 1;
int lines = 0;
int score = 0;
int TimeToDrop = 5000; // in ms, the time required for a piece to drop by one
Texture texture[7]; //Storage for textures
Tetromino nextTetromino;
Tetromino shadowTetromino;
bool gameover = false;
enum { ISHAPE, LSHAPE, JSHAPE, SSHAPE, ZSHAPE, TSHAPE, OSHAPE};
// Used to render font to screen
void renderBitmapString(float x, float y, float z, void *font, char *string)
{
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(font, *c);
}
}
//NOTE: some code here borrowed from TGALoader
bool LoadGLTextures() // Load Bitmaps And Convert To Textures
{
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
bool Status = false; // Status Indicator
// Load The Bitmap, Check For Errors.
if (LoadTGA(&texture[0], "block.tga") && LoadTGA(&texture[1], "logo.tga"))// && LoadTGA(&texture[2], "leftside_02.tga") && LoadTGA(&texture[3], "leftside_03.tga") && LoadTGA(&texture[4], "rightside_01.tga") && LoadTGA(&texture[5], "rightside_02.tga") && LoadTGA(&texture[6], "rightside_03.tga"))
{
Status = true; // Set The Status To TRUE
for (int loop=0; loop<7; loop++) // Loop Through Both Textures
{
// Typical Texture Generation Using Data From The TGA
glGenTextures(1, &texture[loop].texID); // Create The Texture
glBindTexture(GL_TEXTURE_2D, texture[loop].texID);
glTexImage2D(GL_TEXTURE_2D, 0, texture[loop].bpp / 8, texture[loop].width, texture[loop].height, 0, texture[loop].type, GL_UNSIGNED_BYTE, texture[loop].imageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
if (texture[loop].imageData) // If Texture Image Exists
{
free(texture[loop].imageData); // Free The Texture Image Memory
}
}
}
return Status;
}
// true if game is over
// false o.w.
bool checkGameOver()
{
if(TetrisBoard.gameBoard[0][2][19].exists == true || TetrisBoard.gameBoard[1][2][19].exists == true || TetrisBoard.gameBoard[2][2][19].exists == true || TetrisBoard.gameBoard[3][2][19].exists == true || TetrisBoard.gameBoard[2][0][19].exists == true || TetrisBoard.gameBoard[2][1][19].exists == true || TetrisBoard.gameBoard[1][0][19].exists == true || TetrisBoard.gameBoard[1][1][19].exists == true)
return true;
else
return false;
}
// post attach clean up
// create a new tetromino
// check if a row is clearable, if so, then clear it.
// update game information (score, level, lines)
void attachedToBoard()
{
int linesCleared = 0;
for(int z = 0; z < TetrisBoard.depth; z++)
{
bool levelIsClear = true;
for(int x = 0; x < TetrisBoard.width; x++)
{
for(int y = 0; y < TetrisBoard.height; y++)
{
if(TetrisBoard.gameBoard[x][y][z].exists != true)
{
levelIsClear = false;
}
}
}
if(levelIsClear == true)
{
// move all levels down one, therefore deleting the complete level
for(int i = z; i < TetrisBoard.depth-1; i++)
{
for(int x = 0; x < TetrisBoard.width; x++)
{
for(int y = 0; y < TetrisBoard.height; y++)
{
TetrisBoard.gameBoard[x][y][i] = TetrisBoard.gameBoard[x][y][i+1];
}
}
}
//the top most level is a new blank level
for(int x = 0; x < TetrisBoard.width; x++)
{
for(int y = 0; y < TetrisBoard.height; y++)
{
TetrisBoard.gameBoard[x][y][TetrisBoard.depth-1].exists = false;
}
}
linesCleared += 1;
z = -1; // reset our search
}
}
// Update the score
if(linesCleared == 1)
score += (100 * level);
else if(linesCleared == 2)
score += (300 * level);
else if(linesCleared == 3)
score += (500 * level);
else if(linesCleared == 4)
score += (800 * level);
// Update lines and level
for(int i = 0; i < linesCleared; i++)
{
// Update line clear
lines += 1;
// Check if it is a new level
if(lines % 10 == 0)
{
level += 1;
if(TimeToDrop > 600)
TimeToDrop -= 200;
}
}
currentTetromino = nextTetromino;
// check if a new tetromino can be placed, if not, GAME OVER!
if(checkGameOver())
{
TimeToDrop = 0;
gameover = true;
}
else
{
Tetromino newTetromino;
nextTetromino = newTetromino;
}
}
void draw()
{
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glClearColor( 0, 0, 0, 0 );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// Draw the UI
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
// Sets up screen to draw by pixels
glOrtho(0, 800, 0, 600, -1, 1000);
// Draw all of the text areas
glColor3f(1.0f, 1.0f, 1.0f);
if(gameover)
renderBitmapString(250, 25, 0, GLUT_BITMAP_HELVETICA_18, "Game Over! Press F2 to try again.");
renderBitmapString(25, 200, 0, GLUT_BITMAP_HELVETICA_18, "W/S - rotate X axis");
renderBitmapString(25, 175, 0, GLUT_BITMAP_HELVETICA_18, "A/D - rotate Y axis");
renderBitmapString(25, 150, 0, GLUT_BITMAP_HELVETICA_18, "Z/X - rotate Z axis");
renderBitmapString(25, 125, 0, GLUT_BITMAP_HELVETICA_18, "Arrows - move piece");
renderBitmapString(25, 100, 0, GLUT_BITMAP_HELVETICA_18, "Space - hard drop");
renderBitmapString(25, 75, 0, GLUT_BITMAP_HELVETICA_18, "R/F - move camera up/down");
renderBitmapString(25, 50, 0, GLUT_BITMAP_HELVETICA_18, "C/V - move camera left/right");
renderBitmapString(25, 25, 0, GLUT_BITMAP_HELVETICA_18, "F2 - restart game");
renderBitmapString(55, 570, 0, GLUT_BITMAP_HELVETICA_18, "Next Piece");
// Convert ints to strings
string scoreString;
string levelString;
string linesString;
std::ostringstream a;
a << scoreString << "" << score;
scoreString = a.str();
std::ostringstream b;
b << levelString << "" << level;
levelString = b.str();
std::ostringstream c;
c << linesString << "" << lines;
linesString = c.str();
renderBitmapString(650, 375, 0, GLUT_BITMAP_HELVETICA_18, "Score");
renderBitmapString(650, 350, 0, GLUT_BITMAP_HELVETICA_18, &scoreString[0]);
renderBitmapString(650, 300, 0, GLUT_BITMAP_HELVETICA_18, "Level");
renderBitmapString(650, 275, 0, GLUT_BITMAP_HELVETICA_18, &levelString[0]);
renderBitmapString(650, 225, 0, GLUT_BITMAP_HELVETICA_18, "Lines");
renderBitmapString(650, 200, 0, GLUT_BITMAP_HELVETICA_18, &linesString[0]);
// Draw logo
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[1].texID); // logo texture
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 775, 595, 0.2f); // Top Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(647, 595, 0.2f); // Top Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(647, 447, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(775, 447, 0.2f); // Bottom Right Of The Quad
glEnd();
glDisable(GL_TEXTURE_2D);
// Draw the box that will show the "next piece"
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,1.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 175, 555.0f, 0.0f); // Top Right Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(25.0f, 555.0f, 0.0f); // Top Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(25.0f, 405.0f, 0.0f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(175.0f, 405.0f, 0.0f); // Bottom Right Of The Quad
glEnd();
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,0.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 160.0f, 540.0f, 0.1f); // Top Right Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(40.0f, 540.0f, 0.1f); // Top Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(40.0f, 420.0f, 0.1f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(160.0f, 420.0f, 0.1f); // Bottom Right Of The Quad
glEnd();
// Determine and draw the next piece
glEnable(GL_TEXTURE_2D);
if(nextTetromino.type == ISHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,1.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 70.0f, 495.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(40.0f, 495.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(40.0f, 465.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(70.0f, 465.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 100.0f, 495.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(70.0f, 495.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(70.0f, 465.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(100.0f, 465.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 130.0f, 495.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(100.0f, 495.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(100.0f, 465.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(130.0f, 465.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 160.0f, 495.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(130.0f, 495.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(130.0f, 465.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(160.0f, 465.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
else if (nextTetromino.type == LSHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(1.0f,0.65f,0.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 85.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(55.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(55.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 145.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(115.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(145.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 85.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(55.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(55.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(85.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
else if (nextTetromino.type == JSHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 85.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(55.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(55.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 145.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(115.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(145.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 145.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(115.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(145.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
else if (nextTetromino.type == SSHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 85.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(55.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(55.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(85.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 145.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(115.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(145.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
else if (nextTetromino.type == ZSHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 85.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(55.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(55.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 145.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(115.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(145.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
else if (nextTetromino.type == TSHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(0.63f,0.13f,0.94f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 85.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(55.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(55.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(85.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 145.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(115.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(115.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(145.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 115.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(85.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(85.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(115.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
else if (nextTetromino.type == OSHAPE)
{
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS);
glColor3f(1.0f,1.0f,0.0f);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 100.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(70.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(70.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(100.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 130.0f, 510.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(100.0f, 510.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(100.0f, 480.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(130.0f, 480.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 130.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(100.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(100.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(130.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 100.0f, 480.0f, 0.2f); // Top Right Of The Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(70.0f, 480.0f, 0.2f); // Top Left Of The Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(70.0f, 450.0f, 0.2f); // Bottom Left Of The Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(100.0f, 450.0f, 0.2f); // Bottom Right Of The Quad
glEnd();
}
glDisable(GL_TEXTURE_2D);
// Restore the camera
glLoadIdentity();
gluPerspective( 90,1.0,0.1,100000 );
glMatrixMode( GL_MODELVIEW);
glLoadIdentity(); // clear modelview
glPushMatrix(); // save camera
// setup camera, rotate it then move it away from board
glTranslatef(0, BoardDistance, CameraDistance);
glRotatef(CameraPitch, 1, 0, 0);
glRotatef(CameraAngle, 0, 1, 0 );
// draw bottom of board squares
for(int x = 0; x < TetrisBoard.width; x++)
{
for(int y = 0; y < TetrisBoard.height; y++)
{
// save the camera matrix
glPushMatrix();
glTranslatef(2*x, 0, 2*y);
glTranslatef(-TetrisBoard.width+1, 0, -TetrisBoard.height+1); // centers to middle of board
glBegin(GL_LINE_LOOP);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad
glEnd();
// return the camera
glPopMatrix();
}
}
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
// go through each section of the board and check if there is a block there.
// if there is, then draw the block.
for(int x = 0; x < TetrisBoard.width; x++)
{
for(int y = 0; y < TetrisBoard.height; y++)
{
for(int z = 0; z < TetrisBoard.depth; z++)
{
if(TetrisBoard.gameBoard[x][y][z].exists == true)
{
float red = TetrisBoard.gameBoard[x][y][z].red;
float green = TetrisBoard.gameBoard[x][y][z].green;
float blue = TetrisBoard.gameBoard[x][y][z].blue;
// save the camera matrix
glPushMatrix();
glTranslatef( 2*x,2*z,2*y ); // translate to the spot
glTranslatef(-TetrisBoard.width+1, 0, -TetrisBoard.height+1); // centers to middle of board
// Drawing the cube
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS); // using quadrilaterals as primitive
glColor3f(red,green,blue);
glNormal3d(0, 1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(red,green,blue);
glNormal3d(0, -1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(red,green,blue);
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(red,green,blue);
glNormal3d(0, 0, -1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glColor3f(red,green,blue);
glNormal3d(-1, 0, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(red,green,blue);
glNormal3d(1, 0, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();
// restore the camera matrix
glPopMatrix();
}
}
}
}
// Update the shadow Tetromino
shadowTetromino = currentTetromino;
// check if the piece will attach itself to the board (bottom level or any other piece)
while(!shadowTetromino.ShadowDropOne()) // only true if it hits bottom
{
// do nothing, shadow drop increments by one until it hits bottom and returns true
}
// Draw the shadow tetromino
for(int i = 0; i < 4; i++)
{
int x = shadowTetromino.blocks[i].x;
int y = shadowTetromino.blocks[i].y;
int z = shadowTetromino.blocks[i].z;
// gray shadow color
float red = 0.39;
float green = 0.39;
float blue = 0.39;
// save the camera matrix
glPushMatrix();
glTranslatef( 2*x,2*z,2*y ); // translate to the spot
glTranslatef(-TetrisBoard.width+1, 0, -TetrisBoard.height+1); // centers to middle of board
// Drawing the cube
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS); // using quadrilaterals as primitive
glColor3f(red,green,blue); // Color
glNormal3d(0, 1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(red,green,blue); // Color
glNormal3d(0, -1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(red,green,blue); // Color
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(red,green,blue); // Color
glNormal3d(0, 0, -1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glColor3f(red,green,blue); // Color
glNormal3d(-1, 0, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(red,green,blue); // Color
glNormal3d(1, 0, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();
// restore the camera matrix
glPopMatrix();
}
// draw the current tetromino
for(int i = 0; i < 4; i++)
{
int x = currentTetromino.blocks[i].x;
int y = currentTetromino.blocks[i].y;
int z = currentTetromino.blocks[i].z;
float red = currentTetromino.blocks[i].red;
float green = currentTetromino.blocks[i].green;
float blue = currentTetromino.blocks[i].blue;
// save the camera matrix
glPushMatrix();
glTranslatef( 2*x,2*z,2*y ); // translate to the spot
glTranslatef(-TetrisBoard.width+1, 0, -TetrisBoard.height+1); // centers to middle of board
// Drawing the cube
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glBegin(GL_QUADS); // using quadrilaterals as primitive
glColor3f(red,green,blue); // Color
glNormal3d(0, 1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
glColor3f(red,green,blue); // Color
glNormal3d(0, -1, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)
glColor3f(red,green,blue); // Color
glNormal3d(0, 0, 1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)
glColor3f(red,green,blue); // Color
glNormal3d(0, 0, -1);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)
glColor3f(red,green,blue); // Color
glNormal3d(-1, 0, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)
glColor3f(red,green,blue); // Color
glNormal3d(1, 0, 0);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd();
// restore the camera matrix
glPopMatrix();
}
// restore camera matrix
glPopMatrix();
glutSwapBuffers();
glFlush();
}
void processNormalKeys(unsigned char key, int x, int y)
{
//cout << (int)key << endl;
if((int)key == 114) //r
{
if(CameraPitch == 70)
{
CameraPitch = 70;
}
else
{
CameraPitch += 10;
CameraDistance -= 2;
BoardDistance += 1;
}
}
if((int)key == 102) //f
{
if(CameraPitch == 0)
{
CameraPitch = 0;
}
else
{
CameraPitch += -10;
CameraDistance += 2;
BoardDistance -=1;
}
}
if((int)key == 99) //c
{
if(CameraAngle == 0)
CameraAngle = 315;
else
{
CameraAngle += -45;
}
}
if((int)key == 118) //v
{
if(CameraAngle == 315)
CameraAngle = 0;
else
{
CameraAngle += 45;
}
}
if(!gameover)
{
if((int)key == 120) //x
{
currentTetromino.RotateZCW();
}
if((int)key == 122) //z
{
currentTetromino.RotateZCCW();
}
if((int)key == 97) //a
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.RotateYCW();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.RotateXCW();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.RotateYCCW();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.RotateXCCW();
}
}
if((int)key == 100) //d
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.RotateYCCW();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.RotateXCCW();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.RotateYCW();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.RotateXCW();
}
}
if((int)key == 119) //w
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.RotateXCW();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.RotateYCCW();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.RotateXCCW();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.RotateYCW();
}
}
if((int)key == 115) //s
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.RotateXCCW();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.RotateYCW();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.RotateXCW();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.RotateYCCW();
}
}
if((int)key == 32) //space
{
currentTetromino = shadowTetromino;
bool attached = currentTetromino.DropOne();
if (attached)
{
attachedToBoard();
}
}
}
draw();
}
void processSpecialKeys(int key, int x, int y) {
//cout << (int)key << endl;
if(key == GLUT_KEY_UP)
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.MoveBack();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.MoveRight();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.MoveForward();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.MoveLeft();
}
}
else if(key == GLUT_KEY_RIGHT)
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.MoveRight();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.MoveForward();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.MoveLeft();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.MoveBack();
}
}
else if(key == GLUT_KEY_DOWN)
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.MoveForward();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.MoveLeft();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.MoveBack();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.MoveRight();
}
}
else if(key == GLUT_KEY_LEFT)
{
if(CameraAngle == 0 || CameraAngle == 315)
{
currentTetromino.MoveLeft();
}
else if(CameraAngle == 90 || CameraAngle == 45)
{
currentTetromino.MoveBack();
}
else if(CameraAngle == 180 || CameraAngle == 135)
{
currentTetromino.MoveRight();
}
else if(CameraAngle == 270 || CameraAngle == 225)
{
currentTetromino.MoveForward();
}
}
else if(key == GLUT_KEY_F2)
{
// Reset the game
gameover = false;
level = 1;
lines = 0;
score = 0;
TimeToDrop = 5000;
Tetromino newCurrentTetromino;
Tetromino newNextTetromino;
currentTetromino = newCurrentTetromino;
nextTetromino = newNextTetromino;
TetrisBoard.clearBoard();
}
draw();
}
void IncrementGameTime( int value )
{
// check if it was attached to the board after dropping by one
bool attached = currentTetromino.DropOne();
if (attached)
{
attachedToBoard();
}
glutPostRedisplay();
glutTimerFunc(TimeToDrop, IncrementGameTime, 1);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( 800,600 );
glutCreateWindow( "3D Tetris --- Bryan Denny" );
glutDisplayFunc(draw);
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
glutTimerFunc(TimeToDrop, IncrementGameTime, 1 );
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); //fixes some depth issues
// enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
//GLfloat position[] = { -1.5f, 1.0f, -4.0f, 1.0f };
// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
//glLightfv(GL_LIGHT0, GL_POSITION, position);
// Enable/Load textures
glEnable( GL_TEXTURE_2D );
LoadGLTextures();
glutMainLoop();
}