0

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...)

AJ_83
  • 289
  • 1
  • 8
  • 22
  • 1
    Not sure what you ask... Something funny I see in your code is that you include quotes in the filename. Not sure if that's required by your library, but it definitely looks funny... – jsantander Jun 12 '14 at 17:07
  • Allegro requires quotes to open a filename, for example: "./images/1010.png". I have several images, but only few should be loaded in a session, that's why the number has to be variable. – AJ_83 Jun 12 '14 at 17:14
  • I'm not sure about that... might be wrong, since I don't even know what Allegro is for... but googling, I found https://www.allegro.cc/forums/thread/606701 where the code snipped is `Image = al_load_bitmap("Media/NextShapeBox.png");` ... no quotes. – jsantander Jun 12 '14 at 17:18

2 Answers2

2

The quotation marks do not belong in your buffer.

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

gsamaras
  • 71,951
  • 46
  • 188
  • 305
ScottMcP-MVP
  • 10,337
  • 2
  • 15
  • 15
0

Is it possible to convert my array to load the bitmap file?

Yes.

Your char buf[40] is implicitly convertible to a const char*. You don't need to code anything for the conversion to happen. It's automatic.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180