Search code examples
c++bitmapallegro5

Allegro 5 how to hide/unload a bitmap


I am programming a simple 2D game and I am encountering a small but annoying problem, I can't unload bitmaps. For example, when the game launches a splash screen appears with an image that is supposed to unload after 2 seconds (like in any games, when it launches, we can see the name of the company that made it...). After 2 seconds the bitmap isn't unloaded.

Code:

void SplashScreen::loadContent()
{   
    image1 = al_load_bitmap("SplashScreen/image1.png");
    image2 = al_load_bitmap("SplashScreen/image2.png");

    al_draw_bitmap(image1, 0, 0, NULL);
    al_flip_display();
    al_rest(2);
    al_destroy_bitmap(image1);
    al_flip_display();

    al_draw_bitmap(image2, 0, 0, NULL);
    al_flip_display();
    al_rest(2);
    al_destroy_bitmap(image2);
    al_flip_display();
}

Thank you for your help and your viewing.


Solution

  • To 'unload' your bitmap, there are some other things you have to do. There is no such thing as 'clearing 1 bitmap without clearing the screen', at least not in the way you want to do it.

    In this case I think you actually do want to clear the full screen.

    You want something like this

    void SplashScreen::loadContent() {   
        image1 = al_load_bitmap("SplashScreen/image1.png");
        image2 = al_load_bitmap("SplashScreen/image2.png");
    
        //clear screen
        clearScreen();
    
        //draw your splash
        al_draw_bitmap(image1, 0, 0, NULL);
    
        //display it
        al_flip_display();
    
        //wait for 2 seconds
        al_rest(2);
    
        //fresh new frame
        clearScreen();
    
        //draw this second image, I don't know what this is
        al_draw_bitmap(image2, 0, 0, NULL);
    
        //display it
        al_flip_display();
    
        //wait for 2 seconds
        al_rest(2);
    
        //fresh screen again
        clearScreen();
    
        //display it
        al_flip_display();
    }
    

    The general drawing procedure for a game goes something like this
    1-clear your screen
    2-draw everything you want
    3-flip the display
    4-wait a few milliseconds
    5-restart back at 1

    This way you get a fresh screen at the start of each frame. The only difference for a splash screen is you wait 2 seconds, not a few milliseconds

    At the end of your program, call destroy_bitmap to free the resources.