Skip to content

Instantly share code, notes, and snippets.

@NickBeeuwsaert
Created September 3, 2013 20:36
Show Gist options
  • Save NickBeeuwsaert/6429202 to your computer and use it in GitHub Desktop.
Save NickBeeuwsaert/6429202 to your computer and use it in GitHub Desktop.
Loads a SVG file into a SDL_Texture
/* Reads a SVG file into a SDL_Texture
inputs:
SDL_Renderer *r: the SDL_Renderer to use,
const char *filename: the file to load
returns:
SDL_Texture*: the loaded texture, or NULL if something went wrong
*/
SDL_Texture *SDL_TextureFromSVG(SDL_Renderer *r, const char *filename){
SDL_Texture *texture = NULL;
SDL_Surface *tmpSurface = NULL;
svg_cairo_t *svgFile;
cairo_surface_t *surface;
cairo_t *context;
unsigned int w, h;
//INITIALIZE ALL THE THINGS
//The user don't need no reason WHY we couldn't load the SVG!
// But should probably create a way to get the error out..
if(svg_cairo_create(&svgFile) != SVG_CAIRO_STATUS_SUCCESS) return NULL;
if(svg_cairo_parse(svgFile, filename) != SVG_CAIRO_STATUS_SUCCESS) return NULL;
svg_cairo_get_size(svgFile, &w, &h);
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, w,h);
if(cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) return NULL;
context = cairo_create(surface);
if(cairo_status(context) != CAIRO_STATUS_SUCCESS) return NULL;
svg_cairo_render(svgFile, context);
//Yay! Endianness my favourite subject
Uint32 rmask, gmask, bmask, amask;
#ifdef SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0x00FF0000;
gmask = 0x0000FF00;
bmask = 0x000000FF;
amask = 0xFF000000;
#else
rmask = 0x0000FF00;
gmask = 0x00FF0000;
bmask = 0xFF000000;
amask = 0x000000FF;
#endif
tmpSurface = SDL_CreateRGBSurfaceFrom(cairo_image_surface_get_data(surface),
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
32,
cairo_image_surface_get_stride (surface),
rmask,
gmask,
bmask,
amask);
texture = SDL_CreateTextureFromSurface(r, tmpSurface);
//FREE ALL Y'ALL
SDL_FreeSurface(tmpSurface);
cairo_surface_destroy(surface);
cairo_destroy(context);
svg_cairo_destroy(svgFile);
return texture;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment