如何通过从内存加载 svg 来创建 SDL 纹理?

ell*_*oor 5 svg sdl

我有以下代码,我尝试从字符串加载 SVG

  const std::string svg =
    "<svg height='200' width='200'><circle cx='100' cy='100' r='80' stroke='white' stroke-width='4' fill='black'/></svg>";

  SDL_RWops *rw = SDL_RWFromConstMem(&svg, svg.size());
  SDL_Surface *surface = IMG_Load_RW(rw, 1);
  SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
Run Code Online (Sandbox Code Playgroud)

但是当我使用纹理时它不会绘制任何东西。

当我使用 IMG_LoadTexture 从文件加载相同的 SVG 时效果很好,所以我确信这是可以完成的,但我找不到方法。

我认为这里的问题可能是我如何将 SVG 传递到 SDL_RWFromConstMem

ell*_*oor 6

最后解决办法很简单,

const std::string svg =
    "<svg height='200' width='200'><circle cx='100' cy='100' r='80' stroke='white' stroke-width='4' fill='black'/></svg>";

  SDL_RWops *rw = SDL_RWFromConstMem(svg.c_str(), svg.size());
  SDL_Surface *surface = IMG_Load_RW(rw, 1);
  SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
Run Code Online (Sandbox Code Playgroud)

  • 对于其他试图找出差异的人来说,它只是将 svg 作为类型“char*”而不是“std::string&amp;”传递 (2认同)