如何将图像作为背景 (Allegro 5)
How to put an image as background (Allegro 5)
我是 Allegro 的新手 (5),我正在制作我的第一款游戏。我需要将图像作为背景,并在其前面移动一个小方块。
在我的代码中,在每一帧上,图像都绘制在 (0;0) 坐标处,然后正方形绘制在 (something;something) 处,而且我认为,它应该出现在图像上方,因为它是在它之后绘制的,但事实并非如此。
谁能告诉我该怎么做?谢谢
PS : 上网查了下发现这可能和blitting有关,谁能解释一下那个操作是什么?
我将 post 代码仅在显示时绘制背景。我会一点一点解释
#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();
如果你要初始化很多快板的东西,你可能想要一起初始化,例如:
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 有很多教程,如果您感兴趣的话,这些教程更清晰,主题更广泛。
我是 Allegro 的新手 (5),我正在制作我的第一款游戏。我需要将图像作为背景,并在其前面移动一个小方块。 在我的代码中,在每一帧上,图像都绘制在 (0;0) 坐标处,然后正方形绘制在 (something;something) 处,而且我认为,它应该出现在图像上方,因为它是在它之后绘制的,但事实并非如此。
谁能告诉我该怎么做?谢谢
PS : 上网查了下发现这可能和blitting有关,谁能解释一下那个操作是什么?
我将 post 代码仅在显示时绘制背景。我会一点一点解释
#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();
如果你要初始化很多快板的东西,你可能想要一起初始化,例如:
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 有很多教程,如果您感兴趣的话,这些教程更清晰,主题更广泛。