【问题标题】:how to load an image using sdl from memory (c++)如何使用 sdl 从内存中加载图像(C++)
【发布时间】:2011-11-12 09:26:24
【问题描述】:

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

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

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);

【问题讨论】:

  • 您的 load_image 例程返回未初始化的 myimage。这只是一个错字,你真正的意思是SDL_Surface *myimage = SDL_DisplayFormat(temp);
  • 我不明白你在说什么,但它停在 printf("IMG_Load_RW: %s\n", IMG_GetError());行并打印一个错误,所以我认为这与此无关
  • 我指的是SDL_Surface *myimage; image = SDL_DisplayFormat(temp);
  • 我们需要知道这条线是什么意思:std::vector &lt;std::string&gt; buffer = c.data(); 具体你从哪里得到 c。关注数据!

标签: c++ sdl


【解决方案1】:

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

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

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

在较旧的编译器中,您可能不得不说&amp;the_image[0] 作为第一个参数,而不是data()

【讨论】:

  • 谢谢,.data() 不起作用,所以我使用了它编译的 &_image[0],但没有加载图像
  • 嗯,您的代码包含其他错误(例如,从未声明过image),因此其他地方完全有可能出现其他问题。
  • 是的,其他地方可能出现问题,但我将图像写入文件并显示它,图像被声明我没有发布整个代码(SDL_Surface *image = NULL;)跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2011-02-22
  • 2010-12-11
相关资源
最近更新 更多