Getting user input in allegro 5
ma3stro

I'm trying to make a basic alegro 5 game. At the end of the game I need to get user info to create a leaderboard. How can I get the user type his/her name and print it on the screen at the same time?

I'm new in allegro. I searched a lot but couldn't find something I can use. Someone suggested using ALLEGRO_EVENT_KEY_CHAR but i don't know how to use it for every single key on the keyboard.

Yodhe23

This is the function I use in my game which probably is full of bad coding, but it might help you...

#SelectExpand
1///Function grabs keypresses - ENTER NAME 2int grab_input(int player) 3{ 4int happen=0; 5char edittext[255]=""; 6char return_string[255]; 7int caret = 0, result = 0; 8const BUFFERSIZE = 255; 9char letter[255]=""; 10char mess[255]; 11int exit=0; 12 13while (exit==0) 14 { 15 ALLEGRO_EVENT ev; 16 ALLEGRO_TIMEOUT timeout; 17 al_init_timeout(&timeout, 0.06); 18 bool get_event = al_wait_for_event_until(event_queue, &ev, &timeout); 19 20 if (ev.type==ALLEGRO_EVENT_DISPLAY_RESIZE){ 21 al_acknowledge_resize(display); 22 ALLEGRO_TRANSFORM trans; 23 al_identity_transform(&trans); 24 al_scale_transform(&trans,al_get_display_width(display)/(float)disp_data.width,al_get_display_height(display)/(float)disp_data.height); 25 al_use_transform(&trans); 26 al_flush_event_queue(event_queue); 27 update_screen_elements(); 28 xscalar=al_get_display_width(display)/(float)disp_data.width; 29 yscalar=al_get_display_height(display)/(float)disp_data.height; 30 } 31 else 32 if (ev.type==ALLEGRO_EVENT_TIMER){ 33 al_draw_scaled_bitmap(background_bmp,0,0,al_get_bitmap_width(background_bmp),al_get_bitmap_height(background_bmp),0,0,disp_data.width,disp_data.height,0); 34 35 sprintf (mess,"ENTER PLAYER %d NAME",player); 36 al_draw_text(Font[disp_data.height/12], al_map_rgb(5,5,5),disp_data.width/2-1, disp_data.height/12-1, ALLEGRO_ALIGN_CENTRE,mess); 37 al_draw_text(Font[disp_data.height/12], al_map_rgb(5,5,5),disp_data.width/2+1, disp_data.height/12+1, ALLEGRO_ALIGN_CENTRE,mess); 38 al_draw_text(Font[disp_data.height/12], al_map_rgb(255,255,255),disp_data.width/2, disp_data.height/12, ALLEGRO_ALIGN_CENTRE,mess); 39 40 al_draw_text(Font[disp_data.height/40], al_map_rgb(5,5,5),disp_data.width/2, disp_data.height-disp_data.height/8, ALLEGRO_ALIGN_CENTRE, 41 "LEAVE BLANK TO GENERATE A RANDOM NAME"); 42 //al_draw_filled_rectangle(0,disp_data.height/3-disp_data.height/8,disp_data.width,disp_data.height/3+disp_data.height/8,al_map_rgb(50,150,50)); 43 sprintf (letter,"%s|",edittext); 44 if (caret>0) al_draw_text(Font[disp_data.height/12],Player[player].colour,disp_data.width/2, disp_data.height/3, ALLEGRO_ALIGN_CENTRE,edittext); 45 else 46 al_draw_text(Font[disp_data.height/12],al_map_rgb(0,0,0),disp_data.width/2, disp_data.height/3, ALLEGRO_ALIGN_CENTRE,"|"); 47 al_flip_display(); 48 } 49 50 if (ev.type==ALLEGRO_EVENT_KEY_UP){ 51 if (ev.keyboard.keycode==ALLEGRO_KEY_SPACE){ 52 edittext[caret] = 32; 53 caret++; 54 } 55 else 56 if (ev.keyboard.keycode==ALLEGRO_KEY_BACKSPACE){ 57 if (caret > 0) caret--; 58 edittext[caret] = '\0'; 59 } 60 else 61 if (ev.keyboard.keycode==ALLEGRO_KEY_ENTER){ 62 exit=1; 63 } 64 else{ 65 sprintf (letter,"%c",ev.keyboard.keycode+64); 66 strcat (edittext,letter); 67 caret++; 68 } 69 } 70 happen=0; 71 } 72 73if (caret<=0){ 74 random_name(player); 75 } 76 else 77 strcpy (Player[player].name,edittext); 78 79return (edittext); 80} 81//

ma3stro

Thanks a lot, that gave me an idea.

I have one more question: Can I get user name from console screen then print it to the project screen? I tried a little bit but whenever i try to use 'stdio.h' functions, program crashes.

torhu

ALLEGRO_EVENT_KEY_CHAR is usually the best way to get text input. You can still use the keycode field to check for arrow keys, etc.

ma3stro said:

I have one more question: Can I get user name from console screen then print it to the project screen? I tried a little bit but whenever i try to use 'stdio.h' functions, program crashes.

Hard to say what's wrong without seeing your code...

ma3stro

If I'm not asking too much can you post a simple code about usage of ALLEGRO_EVENT_KEY_CHAR? I can use the arrow keys, enter and esc but not combined letters.

SiegeLord

Here's a basic example of keyboard entry + backspace:

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_font.h> 3 4int main() 5{ 6 al_init(); 7 al_init_font_addon(); 8 al_install_keyboard(); 9 ALLEGRO_DISPLAY* d = al_create_display(800, 600); 10 ALLEGRO_FONT* font = al_create_builtin_font(); 11 ALLEGRO_EVENT_QUEUE* q = al_create_event_queue(); 12 al_register_event_source(q, al_get_keyboard_event_source()); 13 al_register_event_source(q, al_get_display_event_source(d)); 14 ALLEGRO_USTR* str = al_ustr_new("Type something..."); 15 int pos = (int)al_ustr_size(str); 16 17 bool quit = false; 18 while(!quit) 19 { 20 al_clear_to_color(al_map_rgb_f(0, 0, 0)); 21 al_draw_ustr(font, al_map_rgb_f(1, 1, 1), 400, 300, ALLEGRO_ALIGN_CENTRE, str); 22 al_flip_display(); 23 24 ALLEGRO_EVENT e; 25 al_wait_for_event(q, &e); 26 switch(e.type) 27 { 28 case ALLEGRO_EVENT_DISPLAY_CLOSE: 29 quit = true; 30 break; 31 case ALLEGRO_EVENT_KEY_CHAR: 32 if(e.keyboard.unichar >= 32) 33 { 34 pos += al_ustr_append_chr(str, e.keyboard.unichar); 35 } 36 else if(e.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) 37 { 38 if(al_ustr_prev(str, &pos)) 39 al_ustr_truncate(str, pos); 40 } 41 42 break; 43 } 44 } 45}

ma3stro

Thank you, it helped me a lot.

Thread #613334. Printed from Allegro.cc