This post is a portion of Part 3 in my on going series about 2D Game Development using the Allegro 5 library. These posts are in course order. You can see all of the posts in this course by clicking the “2D Game Development” button at the top of this site.
- Part 3.0: Player Input and Events
- Part 3.1: Keyboard Input Part 1
- Part 3.2: Keyboard Input Part 2
- Part 3.3: Mouse Inputs in Allegro 5
In this video we will be looking at the basic usage of keyboard input in Allegro 5. We will look at two new variable types:
ALLEGRO_EVENT_QUEUE * ALLEGRO_EVENT
Source code can be found here.
Notice that the source code is slightly different from the video? Here is why.
I really liked the article, and the very cool blog
I wish you used youtube. The random ads in the video are annoying and time-consuming
Actually, I used Youtube to start but it was terrible. They only let you have 10 minute videos.
I am in the process of converting all of my video to self hosting. My newer ones are like that. No more advertisements. Eventually all of mine will be that way
on this one i got an error when typing in
al_clear_to_color(al_map_rgb(0,0,0));
it doesnt like where i put it which is the same as where u put it?
wow again i copied and pased into a new project and it worked?
That is odd. I am not sure why that would happen.
idk why but when i first wrote this code, i was able to move multiple times by hold the key down
Strange
Hey Mike, no problems here, which is new for me when it comes to c++. Just wanted to let you know how much I appreciate your site, best tutorials I’ve found so far.
Wish you had posted as text, I hate videos cause they’re harder to refer back to when you’ve forgotten a single thing or two…
Hello Mike, im just starting using allegro and your tutorials help alot!
Just one thing the source code u posted here, has an error. The second include is written wrong.
It has this : #include
when it should be: #include
(the slash is the wrong one).
Couldn’t test because of that.
Cheers and keep up the good work
Hey! Thanks for the tutorial! Really helpfull! But i have a problem:
After typing:
“al_register_event_source(event_queue, al_get_keyboard_event_source());”
I get an error message in the command-window:
“Assertion failed: new_keyboard_driver, file allegro-5.0.xsrckeybdnu.c, line 195”
Any idea what that might be?
—
Edvin
Did you install the keyboard?
al_install_keyboard();
These tutorials are awesome, but i have a problem… My program closes after one dot is placed… this is the code
#include
#include
#include
#include
#include
int main ()
{
int width=640;
int height=480;
bool done = false;
int pos_x= width/2;
int pos_y= height/2;
ALLEGRO_DISPLAY *display =NULL;
ALLEGRO_EVENT_QUEUE *eventqueue =NULL;
if (!al_init())
{
al_show_native_message_box (NULL, NULL, NULL,
“FAILED TO INITIALIZE ALLEGRO”, NULL, NULL);
return -1;
}
display = al_create_display (width, height);
if (!display)
{
al_show_native_message_box (NULL, NULL, NULL,
“FAILED TO INITIALIZE display”, NULL, NULL);
return -1;
}
al_init_font_addon();
al_init_ttf_addon();
ALLEGRO_FONT *font24 = al_load_font (“arial.ttf”, 24, 0);
ALLEGRO_FONT *font36 = al_load_font (“arial.ttf”, 36, 0);
ALLEGRO_FONT *font18 = al_load_font (“arial.ttf”, 18, 0);
al_init_primitives_addon();
al_install_keyboard();
eventqueue = al_create_event_queue();
al_register_event_source(eventqueue, al_get_keyboard_event_source());
// do not code above this, above this is initializations
al_clear_to_color (al_map_rgb (0,0,0));
do
{
ALLEGRO_EVENT ev;
al_wait_for_event (eventqueue, &ev);
if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch (ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
pos_y -= 10;
break;
case ALLEGRO_KEY_DOWN:
pos_y += 10;
break;
case ALLEGRO_KEY_LEFT:
pos_x -= 10;
break;
case ALLEGRO_KEY_RIGHT:
pos_x += 10;
break;
}
if (ev.type == ALLEGRO_EVENT_KEY_UP)
{
if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
done = true;
}
al_draw_rectangle(pos_x,pos_y,pos_x-1, pos_y-1, al_map_rgb (255,255,0),1);
al_flip_display();
al_destroy_display(display);
return 0;
}
}while (done == false);
}
pls help
I have a few (quite minor) problems.
First, until I press an arrow key, the background colour is white, not black. After a key is pressed, the background becomes the correct colour.
Another problem is that when I hold a key down, the square continues to move. I can’t find any differences between the video and my code which could cause this, yet this isn’t supposed to happen.
Finally, just wondering, should a command prompt box appear when debugging? I haven’t noticed it in any videos so far…
I can send/post my code if necessary. Thanks in advance.
It’s because of the timer event. If you want
to update it before you have to press any key
or even move out the mouse you must create a
timer and start it. So you could verify if
the ALLEGRO_EVENT_TIMER is on and it will
redraw every time you verify the events on
your main loop.
#define FPS 60
ALLEGRO_TIMER * timer = al_create_timer(1.0 * FPS);
al_start_timer(timer);
while(true){
…
}
Hey Mike
My screen is translucent until I start drawing with your code, I am not sure what I am doing wrong with the IDE.
Eddy
For everyone having the white screen before movement, it is because you aren’t applying a color and updating the screen before the program waits for input.
put
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_filled_circle(pos_x, pos_y, 20, al_map_rgb(255, 255, 255));
al_flip_display();
right before your while loop (a.k.a. game loop) where
al_draw_filled_circle(pos_x, pos_y, 20, al_map_rgb(255, 255, 255));
is the shape of your moving object. (i used a white a circle 😛 )
Also, Mike I love the tutorials, Thank you so much for all the help.
Hi Mike,
Great video series!
Any reason why the event variable can’t be defined above the while loop? Not sure why, it just strikes me as odd defining it in the body of the while loop.
thanks,
-sinoodle
Hey! Awesome tutorials, without it i wouldn’t return to allegro… And one little question(like everyone here), how to get that hints which pops out when starting to write a function or object member? (For example, when you type “ev” the list of available members, and you can click “type” and get “ev.type). This is a great help when writing function parameters
Hello Mike! I’m really liking these videos, you have a very efficient way of teaching.
However, I think you made a little mistake at 12:20 in the video. You said that the events KEY_DOWN and KEY_UP can’t happen at the same time. I think that, for example, if a person would hold the left key, then press the right key at the same milisecond as he releases the left key, the events would both happen at the same time.
Thanks for the lessons and keep making cool things!
Your code for textinput1.cpp is no longer on the dropbox location or has a new file name.
The link works for me. Are you getting an error?
hey Mike! in part 3.0 you mentioned the “procedural way” of creating a game loop and told us why the event driven way is better, tnx for that. this kind of explanation really helps to understand the topic form the ground.
bt can u show us one example of the “procedural way”? i would be really helpful.
+
if you could elaborate how event driven system works it would be really helpful
another ques, why did you declare the EVENT_QUEUE variable inside the while loop? why not outside the while loop? is there any particular reason?
whenever I run this I am getting a transparent screen, when I press a button it says “Allegro.exe has stopped working”. I copy and pasted the source code your exact source code and I am still having this issue. The only difference is that I am using code::blocks with a static link instead of visual studio 2010. Do you have any idea why this is happening or what I can do to fix it?
When you typed the ‘&’ symbol before ‘ev’ you said, “A dereferenced ev”. Dereferencing is actually done by the ‘*’ symbol, whereas the ‘&’ symbol means, “The address of”. You are sending the address of ev (using &) and not the data it points to (which would be *).
Anyway, great tutorials 🙂
Ya, I always say them wrong.
should’t you destroy the event_queue at the end of the code? It’s a pointer right? 😀
wait, now i’ve noticed the “Here is why.” under the video so nevermind.
The code doesn’t work for me. The square only appears when the y value is negative, and then it is really big.
Nevermind, seems my game recording software, PlayClaw, was somehow causing that problem. Works fine now. Aside from that issue, I’ve been really enjoying these tutorials. Best tutorials I have ever found on the internet probably.