summaryrefslogtreecommitdiff
path: root/src/svg-node.c
blob: 7b5541bb4087e2b9dc46e256947320cef11915aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 *  Copyright (C) 2018  Vito Caputo - <vcaputo@pengaru.com>
 *
 *  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 <http://www.gnu.org/licenses/>.
 */

#include <SDL.h>

#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);
}
© All Rights Reserved