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
Important, during the video I mention how to turn off the mouse cursor. In the video, the cursor is still visible. This is due to the recording software. I assure you it works.
Continuing our look at input in Allegro 5, it is time to talk about mouse inputs. You will find that mouse inputs (in my opinion) are easier to utilize than keyboard inputs. This makes sense, of course, if you consider how many more keys there are than mouse inputs.
This will conclude our look at inputs in Allegro 5. In Part 4 we will be looking more into the event system and learning how to make a good, consistent game loop.
Source code can be found here.
Notice that the source code is slightly different from the video? Here is why.
Thank you for writing these tutorials. I recently downloaded Allegro 5, and these have been very helpful. This is the first Allegro 5 tutorial I have seen outside of the Allegro wiki, and, to be honest, yours is much better.
Glad you like them. Stay tuned as I have more coming.
can u tutorial continue?
i want to learn fast….
Good points
Hello, Mike ! I’m a newbie programmer and with your tuts I could understand Allegro 5 better. Thank you for opening our ways in the game programming!
Hello Mike,
Can I ask you what’s the diference between draw = !draw and draw = false, doesn’t it the same thing? because in this way doesn’t work correctly. Anyway I just going to say thank you for this tutorials it’s a really great job, and very usefull.
well, draw = !draw is a toggle. That means that it will turn a true into a false, and a false into a true. If I do draw = false; it can only set draw to false and never true
Really good tutorials, very helpful
Thanks
I need some help! Im getting an error that says that event was not declared in this scope. Here’s my code:
#include //Holy allegro header
#include //Our primitive header file
int main() {
//Normal Variables
int mousePositionX;
int mousePositionY;
const int screenWidth=640;
const int screenHeight=480;
//Error Check
if(!al_init()){return -1;}
//–Initializing ALLEGRO variables–
//A-Variable Display
ALLEGRO_DISPLAY *screenBit = NULL;
screenBit = al_create_display(screenWidth,screenHeight);
if(!screenBit){return -1;}
//A-Variable Timer
ALLEGRO_TIMER *mainTimer= NULL;
//Initializing the primitives addon, along with the keyboard.
al_init_primitives_addon();
al_install_keyboard();
al_install_mouse();
//A-variable Event queue
ALLEGRO_EVENT_QUEUE *eventQueue = NULL;
eventQueue = al_create_event_queue();
//Registering sources
al_register_event_source(eventQueue,al_get_keyboard_event_source());
al_register_event_source(eventQueue,al_get_mouse_event_source());
al_register_event_source(eventQueue,al_get_display_event_source(screenBit));
//Hides mouse cursor
al_hide_mouse_cursor(screenBit);
while(true){
//Creates an event variable
ALLEGRO_EVENT event;
//Waits for the event using the event queue and telling it that the A-variable which we want to use is the event A-variable.
al_wait_for_event(eventQueue,&event);
//–EVENT CHECKS–//
//IF the user presses down a key
if(event.type==ALLEGRO_EVENT_KEY_DOWN){
//IF the key pressed was escape
if(event.keyboard.keycode==ALLEGRO_KEY_ESCAPE){
break;
}
}
break;
}
//IF the user moves the mouse
if(event.type==ALLEGRO_EVENT_MOUSE_AXES){
mousePositionX=event.mouse.x;
mousePositionY=event.mouse.y;
}
//IF the user clicks the close button
else if(event.type==ALLEGRO_EVENT_DISPLAY_CLOSE){
break;
}
}
al_destroy_display(screenBit);
return 0;
}
Nevermind…. The code didnt come out correctly in the comment.
Glad you were able to resolve it.
Is it possible to check if the cursor is on/off of our window?
Hey Mike, just noticed (through a fello a.cc member) than you use the sintax:
if(ev.mouse.button & 1)
but this would work only up to 2nd term being 2, that sohuld more correctly read
if(ev.mouse.button == 1)
Cheers
Andy
To make this rectangle be very responsible try all event checking wrap in
while (!al_is_event_queue_empty(event_queue)) {
// event checking
}
It make all events be processed before drawing next frame and avoid overflowing event_queue.
responsive* ;D
Hello, I can’t turn off the window despite clicking the red [X]. Isn’t the codes below suppose to do that escape task?
if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
done = true;
}
Oh, I found out. I don’t have the code
al_register_event_source(event_queue, al_get_display_event_source(display));