
Allegro is a cross-platform library primarily aimed at game development, but is also widely used in multimedia programming.
In this article we will see how to install the Allegro library on Windows and compile our code with Allegro in two ways: with Visual Studio and with GCC/G++ with MinGW, in case you want to use it with a different code editor or IDE.
In addition to also knowing how to install on GNU/Linux in any distribution.
Let’s see through this step by step how to install on Windows and compile with Visual Studio first.
.cpp extension and other related ones, you can also create a .c file, in this case I will create a file with the .c extension (for C language) and name: main.c and click Add#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
int main (){
al_init();
al_init_font_addon();
ALLEGRO_DISPLAY * display = al_create_display(640,480);
al_set_window_position(display, 200, 200);
ALLEGRO_FONT* font = al_create_builtin_font();
ALLEGRO_TIMER* timer = al_create_timer(1.0 / 60.0);
ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_start_timer(timer);
while(true){
ALLEGRO_EVENT event;
al_wait_for_event(event_queue, &event);
if( event.type == ALLEGRO_EVENT_DISPLAY_CLOSE ){
break;
}
al_clear_to_color(al_map_rgb(255,255,255));
al_draw_text(font, al_map_rgb(0, 0, 0), 230, 200, 0, "Allegro is working!");
al_flip_display();
}
al_destroy_font(font);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}
Make sure you already have GCC/G++ with MinGW properly installed and configured on your Windows, see how to do it correctly in this article.
1. Now download the file: allegro-x86_64-w64-mingw32-gcc-12.1.0-posix-seh-static-5.2.8.0.zip from https://github.com/liballeg/allegro5/releases
2. Extract the file and move to C:\ drive
3. Open CMD or PowerShell (which should already be properly configured with MinGW for GCC/G++) and enter your project where there is the file(s) with the Allegro code, example: MyProject/main.c
cd MyProjectC:\allegro folder into your project:The
liballegro_monolith.dll.alibrary:
xcopy C:\allegro\lib\liballegro_monolith.dll.aAnd
allegro_monolith-5.2.dlllibrary:
xcopy C:\allegro\bin\allegro_monolith-5.2.dllgcc -I C:\allegro\include -c main.cIf you have a problem, increment the command with
-L,example:gcc -I C:\allegro\include -L C:\allegro\lib -c main.c, note that after-Lthe path is from\lib
gcc -I C:\allegro\include main.o -o program.exe liballegro_monolith.dll.aIf you have a problem, increment the command with
-L, for example:gcc -I C:\allegro\include -L C:\allegro\lib main.o -o program.exe liballegro_monolith.dll.a.
program.exe
make com MakefileIf you want to automate the compilation, use a Makefile. Create a file named Makefile (Do not use Notepad, it adds a .txt to the end of the file name, and this file cannot have an extension)
Copy the code below and paste it inside your Makefile, and save the file inside your project
TARGET=program.exe
CC=gcc
ALLEGRO=C:\allegro\include
STATIC=liballegro_monolith.dll.a
LD=gcc
OBJS=main.o
all: $(OBJS)
$(LD) -I $(ALLEGRO) -o $(TARGET) $(OBJS) $(STATIC)
$(TARGET)
main.o: main.c
$(CC) -I $(ALLEGRO) -c main.c -o main.oRename the mingw32-make.exe file in the C:\mingw64\bin\mingw32-make folder to just make.exe.
Now when you are inside your project via CMD or PowerShell, just run the make command, it will compile and run in an easier way
cd MyProject
make
Make sure you already have the build tools: compilers: gcc and g++, scons: make, pkg-config and/or others.
sudo apt install liballegro5.2 liballegro5-devsudo dnf install allegro5 allegro5-develsudo pacman -S allegrosudo emerge allegroTo compile and run with any header use the command below or the Makefile also below
gcc main.c $(pkg-config --list-all | grep -oE 'allegro_[a-z]+ ' | sed 's/^/-l/g' | tr '\n' ' ') -lallegro
./a.out
Makefile
TARGET=a.out
CC=gcc
DEBUG=-g
OPT=-O0
WARN=-Wall
ALLEGRO=-lallegro -lallegro_main -lallegro_audio -lallegro_dialog -lallegro_ttf -lallegro_image -lallegro_color -lallegro_memfile -lallegro_acodec -lallegro_primitives -lallegro_font
CCFLAGS=$(DEBUG) $(OPT) $(WARN) $(ALLEGRO)
LD=gcc
OBJS= main.o
all: $(OBJS)
$(LD) -o $(TARGET) $(OBJS) $(CCFLAGS)
@rm *.o
@./$(TARGET)
main.o: main.c
$(CC) -c $(CCFLAGS) main.c -o main.oThe example below is code written with Allegro. It’s just an animation of a Dragon and it moves when we press the arrow keys ⇐ ⇑ ⇒ ⇓.
Read the source code to understand the lines, the files used (images and font) are after the code.


Click here to download the font
main.c#include <allegro5/allegro.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_image.h>
#include <allegro5/keyboard.h>
int main (){
al_init();
al_init_font_addon();
al_init_ttf_addon();
al_install_keyboard();
ALLEGRO_DISPLAY * display = al_create_display(1280,720);
al_set_window_position(display, 200, 200);
al_set_window_title(display, "Here be Dragons!");
al_init_image_addon();
ALLEGRO_BITMAP *sprite = al_load_bitmap("./dragon.png");
ALLEGRO_BITMAP *bg = al_load_bitmap("./bg.png");
double frame = 0.f;
int pos_x = 0, pos_y = 0;
int current_frame_y = 161, velocity = 20;
ALLEGRO_FONT* font = al_load_font("./font.ttf", 40, 0);
ALLEGRO_TIMER* timer = al_create_timer(1.0 / 30.0);
ALLEGRO_EVENT_QUEUE * event_queue = al_create_event_queue();
al_register_event_source(event_queue, al_get_display_event_source(display));
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_start_timer(timer);
while(true){
ALLEGRO_EVENT event;
al_wait_for_event(event_queue, &event);
if( event.type == ALLEGRO_EVENT_DISPLAY_CLOSE ){
break;
}else if( event.keyboard.keycode == ALLEGRO_KEY_RIGHT ){
current_frame_y = 161;
pos_x += velocity;
}else if( event.keyboard.keycode == ALLEGRO_KEY_UP ){
current_frame_y = 0;
pos_y += velocity;
}else if( event.keyboard.keycode == ALLEGRO_KEY_DOWN ){
current_frame_y = 161 * 2;
pos_y -= velocity;
}else if( event.keyboard.keycode == ALLEGRO_KEY_LEFT ){
current_frame_y = 161 * 3;
pos_x -= velocity;
}
frame += 0.3f;
if( frame > 3 ){
frame -= 3;
}
al_clear_to_color(al_map_rgb(255,255,255));
al_draw_bitmap(bg, 0, 0, 0);
al_draw_text(font, al_map_rgb(0, 0, 0), 2, 2, 0, "score: dragon");
al_draw_text(font, al_map_rgb(255, 255, 255), 0, 0, 0, "score: dragon");
al_draw_bitmap_region(sprite, 191 * (int)frame, current_frame_y, 191, 161, pos_x, 720 - 161 - pos_y, 0);
al_flip_display();
}
al_destroy_font(font);
al_destroy_bitmap(bg);
al_destroy_bitmap(sprite);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}If you want the step by step of how the lines were included, watch the video below.
The video is in Portuguese, enable Youtube’s automatic translation and translate it into your language.
allegro gamedev clanguage cpp cppdaily gcc windows gnu linux