首页 > 解决方案 > 如何通过从内存加载 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);

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

当我使用 IMG_LoadTexture 从文件加载相同的 SVG 时工作正常,所以我确定可以这样做,但我找不到方法。

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

标签: svgsdl

解决方案


最后解决方案很简单,

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

推荐阅读