Allegro.cc - Online Community
Post Reply

Allegro.cc Forums » Programming Questions » Text input using allegro 5

rss feed Print
 1   2   3 
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
comments on this site concerning user input! All the comments seem to be somewhat over my head! I have just started using allegro 5. What I am concerned with is
getting user input from the keyboard such as a user's name. I would prefer that
it would be in ascii text using graphics mode. If someone could post a small portion
of code in C language showing a switch statement I would be most grateful. If you
would like more info I would be glad to post it. Thanks to all that would like to
help me with this problem.
There was one post by AmnesiA that was posted on 9-27-2013 @ 9:58 AM that was very
helpful but I never got anything it to work.

Edgar Reynaldo
Member #8,592
May 2007
avatar

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 :

#SelectExpand
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));

Scooter
Member #16,799
January 2018

Thanks Edgar for your prompt reply.
I will slip it in my program and see what happens.

Edgar Reynaldo
Member #8,592
May 2007
avatar

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
You can check out my TINS 2017 entry to see how to monitor text. Take a look here :

https://github.com/EdgarReynaldo/EagleGUI/blob/master/TINS2017/src/Game.cpp#L380-L479

Scooter
Member #16,799
January 2018

Good morning Edgar:
I plugged your code into my program and code:blocks generated a few errors.
Some I have no idea what to do about! So, after 3 weeks dealing with this one
problem, I have had enough! I am going to try to forget about it! Maybe later
I will try again. I appreciate your time but Allegro is too much of a problem
for me! I had to give up on Python for the exact same reason. Thanks for your
time but it appears I can't master that keyboard! Have a great day my friend!

LennyLen
Member #5,313
December 2004
avatar

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
Member #8,592
May 2007
avatar

My apologies, that code was missing brackets and other things. Try this structure instead :

#SelectExpand
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:
I plugged your code into my program and code:blocks generated a few errors.
Some I have no idea what to do about! So, after 3 weeks dealing with this one
problem, I have had enough! I am going to try to forget about it! Maybe later
I will try again. I appreciate your time but Allegro is too much of a problem
for me! I had to give up on Python for the exact same reason. Thanks for your
time but it appears I can't master that keyboard! Have a great day my friend!

Scooter,
If you keep giving up you'll never learn anything. Failure is normal. You need to keep going. When you get an error, read what it says, usually they help you figure out what is wrong. Also, you need some basic C programming skills. Python is actually not that difficult to understand.

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.

Scooter
Member #16,799
January 2018

Note to LennyLen:

I appreciate your concern!
I have accomplished many GREAT things in my lifetime.
I consider what little programming I do to be a lot of FUN!
If it ever becomes NOT FUN, then I consider it NOT WORTHWHILE!
Not everyone can become an expert in programming!

Edgar Reynaldo
Member #8,592
May 2007
avatar

We can help answer any questions you may have, but you need a good book on Programming in C. There are a lot of good websites out there too, such as http://cppreference.com (which has a good C reference as well). Once you're comfortable with C, you need to become comfortable with reading documentation for the libraries you're using, such as http://liballeg.org/a5docs/trunk/ .

Getting started : http://liballeg.org/a5docs/trunk/getting_started.html

There are tutorials for Allegro on the wiki, here :

https://wiki.allegro.cc/index.php?title=Allegro_5_API_Tutorials

If you don't understand something on one of those pages, ask here.

Scooter
Member #16,799
January 2018

Good afternoon Edgar:
I may have left the wrong impression. I have been using the C language off and
on for the last 25 to 30 years. I don't consider myself an expert but I do have many
programs I have written. I started back in the day when Borland Turbo C was popular.
I guess I just told my age. Nonetheless, I am somewhat familiar with Allegro 5. I
have a Poker game I have written and a program displaying my favorite cars in a slide
show with music playing in the background. These programs all use the mouse to
navigate through the program. I would not consider either one professional, but they
both work perfectly. My main problem is the keyboard. None of the programs I have
found are using the keyboard like I am trying to. I do not have a problem with using
the arrow keys like many of the programs show. What I want to do is press a key and
it show on the screen the key I pressed. This would be like a person's name which
would be copied to a structure like an address book. None of the programs show how
this is done in Allegro 5. I guess my problem is the syntax in Allegro 5. At my age
this is something I do for fun and fun only. I have no intention of being a pro-
fessional programmer. I appreciate your help but I wanted to let you know what my
goals were. Have a great day!

Edgar Reynaldo
Member #8,592
May 2007
avatar

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.

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.

#SelectExpand
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
30 al_utf8_encode(multibyte, unichar <= 32 ? ' ' : unichar);
31 key_name = al_keycode_to_name(keycode);
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.

Scooter
Member #16,799
January 2018

Good afternoon Edgar:

Had no luck with your code segment! Finally wrote what I needed in C, away
from Allegro. Entered my info, saved to disk and loaded into my Allegro program.
A little extra work but worked out fine! That was my only hope! There is something
about that keyboard in Allegro that I can't understand. But I got the job done
and I am happy. Too many sleepless nights worrying about it! Thanks for your time.

Neil Roy
Member #2,229
April 2002
avatar

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)

#SelectExpand
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:
Thanks for replying. I am going to try this again. I believe my problem
is the unichar. How would it look to use ascii text. Using the unichar my
program compiles fine, but when I sprintf the string in my buffer and use
textf to print buffer to screen, nothing shows on the screen. I really don't
know where my problem is. I have tried different ways and they all compile with
no warnings or errors, but I can't get a thing printed to the screen. That is
the reason I broke away from allegro and wrote it in straight C to get the job
done. I then saved my info to disk, brought up allegro and imported my info
to my structure and then printed to the screen. Everything worked as I wanted.
I believe I need to use straight ascii text and I would be fine. Any ideas on
that. Thanks for your time. This seems to be my main problem using allegro 5.
I have no problem with the arrow keys or the mouse.

Neil Roy
Member #2,229
April 2002
avatar

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
2) am I copying text using unicode functions? al_ustr_assign()
3) am I using unicode functions to display it?

etc... you can do this. I know, I was frustrated with this as well until one day when you get it right. ;)

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

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
Member #8,592
May 2007
avatar

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)) {...}

Neil Roy
Member #2,229
April 2002
avatar

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!

Edgar Reynaldo
Member #8,592
May 2007
avatar

Neil Roy
Member #2,229
April 2002
avatar

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
Member #8,592
May 2007
avatar

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?

Neil Roy
Member #2,229
April 2002
avatar

"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)
Do not rebuke an older man harshly, but exhort him as if he were your father.

LennyLen
Member #5,313
December 2004
avatar

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
Member #8,592
May 2007
avatar

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.

Post Reply
 1   2   3 


Go to: