編譯器: TDM GCC 4.6.1
以下教學是讓GCC以Static Build編譯Allegro程式,因為我覺得Static Build比較好
如偏好Dynamic Build可直接看官方wiki教學 (Static和Dynamic比較)
本文撰寫時最新穩定版為5.0.6 如官網有更新請使用最新版
先安裝TDM GCC4.6.1
安裝完畢後,到Allegro官網下載MinGW 4.6.1 (TDM)版Binaries (檔名allegro-5.0.6-mingw-4.6.1-tdm.7z)
下載後打開壓縮檔,將include資料夾丟到MinGW主錄錄中
而lib裡複製以下檔案尾巴是-static-mt.a的19個檔案:
liballegro_acodec-5.0.6-static-mt.a
liballegro_audio-5.0.6-static-mt.a
liballegro_color-5.0.6-static-mt.a
liballegro_dialog-5.0.6-static-mt.a
liballegro_font-5.0.6-static-mt.a
liballegro_image-5.0.6-static-mt.a
liballegro_memfile-5.0.6-static-mt.a
liballegro_physfs-5.0.6-static-mt.a
liballegro_primitives-5.0.6-static-mt.a
liballegro_ttf-5.0.6-static-mt.a
liballegro-5.0.6-monolith-static-mt.a
liballegro-5.0.6-static-mt.a
libdumb-0.9.3-static-mt.a
libFLAC-1.2.1-static-mt.a
libfreetype-2.4.8-static-mt.a
libogg-1.2.1-static-mt.a
libvorbis-1.3.2-static-mt.a
libvorbisfile-1.3.2-static-mt.a
libzlib-1.2.5-static-mt.a
也可以另外開新資料夾放這些檔案
例如我是放在(MinGW目錄)\lib\allegro5
接著開啟Code::Blocks,開新專案,類型選擇Console Application
剩下的部份視需要自行設定
引入Allegro函式庫方法
專案建好後,進入專案的Properties(左邊專案按右鍵)
選Built Targets→Build Options
左邊樹狀目錄點一下最上面的專案名稱,選Linker Setings
左邊的Link Libraries選擇Add,將下列字串貼到路徑欄:
(安裝目錄不同自己用記事本取代修改)
C:\MinGW32\lib\liballegro-5.0.6-monolith-static-mt.a;C:\MinGW32\lib\liballegro-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_acodec-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_audio-5.0.6-static-mt.a;C:\MinGW32\lib\libvorbisfile-1.3.2-static-mt.a;C:\MinGW32\lib\libvorbis-1.3.2-static-mt.a;C:\MinGW32\lib\liballegro_color-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_dialog-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_font-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_image-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_memfile-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_physfs-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_primitives-5.0.6-static-mt.a;C:\MinGW32\lib\liballegro_ttf-5.0.6-static-mt.a;C:\MinGW32\lib\libdumb-0.9.3-static-mt.a;C:\MinGW32\lib\libFLAC-1.2.1-static-mt.a;C:\MinGW32\lib\libfreetype-2.4.8-static-mt.a;C:\MinGW32\lib\libogg-1.2.1-static-mt.a;C:\MinGW32\lib\libzlib-1.2.5-static-mt.a;
再選一次Add,貼上下列字串到路徑欄裡:
(安裝目錄不同自己用記事本取代修改)
C:\MinGW32\lib\libgdiplus.a;C:\MinGW32\lib\libuuid.a;C:\MinGW32\lib\libkernel32.a;C:\MinGW32\lib\libwinmm.a;C:\MinGW32\lib\libpsapi.a;C:\MinGW32\lib\libopengl32.a;C:\MinGW32\lib\libglu32.a;C:\MinGW32\lib\libuser32.a;C:\MinGW32\lib\libcomdlg32.a;C:\MinGW32\lib\libgdi32.a;C:\MinGW32\lib\libshell32.a;C:\MinGW32\lib\libole32.a;C:\MinGW32\lib\libadvapi32.a;C:\MinGW32\lib\libws2_32.a;C:\MinGW32\lib\libshlwapi.a
確定後儲存專案即可編寫Allegro 5.0.6程式
範例程式:
#include <stdio.h>
#include <allegro5/allegro.h>
const float FPS = 60;
const int SCREEN_W = 640;
const int SCREEN_H = 480;
const int BOUNCER_SIZE = 32;
int main(int argc, char **argv){
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()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
timer = al_create_timer(1.0 / FPS);
if(!timer) {
fprintf(stderr, "failed to create timer!\n");
return -1;
}
display = al_create_display(SCREEN_W, SCREEN_H);
if(!display) {
fprintf(stderr, "failed to create display!\n");
al_destroy_timer(timer);
return -1;
}
bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
if(!bouncer) {
fprintf(stderr, "failed to create bouncer bitmap!\n");
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) {
fprintf(stderr, "failed to create event_queue!\n");
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;
}
參考資料:http://wiki.allegro.cc/index.php?title=Windows_Vista,_Code::Blocks_10.05_and_Allegro_5
http://wiki.allegro.cc/index.php?title=Allegro_5_Tutorial/Bitmaps
沒有留言:
張貼留言