如何在SFML中从内存加载图像?

时间:2015-05-16 17:54:11

标签: c++ memory base64 png sfml

我目前正在尝试在base64中编码一些png文件,当SFML应用程序启动时,文件被解码,加载到内存中并传递给从内存加载纹理的函数,但是如果我这样做,我会得到一个错误,表示该文件不受支持或损坏。如果我从内存中保存文件并在图像查看器中打开它,则显示没有任何错误。 以下是我做的所有事情,我做错了什么?:

B64.cpp

std::string StringFromFile(std::string file)
{
    std::ifstream fin;
    fin.open(file, std::ios::binary);
    std::ostringstream out;
    out << fin.rdbuf();
    fin.close();
    std::string data(out.str());
    return data;
}

void Encode(std::string in, std::string out)
{
    std::ifstream ins;
    ins.open(in);
    base64::encoder enc(GetFileSize(in));
    std::ofstream outs;
    outs.open(out);
    enc.encode(ins, outs);
    ins.close();
    outs.close();
}

void* Decode(std::string in)
{
    std::ifstream ins;
    ins.open(in);
    base64::decoder dec(GetFileSize(in));
    std::ofstream outs;
    outs.open("/tmp.b64");
    dec.decode(ins, outs);
    std::string *file = new std::string(StringFromFile("/tmp.b64"));
    void* data;
    data = file;
    //std::remove("tmp.b64");
    ins.close();
    outs.close();
    return data;
}

size_t GetDataSize(std::string file)
{
    std::ifstream fin;
    fin.open(file);
    std::ostringstream out;
    out << fin.rdbuf();
    fin.close();
    std::string data(out.str());
    return data.size();
}

size_t GetFileSize(std::string file)
{
    std::ifstream ins;
    ins.open(file, std::ios::ate | std::ios::binary);
    return ins.tellg();
}

Game.cpp

void Load()
{
    ...
    Encode("Textures/player.png", "Textures/player.b64");
    sf::Texture tstx;
    tstx.loadFromMemory(Decode("Textures/player.b64"), 1422);
    ...
}

0 个答案:

没有答案