-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
262 lines (202 loc) · 4.89 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
#include <rubik.h>
#include <scene.h>
#include <object.h>
#include <light.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <cmath>
#include <cstdlib>
#include <string>
// Macros para scroll
#ifndef GLUT_WHEEL_DOWN
#define GLUT_WHEEL_DOWN 0x0003
#endif
#ifndef GLUT_WHEEL_UP
#define GLUT_WHEEL_UP 0x0004
#endif
// Objetos
Light *light = NULL;
Scene *scene = NULL;
Rubik *rubik = NULL;
Minicube *minicube = NULL;
// Informacion de pantalla
GLint width = 800;
GLint height = 600;
GLfloat fovy = 45.0F;
GLfloat zFar = 100.0F;
GLfloat zNear = 1.0F;
// Controlador de loop
const GLint tick = 1000 / 60;
const GLint skip = 10;
GLint game_ms = 0;
GLint loops = 0;
// Contador de fps
GLint fps_ms = 0;
GLint fraps = 0;
// Banderas de eventos del mouse
bool move = false;
bool rotate = false;
// Callbacks de GLUT
// Redimensionar ventana
void reshape (int w, int h)
{
// Actualiza las dimensiones de la pantalla
width = w;
height = h;
// Ajusta el viewport a las dimensiones de la ventana
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
// Actualiza la informacion de la pantalla de los objetos
Object::setWindow((GLfloat) w, (GLfloat) h, fovy, zNear, zFar);
minicube->updatePosition();
}
// Dibujar escena
void display ()
{
// Limpiar buffers
glClearColor(0.25F, 0.25F, 0.25F, 1.0F);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Objetos 3D usan proyeccion perspectiva
Object::perspective();
// Escenario y cubo
scene->draw();
rubik->draw();
// Objectos 2D usan proyeccion ortogonal
Object::orthogonal();
// Minicubo guia
minicube->draw();
// Intercamiar buffers
glutSwapBuffers();
}
// Animaciones
void idle ()
{
// Actualizar juego
loops = 0;
if ((glutGet(GLUT_ELAPSED_TIME) >= game_ms) && (loops < skip))
{
scene->animate();
rubik->animate();
loops = 0;
game_ms += tick;
}
// Contador de FPS
fraps++;
if ((glutGet(GLUT_ELAPSED_TIME) - fps_ms) >= 1000)
{
std::clog << "FPS: " << fraps << std::endl;
fraps = 0;
fps_ms += 1000;
}
// Dibujar
glutPostRedisplay();
}
// Eventos del mouse
// Click y scroll
void mouse (int button, int state, int x, int y)
{
// Scroll hacia arriba
if ((button == GLUT_WHEEL_UP) && (state == GLUT_DOWN))
{
rubik->zoomOut();
return;
}
// Scrool hacia abajo
if ((button == GLUT_WHEEL_DOWN) && (state == GLUT_DOWN))
{
rubik->zoomIn();
return;
}
// Click izquierdo
if (button == GLUT_LEFT_BUTTON)
{
rotate = (state == GLUT_DOWN);
if (rotate) rubik->rotateBegin(x, y);
return;
}
// Click derecho
if (button == GLUT_RIGHT_BUTTON)
{
move = (state == GLUT_DOWN);
if (move) rubik->dragBegin(x, y);
}
}
// Arrastrar o rotar
void motion (int x, int y)
{
// Arrastrar
if (move) rubik->dragEnd(x, y);
// Rotar
if (rotate) rubik->rotateEnd(x, y);
}
// Teclado
void keyboard (unsigned char key, int, int)
{
// Captura ALT
const bool alt = (GLUT_ACTIVE_ALT == glutGetModifiers());
switch (key)
{
// Ctrl + Enter alterna el modo pantalla completa
case 13: if (alt) glutFullScreenToggle(); return;
// Jugar
// Sentido de las agujas del reloj
case 'q': case 'Q': rubik->play(Rubik::U0); return;
case 'w': case 'W': rubik->play(Rubik::D0); return;
case 'e': case 'E': rubik->play(Rubik::L0); return;
case 'r': case 'R': rubik->play(Rubik::R0); return;
case 't': case 'T': rubik->play(Rubik::F0); return;
case 'y': case 'Y': rubik->play(Rubik::B0); return;
// Sentido opuesto a las agujas del reloj
case 'a': case 'A': rubik->play(Rubik::U1); return;
case 's': case 'S': rubik->play(Rubik::D1); return;
case 'd': case 'D': rubik->play(Rubik::L1); return;
case 'f': case 'F': rubik->play(Rubik::R1); return;
case 'g': case 'G': rubik->play(Rubik::F1); return;
case 'h': case 'H': rubik->play(Rubik::B1); return;
}
}
// Al finalizar la aplicacion
void close ()
{
delete light;
delete scene;
delete rubik;
delete minicube;
}
int main(int argc, char **argv)
{
// Inicializar GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH) - width) >> 1, (glutGet(GLUT_SCREEN_HEIGHT) - height) >> 1);
glutInitWindowSize(width, height);
glutCreateWindow("Rubik");
glutCreateMenu(NULL);
// Inicializar GLEW
glewInit();
// Callbacks de GLUT
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutCloseFunc(close);
// Obtiene directorio de archivos
std::string path = argv[0];
path.erase(path.find_last_of("/\\"));
path.erase(path.find_last_of("/\\"));
path += "/Files";
// Informa velocidad de animacion y directorio de archivos
Object::setFPS(tick);
Object::setPath(path);
// Activa la iluminacion
Light::globalTurnOn();
// Construir objetos
light = new Light();
scene = new Scene();
rubik = new Rubik();
minicube = new Minicube(rubik);
// Loop principal de GLUT
glutMainLoop();
return 0;
}