我将发布代码以在显示屏上绘制背景。我会一点一点地解释它
#include <stdio.h>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#define BACKGROUND_FILE "background.png"
int main(void){
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_BITMAP *background=NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
al_init_image_addon();//should check for errors
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
background=al_load_bitmap(BACKGROUND_FILE);
if(!background)
{
fprintf(stderr, "failed to load background bitmap!\n");
return -1;
}
al_draw_bitmap(background,0,0,0);
al_flip_display();
al_rest(2.0);
al_destroy_display(display);
al_destroy_bitmap(background);
al_uninstall_system();
return 0;
}
这部分启动 allegro,al_init 启动 allegro 系统,al_init_image_addon 让我们使用位图,这是 allegro 管理图像的方式:
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
al_init_image_addon();//should check for errors
这里我们创建一个显示并检查它是否创建成功:
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
这里我们加载一个图像,您只需输入文件名,allegro 就会为您加载它并返回一个 ALLEGRO_BITMAP *(如果不成功则为 NULL)。调用 al_load_bitmap 后,我们检查位图是否加载成功。
background=al_load_bitmap(BACKGROUND_FILE);
if(!background)
{
fprintf(stderr, "failed to load background bitmap!\n");
return -1;
}
Allegro 绘制到后备缓冲区,这意味着它不会直接绘制到显示器。相反,它将绘制到显示(后备缓冲区)的副本,并且一旦您翻转后缓冲区(al_flip_display()),这个副本(您正在其上绘制)将立即显示出来。
可以在这里看到:
al_draw_bitmap(background,0,0,0);
al_flip_display();
如果你要初始化很多 allegro 的东西,你可能想一起初始化,例如:
int allegro_startup(void)
{
if(al_init())
{
if(al_init_primitives_addon())
{
if(al_install_keyboard())
{
if(al_install_mouse())
{
if(al_init_image_addon())
{
al_init_font_addon(); //Void
if(al_init_ttf_addon())
{
if(al_install_audio())
{
if(al_init_acodec_addon())
{
if(al_reserve_samples(1))
{
return AL_STARTUP_SUCCESS;
}
else
fprintf(stderr,"ERROR: Failed to reserve samples:(\n");
//al_shutdown_acodec_addon(); Does not exist
}
else
fprintf(stderr,"ERROR: Failed to initialize acodec addon\n");
al_uninstall_audio();
}
else
fprintf(stderr,"ERROR: Failed to install audio\n");
al_shutdown_ttf_addon();
}
else
fprintf(stderr,"ERROR: Failed to initialize ttf addon\n");
al_shutdown_font_addon();
al_shutdown_image_addon();
}
else
fprintf(stderr,"ERROR: Failed to initialize image addon\n");
al_uninstall_mouse();
}
else
fprintf(stderr,"ERROR: Failed to install mouse\n");
al_uninstall_keyboard();
}
else
fprintf(stderr,"ERROR: Failed to load primitives addon\n");
al_shutdown_primitives_addon();
}
else
fprintf(stderr,"ERROR: Failed to install keyboard\n");
al_uninstall_system();
}
else
fprintf(stderr,"ERROR: Failed to initialize allegro system\n");
return AL_STARTUP_ERROR;
}
void allegro_shut_down(ALLEGRO_DISPLAY *display,ALLEGRO_EVENT_QUEUE *event_queue)
{
al_destroy_display(display);
al_destroy_event_queue(event_queue);
al_uninstall_audio();
al_shutdown_ttf_addon();
al_shutdown_font_addon();
al_shutdown_image_addon();
al_uninstall_mouse();
al_uninstall_keyboard();
al_shutdown_primitives_addon();
al_uninstall_system();
}
Here 有很多教程,如果您有兴趣,它们会更清晰,主题更广泛。