[Tutorial] Utilizando Allegro 5 no MinGW
Faz, mais ou menos, 1 mês que estou tentando linkar a biblioteca Allegro 5 em meu código fonte. Existem diversos tutoriais que ensinam utilizando IDEs, mas eu quero fazer ele na "unha". Então com a ajuda de Renato Utsch e Karl Phillip Buhrdss consegui resolver meu problema e agora desejo compartilhar a solução com vocês do iMasters.
Makefile:
CXX = g++
CFLAGS = -Wall -Werror
LDFLAGS = -L/lib -lallegro-5.0.3-mt -lallegro_acodec-5.0.3-mt -lallegro_audio-5.0.3-mt \
-lallegro_color-5.0.3-mt -lallegro_dialog-5.0.3-mt -lallegro_font-5.0.3-mt \
-lallegro_image-5.0.3-mt -lallegro_memfile-5.0.3-mt -lallegro_physfs-5.0.3-mt \
-lallegro_primitives-5.0.3-mt -lallegro_ttf-5.0.3-mt -ldumb-0.9.3-static-mt \
-lFLAC-1.2.1-static-mt -lfreetype-2.4.4-static-mt -logg-1.2.1-static-mt -lzlib-1.2.5-static-mt
INCLUDE = -I/include
SRC = src/main.cpp
OUT = WarWorld
build: $(SRC)
$(CXX) -o $(OUT) $(SRC) $(INCLUDE) $(CFLAGS) $(LDFLAGS)
src/main.cpp
// STD Librarys
#include <iostream>
// Other Librarys
#include "wtypes.h"
// Allegro5 Librarys
#include <allegro5/allegro.h>
// Prototype
void GetDesktopResolution(int& horizontal, int& vertical);
const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int BOUNCER_SIZE = 32;
// Main
int main( void ) {
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *bouncer = NULL;
float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
float bouncer_dx = -4.0, bouncer_dy = 4.0;
bool redraw = true;
if(!al_init()) {
std::cout << "[ERROR] Failed on initialize Allegro!" << std::endl;
return -1;
}
timer = al_create_timer(1.0 / FPS);
if(!timer) {
std::cout << "[ERROR] Failed to create timer!" << std::endl;
return -1;
}
al_set_new_display_flags(ALLEGRO_FULLSCREEN);
display = al_create_display(SCREEN_W, SCREEN_H);
if(!display) {
std::cout << "[ERROR] Failed on create interface!" << std::endl;
return -1;
}
bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
if(!bouncer) {
std::cout << "[ERROR] Failed on create bitmaps!" << std::endl;
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
al_set_target_bitmap(bouncer);
al_clear_to_color(al_map_rgb(255, 0, 255));
al_set_target_bitmap(al_get_backbuffer(display));
event_queue = al_create_event_queue();
if(!event_queue) {
std::cout << "[ERROR] Failed on create bitmaps!" << std::endl;
al_destroy_bitmap(bouncer);
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}
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_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_start_timer(timer);
while(1) {
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if(ev.type == ALLEGRO_EVENT_TIMER) {
if(bouncer_x < 0 || bouncer_x > SCREEN_W - BOUNCER_SIZE) {
bouncer_dx = -bouncer_dx;
}
if(bouncer_y < 0 || bouncer_y > SCREEN_H - BOUNCER_SIZE) {
bouncer_dy = -bouncer_dy;
}
bouncer_x += bouncer_dx;
bouncer_y += bouncer_dy;
redraw = true;
}
else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
break;
}
if(redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(al_map_rgb(0,0,0));
al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
al_flip_display();
}
}
al_destroy_bitmap(bouncer);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);
return 0;
}
// Functions
void GetDesktopResolution(int& horizontal, int& vertical) {
RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
horizontal = desktop.right;
vertical = desktop.bottom;
}
O código do main.cpp, foi retirado do Wiki do Allegro 5. Foi alterado alguns detalhes para funcionar em fullscreen.
Obs: Em sua pasta /lib deve conter todas os arquivos *.a disponível no pacote binário do Allegro 5, e na pasta /include deve conter os arquivos que estão na pasta /include do pacote dos binários do Allegro5.
Discussão (1)
Carregando comentários...