/* * Copyright (C) 2018 Vito Caputo - * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3 as published * by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include "aabb.h" #include "macros.h" #include "whale-svg.h" #include "stage.h" static int svg_node_render(SDL_Renderer *renderer, void *object, int width, int height, SDL_Texture **texture) { SDL_Surface *surface; if (*texture) /* if there's a cached texture, do nothing. */ return 0; surface = whale_svg_render((whale_svg_t *)object, width, height); fatal_if(!surface, "Unable to render svg surface"); *texture = SDL_CreateTextureFromSurface(renderer, surface); fatal_if(!*texture, "Unable to create texture from svg surface"); SDL_FreeSurface(surface); return 1; } static void svg_node_free(void *object) { whale_svg_free(object); } stage_node_t * svg_node_new_file(stage_t *stage, const char *name, const char *file, int layer, aabb_t aabb) { stage_node_t *node; whale_svg_t *svg; svg = whale_svg_new_file(file); fatal_if(!svg, "Unable to open svg \"%s\"", file); node = stage_node_new(stage, layer, name, svg, svg_node_render, svg_node_free); fatal_if(!node, "Unable to create stage node"); stage_node_set_aabb(stage, node, &aabb); return node; } stage_node_t * svg_node_new_buffer(stage_t *stage, const char *name, const char *buf, size_t count, int layer, aabb_t aabb) { stage_node_t *node; whale_svg_t *svg; svg = whale_svg_new_buffer(buf, count); fatal_if(!svg, "Unable to open svg buffer for \"%s\"", name); node = stage_node_new(stage, layer, name, svg, svg_node_render, svg_node_free); fatal_if(!node, "Unable to create stage node"); stage_node_set_aabb(stage, node, &aabb); return node; } void svg_node_replace_buffer(stage_t *stage, stage_node_t *node, const char *name, const char *buf, size_t count) { whale_svg_t *svg; svg = whale_svg_new_buffer(buf, count); fatal_if(!svg, "Unable to open svg buffer for \"%s\"", name); stage_node_replace(stage, node, name, svg, svg_node_render, svg_node_free); }