Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 传递变量以加载位图文件?_C++_Arrays_Bitmap_Allegro - Fatal编程技术网

C++ 传递变量以加载位图文件?

C++ 传递变量以加载位图文件?,c++,arrays,bitmap,allegro,C++,Arrays,Bitmap,Allegro,我想在位图文件名中传递一个变量,但加载位图需要一个常量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];

我想在位图文件名中传递一个变量,但加载位图需要一个常量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);
}
#包括
#包括
#包括
#包括
int main(){
char buf[40],char_nr[10],png_文件[10];
整数=1010;
快板显示*DISPLAY=nullptr;al_init();
ALLEGRO_BITMAP*image=nullptr;al_init_image_addon();
显示=al_创建_显示(800400);
strcpy(buf,“\”/images/”;
snprintf(字符号,大小(字符号),%d”,整数);
strcat(buf,字符);
strcpy(png_文件“.png\”);
strcat(buf、png_文件);
image=al_load_bitmap(buf);//buf=“./images/1010.png”

std::cout引号不属于缓冲区

您的
char*
数组可以传递给需要
const char*

的方法是否可以将我的数组转换为加载位图文件


你的
charbuf[40]
隐式转换为
const char*
。转换不需要任何代码。它是自动的。

不确定你问什么…我在代码中看到的有趣之处是你在文件名中包含引号。不确定你的库是否需要引号,但它看起来确实很有趣…Allegro需要引号tes打开一个文件名,例如:“./images/1010.png”。我有几个图像,但在一个会话中只能加载很少的图像,这就是为什么数字必须是可变的。我不确定这一点……可能是错误的,因为我甚至不知道Allegro是干什么的……但是通过谷歌搜索,我发现截取的代码是
Image=al\u load\u位图(“Media/NextShapeBox.png”);
…无引号。