我是Allegro的新手( 5 ),我正在制作我的第一个游戏。我需要把一个图像作为背景和一个在它前面移动的小方块。 在我的代码中,在每个帧上,图像在(0; 0)坐标处绘制,然后方形被绘制在(某事物;某些东西),并且,我认为它应该出现在图像上,因为它是在它之后绘制的,但它没有。
有人可以告诉我该怎么做吗?感谢
PS:在互联网上环顾四周,我发现这可能与 blitting 有关,有人可以解释一下这个操作是什么吗?答案 0 :(得分:2)
我会发布代码,只是在显示屏上绘制背景。我会一点一点地解释它
#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是很多教程,如果你感兴趣的话会更清晰,更广泛的主题。