summaryrefslogtreecommitdiff
path: root/src/svg-node.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/svg-node.c')
-rw-r--r--src/svg-node.c88
1 files changed, 88 insertions, 0 deletions
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 - <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