Subscribe to GameFromScratch on YouTube Support GameFromScratch on Patreon


6. November 2017


In the first tutorial we created a simple Hello World application using Allegro.  However, that application was very limited.  In fact, it couldn’t even be closed!  It simply looped forever.  In this example we are going to change that up slightly, by allowing the application to exit if the user hits a key or clicks the close icon on the app window.


In order to accomplish this we are going to cover the basics of handling events and keyboards.  To handle events of any kind we need to first create an event queue.  As we saw earlier with addons, in Allegro you enable the functionality you need, this includes the keyboard.  Without further ado, lets jump into our new example.

#include "stdafx.h"
#include <allegro5\allegro.h>

int main()
{
	ALLEGRO_DISPLAY * display;
	ALLEGRO_EVENT_QUEUE *queue;

	al_init();
	display = al_create_display(640, 480);
	queue = al_create_event_queue();

	al_install_keyboard();
	al_register_event_source(queue, al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_display_event_source(display));


	bool running = true;
	while (running) {
		al_flip_display();

		ALLEGRO_EVENT event;
		al_wait_for_event(queue, &event);
		if (event.type == ALLEGRO_EVENT_KEY_UP || event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			running = false;
	}

	al_destroy_display(display);
	al_uninstall_keyboard();

	return 0;
}


When run this example will simply display an empty window.  The big difference is now it can be closed using the X icon or by pressing (and releasing) any key on the keyboard.  You will also notice in this example that since we can now exit our main loop, after the main game loop we do some cleanup work.  Generally every create function has a corresponding destroy function to clean up resources used.  This is also true with install and uninstall functions.


It’s possible that you want your game loop to run at a fixed rate, for example 30 or 60 frames per second.  This is also dont using ALLEGRO_EVENT, in this case a timer.  Here is a simple tweak to the above example that causes the graphics to be updated 60 times per second.

#include "stdafx.h"
#include <allegro5\allegro.h>

int main()
{
	ALLEGRO_DISPLAY * display;
	ALLEGRO_EVENT_QUEUE *queue;
	ALLEGRO_TIMER *timer;

	al_init();
	display = al_create_display(640, 480);
	queue = al_create_event_queue();
	timer = al_create_timer(1.0 / 60.0);

	al_install_keyboard();
	al_register_event_source(queue, al_get_keyboard_event_source());
	al_register_event_source(queue, al_get_display_event_source(display));
	al_register_event_source(queue, al_get_timer_event_source(timer));

	bool running = true;
	while (running) {
		

		ALLEGRO_EVENT event;
		al_wait_for_event(queue, &event);
		if (event.type == ALLEGRO_EVENT_KEY_UP || event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
			running = false;

		if (event.type == ALLEGRO_EVENT_TIMER) {
			al_flip_display();
		}
	}

	al_destroy_display(display);
	al_uninstall_keyboard();
	al_destroy_timer(timer);

	return 0;
}


Back to Table Of Contents

Programming , ,

blog comments powered by Disqus

Month List

Popular Comments