|
Keyboard input trought allegro |
TeachMeHowToPro
Member #15,892
February 2015
|
Hello. I have a simple question: Any help would be welcome! |
Thomas Fjellstrom
Member #476
June 2000
|
The easiest way is to find a gui library you like (there's a few). Or you can use the ALLEGRO_EVENT_KEY_CHAR and the unichar field to get the unicode character (and use the allegro unicode string api). -- |
Yodhe23
Member #8,726
June 2007
|
This is some 'orrible code that works from an old game of mine, 1const char * a5_grab_input(int ytextpos)
2{
3int happen=0;
4char *edittext=malloc (sizeof (char) * 128);
5int caret = 0;
6int running=1;
7al_flush_event_queue(event_queue);
8
9al_draw_filled_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(0,0,0));
10al_draw_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(150,150,150),3);
11al_flip_display();
12 do
13 {
14 al_init_timeout(&timeout, 0.06);
15 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
16
17 if (get_event && ev.type==ALLEGRO_EVENT_KEY_CHAR){
18 int newkey = ev.keyboard.unichar;
19 char ASCII = newkey & 0xff;
20 char scancode = ev.keyboard.keycode;
21
22 ///* a character key was pressed; add it to the string */
23 if(scancode == ALLEGRO_KEY_ENTER) running=0;
24 else
25 if (scancode==ALLEGRO_KEY_SPACE) ;
26 else
27 if(scancode == ALLEGRO_KEY_BACKSPACE ){
28 if (caret > 0) caret--;
29 happen=1;
30 edittext[caret] = '\0';
31 }
32 else
33 if(ASCII >= 32 && ASCII <= 126){
34 if (ASCII>=97 AND ASCII <=122 AND (caret==0 OR edittext[caret-1]==' ')) ASCII-=32;
35 if(caret < 20 - 1){
36 edittext[caret] = ASCII;
37 caret++;
38 edittext[caret] = '\0';
39 happen=1;
40 }
41 }
42 }
43
44 if (happen==1){
45 al_draw_filled_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(0,0,0));
46 al_draw_rectangle(disp_data.width/5,ytextpos-20,disp_data.width/5*4,ytextpos+50,al_map_rgb(150,150,150),3);
47 al_draw_text(font,al_map_rgb(250,250,250),disp_data.width/2, ytextpos,ALLEGRO_ALIGN_CENTER,edittext);
48 al_flip_display();
49 }
50 happen=0;
51 }
52 while(running==1);
53
54
55 //If no name - 0 characters inputed then return random name
56 if (caret==0){
57 switch (rand()%3){
58 case 0:
59 strcpy (edittext, "Vasya Pupkin");
60 break;
61 case 1:
62 strcpy (edittext, "Joe Bloggs");
63 break;
64 default:
65 strcpy (edittext, "Luther Blissett");
66 break;
67 }
68 }
69
70return edittext;
71}
www.justanotherturn.com |
StevenVI
Member #562
July 2000
|
That really is 'orrible! Never share that snippet with anyone ever again! __________________________________________________ |
Yodhe23
Member #8,726
June 2007
|
'Orrible but it works, I guess it's the hacker in me. www.justanotherturn.com |
TeachMeHowToPro
Member #15,892
February 2015
|
Can anyone recommend a GUI library? I need it to work on Android and IOS also |
jmasterx
Member #11,410
October 2009
|
My GUI API Agui is used in the popular game Factorio: http://www.factorio.com/ and I have been using it for years. I am currently porting my game to ios and in doing so I am adding some touch compatibility. I have obj-c classes to do keyboard support in ios too. There's also: Agui GUI API -> https://github.com/jmasterx/Agui |
|