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 continue our look at keyboard input in Allegro 5. In the previous video, we did not have a way to track “holding” a button for continuous movement. You will see that using an enumeration and an array of boolean values that more in depth input it easy.
Source code can be found here.
Notice that the source code is slightly different from the video? Here is why.
All tutorials are great! How did you learned allegro 5.0 so fast? Just from the manual?
That and Allegro’s great forum community
Hey man, I appreciate Your tutorials alot. I have a half-term task to create a 2D game and Your works done here is really helpful for me.
About the keyboard, I was wondering how to ged rid of the short “lag” when pushing 2nd button. I mean, when we hold down right arrow and during movement push e.g. upper arrow, the movement is not continuous, the squere stops for a moment. Will it be enought just to make our event depend on timer and FPS?
Go on to the next section. We fix it when we time our games.
Yeah, I just wanted to write that I reached 4.2 tutorial and I found the answer 😉
Hi Mike,
I noticed that the video in this section is supposed to be Keyboard Input in Allegro 5 Part 2. But it’s showing Basic Mouse Input in Allegro 5 which I think belongs to the next section.
Thanks, I need to fix that
I think this chapter is a huge jump for beginners for two reasons. The first is that you actually begin to learn at a more than basic level how the computer is processing all keyboard and mouse key presses/movements/clicks. I love the awe on eager faces at the realization that pressing a key and letting go are actually two different events. =) But more so I think it is important because for the first time programmers can see a reaction between what they are physically doing on keyboard and mouse, and what is happening on the screen. I can already see millions of games forming in my mind just after seeing this video. It really starts to build up from here on out for me. And I can honestly say that this is the best tutorial I have ever seen for it. Mike, you…are…amazing. Your students are incredibly lucky to have a teacher like you. You’re my idol. =) I really hope you make it to GDC. Good luck and THANK YOU. =D
I would just like say I agree completely! 🙂
I got a problem….
when ever i try to use al_draw_text(); in this program.. it builds perfectly but dont debug….Take a look at it…
#include
#include
#include
#include
enum KEYS {UP,DOWN,LEFT,RIGHT};
int main()
{
int x=1280;
int y=720;
int pos_x=x/2;
int pos_y=y/2;
bool keys[4]={false,false,false,false};
bool done=false;
ALLEGRO_DISPLAY *display=NULL;
ALLEGRO_EVENT_QUEUE *ev_queue;
al_init();
display=al_create_display(x,y);
al_init_font_addon();
al_init_ttf_addon();
al_draw_text(al_load_font(“alba.ttf”,62,NULL),al_map_rgb(255,255,255),pos_x,30,1,”GDROCKERS”);
al_init_primitives_addon();
al_install_keyboard();
ALLEGRO_COLOR color = al_map_rgb(255,0,255);
ev_queue=al_create_event_queue();
al_register_event_source(ev_queue,al_get_keyboard_event_source());
al_register_event_source(ev_queue,al_get_display_event_source(display));
al_draw_filled_rectangle(pos_x-20,pos_y-20,pos_x+20,pos_y+20,color);
al_flip_display();
while(!done)
{
ALLEGRO_EVENT ev;
al_wait_for_event(ev_queue, &ev);
if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP]=true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN]=true;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT]=true;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT]=true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
done=true;
else if(ev.type = ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_UP:
keys[UP]=false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN]=false;
break;
case ALLEGRO_KEY_LEFT:
keys[LEFT]=false;
break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT]=false;
break;
case ALLEGRO_KEY_ESCAPE:
done=true;
break;
}
}
pos_y -= keys[UP] *20;
pos_y += keys[DOWN] *20;
pos_x -= keys[LEFT]*20;
pos_x += keys[RIGHT]*20;
al_draw_text(al_load_font(“alba.ttf”,62,NULL),al_map_rgb(255,255,255),pos_x,32,1,”GDROCKERS”);
al_draw_filled_rectangle(pos_x-20,pos_y-20,pos_x+20,pos_y+20,color);
al_flip_display();
al_clear_to_color(al_map_rgb(0,0,0));
}
al_destroy_display(display);
return 0;
}
#include
#include
#include
#include
Dont move on holding????????
allegro5allegro.h
allegro5allegro_primitives.h
allegro5allegro_font.h
allegro5allegro_ttf.h
missing in front of include…
So, total idiot question. I know my initial thoughts are wrong, but I’d like for someone to explain why they’re wrong pretty quickly…
It looks to me like the position update equations are handled within the same loop that is waiting for events… So why does it continue to run and update their position without additional events occurring? I understand the keyboard status is tracked, but it still looks to me like it shouldn’t reach those commands unless al_wait_for_event isn’t a true wait?
I was wondering the same thing, could someone please clarify =]
I’m actually wondering the same thing. At least according to the manual(http://www.allegro.cc/manual/5/al_wait_for_event), I undestand that the function would wait until a new event comes, so the loop should get stuck there. Is it reciving any other type of event?
It would be great if someone could clarify this.
It is been almost an year since the question has been made. But since it is an interesting question it seems worthy answering it…
I printed every key event that my program was capturing (event.type) and this is what I got:
When I pressed a key it printed the code 10 (KEY_DOWN), but if I kept pressing it, it would print code 11 (KEY_CHAR event). Whenever I released the key … it would print code 12 (KEY_UP event)
So the thing is. When you are holding the key an event in being triggered every time, and that event is called ALLEGRO_EVENT_KEY_CHAR
Since we are only checking for KEY_DOWN events in the first video, the square would move only one time.. the one time the code was KEY_DOWN. The next loop it would turn to code 11 and ignore the if-statement.
In the second video the movement is done outside the if, so it is executed every single frame, since code 11 is fired every single frame.
awesome answer mate!
one ques, how did you print the eventType?
hey mike,i need an explanation over this thing..:the block when pressed down/left/right/up continuously it moves in the specified direction and moves across the specified window boundary.Also it returns back when u press the arrow keys accordingly…so where does this block go actually?i mean it should not cross the window boundary and enter into it rather stop whenever it encounters the boundary…any clues..thanxs for the great tut anyways..!
It just goes off screen. The coordinates keep being changes and Allegro is smart enough to not render things that can’t be seen.
How about combining the statements to
pos_y += keys[RIGHT] * 10 - keys[LEFT] * 10;
but I’m not sure about it being simpler nor faster.pos_x += keys[DOWN] * 10 - keys[UP] * 10;