2014-06-12 11 views
0

我想传递一个位图文件名的变量,但要加载一个位图,它需要一个const char *。是否有可能转换我的数组加载位图文件或有任何其他解决方案来解决我的问题?传递一个变量来加载位图文件?

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

我运行后程序崩溃了。 (allegro.exe已停止工作...)

+1

不知道你在问什么......有趣的是,我在你的代码中看到的是,你在文件名中包含引号。不知道这是你的图书馆所需要的,但它肯定看起来很有趣... – jsantander

+0

Allegro需要引号才能打开文件名,例如:“./images/1010.png”。我有几个图像,但只有少数应该在会话中加载,这就是为什么数字必须是可变的。 –

+0

我不确定...可能是错误的,因为我甚至不知道Allegro的用途是什么...但是使用谷歌搜索,我找到了https://www.allegro.cc/forums/thread/606701其中剪切的代码是'Image = al_load_bitmap(“Media/NextShapeBox.png”);'......没有引号。 – jsantander

回答

2

引号不属于您的缓冲区。可以将char*数组传递给需要const char*的方法。

0

是否有可能将我的数组转换为加载位图文件?

是的。

您的char buf[40]隐式转换成转换成const char*。您无需为转换发生任何代码。它是自动的。