-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
262 lines (224 loc) · 7.72 KB
/
main.cpp
File metadata and controls
262 lines (224 loc) · 7.72 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
#include "pieces.h"
float lpos[4] = {1,0.5,1,0};
int screen_width = 850,screen_height = 850;
//Some piece variables
Piece* board;
Piece* selected_piece = NULL;
//helper function to print grid 2d array variable
void print_grid_pieces(void){
for(int row = 0; row <= 7; row++){
for(int col = 0; col <= 7; col++){
int element = grid_pieces[row][col];
if(element > 0){
printf("[%i]",element);
}else if(element == 0){
printf("[0000]");
}
}
printf("\n");
}
printf("\n");
}
void loadPieces(){
pieces.push_back(new King("data/models/king.dae","data/textures/white_king.jpg",0,'e',1));
pieces.push_back(new King("data/models/king.dae","data/textures/black_king.jpg",1,'e',8));
pieces.push_back(new Queen("data/models/queen.dae","data/textures/white_queen.jpg",2,'d',1));
pieces.push_back(new Queen("data/models/queen.dae","data/textures/black_queen.jpg",3,'d',8));
for(int i = 0; i <= 1; i++){
pieces.push_back(new Rook("data/models/rook.dae","data/textures/white_rook.jpg",4,column[i*7],1)); //white rooks
pieces.push_back(new Rook("data/models/rook.dae","data/textures/black_rook.jpg",5,column[i*7],8)); //black rooks
pieces.push_back(new Bishop("data/models/bishop.dae","data/textures/white_bishop.jpg",6,column[(i*3)+2],1)); //white bishops
pieces.push_back(new Bishop("data/models/bishop.dae","data/textures/black_bishop.jpg",7,column[(i*3)+2],8)); //black bishops
pieces.push_back(new Knight("data/models/knight.dae","data/textures/white_knight.jpg",8,column[(i*5)+1],1)); //white knights
pieces.push_back(new Knight("data/models/knight.dae","data/textures/black_knight.jpg",9,column[(i*5)+1],8)); //black knights
}
for(int i = 0; i <= 7; i++){
pieces.push_back(new Pawn("data/models/pawn.dae","data/textures/white_pawn.jpg",10,column[i],2)); //white pawns
pieces.push_back(new Pawn("data/models/pawn.dae","data/textures/black_pawn.jpg",11,column[i],7)); //black pawns
}
board = new Piece("data/models/board.dae","data/textures/board.jpg",12,'z',0); //chess board
printf("loaded starting pieces\n");
}
void drawBoard(){
board->draw();
}
void drawPieces(){
for(int i = 0; i <= pieces.size()-1; i++){ //draw pieces on the board
glLoadName(1+i);
pieces.at(i)->draw();
}
int white_counter = 0, black_counter = 0;
for(int j = 0; j < side_pieces.size(); j++){ //draw dead pieces on the side
Piece* temp = side_pieces.at(j);
if(temp->color == BLACK){
if(black_counter <= 7){ //front row
temp->draw(true, -11.3, 8-(2*black_counter));
}else{ //back row
temp->draw(true, -13.3, 8-(2*(black_counter-8)));
}
black_counter++;
}else if(temp->color == WHITE){
if(white_counter <= 7){ //front row
temp->draw(true, 11.2, 8-(2*white_counter));
}else{ //back row
temp->draw(true, 13.2, 8-(2*(white_counter-8)));
}
white_counter++;
}
}
}
void draw(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,25,15,
0,0,0,
0,1,0);
glLightfv(GL_LIGHT0,GL_POSITION,lpos);
glLoadName(99);
drawBoard();
drawGrid();
drawPieces();
glutSwapBuffers();
}
void resize(int w, int h){
if(h == 0)
h = 1;
float ratio = w*1.0f/h;
screen_width = w;
screen_height = h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0,0,w,h);
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
}
void keyEvents(unsigned char key, int x, int y){
switch(key){
case 27: //escape key
glDetachShader(p,v);
glDetachShader(p,f);
glDeleteShader(v);
glDeleteShader(f);
glDeleteProgram(p);
for(int i = 0; i < pieces.size(); i++){
free(pieces.at(i));
}
printf("shaders freed\n");
exit(0);
break;
}
}
void specialKeys(int key, int x, int y){
//no events to handle here yet...
}
void initGL(void){
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(10);
}
//calls pick function on the piece at the position specified
void pickPiece(int col, int row){
Piece* piece = piece_at(col,row);
if(piece != NULL){
if((piece->color == BLACK && gamestate == BLACK_TURN) || (piece->color == WHITE && gamestate == WHITE_TURN)){
for(int j = 0; j <= pieces.size()-1; j++){
pieces.at(j)->unpick();
}
piece->pick();
selected_piece = piece;
}
}
}
//swaps turns from white to black or black to white
void swap_turns(void){
if(gamestate == WHITE_TURN){
gamestate = BLACK_TURN;
}else if(gamestate == BLACK_TURN){
gamestate = WHITE_TURN;
}
}
//processes which piece is clicked and which tile is clicked
void list_hits(GLint hits, GLuint *names){
for(int i = 0; i <= pieces.size()-1; i++){
pieces.at(i)->unpick();
}
bool move_pressed = false;
for (int i = 0; i < hits; i++){
int name = (GLubyte)names[i*4+3];
if(name == 165){
move_pressed = true;
break;
}
if(name >= 101){
name -= 100;
grid_row = 9-(name%8);
if(grid_row == 9)
grid_row = 1;
grid_col = ceil((float)name/8.0f);
grid_column = column[grid_col-1];
printf("row = %i col = %i name = %i\n",grid_row,grid_col,name);
}
}
if(move_pressed){
if((grid_pieces[grid_row-1][grid_col-1] == BLACK && gamestate == WHITE_TURN) || (grid_pieces[grid_row-1][grid_col-1] == WHITE && gamestate == BLACK_TURN)) //if the possible move is a capture move
remove_piece(grid_col,grid_row); //... then remove the piece that's eaten
selected_piece->move((unsigned int)grid_col,(unsigned int)grid_row);
swap_turns();
clearMovesList();
selected_piece->unpick();
}
pickPiece(grid_col,grid_row);
}
//sets up gl select mode
void gl_select(int x, int y){
GLuint buff[64] = {0};
GLint hits, view[4];
glSelectBuffer(64, buff);
glGetIntegerv(GL_VIEWPORT, view);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix(x,y,1.0,1.0,view);
gluPerspective(45,1.0,0.0001,1000.0);
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
draw();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
hits = glRenderMode(GL_RENDER);
list_hits(hits,buff);
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button, int state, int x, int y){
if(state == GLUT_DOWN){
gl_select(x,screen_height-y);
}
}
int main(int argc, char **argv){
glutInit(&argc,argv);
glutInitWindowPosition(-1,-1); //starts at the default position
glutInitWindowSize(screen_width,screen_height);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitContextVersion(2, 1);
glutCreateWindow("Chess 3D");
glewExperimental = GL_TRUE;
glewInit();
initGL();
initDLs();
loadPieces();
gamestate = WHITE_TURN; //white starts first
glClearColor(0.8, 0.8, 0.8, 1);
glutDisplayFunc(draw);
glutIdleFunc(draw);
glutReshapeFunc(resize);
glutKeyboardFunc(keyEvents);
glutSpecialFunc(specialKeys);
glutMouseFunc(mouse);
glutMainLoop();
return 1;
}