|
Text input using allegro 5 |
Scooter
Member #16,799
January 2018
|
I am having a problem getting user input using allegro 5. I have seen all the |
Edgar Reynaldo
Major Reynaldo
May 2007
|
It's really not that hard. Hook an event queue up to the keyboard and listen for ALLEGRO_EVENT_KEY_CHAR events. Listen for ev.keyboard.unichar and ev.keyboard.keycode to determine what to do with the input. Here's a quick example of how to check these events : 1 do {
2 ALLEGRO_EVENT ev;
3 al_wait_for_event(q , &ev);
4 if (ev.type == ALLEGRO_EVENT_KEY_CHAR) {
5 switch(ev.keyboard.keycode) :
6 case ALLEGRO_KEY_ENTER :
7 /// Enter string, process
8 break;
9 case ALLEGRO_KEY_BACKSPACE :
10 /// Remove character before caret
11 break;
12 case ALLEGRO_KEY_DELETE :
13 /// Remove character at caret
14 break;
15 default :
16 /// Add character to our string
17 break;
18 }
19
20 } while (!al_is_event_queue_empty(q));
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Scooter
Member #16,799
January 2018
|
Thanks Edgar for your prompt reply. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Your text will be built from the ev.keyboard.unichar field, and your commands come from ev.keyboard.keycode. If you don't get a command, you've got a char. Let me know how it goes. EDIT https://github.com/EdgarReynaldo/EagleGUI/blob/master/TINS2017/src/Game.cpp#L380-L479 My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Scooter
Member #16,799
January 2018
|
Good morning Edgar: |
LennyLen
Member #5,313
December 2004
|
Scooter said: I appreciate your time but Allegro is too much of a problemfor me! I had to give up on Python for the exact same reason. If you keep giving up every time you run into trouble, you'll never get anywhere with anything. What were the errors you had? If we can help you fix them you might learn how to fix the next problem yourself.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
My apologies, that code was missing brackets and other things. Try this structure instead : 1 do {
2 ALLEGRO_EVENT ev;
3 al_wait_for_event(q , &ev);
4 if (ev.type == ALLEGRO_EVENT_KEY_CHAR) {
5 switch (ev.keyboard.keycode) {
6 case ALLEGRO_KEY_ENTER :
7 /// Enter string, process
8 break;
9 case ALLEGRO_KEY_BACKSPACE :
10 /// Remove character before caret
11 break;
12 case ALLEGRO_KEY_DELETE :
13 /// Remove character at caret
14 break;
15 default :
16 /// Add character to our string
17 break;
18 }
19 }
20
21 } while (!al_is_event_queue_empty(q));
Scooter said:
Good morning Edgar:
Scooter, Don't just plug code into your program. You need to understand what it does. Copy Paste Coding is the Source of all EvilTM. Also, I didn't show where I initialized the 'q' variable : and hook it up to the keyboard : THEN, you can use it later on. But you have to set it up properly before doing so. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Scooter
Member #16,799
January 2018
|
Note to LennyLen: I appreciate your concern! |
Scooter
Member #16,799
January 2018
|
Good afternoon Edgar: |
Edgar Reynaldo
Major Reynaldo
May 2007
|
You should take a look at ex_keyboard_events.c. It shows you exactly how to use the keyboard. What you need to understand about Allegro 5 is that it uses events. Keyboard information comes in the form of events. There are ALLEGRO_EVENT_KEY_DOWN, ALLEGRO_EVENT_KEY_UP, and ALLEGRO_EVENT_KEY_CHAR events. When you need to know what key was pressed you use ALLEGRO_EVENT_KEY_DOWN, and when you need to know what character was typed, you use ALLEGRO_EVENT_KEY_CHAR. You may not be aspiring to be a professional programmer, but at the same time, this is pretty simple stuff. I believe that if a person is going to do something, they should do it to the best of their ability or not at all. Giving up because something is hard is self destructive. You will start to believe that you can't do it, and if you believe you can't then you actually can't do it. It's a self fulfilling prophecy. However, if you believe you can, you can. Mind set is everything.
25static void log_key(char const *how, int keycode, int unichar, int modifiers)
26{
27 char multibyte[5] = {0, 0, 0, 0, 0};
28 const char* key_name;
29
32 log_printf("%-8s code=%03d, char='%s' (%4d), modifiers=%08x, [%s]\n",
33 how, keycode, multibyte, unichar, modifiers, key_name);
34}
They use two functions to display the character. al_utf8_encode, and al_keycode_to_name. The first gives you the character's unicode code point, and the second gives you the name of the key that was pressed. Notice how al_utf8_encode takes ev.keyboard.unichar? And al_keycode_to_name takes ev.keyboard.keycode? I hope this helps clear things up. Please ask if you need help. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Scooter
Member #16,799
January 2018
|
Good afternoon Edgar: Had no luck with your code segment! Finally wrote what I needed in C, away |
Neil Roy
Member #2,229
April 2002
|
LennyLen said: If you keep giving up every time you run into trouble, you'll never get anywhere with anything. Agreed. I wrote a complete game with all the various inputs for Allegro 4 (Deluxe Pacman 1 below) and for Allegro 5 (Deluxe Pacman 2 below). All work just fine. You won't find it much different with anything else. Maybe programming is too much, perhaps game making software is more your style, Game Maker or such. This is the high score entry code I have for my Deluxe Pacman 2... written in C (as my whole game is) 1while(!done) {
2 if(pos > 0) al_ustr_assign(hiscore[difficulty][myscore].name, yourname);
3
4 al_wait_for_event(event_queue, &event);
5 switch(event.type) {
6 case ALLEGRO_EVENT_DISPLAY_CLOSE:
7 close_button = true;
8 done = true;
9 break;
10 case ALLEGRO_EVENT_TIMER:
11 redraw = true;
12 break;
13 case ALLEGRO_EVENT_KEY_CHAR:
14 if(event.keyboard.unichar >= 32) {
15 // keyboard typing sound effect
16 if(setting.sound_on) al_play_sample(sfx_type, setting.sound_volume, 0, 1,
17 ALLEGRO_PLAYMODE_ONCE, NULL);
18 pos += al_ustr_append_chr(yourname, event.keyboard.unichar);
19 // Get the width of the name in pixels (because TTF varies in width)
20 x = al_get_text_width(font_verdana, al_cstr(yourname));
21 if(x > MAX_NAME_LEN) { // Check if the width in pixels is greater than the allowed size
22 if(al_ustr_prev(yourname, &pos)) al_ustr_truncate(yourname, pos);
23 }
24 }
25 else if(event.keyboard.keycode == ALLEGRO_KEY_BACKSPACE) {
26 if(al_ustr_prev(yourname, &pos)) al_ustr_truncate(yourname, pos);
27 }
28 else if(event.keyboard.keycode == ALLEGRO_KEY_ENTER ||
29 event.keyboard.keycode == ALLEGRO_KEY_PAD_ENTER) {
30 al_ustr_assign(hiscore[difficulty][myscore].name, yourname);
31 done = true;
32 }
33 else if(event.keyboard.keycode == ALLEGRO_KEY_F12) {
34 if(setting.sound_on)
35 al_play_sample(sfx_camera, setting.sound_volume, 0, 1, ALLEGRO_PLAYMODE_ONCE, NULL);
36 a5_screenshot("Screenshots/DeluxePacman2");
37 }
38 break;
39 }
40
41 if(redraw && al_is_event_queue_empty(event_queue)) {
42 redraw = false;
43 al_draw_bitmap(buffer, 0, 0, 0);
44 draw_entry_box(myscore);
45 al_flip_display();
46 }
47 }
48 // Wait if a key is being held down
49 do al_wait_for_event(event_queue, &event);
50 while(event.type != ALLEGRO_EVENT_KEY_UP);
51
52 // display score once it is entered, add 10 to player # to flag the function not to check
53 // to see if the player topped the high score list, this is just to redisplay the scores.
54 score_table(score, player + 10, level);
55 }
--- |
Scooter
Member #16,799
January 2018
|
Neil: |
Neil Roy
Member #2,229
April 2002
|
My function above uses unicode to support all sorts of text as I have had people play my game from all over the world and there needed to be support for international characters etc. It wasn't difficult to be honest. The one problem I had when I first converted over to unichar or whatever in heck they are called (been a while). Was my main project files needed to be encoded as UTF-8 files. If you look at the source I shared, it works (download and run my Deluxe Pacman 2 game, that is the code straight from that game for the high score input). If you look at my code, you will see I use special Allegro functions which will assign unicode text to things like my highscore array. al_ustr_assign(hiscore[difficulty][myscore].name, yourname); Here I assign "yourname" (the name you entered) to the hiscore array, which is just the list of highscores. Here's the variables I use to store the name in... typedef struct HISCORES { ALLEGRO_USTR *name; unsigned long score; int level; char time[TIME_LEN]; // Adjust the size to match characters needed for storage } HISCORES; static HISCORES hiscore[DIFFICULTY][NUM_SCORES]; static ALLEGRO_USTR *yourname; As you can see, the "name" part of the struct where I store the text NEEDS to be an ALLEGRO_USTR in order to be able to store it properly. It may seem like a bit of a pain now, but it really is worth it to understand how to handle unicode if you wish to make anything that can be used internationally. For my Deluxe Pacman games, I have had literally millions of downloads (free game), most from South America and other foreign nations, so the unicode support was a good thing to have. If your text is not appearing it could be due to not storing it properly or using the right functions to display it. Go over your code a step at a time. 1) Am I using unicode storage? ALLEGRO_USTR etc... you can do this. I know, I was frustrated with this as well until one day when you get it right. --- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
You don't need any ALLEGRO_USTR's to use ev.keyboard.unichar . Encode it to utf-8 and then you have the ascii value in the lower byte. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Neil Roy
Member #2,229
April 2002
|
I already posted code... `if(event.keyboard.unichar >= 32) {` And what I posted, works. But feel free to piss around with other methods. --- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Feel free to needlessly complicate things by using an ALLEGRO_USTR if you want Neil. The lower byte of ev.keyboard.unichar IS ascii, due to the way it is encoded. And you have to check for the upper bound as well, not just the lower bound. If ((unichar >= 32 && unichar <= 127)) {...} My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Neil Roy
Member #2,229
April 2002
|
If you want to use pure ASCII, than ditch Unicode entirely and just do ASCII. It is absolutely pointless to have Unicode when you want ASCII. But if you want to support Unicode, than you don't want an upper boundary! In my code you absolutely do not want an upper boundary as it is a highscore input and people will be typing in their names which may be using characters that go well above 127! You do realize that there are other characters in the world right and that is the whole point of Unicode? To set boundaries to ASCII in a Unicode routine doesn't make any sort of sense. At least not for the use I have in my own code example. As soon as someone from another nation, say a French person, types in "é", limiting your characters to ASCII will miss this! --- |
Neil Roy
Member #2,229
April 2002
|
He wants user input like a user NAME, so he will want Unicode, not ASCII. As I stated, I already gave an example of using it that works and I explained why I do not have an upper limit. If you want ASCII, than don't use Unicode! But don't fucking criticize MY Unicode because it doesn't have an upper limit, it only shows off your ignorance. --- |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Listen closely you old fart. You were the one who criticized me first. I don't appreciate it. Watch your mouth. Neil Roy said: I already posted code... `if(event.keyboard.unichar >= 32) {` And what I posted, works. But feel free to piss around with other methods. If all you want is ascii, then characters above 127 are extranneous. If you want to piss around with ALLEGRO_USTRs, by all means go ahead, but for someone who only wants ASCII, it is a ton of wasted work. See how that sounds? So piss off. Seems to be your favorite saying, hope you enjoy people saying it to you. Scooter said: I would prefer that it would be in ascii text Having trouble reading? My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Neil Roy
Member #2,229
April 2002
|
"piss around with" is the same as saying "mess around with" or "screw around with" or whatever. You take offense too easily. He wanted input for names which is why I recommended using Unicode. As for my age (53, hardly old)... 1 Timothy 5:1 (NIV) --- |
LennyLen
Member #5,313
December 2004
|
Neil Roy said: He wanted input for names which is why I recommended using Unicode. Not to mention that it's usually been the policy around here to point people towards the proper way of doing things, not the quick'n'easy way.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
My apologies Neil. But you really know how to get on my nerves. As for the OP, he's clearly struggling with this, and complicating things with ALLEGRO_USTRs is bound to make things harder for him. And to be honest, the USTR api in allegro is probably my least favorite. Handling strings in C is a pain in the rear. That there isn't a better C++ standard string is quite frankly appalling, given the number of decades they've had to make one. std::string is still better than messing around with c-strings though. It stems directly from C++ and C's poor way of handling dynamic arrays. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|
|