heuristicas começando a implementar ...
[xYpjg3TdSw.git] / Window.cpp
blob81e167f6ea053663104a1e27e3f9848418351448
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 /* IDs para o callback da GLUI (ControlFunc) */
22 enum {
23 ID_MINIMAX_DEPTH = 0,
24 ID_FIRST_MOVE,
25 ID_COMPUTER_COLOR,
26 ID_PLAY_BUTTON,
27 ID_STOP_BUTTON,
28 ID_UNDO_BUTTON,
29 ID_REDO_BUTTON
32 /* Para que os callbacks da GLUT possam acessar o objeto */
33 static Window *window = NULL;
35 /* GLUT callbacks wrappers */
36 void RenderScene(void) { window->render_scene(); }
37 void SpecialKeys(int key, int x, int y) { window->special_keys(key, x, y); }
38 void KeyboardFunc(unsigned char key, int x, int y) { window->keyboard_func(key, x, y); }
39 void ReshapeFunc(int width, int height) { window->reshape_func(width, height); }
40 void MouseFunc(int button, int state, int x, int y) { window->mouse_func(button, state, x, y); }
41 void TimerFunc(int key) { window->timer_func(key); }
42 void ControlFunc(int id) { window->control_func(id); }
44 Window::Window(int argc, char **argv, Controller *control)
45 : _control(control)
47 window = this;
49 glutInit(&argc, argv);
50 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
51 //glDisable(GL_DEPTH_TEST);
52 glutInitWindowSize(MAIN_WINDOW_WIDTH, MAIN_WINDOW_HEIGHT);
53 glutInitWindowPosition(MAIN_WINDOW_POSITION_X, MAIN_WINDOW_POSITION_Y);
55 _window_id = glutCreateWindow(MAIN_WINDOW_TITLE);
57 glClearColor(223.0f / 255, 203.0f / 255, 155.0f / 255, 1.0f);
59 // GLUT callbacks
60 glutDisplayFunc(RenderScene);
61 glutKeyboardFunc(KeyboardFunc);
62 glutSpecialFunc(SpecialKeys);
63 glutReshapeFunc(ReshapeFunc);
64 glutMouseFunc(MouseFunc);
65 //glutTimerFunc(TIME_INTERVAL, TimerFunc, 0);
68 glui = GLUI_Master.create_glui(CHILD_WINDOW_TITTLE, 0, CHILD_WINDOW_POSITION_X, CHILD_WINDOW_POSITION_Y);
70 GLUI_Panel *Panel_up = glui->add_panel("", GLUI_PANEL_NONE);
72 GLUI_Panel *Panel_game = glui->add_panel_to_panel(Panel_up, "Jogo:");
73 _Button_start = glui->add_button_to_panel(Panel_game, "Iniciar", ID_PLAY_BUTTON, ControlFunc);
74 _Button_stop = glui->add_button_to_panel(Panel_game, "Parar", ID_STOP_BUTTON, ControlFunc);
76 GLUI_Panel *Panel_undoRedo = glui->add_panel_to_panel(Panel_game, "", GLUI_PANEL_NONE);
77 _Button_undo = glui->add_button_to_panel(Panel_undoRedo, "<-", ID_UNDO_BUTTON, ControlFunc);
78 //glui->add_column_to_panel(Panel_undoRedo, false);
79 _Button_redo = glui->add_button_to_panel(Panel_undoRedo, "->", ID_REDO_BUTTON, ControlFunc);
81 glui->add_column_to_panel(Panel_up, false);
83 GLUI_Panel *Panel_computerColor = glui->add_panel_to_panel(Panel_up, "Cor do Computador:");
84 _RadioGroup_computerColor = glui->add_radiogroup_to_panel(Panel_computerColor, &_computer_color, ID_COMPUTER_COLOR,
85 ControlFunc);
86 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Nenhuma");
87 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Brancas");
88 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Pretas");
89 glui->add_radiobutton_to_group(_RadioGroup_computerColor, "Ambas");
90 _RadioGroup_computerColor->set_int_val(2);
91 _control->set_computer_color(2);
93 GLUI_Panel *Panel_down = glui->add_panel("", GLUI_PANEL_NONE);
95 GLUI_Panel *Panel_firstMove = glui->add_panel_to_panel(Panel_down, CHILD_WINDOW_PANEL_FIRST_MOVE_TITLE);
96 _RadioGroup_firstMove = glui->add_radiogroup_to_panel(Panel_firstMove, &_first_move, ID_FIRST_MOVE,
97 ControlFunc);
98 glui->add_radiobutton_to_group(_RadioGroup_firstMove, "Brancas");
99 glui->add_radiobutton_to_group(_RadioGroup_firstMove, "Pretas");
100 _RadioGroup_firstMove->set_int_val(0);
102 glui->add_column_to_panel(Panel_down, false);
104 GLUI_Panel *Panel_minimax = glui->add_panel_to_panel(Panel_down, "Minimax:");
105 _Spinner_minimax = glui->add_spinner_to_panel(Panel_minimax, "Profundidade:", GLUI_SPINNER_INT,
106 &_minimax_depth, ID_MINIMAX_DEPTH, ControlFunc);
107 _Spinner_minimax->set_int_limits(0, 10, GLUI_LIMIT_CLAMP);
108 _Spinner_minimax->set_int_val(4);
110 GLUI_Panel *Panel_about = glui->add_panel("Sobre:");
111 glui->add_statictext_to_panel(Panel_about, "Disciplina: INF01048 - Inteligencia Artificial");
112 glui->add_statictext_to_panel(Panel_about, "Professor: Paulo Martins Engel.");
113 glui->add_statictext_to_panel(Panel_about, "Autores:");
114 glui->add_statictext_to_panel(Panel_about, " Bruno Coswig Fiss - 171359");
115 glui->add_statictext_to_panel(Panel_about, " Kaue Soares da Silveira - 171671");
116 glui->add_statictext_to_panel(Panel_about, "Semestre: 2010/1");
118 /** Tell GLUI window which other window to recognize as the main gfx window **/
119 glui->set_main_gfx_window(_window_id);
121 /** Register the Idle callback with GLUI (instead of with GLUT) **/
122 //GLUI_Master.set_glutIdleFunc( myGlutIdle );
124 // cool cursors: GLUT_CURSOR_INFO, GLUT_CURSOR_DESTROY, GLUT_CURSOR_WAIT, GLUT_CURSOR_CROSSHAIR, GLUT_CURSOR_NONE
125 glutSetCursor(GLUT_CURSOR_INFO);
128 Window::~Window()
130 GLUI_Master.close_all();
133 void Window::main_loop()
135 _control->stop();
136 _control->set_minimax_depth(_minimax_depth);
137 glutMainLoop();
140 void Window::enable_all()
142 _RadioGroup_firstMove->enable();
143 _RadioGroup_computerColor->enable();
144 _Spinner_minimax->enable();
145 _Button_start->enable();
146 _Button_stop->enable();
147 _Button_undo->enable();
148 _Button_redo->enable();
151 void Window::disable_all()
153 _RadioGroup_firstMove->disable();
154 _RadioGroup_computerColor->disable();
155 _Spinner_minimax->disable();
156 _Button_start->disable();
157 _Button_stop->disable();
158 _Button_undo->disable();
159 _Button_redo->disable();
162 void Window::enable(const char *s)
164 if(!strcmp(s, "stop")) _Button_stop->enable();
165 else if(!strcmp(s, "undo")) _Button_undo->enable();
166 else if(!strcmp(s, "redo")) _Button_redo->enable();
167 else assert("not found" && false);
170 void Window::disable(const char *s)
172 if(!strcmp(s, "stop")) _Button_stop->disable();
173 else if(!strcmp(s, "undo")) _Button_undo->disable();
174 else if(!strcmp(s, "redo")) _Button_redo->disable();
175 else assert("not found" && false);
178 void Window::display()
180 glutPostRedisplay();
183 /* GLUT callacks */
185 void Window::render_scene(void)
187 glClear(GL_COLOR_BUFFER_BIT);
189 glMatrixMode(GL_PROJECTION);
190 glLoadIdentity();
191 glViewport(0, 0, _width, _height);
192 //GLUI_Master.auto_set_viewport();
193 gluOrtho2D(0, _width, _height, 0);
194 glMatrixMode(GL_MODELVIEW);
195 glLoadIdentity();
197 glClearColor(223.0f / 255, 203.0f / 255, 155.0f / 255, 1.0f);
199 draw_tab();
201 glutSwapBuffers();
203 _control->after_display();
206 void Window::special_keys(int key, int x, int y)
208 switch(key) {
209 case GLUT_KEY_UP:
210 //KeyboardFunc('w', 0, 0);
211 break;
212 case GLUT_KEY_DOWN:
213 //KeyboardFunc('s', 0, 0);
214 break;
215 case GLUT_KEY_RIGHT:
216 //KeyboardFunc('d', 0, 0);
217 break;
218 case GLUT_KEY_LEFT:
219 //KeyboardFunc('a', 0, 0);
220 break;
221 case GLUT_KEY_PAGE_UP:
222 break;
224 glutPostRedisplay();
227 void Window::keyboard_func(unsigned char key, int x, int y)
229 switch(tolower(key)) {
230 case 'a':
231 break;
232 case 's':
233 _control->stop();
234 break;
235 case 'd':
236 break;
237 case 'w':
238 break;
239 case 'h':
240 break;
241 case 'c':
242 break;
243 case 'r':
244 break;
245 case 'q':
246 exit(0);
247 break;
248 default:
249 break;
252 glutPostRedisplay();
255 void Window::reshape_func(int width, int height)
257 _width = width;
258 _height = height;
261 void Window::mouse_func(int button, int state, int x, int y)
263 int s = MIN(_width, _height) / 8,
264 rx = x / s,
265 ry = y / s;
267 switch(button) {
268 case GLUT_LEFT_BUTTON:
269 switch(state) {
270 case GLUT_UP:
271 _control->click(rx, ry);
272 break;
274 break;
275 case GLUT_RIGHT_BUTTON:
276 switch(state) {
277 case GLUT_UP:
278 break;
280 break;
283 glutPostRedisplay();
286 void Window::timer_func(int key)
288 //glutTimerFunc(TIME_INTERVAL, TimerFunc, 0);
289 glutPostRedisplay();
292 /* GLUI callback */
293 void Window::control_func(int id)
295 switch(id) {
296 case ID_MINIMAX_DEPTH:
297 _control->set_minimax_depth(_minimax_depth);
298 break;
299 case ID_FIRST_MOVE:
300 _control->set_first_move(_first_move);
301 break;
302 case ID_COMPUTER_COLOR:
303 _control->set_computer_color(_computer_color);
304 break;
305 case ID_PLAY_BUTTON:
306 _control->play();
307 break;
308 case ID_STOP_BUTTON:
309 _control->stop();
310 break;
311 case ID_UNDO_BUTTON:
312 _control->undo();
313 break;
314 case ID_REDO_BUTTON:
315 _control->redo();
316 break;
318 glutPostRedisplay();
321 /* Desenho */
323 void Window::draw_tab()
325 draw_tab_bg();
327 rep(i, 8)
328 rep(j, 8)
329 if(!_control->is_empty(i, j))
330 draw_circle(i, j, _control->piece(i, j));
333 void Window::draw_tab_bg()
335 rep(i, 8)
336 rep(j, 8) {
337 if(_control->is_highlighted(i, j))
338 draw_square(i, j, _control->highlight(i, j) + 2);
339 else draw_square(i, j, (i + j) % 2);
343 void Window::draw_circle(int x, int y, int c)
345 double s = MIN(_width, _height) / 8,
346 rx = x * s,
347 ry = y * s;
349 s /= 2;
350 rx += s;
351 ry += s;
352 s *= 0.9;
354 static
355 float color[][3] = {{244.0f / 255, 244.0f / 255, 244.0f / 255},
356 {30.0f / 255, 30.0f / 255, 30.0f / 255}};
358 glColor3f(color[c][0], color[c][1], color[c][2]);
360 glBegin(GL_TRIANGLE_FAN);
361 glVertex2f(rx, ry);
362 repi(i, 380, 25) {
363 double angle = i * 3.14 / 180;
364 glVertex2f(rx + sin(angle) * s, ry + cos(angle) * s);
366 glEnd();
369 void Window::draw_square(int x, int y, int c)
371 int s = MIN(_width, _height) / 8,
372 rx = x * s,
373 ry = y * s;
375 static
376 float color[][3] = {{230.0f / 255, 187.0f / 255, 130.0f / 255},
377 {218.0f / 255, 155.0f / 255, 71.0f / 255},
378 {0.0f, 0.0f, 1.0f},
379 {1.0f, 0.0f, 0.0f}
382 glColor3f(color[c][0], color[c][1], color[c][2]);
384 glBegin(GL_QUADS);
385 glVertex2f(rx , ry );
386 glVertex2f(rx , ry + s);
387 glVertex2f(rx + s, ry + s);
388 glVertex2f(rx + s, ry );
389 glEnd();
392 glColor3f(0.0f, 0.0f, 0.0f);
394 glBegin(GL_LINE_LOOP);
395 glVertex2f(rx , ry );
396 glVertex2f(rx , ry + s);
397 glVertex2f(rx + s, ry + s);
398 glVertex2f(rx + s, ry );
399 glEnd();