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 /* IDs para o callback da GLUI (ControlFunc) */
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
)
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
);
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
,
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
,
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
);
130 GLUI_Master
.close_all();
133 void Window::main_loop()
136 _control
->set_minimax_depth(_minimax_depth
);
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()
185 void Window::render_scene(void)
187 glClear(GL_COLOR_BUFFER_BIT
);
189 glMatrixMode(GL_PROJECTION
);
191 glViewport(0, 0, _width
, _height
);
192 //GLUI_Master.auto_set_viewport();
193 gluOrtho2D(0, _width
, _height
, 0);
194 glMatrixMode(GL_MODELVIEW
);
197 glClearColor(223.0f
/ 255, 203.0f
/ 255, 155.0f
/ 255, 1.0f
);
203 _control
->after_display();
206 void Window::special_keys(int key
, int x
, int y
)
210 //KeyboardFunc('w', 0, 0);
213 //KeyboardFunc('s', 0, 0);
216 //KeyboardFunc('d', 0, 0);
219 //KeyboardFunc('a', 0, 0);
221 case GLUT_KEY_PAGE_UP
:
227 void Window::keyboard_func(unsigned char key
, int x
, int y
)
229 switch(tolower(key
)) {
255 void Window::reshape_func(int width
, int height
)
261 void Window::mouse_func(int button
, int state
, int x
, int y
)
263 int s
= MIN(_width
, _height
) / 8,
268 case GLUT_LEFT_BUTTON
:
271 _control
->click(rx
, ry
);
275 case GLUT_RIGHT_BUTTON
:
286 void Window::timer_func(int key
)
288 //glutTimerFunc(TIME_INTERVAL, TimerFunc, 0);
293 void Window::control_func(int id
)
296 case ID_MINIMAX_DEPTH
:
297 _control
->set_minimax_depth(_minimax_depth
);
300 _control
->set_first_move(_first_move
);
302 case ID_COMPUTER_COLOR
:
303 _control
->set_computer_color(_computer_color
);
323 void Window::draw_tab()
329 if(!_control
->is_empty(i
, j
))
330 draw_circle(i
, j
, _control
->piece(i
, j
));
333 void Window::draw_tab_bg()
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,
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
);
363 double angle
= i
* 3.14 / 180;
364 glVertex2f(rx
+ sin(angle
) * s
, ry
+ cos(angle
) * s
);
369 void Window::draw_square(int x
, int y
, int c
)
371 int s
= MIN(_width
, _height
) / 8,
376 float color
[][3] = {{230.0f
/ 255, 187.0f
/ 255, 130.0f
/ 255},
377 {218.0f
/ 255, 155.0f
/ 255, 71.0f
/ 255},
382 glColor3f(color
[c
][0], color
[c
][1], color
[c
][2]);
385 glVertex2f(rx
, ry
);
386 glVertex2f(rx
, ry
+ s
);
387 glVertex2f(rx
+ s
, ry
+ s
);
388 glVertex2f(rx
+ s
, ry
);
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
);