Search code examples
c++arraysbitmapallegro

Passing a variable to load a bitmap file?


I want to pass a variable in bitmap filename, but to load a bitmap it needs a const char*. Is it possible to convert my array to load the bitmap file or are there any other solutions to my problem?

#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>
#include <cstdio>
#include <iostream>

int main(){
    char buf[40], char_nr[10], png_file[10];
    int int_number = 1010;

    ALLEGRO_DISPLAY *display = nullptr; al_init();
    ALLEGRO_BITMAP *image = nullptr; al_init_image_addon();

    display = al_create_display(800,400);

    strcpy(buf, "\"./images/");
    snprintf(char_nr, sizeof(char_nr), "%d", int_number);
    strcat(buf, char_nr);
    strcpy(png_file, ".png\"");
    strcat(buf, png_file);

    image = al_load_bitmap(buf);//buf="./images/1010.png"

    std::cout << buf;//for test purposes

    al_clear_to_color(al_map_rgb(0,0,0));
    al_draw_bitmap(image, 10, 10, 0);
    al_flip_display();

    al_rest(3.0);
}

My programm crashes after I running. (allegro.exe has stopped working...)


Solution

  • The quotation marks do not belong in your buffer.

    Your char* array can be passed to a method that requires a const char*.