2 #include "Controller.h"
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) */
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
)
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
);
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
,
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
,
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
);
133 GLUI_Master
.close_all();
136 void Window::main_loop()
139 _control
->set_minimax_depth(_minimax_depth
);
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()
188 void Window::render_scene(void)
190 glClear(GL_COLOR_BUFFER_BIT
);
192 glMatrixMode(GL_PROJECTION
);
194 glViewport(0, 0, _width
, _height
);
195 //GLUI_Master.auto_set_viewport();
196 gluOrtho2D(0, _width
, _height
, 0);
197 glMatrixMode(GL_MODELVIEW
);
200 glClearColor(223.0f
/ 255, 203.0f
/ 255, 155.0f
/ 255, 1.0f
);
206 _control
->after_display();
209 void Window::special_keys(int key
, int x
, int y
)
213 //KeyboardFunc('w', 0, 0);
216 //KeyboardFunc('s', 0, 0);
219 //KeyboardFunc('d', 0, 0);
222 //KeyboardFunc('a', 0, 0);
224 case GLUT_KEY_PAGE_UP
:
230 void Window::keyboard_func(unsigned char key
, int x
, int y
)
232 switch(tolower(key
)) {
258 void Window::reshape_func(int width
, int height
)
264 void Window::mouse_func(int button
, int state
, int x
, int y
)
266 int s
= MIN(_width
, _height
) / 8,
271 case GLUT_LEFT_BUTTON
:
274 _control
->click(rx
, ry
);
278 case GLUT_RIGHT_BUTTON
:
289 void Window::timer_func(int key
)
291 //glutTimerFunc(TIME_INTERVAL, TimerFunc, 0);
296 void Window::control_func(int id
)
299 case ID_MINIMAX_DEPTH
:
300 _control
->set_minimax_depth(_minimax_depth
);
303 _control
->set_first_move(_first_move
);
305 case ID_COMPUTER_COLOR
:
306 _control
->set_computer_color(_computer_color
);
326 void Window::draw_tab()
332 if(!_control
->is_empty(i
, j
))
333 draw_circle(i
, j
, _control
->piece(i
, j
));
336 void Window::draw_tab_bg()
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,
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
);
366 double angle
= i
* 3.14 / 180;
367 glVertex2f(rx
+ sin(angle
) * s
, ry
+ cos(angle
) * s
);
372 void Window::draw_square(int x
, int y
, int c
)
374 int s
= MIN(_width
, _height
) / 8,
379 float color
[][3] = {{230.0f
/ 255, 187.0f
/ 255, 130.0f
/ 255},
380 {218.0f
/ 255, 155.0f
/ 255, 71.0f
/ 255},
385 glColor3f(color
[c
][0], color
[c
][1], color
[c
][2]);
388 glVertex2f(rx
, ry
);
389 glVertex2f(rx
, ry
+ s
);
390 glVertex2f(rx
+ s
, ry
+ s
);
391 glVertex2f(rx
+ s
, ry
);
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
);