relatorio
[xYpjg3TdSw.git] / Window.cpp
blobea5ba326f8f27cfcbaff27f49a69fd0316c4f3f4
1 #include "Window.h"
2 #include "Controller.h"
3 #include "common.h"
5 #include "GL/glui.h"
6 #include <cctype>
7 #include <cmath>
8 #include <cassert>
10 #define MAIN_WINDOW_WIDTH 500
11 #define MAIN_WINDOW_HEIGHT 500
12 #define MAIN_WINDOW_POSITION_X 100
13 #define MAIN_WINDOW_POSITION_Y 100
14 #define MAIN_WINDOW_TITLE "IA"
16 #define CHILD_WINDOW_POSITION_X 605
17 #define CHILD_WINDOW_POSITION_Y 100
18 #define CHILD_WINDOW_TITTLE "Opções"
19 #define CHILD_WINDOW_PANEL_FIRST_MOVE_TITLE "Inicial:"
21 #define DEFAULT_MINIMAX_DEPTH 1
22 #define DEFAULT_COMPUTER_COLOR 1
24 /* IDs para o callback da GLUI (ControlFunc) */
25 enum {
26 ID_MINIMAX_DEPTH = 0,
27 ID_FIRST_MOVE,
28 ID_COMPUTER_COLOR,
29 ID_PLAY_BUTTON,
30 ID_STOP_BUTTON,
31 ID_UNDO_BUTTON,
32 ID_REDO_BUTTON
35 /* Para que os callbacks da GLUT possam acessar o objeto */
36 static Window *window = NULL;
38 /* GLUT callbacks wrappers */
39 void RenderScene(void) { window->render_scene(); }
40 void SpecialKeys(int key, int x, int y) { window->special_keys(key, x, y); }
41 void KeyboardFunc(unsigned char key, int x, int y) { window->keyboard_func(key, x, y); }
42 void ReshapeFunc(int width, int height) { window->reshape_func(width, height); }
43 void MouseFunc(int button, int state, int x, int y) { window->mouse_func(button, state, x, y); }
44 void TimerFunc(int key) { window->timer_func(key); }
45 void ControlFunc(int id) { window->control_func(id); }
47 Window::Window(int argc, char **argv, Controller *control)
48 : _control(control)
50 window = this;
52 glutInit(&argc, argv);
53 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
54 //glDisable(GL_DEPTH_TEST);
55 glutInitWindowSize(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
56 glutInitWindowPosition(MAIN_WINDOW_POSITION_X, MAIN_WINDOW_POSITION_Y);
58 _window_id = glutCreateWindow(MAIN_WINDOW_TITLE);
60 glClearColor(223.0f / 255, 203.0f / 255, 155.0f / 255, 1.0f);
62 // GLUT callbacks
63 glutDisplayFunc(RenderScene);
64 glutKeyboardFunc(KeyboardFunc);
65 glutSpecialFunc(SpecialKeys);
66 glutReshapeFunc(ReshapeFunc);
67 glutMouseFunc(MouseFunc);
68 //glutTimerFunc(TIME_INTERVAL, TimerFunc, 0);
71 glui = GLUI_Master.create_glui(CHILD_WINDOW_TITTLE, 0, CHILD_WINDOW_POSITION_X, CHILD_WINDOW_POSITION_Y);
73 GLUI_Panel *Panel_up = glui->add_panel("", GLUI_PANEL_NONE);
75 GLUI_Panel *Panel_game = glui->add_panel_to_panel(Panel_up, "Jogo:");
76 _Button_start = glui->add_button_to_panel(Panel_game, "Iniciar", ID_PLAY_BUTTON, ControlFunc);
77 _Button_stop = glui->add_button_to_panel(Panel_game, "Parar", ID_STOP_BUTTON, ControlFunc);
79 GLUI_Panel *Panel_undoRedo = glui->add_panel_to_panel(Panel_game, "", GLUI_PANEL_NONE);
80 _Button_undo = glui->add_button_to_panel(Panel_undoRedo, "<-", ID_UNDO_BUTTON, ControlFunc);
81 //glui->add_column_to_panel(Panel_undoRedo, false);
82 _Button_redo = glui->add_button_to_panel(Panel_undoRedo, "->", ID_REDO_BUTTON, ControlFunc);
84 glui->add_column_to_panel(Panel_up, false);
86 GLUI_Panel *Panel_computerColor = glui->add_panel_to_panel(Panel_up, "Cor do Computador:");
87 _RadioGroup_computerColor = glui->add_radiogroup_to_panel(Panel_computerColor, &_computer_color, ID_COMPUTER_COLOR,
88 ControlFunc);
89 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Nenhuma");
90 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Brancas");
91 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Pretas");
92 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Ambas");
93 _RadioGroup_computerColor->set_int_val(DEFAULT_COMPUTER_COLOR);
94 _control->set_computer_color(DEFAULT_COMPUTER_COLOR);
96 GLUI_Panel *Panel_down = glui->add_panel("", GLUI_PANEL_NONE);
98 GLUI_Panel *Panel_firstMove = glui->add_panel_to_panel(Panel_down, CHILD_WINDOW_PANEL_FIRST_MOVE_TITLE);
99 _RadioGroup_firstMove = glui->add_radiogroup_to_panel(Panel_firstMove, &_first_move, ID_FIRST_MOVE,
100 ControlFunc);
101 glui->add_radiobutton_to_group(_RadioGroup_firstMove, "Brancas");
102 glui->add_radiobutton_to_group(_RadioGroup_firstMove, "Pretas");
103 _RadioGroup_firstMove->set_int_val(0);
105 glui->add_column_to_panel(Panel_down, false);
107 GLUI_Panel *Panel_minimax = glui->add_panel_to_panel(Panel_down, "Minimax:");
108 _Spinner_minimax = glui->add_spinner_to_panel(Panel_minimax, "Profundidade:", GLUI_SPINNER_INT,
109 &_minimax_depth, ID_MINIMAX_DEPTH, ControlFunc);
110 _Spinner_minimax->set_int_limits(0, 10, GLUI_LIMIT_CLAMP);
111 _Spinner_minimax->set_int_val(DEFAULT_MINIMAX_DEPTH);
113 GLUI_Panel *Panel_about = glui->add_panel("Sobre:");
114 glui->add_statictext_to_panel(Panel_about, "Disciplina: INF01048 - Inteligencia Artificial");
115 glui->add_statictext_to_panel(Panel_about, "Professor: Paulo Martins Engel.");
116 glui->add_statictext_to_panel(Panel_about, "Autores:");
117 glui->add_statictext_to_panel(Panel_about, " Bruno Coswig Fiss - 171359");
118 glui->add_statictext_to_panel(Panel_about, " Kaue Soares da Silveira - 171671");
119 glui->add_statictext_to_panel(Panel_about, "Semestre: 2010/1");
121 /** Tell GLUI window which other window to recognize as the main gfx window **/
122 glui->set_main_gfx_window(_window_id);
124 /** Register the Idle callback with GLUI (instead of with GLUT) **/
125 //GLUI_Master.set_glutIdleFunc( myGlutIdle );
127 // cool cursors: GLUT_CURSOR_INFO, GLUT_CURSOR_DESTROY, GLUT_CURSOR_WAIT, GLUT_CURSOR_CROSSHAIR, GLUT_CURSOR_NONE
128 glutSetCursor(GLUT_CURSOR_INFO);
131 Window::~Window()
133 GLUI_Master.close_all();
136 void Window::main_loop()
138 _control->stop();
139 _control->set_minimax_depth(_minimax_depth);
140 glutMainLoop();
143 void Window::enable_all()
145 _RadioGroup_firstMove->enable();
146 _RadioGroup_computerColor->enable();
147 _Spinner_minimax->enable();
148 _Button_start->enable();
149 _Button_stop->enable();
150 _Button_undo->enable();
151 _Button_redo->enable();
154 void Window::disable_all()
156 _RadioGroup_firstMove->disable();
157 _RadioGroup_computerColor->disable();
158 _Spinner_minimax->disable();
159 _Button_start->disable();
160 _Button_stop->disable();
161 _Button_undo->disable();
162 _Button_redo->disable();
165 void Window::enable(const char *s)
167 if(!strcmp(s, "stop")) _Button_stop->enable();
168 else if(!strcmp(s, "undo")) _Button_undo->enable();
169 else if(!strcmp(s, "redo")) _Button_redo->enable();
170 else assert("not found" && false);
173 void Window::disable(const char *s)
175 if(!strcmp(s, "stop")) _Button_stop->disable();
176 else if(!strcmp(s, "undo")) _Button_undo->disable();
177 else if(!strcmp(s, "redo")) _Button_redo->disable();
178 else assert("not found" && false);
181 void Window::display()
183 glutPostRedisplay();
186 /* GLUT callacks */
188 void Window::render_scene(void)
190 glClear(GL_COLOR_BUFFER_BIT);
192 glMatrixMode(GL_PROJECTION);
193 glLoadIdentity();
194 glViewport(0, 0, _width, _height);
195 //GLUI_Master.auto_set_viewport();
196 gluOrtho2D(0, _width, _height, 0);
197 glMatrixMode(GL_MODELVIEW);
198 glLoadIdentity();
200 glClearColor(223.0f / 255, 203.0f / 255, 155.0f / 255, 1.0f);
202 draw_tab();
204 glutSwapBuffers();
206 _control->after_display();
209 void Window::special_keys(int key, int x, int y)
211 switch(key) {
212 case GLUT_KEY_UP:
213 //KeyboardFunc('w', 0, 0);
214 break;
215 case GLUT_KEY_DOWN:
216 //KeyboardFunc('s', 0, 0);
217 break;
218 case GLUT_KEY_RIGHT:
219 //KeyboardFunc('d', 0, 0);
220 break;
221 case GLUT_KEY_LEFT:
222 //KeyboardFunc('a', 0, 0);
223 break;
224 case GLUT_KEY_PAGE_UP:
225 break;
227 glutPostRedisplay();
230 void Window::keyboard_func(unsigned char key, int x, int y)
232 switch(tolower(key)) {
233 case 'a':
234 break;
235 case 's':
236 _control->stop();
237 break;
238 case 'd':
239 break;
240 case 'w':
241 break;
242 case 'h':
243 break;
244 case 'c':
245 break;
246 case 'r':
247 break;
248 case 'q':
249 exit(0);
250 break;
251 default:
252 break;
255 glutPostRedisplay();
258 void Window::reshape_func(int width, int height)
260 _width = width;
261 _height = height;
264 void Window::mouse_func(int button, int state, int x, int y)
266 int s = MIN(_width, _height) / 8,
267 rx = x / s,
268 ry = y / s;
270 switch(button) {
271 case GLUT_LEFT_BUTTON:
272 switch(state) {
273 case GLUT_UP:
274 _control->click(rx, ry);
275 break;
277 break;
278 case GLUT_RIGHT_BUTTON:
279 switch(state) {
280 case GLUT_UP:
281 break;
283 break;
286 glutPostRedisplay();
289 void Window::timer_func(int key)
291 //glutTimerFunc(TIME_INTERVAL, TimerFunc, 0);
292 glutPostRedisplay();
295 /* GLUI callback */
296 void Window::control_func(int id)
298 switch(id) {
299 case ID_MINIMAX_DEPTH:
300 _control->set_minimax_depth(_minimax_depth);
301 break;
302 case ID_FIRST_MOVE:
303 _control->set_first_move(_first_move);
304 break;
305 case ID_COMPUTER_COLOR:
306 _control->set_computer_color(_computer_color);
307 break;
308 case ID_PLAY_BUTTON:
309 _control->play();
310 break;
311 case ID_STOP_BUTTON:
312 _control->stop();
313 break;
314 case ID_UNDO_BUTTON:
315 _control->undo();
316 break;
317 case ID_REDO_BUTTON:
318 _control->redo();
319 break;
321 glutPostRedisplay();
324 /* Desenho */
326 void Window::draw_tab()
328 draw_tab_bg();
330 rep(i, 8)
331 rep(j, 8)
332 if(!_control->is_empty(i, j))
333 draw_circle(i, j, _control->piece(i, j));
336 void Window::draw_tab_bg()
338 rep(i, 8)
339 rep(j, 8) {
340 if(_control->is_highlighted(i, j))
341 draw_square(i, j, _control->highlight(i, j) + 2);
342 else draw_square(i, j, (i + j) % 2);
346 void Window::draw_circle(int x, int y, int c)
348 double s = MIN(_width, _height) / 8,
349 rx = x * s,
350 ry = y * s;
352 s /= 2;
353 rx += s;
354 ry += s;
355 s *= 0.9;
357 static
358 float color[][3] = {{244.0f / 255, 244.0f / 255, 244.0f / 255},
359 {30.0f / 255, 30.0f / 255, 30.0f / 255}};
361 glColor3f(color[c][0], color[c][1], color[c][2]);
363 glBegin(GL_TRIANGLE_FAN);
364 glVertex2f(rx, ry);
365 repi(i, 380, 25) {
366 double angle = i * 3.14 / 180;
367 glVertex2f(rx + sin(angle) * s, ry + cos(angle) * s);
369 glEnd();
372 void Window::draw_square(int x, int y, int c)
374 int s = MIN(_width, _height) / 8,
375 rx = x * s,
376 ry = y * s;
378 static
379 float color[][3] = {{230.0f / 255, 187.0f / 255, 130.0f / 255},
380 {218.0f / 255, 155.0f / 255, 71.0f / 255},
381 {0.0f, 0.0f, 1.0f},
382 {1.0f, 0.0f, 0.0f}
385 glColor3f(color[c][0], color[c][1], color[c][2]);
387 glBegin(GL_QUADS);
388 glVertex2f(rx , ry );
389 glVertex2f(rx , ry + s);
390 glVertex2f(rx + s, ry + s);
391 glVertex2f(rx + s, ry );
392 glEnd();
395 glColor3f(0.0f, 0.0f, 0.0f);
397 glBegin(GL_LINE_LOOP);
398 glVertex2f(rx , ry );
399 glVertex2f(rx , ry + s);
400 glVertex2f(rx + s, ry + s);
401 glVertex2f(rx + s, ry );
402 glEnd();