From 1d1ce1b2df4a5f97a1c34a90337d0c3732f7959e Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 15 Apr 2018 14:57:17 -0700 Subject: *: initial commit of whale compo This is my contribution to our Hungrycat production blender 2018 entry, assets to follow. --- src/svg-node.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/svg-node.c (limited to 'src/svg-node.c') diff --git a/src/svg-node.c b/src/svg-node.c new file mode 100644 index 0000000..7b5541b --- /dev/null +++ b/src/svg-node.c @@ -0,0 +1,88 @@ +/* + * 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); +} -- cgit v1.2.3