【问题标题】:Passing a variable to load a bitmap file?传递变量来加载位图文件?
【发布时间】:2014-06-12 17:00:51
【问题描述】:

我想在位图文件名中传递一个变量,但要加载位图,它需要一个 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 已停止工作...)

【问题讨论】:

  • 不确定您要问什么...我在您的代码中看到的有趣之处在于您在文件名中包含引号。不确定您的图书馆是否需要,但它看起来确实很有趣...
  • Allegro 需要引号来打开文件名,例如:“./images/1010.png”。我有几张图片,但只有少数应该在会话中加载,这就是为什么数字必须是可变的。
  • 我不确定...可能是错的,因为我什至不知道 Allegro 是做什么用的...但是谷歌搜索,我发现allegro.cc/forums/thread/606701 被剪断的代码是@ 987654323@ ... 没有引号。

标签: c++ arrays bitmap allegro


【解决方案1】:

引号不属于您的缓冲区。

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

【讨论】:

    【解决方案2】:

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

    是的。

    您的char buf[40]隐式转换const char*。您无需编写任何代码即可进行转换。这是自动的。

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 2015-12-05
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      相关资源
      最近更新 更多