AndreaTerenz
AndreaTerenz

Reputation: 45

How to put an image as background (Allegro 5)

I am new to Allegro (5) and I am making my first game. I need to put an image as background and a little square that moves in front of it. In my code, on each frame the image is drawn at the (0;0) coordinates and then the square is drawn at (something;something), and, I think, it should appear over the image because it was drawn after it, but it doesn't.

Can someone explein me how to do this? Thanks

PS : Looking around in the Internet I found that this could have something to do with blitting, could someone explain me what that operation is?

Upvotes: 2

Views: 3338

Answers (1)

Mr. Branch
Mr. Branch

Reputation: 442

I'll post the code to just draw the background on display. And I'll explain it little by little

#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;
}

This part starts up allegro, al_init starts up the allegro system, and al_init_image_addon lets us use bitmaps which is how allegro manages images:

    if(!al_init()) {
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
    }
    al_init_image_addon();//should check for errors

Here we create a display and check if it was successfully created:

    display = al_create_display(640, 480);
    if(!display) {
        fprintf(stderr, "failed to create display!\n");
        return -1;
    }

Here we load an image, you just simply put in the file name and allegro will load it for you returning a ALLEGRO_BITMAP * (NULL if unsuccessful). After calling al_load_bitmap we check if the bitmap was successfully loaded.

background=al_load_bitmap(BACKGROUND_FILE);
if(!background)
{
    fprintf(stderr, "failed to load background bitmap!\n");
    return -1;
}

Allegro draws to a backbuffer, what this means is that it won't directly draw to the display. Instead, it will draw to a copy of the display(backbuffer) and once you flip the backbuffer(al_flip_display()), this copy (which you were drawing on top of) will show up all at once.

This can be see seen here:

al_draw_bitmap(background,0,0,0);

al_flip_display();

If you're going to be initializing lots of allegro stuff, you might want to initialize all together like for example :

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 are a lot of tutorials which are a lot more clearer and a little broader subjects if you're interested.

Upvotes: 4

Related Questions