-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
174 lines (143 loc) · 4.53 KB
/
main.cc
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
#include "chess.hpp"
/* Declaring callback functions */
static void onDisplay(void);
static void onKeyboard(unsigned char key, int x, int y);
static void onSpecialKey(int key, int x, int y);
static void onReshape(int width, int height);
/* Global game variables */
Game* game;
int selFieldFile = 0;
int selFieldRank = 0;
/* Global GLUT variables */
int winWidth = 1280;
int winHeight = 720;
int main(int argc, char **argv)
{
/* Starting a New Game */
game = new Game();
/* Initializing GLUT. */
glutInit(&argc, argv);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
/* Creating a window. */
glutInitWindowSize(winWidth, winHeight);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
/* Registering callback functions. */
glutDisplayFunc(onDisplay);
glutKeyboardFunc(onKeyboard);
glutSpecialFunc(onSpecialKey);
glutReshapeFunc(onReshape);
/* Initializing clear color. */
glClearColor(0.75, 0.75, 0.75, 0);
/* Initializing light & materials. */
GLfloat light_ambient[] = { 0.4, 0.4, 0.4, 1 };
GLfloat light_diffuse[] = { 0.3, 0.3, 0.3, 1 };
GLfloat light_specular[] = { 0.5, 0.5, 0.5, 1 };
GLfloat light_position[] = { -20, 0, -600, 0 };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightModeli(GL_LIGHT_MODEL_AMBIENT,1);
/* The program is entering the main loop. */
glutMainLoop();
return 0;
}
static void onDisplay(void)
{
/* Setting Clear Color and Depth */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* Set the viewport. */
glViewport(0, 0, winWidth, winHeight);
/* Setting up perspective and camera */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(
60,
winWidth/(float)winHeight,
0, 25);
glOrtho(-100.0f, 100.0f, -100.0f, 100.0f, -100.0f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(
0, 0, 1,
0, 0, 0,
0, 1, 0
);
/* This is where the magic happens */
game->display(selFieldFile, selFieldRank);
/* Swap Buffer with the new one */
glutSwapBuffers();
}
static void onKeyboard(unsigned char key, int x, int y)
{
/* If the game is finished (either by draw or one side winning)
this creates a new game on any input. */
if(game->gameFinished){
delete game;
game = new Game();
}
/* Check key */
switch (key) {
case 13: //enter
// if a piece is selected and we're picking a field to move it to
if(game->pickingMove()){
Field* target = new Field(selFieldFile, selFieldRank);
ChessPiece* mov = game->cb->board[game->selFile][game->selRank];
game->playMove(target, mov);
//game->cb->print();
game->checkState();
}
// if we're selecting a piece
else{
game->selectField();
}
break;
case 27: //esc
exit(0);
break;
}
/* Refresh screen because the game state has changed.
Refreshing here in case additional inputs are added later. */
glutPostRedisplay();
}
static void onSpecialKey(int key, int x, int y){
/* The player can still move around the board, but can't select
anything anymore. */
if(game->gameFinished)
return;
/* Selecting fields using arrow keys. */
switch (key) {
case GLUT_KEY_RIGHT:
if(selFieldFile<7)
selFieldFile++;
break;
case GLUT_KEY_LEFT:
if(selFieldFile>0)
selFieldFile--;
break;
case GLUT_KEY_UP:
if(selFieldRank<7)
selFieldRank++;
break;
case GLUT_KEY_DOWN:
if(selFieldRank>0)
selFieldRank--;
break;
}
/* Refresh screen because the game state has changed.
Refreshing here in case additional inputs are added later. */
glutPostRedisplay();
}
/* Proportionally reshape everything when the user rezsizes
the window. */
static void onReshape(int width, int height){
float ratio = (float)width/height;
winWidth = width;
winHeight = (int)(width/ratio);
}