1#include "my_includes.h"
2
3void validate_input
(ALLEGRO_USTR* str
);
4void print_board
(char board,
ALLEGRO_FONT * script
);
5void test_input
(int height,
ALLEGRO_FONT *font)
6{
7
8 bool finished
= false;
9 ALLEGRO_USTR* my_string
;
10 my_string
= al_ustr_new("");
11 int position
;
12 bool redraw
= false;
13 extern bool invalid
;
14 invalid
= false;
15 ALLEGRO_EVENT_QUEUE * event_queue
= NULL
;
16
17 event_queue
= al_create_event_queue();
18 if(!event_queue
) {
19 fprintf(stderr,
"failed to create event_queue!\n");
20 return;
21 }
22
23 al_register_event_source(event_queue,
al_get_keyboard_event_source());
24
25
26 al_draw_text(font, white,
0, height, ALLEGRO_ALIGN_LEFT,
"Enter the coordinates: ");
27 while(!finished
){
28
29 ALLEGRO_EVENT ev
;
30 al_wait_for_event(event_queue,
&ev
);
31 switch(ev.type
){
32
33 case ALLEGRO_EVENT_KEY_CHAR:
34 if(ev.keyboard.unichar
>= 32){
35 al_ustr_append_chr(my_string, ev.keyboard.unichar
);
36 position
= (int)al_ustr_size(my_string
);
37 redraw
= true;
38 }//ev.keyboard.unichar
39
40 else if(ev.keyboard.keycode
== ALLEGRO_KEY_BACKSPACE
){
41 al_ustr_truncate(my_string, position
- 1);
42 redraw
= true;
43 }//keycode == backspace
44
45 else if(ev.keyboard.keycode
== ALLEGRO_KEY_ENTER
){
46 finished
= true;
47 validate_input
(my_string
);
48 redraw
= true;
49 }//allegro_key_enter
50
51 else if(ev.keyboard.keycode
== ALLEGRO_KEY_ESCAPE
)
52 finished
= true;
53 }//switch ev.type
54
55 if(redraw
){
56 if(!invalid
){
57 redraw
= false;
58 invalid
= false;
59 al_clear_to_color(black
);
60 al_draw_textf(font, white,
0, height, ALLEGRO_ALIGN_LEFT,
"You entered %s",
al_cstr(my_string
));
61 print_board
('s',
font);
62 al_flip_display();
63 }// !invalid
64
65 else{
66 finished
= false;
67 redraw
= false;
68 al_clear_to_color(black
);
69 al_draw_textf(font, white,
0, height, ALLEGRO_ALIGN_LEFT,
"Invalid input: %s",
al_cstr(my_string
));
70 print_board
('s',
font);
71 al_flip_display();
72 al_rest(1.
0);
73 al_ustr_truncate(my_string,
0);
74 al_clear_to_color(black
);
75 al_draw_text(font, white,
0, height, ALLEGRO_ALIGN_LEFT,
"Enter the coordinates: ");
76 print_board
('s',
font);
77 al_flip_display();
78
79 }//invalid
80 }//redraw
81 }//not finished
82
83 al_ustr_free(my_string
);
84 al_destroy_event_queue(event_queue
);
85
86 al_rest(1);
87
88}//test_input