ellipticaldoor
ellipticaldoor

Reputation: 1272

How I can create an SDL texture by loading an svg from memory?

I have the following code where I try to load an SVG from an string

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

But when I use the texture it doesn't draws anything.

When I load the same SVG from a file using IMG_LoadTexture works fine, so I'm certain that this can be done, but I can'f find how.

I think probably the issue here is how I'm passing the SVG to SDL_RWFromConstMem

Upvotes: 6

Views: 3153

Answers (1)

ellipticaldoor
ellipticaldoor

Reputation: 1272

In the end the solution was simple,

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

Upvotes: 7

Related Questions