如何使用 sdl 从内存中加载图像 (c++)

如何使用 sdl 从内存中加载图像 (c++)

问题描述:

我在 std::vector<:string> 变量中有一个图像.我使用 winhttp 从网站下载了图像我成功地设法写入文件并显示,但现在我想直接从内存中显示它.我怎样才能加载它?

I have an image inside a std::vector<std::string> variable. I downloaded the image from a website using winhttp I successfully managed to write to file and displayed but now I want to display it directly from memory. How can I load it?

我试过了,但它不起作用:

I have tried this but it does not work:

SDL_Surface *load_image(char * buff,int size) 
{
    SDL_RWops *rw = SDL_RWFromMem(buff,size );
    SDL_Surface *temp = IMG_Load_RW(rw, 1);

    if (temp == NULL) 
    {
        printf("IMG_Load_RW: %s\n", IMG_GetError());
        system("pause");
        exit(1);
    }

    //Convert the image to optimal display format
    SDL_Surface *myimage;
    image = SDL_DisplayFormat(temp);

    //Free the temporary surface
    SDL_FreeSurface(temp);

    //Return our loaded image
    return myimage;
} 


std::vector <std::string> buffer = c.data();
std::string str_buffer =buffer[0];

for( size_t i =1;i<buffer.size();++i)
{
  str_buffer+=buffer[i];
};

image = load_image(const_cast<char*>(str_buffer.c_str()),str_buffer.length()+1);

我不是 100% 确定,但似乎你应该可以这样说:

I'm not 100% sure, but it seems that you should simply be able to say this:

std::vector<char> the_image; // populate somehow

SDL_Surface * s = load_image(the_image.data(), the_image.size());

在较旧的编译器中,您可能必须将 &the_image[0] 作为第一个参数,而不是 data().

In older compilers you may have to say &the_image[0] for the first argument, in lieu of data().