问题
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);
回答1:
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());
In older compilers you may have to say &the_image[0]
for the first argument, in lieu of data()
.
来源:https://stackoverflow.com/questions/8101442/how-to-load-an-image-using-sdl-from-memory-c