-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tetris.cpp
executable file
·1128 lines (911 loc) · 35.4 KB
/
Tetris.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
993
994
995
996
997
998
999
1000
// C Lib for random number
#include <cstdio> /* NULL */
#include <cstdlib> /* srand, rand */
#include <ctime> /* time */
// C++
#include <iostream>
#include <map>
using namespace std;
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// freeglut
# include <GL/freeglut.h>
# include <GL/freeglut_ext.h>
// GLM lib for matrix calculation
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
// SOIL lib for loading pictures
#include <SOIL/SOIL.h>
// FreeType lib for loading fonts
#include <ft2build.h>
#include FT_FREETYPE_H
// Personal classes
#include <shader.h> // My shader class
#include <camera.h> // My camera class
#include <cube.h> // My Cube class
#include <tile.h> // My Tetris Tile class
#include <window.h> // My game Window class
#include <robot.h> // My Robot Arm class
// ---------- Define global variables ------------
// -----------------------------------------------
// Cannot use initialisers here which require the use of opengl functions.
// Because glewInit() hasn't been called yet.
// -----------------------------------------------
// ---- 3D basics ------
Shader tileShader,gridShader,robotShader,lightShader, textShader;
GLuint tileVAO,gridVAO,robotVAO,lightVAO,textVAO;
GLuint textVBO;
GLuint texture[5];
int screenWidth;
int screenHeight;
Camera myCamera(glm::vec3(5.0f,10.0f,0.0f)); // Define camera with position
GLfloat lastX,lastY;
bool firstMouse = true;
bool keys[1024];
GLfloat lastFrame = 0.0f;
bool camLeft,camRight,camUp,camDown;
// ------ Tile --------------
const int CUBENUM = 4;
const int TOTFORMS = 3;
const int transNum[TOTFORMS] = {2,2,4};
glm::ivec3 forms[TOTFORMS][4] = {
{
glm::ivec3(-2,0,0),
glm::ivec3(-1,0,0),
glm::ivec3(0,0,0),
glm::ivec3(1,0,0)
},
{
glm::ivec3(-1,-1,0),
glm::ivec3(0,-1,0),
glm::ivec3(0,0,0),
glm::ivec3(1,0,0)
},
{
glm::ivec3(-1,-1,0),
glm::ivec3(-1,0,0),
glm::ivec3(0,0,0),
glm::ivec3(1,0,0)
}
};
const int TOTCOLORS = 5;
glm::vec3 colors[TOTCOLORS] = {
glm::vec3(1.0f,0.0f,1.0f), // Purple, Grape
glm::vec3(1.0f,0.0f,0.0f), // Red, Apple
glm::vec3(1.0f,1.0f,0.0f), // Yellow, Banana
glm::vec3(0.0f,1.0f,0.0f), // Green, Pear
glm::vec3(1.0f,0.648f,0.0f) // Orange, Orange
};
Tile myTile;
bool hasTile;
// --------Game Window----------
Window myWindow;
bool hasFreeTile;
TileLink* freeTiles;
GLfloat interval = 0.0f;
bool gameSuspend = false;
bool gameEnd = false;
bool gameRestart = false;
// ---------Robot Arm-----------
Robot robot;
bool tileOnTip;
// lighting
glm::vec3 lightColor(1.0f,1.0f,1.0f);
glm::vec3 lightPos;
glm::vec3 lightScale(2.0f,2.0f,2.0f);
// text
struct Character{
GLuint TextureID;
glm::ivec2 Size;
glm::ivec2 Bearing;
GLuint Advance;
};
std::map<GLchar, Character> Characters;
// extra game logic
char countdownText[20];
int secondsLeft;
const int TOTSECONDS = 5;
float pastTime;
char gameOverText[30];
//------------------------------------------------------
Tile newTile(int x1,int x2){
Cube cubes[CUBENUM+1];
int form = rand() % TOTFORMS;
int trans = rand() % transNum[form];
// make a tile of original form
// --------------------------
// make a cube array of form "form"
for(int i=0;i<CUBENUM;i++)
cubes[i] = Cube(forms[form][i],rand() % TOTCOLORS);
// get random position in [x1,x2)
glm::ivec3 pos(rand() % (x2-x1) + x1, 20,0);
// make a tile
Tile tile(CUBENUM,cubes,form,pos);
// rotate the tile according to trans
// ---------------------------
for(int i=0;i<trans;i++)
tile.RotateRight(transNum[form]);
return tile;
}
void SetUpTexture(GLuint &texture, const char path[]){
// Load and create a texture
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
// Set our texture parameters
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
// Set texture filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// Load, create texture and generate mipmaps
int width, height;
unsigned char* image = SOIL_load_image(path,&width,&height,0,SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_UNSIGNED_BYTE,image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D,0);
}
void initFont(){
FT_Library ft;
if(FT_Init_FreeType(&ft))
std::cout << "ERROR::FREETYPE: Could not init FreeType Library" << std::endl;
FT_Face face;
if(FT_New_Face(ft,"fonts/arial.ttf",0,&face))
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
FT_Set_Pixel_Sizes(face,0,48);
glPixelStorei(GL_UNPACK_ALIGNMENT,1); // Disable byte-alignment restriction
for(GLubyte c = 0; c < 128; c++){
// Load character glyph
if(FT_Load_Char(face,c,FT_LOAD_RENDER)){
std::cout << "ERROR::FREETYPE: Failed to load Glyph" << std::endl;
continue;
}
// Generate texture
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// Set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Now store character for later use
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
Characters.insert(std::pair<GLchar, Character>(c,character));
}
glBindTexture(GL_TEXTURE_2D,0);
FT_Done_Face(face);
FT_Done_FreeType(ft);
}
void renderText(Shader &s, std::string text, GLfloat x, GLfloat y, GLfloat scale, glm::vec3 color){
// Activate corresponding render state
s.Use();
glUniform3fv(glGetUniformLocation(s.Program,"textColor"), 1, glm::value_ptr(color));
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(textVAO);
// Iterate through all characters in string
std::string::const_iterator c;
for(c = text.begin(); c != text.end(); c++){
Character ch = Characters[*c];
GLfloat xpos = x + ch.Bearing.x * scale;
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
GLfloat w = ch.Size.x * scale;
GLfloat h = ch.Size.y * scale;
// Update text VBO
GLfloat vertices[6][4] = {
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos, ypos, 0.0, 1.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos, ypos + h, 0.0, 0.0 },
{ xpos + w, ypos, 1.0, 1.0 },
{ xpos + w, ypos+h, 1.0, 0.0 }
};
// Render glyph texture over quad
glBindTexture(GL_TEXTURE_2D,ch.TextureID);
glBindBuffer(GL_ARRAY_BUFFER,textVBO);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER,0);
// Render quad
glDrawArrays(GL_TRIANGLES,0,6);
// Now advance cursors for next glyph (note that advance is number of 1/64 pixels)
x += ( (ch.Advance >> 6) * scale); // Bitshift by 6 to get value in pixels (2^6 = 64)
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D,0);
}
void init(){
// initialize Font textures
initFont();
// Create shader program
// ---------------------------------
tileShader = Shader( "shader/vshader.glsl", "shader/fshader.glsl" );
gridShader = Shader("shader/vshaderGrid.glsl","shader/fshaderGrid.glsl");
robotShader = Shader("shader/robotVshader.glsl","shader/robotFshader.glsl");
lightShader = Shader("shader/lightVshader.glsl","shader/lightFshader.glsl");
textShader = Shader("shader/textVshader.glsl","shader/textFshader.glsl");
//tileShader.Use();
// Set up our vertex data
GLfloat vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f
};
// Set up the grid data for the game window
GLfloat gridVertices[] = {
-0.5f,0.5f,0.5f,
-0.5f,-0.5f,0.5f,
0.5f,-0.5f,0.5f,
0.5f,0.5f,0.5f,
-0.5f,0.5f,-0.5f,
-0.5f,-0.5f,-0.5f,
0.5f,-0.5f,-0.5f,
0.5f,0.5f,-0.5f
};
// Grid indices for element array of gridVertives
GLuint gridIndices[] = {
0,1,
1,2,
2,3,
3,0,
4,5,
5,6,
6,7,
7,4,
0,4,
1,5,
2,6,
3,7
};
// Set up vertex arrays and buffers for the tile.
// ---------------------------------
GLuint VBO;
glGenVertexArrays(1, &tileVAO);
glGenBuffers(1, &VBO);
// Bind our Vertex Array Object first, then bind and set our buffers and pointers.
glBindVertexArray(tileVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint position = glGetAttribLocation(tileShader.Program, "position" );
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(position);
GLuint texCoord = glGetAttribLocation(tileShader.Program, "texCoord" );
glVertexAttribPointer(texCoord, 2, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(texCoord);
// Unbind tileVAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
glBindVertexArray(0);
// Set up robot VAO
glGenVertexArrays(1,&robotVAO);
glBindVertexArray(robotVAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
position = glGetAttribLocation(robotShader.Program,"position");
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(position);
GLuint normal = glGetAttribLocation(robotShader.Program,"normal");
glVertexAttribPointer(normal, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*) (5 * sizeof(GLfloat)));
glEnableVertexAttribArray(normal);
glBindVertexArray(0);
// Set up light VAO
glGenVertexArrays(1,&lightVAO);
glBindVertexArray(lightVAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
position = glGetAttribLocation(robotShader.Program,"position");
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(position);
glBindVertexArray(0);
// Set up grid VAO
// ---------------------------------
GLuint EBO;
glGenVertexArrays(1, &gridVAO);
glGenBuffers(1,&VBO);
glGenBuffers(1,&EBO);
// Bind our Vertex Array Object first, then bind and set our buffers and pointers.
glBindVertexArray(gridVAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(gridVertices), gridVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(gridIndices), gridIndices, GL_STATIC_DRAW);
position = glGetAttribLocation(gridShader.Program, "position" );
glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(position);
// Unbind tileVAO (it's always a good thing to unbind any buffer/array to prevent strange bugs)
glBindVertexArray(0);
// Set up textures
// ---------------------------------
SetUpTexture(texture[0], "./texture/grape.png");
SetUpTexture(texture[1], "./texture/apple.png");
SetUpTexture(texture[2], "./texture/banana.png");
SetUpTexture(texture[3], "./texture/pear.png");
SetUpTexture(texture[4], "./texture/orange.png");
//SetUpTexture(texture[2], "./texture/grape.png");
//SetUpTexture(texture[3], "./texture/apple.png");
//SetUpTexture(texture[4], "./texture/grape.png");
// Set up the text VAO
// ------------------------------------
// use text shader
textShader.Use();
// Set up the text VAO;
glGenVertexArrays(1, & textVAO);
glBindVertexArray(textVAO);
glGenBuffers(1, &textVBO);
glBindBuffer(GL_ARRAY_BUFFER,textVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
GLuint vertex = glGetAttribLocation(textShader.Program, "vertex");
glVertexAttribPointer(vertex, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(vertex);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindVertexArray(0);
// ----------------------------------------------
}
void loadTile(){
myTile = newTile(2,myWindow.width - 1 - 2);
myTile.SetPos(robot.tipPosDiscrete);
tileOnTip = true;
hasTile = false;
secondsLeft = TOTSECONDS;
sprintf(countdownText,"%d seconds left",secondsLeft);
pastTime = 0;
}
void dropTile(){
tileOnTip = false;
hasTile = true;
}
void gameInit(){
myWindow = Window();
freeTiles = NULL;
hasFreeTile = false;
robot = Robot(-5,12,12);
loadTile();
interval = 0.0f;
gameSuspend = gameEnd = gameRestart = false;
strcpy(gameOverText,"");
}
void restartGame(){
myWindow = Window();
for(TileLink* l=freeTiles;l!=NULL;){
TileLink* tmp = l->next;
delete l;
l = tmp;
}
freeTiles = NULL;
hasFreeTile = false;
loadTile();
interval = 0.0f;
gameSuspend = gameEnd = gameRestart = false;
strcpy(gameOverText,"");
}
void keyboard( unsigned char key, int x, int y ){
switch( key ) {
case 033: // Escape Key
case 'q': case 'Q':
exit( EXIT_SUCCESS );
break;
case 'z': case 'Z':
gameSuspend = !gameSuspend;
break;
case 'r': case 'R':
gameRestart = true;
case ' ':
//if(hasTile) myTile.Shuffle();
if(tileOnTip && myWindow.CheckTile(myTile)){
dropTile();
}
else if(hasTile){
//printf("Space detected\n");
myTile.Shuffle();
/*
int controlKey = glutGetModifiers();
if( controlKey & GLUT_ACTIVE_CTRL){
myTile.Shuffle();
printf("CTRL detected\n");
}
*/
}
break;
default: keys[key] = true;
}
}
void keyboardUp ( unsigned char key, int x, int y ){
keys[key] = false;
}
void specialKey ( int key, int x, int y){
//cout << "specialKey" << endl;
int controlKey = glutGetModifiers();
if( controlKey & GLUT_ACTIVE_CTRL){
if(key == GLUT_KEY_UP) camUp = true;
if(key == GLUT_KEY_DOWN) camDown = true;
if(key == GLUT_KEY_LEFT) camLeft = true;
if(key == GLUT_KEY_RIGHT) camRight = true;
}
else{
camUp = camDown = camLeft = camRight = false;
if(! hasTile) return;
/*
if(key == GLUT_KEY_LEFT){
myTile.Left();
if(! myWindow.CheckTile(myTile)) myTile.Right();
}
*/
/*
if(key == GLUT_KEY_RIGHT){
myTile.Right();
if(! myWindow.CheckTile(myTile)) myTile.Left();
}
*/
if(key == GLUT_KEY_DOWN){
myTile.Down();
if(! myWindow.CheckTile(myTile)) myTile.Up();
}
if(key == GLUT_KEY_UP){
int totTrans = transNum[myTile.form];
if(totTrans == 4){
myTile.RotateRight(totTrans);
if(! myWindow.CheckTile(myTile)) myTile.RotateLeft(totTrans);
}
else{
if(myTile.trans == totTrans-1){
myTile.RotateLeft(totTrans);
if(! myWindow.CheckTile(myTile)) myTile.RotateRight(totTrans);
}
else{
myTile.RotateRight(totTrans);
if(! myWindow.CheckTile(myTile)) myTile.RotateLeft(totTrans);
}
}
}
}
}
void specialKeyUp (int key, int x,int y){
if(key == GLUT_KEY_UP) camUp = false;
if(key == GLUT_KEY_DOWN) camDown = false;
if(key == GLUT_KEY_LEFT) camLeft = false;
if(key == GLUT_KEY_RIGHT) camRight = false;
}
void Do_Movement(GLfloat deltaTime){ // Update camera position
//if(keys['w']) myCamera.Move(FORWARD,deltaTime,1); // Move on the Ground
if(camUp) myCamera.Move(UP,deltaTime);
//if(keys['s']) myCamera.Move(BACKWARD,deltaTime,1); // Move on the Ground
if(camDown) myCamera.Move(DOWN,deltaTime);
if(camLeft) myCamera.Move(LEFT,deltaTime);
if(camRight) myCamera.Move(RIGHT,deltaTime);
// Move Robot Arm
bool changePos=false;
if(keys['a']) changePos = robot.Left(deltaTime);
if(keys['d']) changePos = robot.Right(deltaTime);
if(keys['w']) changePos = robot.Up(deltaTime);
if(keys['s']) changePos = robot.Down(deltaTime);
if(tileOnTip && changePos) myTile.SetPos(robot.tipPosDiscrete);
}
void mouse(int button,int state,int x,int y){ // Update camera aspect
GLfloat sensitivity = 5.0;
if( button == 3 || button == 4){
if( state == GLUT_DOWN){
if(button == 3) myCamera.Zoom(-sensitivity);
if(button == 4) myCamera.Zoom(sensitivity);
}
}
}
void reshape( int width, int height ){ // Update viewport
screenWidth = width;
screenHeight = height;
glViewport( 0, 0, width, height );
}
void display(void){ // Render
// Clear the color buffer and depth buffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update camera position
GLfloat currentFrame = (float) glutGet(GLUT_ELAPSED_TIME) /1000;
GLfloat deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
Do_Movement(deltaTime);
// Update seconds left
if(tileOnTip){
pastTime += deltaTime;
if(pastTime >= 1.0){
pastTime = 0.0;
secondsLeft--;
sprintf(countdownText,"%d seconds left",secondsLeft);
if(secondsLeft == 0){
if(myWindow.CheckTile(myTile))
dropTile();
else{
myTile.SetPos(glm::vec3(5,20,0));
dropTile();
}
}
}
}
if(gameRestart) restartGame();
// Update tile position
// --------------------
if(! gameSuspend && ! gameEnd) interval += deltaTime;
// every interval update tile state
if(interval >= 0.6 && ! tileOnTip) {
if(hasTile){ // tile exists, so try to drop down 1 slot, and detect if collides
myTile.Down();
if(! myWindow.CheckTile(myTile)){
myTile.Up();
hasTile = false;
myWindow.AddTile(myTile);
myWindow.Detect();
myWindow.Eliminate();
freeTiles = myWindow.CheckFreeTile(freeTiles);
if(freeTiles != NULL) hasFreeTile = true;
else hasFreeTile = false;
if(! myWindow.CheckEnd()){
gameEnd = true;
printf("Game Ends: Over Stack!\n");
sprintf(gameOverText,"Game Over!");
}
#ifdef DEBUG
if(freeTiles != NULL){
printf("---FreeTiles---\n");
for(TileLink* l=freeTiles; l!=NULL; l=l->next){
for(int i=0;i<l->tile.num;i++){
glm::ivec3 pos = l->tile.GetPos(i);
printf(" (%d,%d)",pos.x,pos.y);
}
printf("\n");
}
printf("---------------\n");
}
else printf(":: No free tiles\n");
#endif
}
}
else if(! hasFreeTile){ // no tile, and no free tile, then bear a new player tile
loadTile();
#ifdef DEBUG
printf("New Tile : ");
for(int i=0;i<myTile.num;i++){
glm::ivec3 pos = myTile.GetPos(i);
printf("(%d,%d) ",pos.x,pos.y);
}
printf("\n");
#endif
/*
hasTile = true;
if(! myWindow.CheckTile(myTile)){
myTile.Up();
gameEnd = true;
printf("Game Ends: No place to generate new tile!\n");
}
*/
}
else{ // has free tiles, then drop free tiles and detect collides
// Try to move all free tiles down 1 position
// and remove tile no longer free
TileLink* pre = NULL;
for(TileLink* l=freeTiles; l!=NULL;){
l->tile.Down();
if(! myWindow.CheckTile(l->tile)){
l->tile.Up();
myWindow.AddTile(l->tile);
if(pre != NULL){
pre->next = l->next;
delete l;
l = pre->next;
}
else{
l = l->next;
TileLink* tmp = freeTiles;
freeTiles = freeTiles->next;
delete tmp;
}
}
else{
pre = l;
l = l->next;
}
}
// Add new free tiles
myWindow.Detect();
myWindow.Eliminate();
freeTiles = myWindow.CheckFreeTile(freeTiles);
if(freeTiles != NULL) hasFreeTile = true;
else hasFreeTile = false;
#ifdef DEBUG
if(freeTiles != NULL){
printf("---FreeTiles---\n");
for(TileLink* l=freeTiles; l!=NULL; l=l->next){
for(int i=0;i<l->tile.num;i++){
glm::ivec3 pos = l->tile.GetPos(i);
printf(" (%d,%d)",pos.x,pos.y);
}
printf("\n");
}
printf("---------------\n");
}
else printf(":: No free tiles\n");
#endif
}
// renew interval
interval = 0.0f;
}
// Set the transform matrices
glm::mat4 model;
glm::mat4 view;
view = myCamera.GetViewMatrix();
glm::mat4 projection;
projection = glm::perspective(glm::radians(myCamera.Aspect), (float)screenWidth / screenHeight,0.1f,1000.0f);
glm::vec3 color;
int type;
// Draw the tile
// -----------------------------------
// Use shader program
tileShader.Use();
// Active and bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture[0]);
glUniform1i(glGetUniformLocation(tileShader.Program,"ourTexture0"),0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,texture[1]);
glUniform1i(glGetUniformLocation(tileShader.Program,"ourTexture1"),1);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D,texture[2]);
glUniform1i(glGetUniformLocation(tileShader.Program,"ourTexture2"),2);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D,texture[3]);
glUniform1i(glGetUniformLocation(tileShader.Program,"ourTexture3"),3);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D,texture[4]);
glUniform1i(glGetUniformLocation(tileShader.Program,"ourTexture4"),4);
GLuint modelLoc = glGetUniformLocation(tileShader.Program,"model");
// model matrix will be calculated later
GLuint viewLoc = glGetUniformLocation(tileShader.Program,"view");
glUniformMatrix4fv(viewLoc,1,GL_FALSE,glm::value_ptr(view));
GLuint projectionLoc = glGetUniformLocation(tileShader.Program,"projection");
glUniformMatrix4fv(projectionLoc,1,GL_FALSE,glm::value_ptr(projection));
GLuint colorLoc = glGetUniformLocation(tileShader.Program,"myColor");
GLuint typeLoc = glGetUniformLocation(tileShader.Program,"type");
// Draw the tile
glBindVertexArray(tileVAO);
//bool inWindow = 0;
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
// Draw the game window tiles with the same shader program and VAO
for(int i=0;i<myWindow.width;i++)
for(int j=0;j<myWindow.height;j++) if(myWindow.bitmap[i][j]){
type = myWindow.type[i][j];
glUniform1i(typeLoc,type);
color = colors[myWindow.type[i][j]];
glUniform3fv(colorLoc,1,glm::value_ptr(color));
model = glm::mat4();
model = glm::translate(model,glm::vec3(i,j,0));
model = glm::scale(model,glm::vec3(0.95f,0.95f,0.95f));
glUniformMatrix4fv(modelLoc,1,GL_FALSE,glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES,0,36);
//glDrawElements(GL_LINES,24, GL_UNSIGNED_INT, 0);
}
// Draw the tile
if(hasTile || tileOnTip){
for(int i=0;i<myTile.num; i++){
glm::vec3 pos = myTile.GetPos(i);
if(pos.x < 0 || pos.x >= myWindow.width || pos.y < 0 || pos.y >= myWindow.height)
continue;
type = myTile[i].type;
glUniform1i(typeLoc,type);
color = colors[myTile[i].type];
if(myWindow.bitmap[int(pos.x)][int(pos.y)]) {
color = glm::vec3(0.75f,0.75f,0.75f);
//printf("collision detected\n");
}
glUniform3fv(colorLoc,1,glm::value_ptr(color));
model = glm::mat4();
model = glm::translate(model,pos);
model = glm::scale(model,glm::vec3(0.96f,0.96f,0.96f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}
// Draw the free tiles
for(TileLink* l=freeTiles; l!=NULL; l=l->next ){
for(int i=0;i<l->tile.num;i++){
glm::vec3 pos = l->tile.GetPos(i);
if(pos.x < 0 || pos.x >= myWindow.width || pos.y < 0 || pos.y >= myWindow.height)
continue;
type = l->tile[i].type;
glUniform1i(typeLoc,type);
color = colors[l->tile[i].type];
glUniform3fv(colorLoc,1,glm::value_ptr(color));
model = glm::mat4();
model = glm::translate(model,pos);
model = glm::scale(model,glm::vec3(0.95f,0.95f,0.95f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 36);
}
}
glBindVertexArray(0);
// ---------------------------------------------------------------
// Draw the grid
// --------------------------------------------------------------
gridShader.Use();
// Get location
modelLoc = glGetUniformLocation(gridShader.Program,"model");
viewLoc = glGetUniformLocation(gridShader.Program,"view");
projectionLoc = glGetUniformLocation(gridShader.Program,"projection");
// Set matrices
glUniformMatrix4fv(viewLoc,1,GL_FALSE,glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc,1,GL_FALSE,glm::value_ptr(projection));
glBindVertexArray(gridVAO);
for(int i=0;i<myWindow.width;i++)
for(int j=0;j<myWindow.height;j++){
model = glm::mat4();
model = glm::translate(model,glm::vec3(i,j,0));
glUniformMatrix4fv(modelLoc,1,GL_FALSE,glm::value_ptr(model));
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glDrawArrays(GL_TRIANGLES,0,36);
glDrawElements(GL_LINES,24, GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
// Draw the robot arm
// -----------------------------------------------------
robotShader.Use();
// vertex shader matrices
modelLoc = glGetUniformLocation(robotShader.Program,"model");
viewLoc = glGetUniformLocation(robotShader.Program,"view");
glUniformMatrix4fv(viewLoc,1,GL_FALSE,glm::value_ptr(view));
projectionLoc = glGetUniformLocation(robotShader.Program,"projection");
glUniformMatrix4fv(projectionLoc,1,GL_FALSE,glm::value_ptr(projection));
// fragment shader matrices
GLint objectColorLoc = glGetUniformLocation(robotShader.Program, "objectColor");
GLint lightColorLoc = glGetUniformLocation(robotShader.Program, "lightColor");
glUniform3fv(lightColorLoc,1, glm::value_ptr(lightColor));
GLint lightPosLoc = glGetUniformLocation(robotShader.Program,"lightPos");
model = glm::mat4();
model = glm::translate(model, glm::vec3(5.0f,10.0f,5.0f));
model = glm::rotate(model, glm::radians((float) glutGet(GLUT_ELAPSED_TIME) /1000 * 25.0f), glm::vec3(0.0f,0.0f,1.0f));
model = glm::translate(model, glm::vec3(14.0f,0.0f,0.0f));
lightPos = glm::vec3(model * glm::vec4(0.0f,0.0f,0.0f,1.0f));
glUniform3fv(lightPosLoc,1, glm::value_ptr(lightPos));
GLuint viewPosLoc = glGetUniformLocation(robotShader.Program,"viewPos");
glm::vec3 viewPos = myCamera.Position;
glUniform3fv(viewPosLoc,1,glm::value_ptr(viewPos));
GLuint blendLoc = glGetUniformLocation(robotShader.Program,"blend");
int blend;
// bind VAO
glBindVertexArray(robotVAO);