summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2018-05-03 01:11:39 -0700
committerVito Caputo <vcaputo@pengaru.com>2018-05-03 01:11:39 -0700
commitde8c678961226d0a7eb1f1276e8f5e842a33c3af (patch)
treebdfe3a077e651c60319dde62535185cde924d90d
*: initial commit derived from whale stage code
This code was used in whale [1], and prior to that was used in a smaller project called mal. Since I seemed to be doing a lot of reusing and building upon this, it seemed prudent to just stick it in a repo as a .a I could submodule in future SDL2 art projects. There are some kludges in this code currently, which will be fixed up in time.
-rw-r--r--Makefile.am1
-rwxr-xr-xbootstrap5
-rw-r--r--configure.ac18
-rw-r--r--src/Makefile.am2
-rw-r--r--src/list.h252
-rw-r--r--src/stage.c453
-rw-r--r--src/stage.h57
7 files changed, 788 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 0000000..af437a6
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1 @@
+SUBDIRS = src
diff --git a/bootstrap b/bootstrap
new file mode 100755
index 0000000..99f6f06
--- /dev/null
+++ b/bootstrap
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+aclocal \
+&& automake --gnu --add-missing \
+&& autoconf
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 0000000..ac0e8b5
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,18 @@
+AC_INIT([libstage], [1.0], [vcaputo@pengaru.com])
+AM_INIT_AUTOMAKE([-Wall -Werror foreign])
+AC_PROG_CC
+AM_PROG_CC_C_O
+AM_PROG_AR
+AC_PROG_RANLIB
+AM_SILENT_RULES([yes])
+
+dnl Check for SDL2
+PKG_CHECK_MODULES(SDL2, sdl2)
+CFLAGS="$CFLAGS $SDL2_CFLAGS"
+LIBS="$LIBS $SDL2_LIBS"
+
+AC_CONFIG_FILES([
+ Makefile
+ src/Makefile
+])
+AC_OUTPUT
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..a57bc54
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,2 @@
+noinst_LIBRARIES = libstage.a
+libstage_a_SOURCES = list.h stage.c stage.h
diff --git a/src/list.h b/src/list.h
new file mode 100644
index 0000000..48bca36
--- /dev/null
+++ b/src/list.h
@@ -0,0 +1,252 @@
+#ifndef __LIST_H
+#define __LIST_H
+
+/* linux kernel linked list interface */
+
+/*
+ * Simple doubly linked list implementation.
+ *
+ * Some of the internal functions ("__xxx") are useful when
+ * manipulating whole lists rather than single entries, as
+ * sometimes we already know the next/prev entries and we can
+ * generate better code by using them directly rather than
+ * using the generic single-entry routines.
+ */
+
+typedef struct list_head {
+ struct list_head *next, *prev;
+} list_head_t;
+
+#define LIST_HEAD_INIT(name) { &(name), &(name) }
+
+#define LIST_HEAD(name) \
+ struct list_head name = LIST_HEAD_INIT(name)
+
+#define INIT_LIST_HEAD(ptr) do { \
+ (ptr)->next = (ptr); (ptr)->prev = (ptr); \
+} while (0)
+
+/*
+ * Insert a new entry between two known consecutive entries.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_add(struct list_head *new,
+ struct list_head *prev,
+ struct list_head *next)
+{
+ next->prev = new;
+ new->next = next;
+ new->prev = prev;
+ prev->next = new;
+}
+
+/**
+ * list_add - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it after
+ *
+ * Insert a new entry after the specified head.
+ * This is good for implementing stacks.
+ */
+static inline void list_add(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head, head->next);
+}
+
+/**
+ * list_add_tail - add a new entry
+ * @new: new entry to be added
+ * @head: list head to add it before
+ *
+ * Insert a new entry before the specified head.
+ * This is useful for implementing queues.
+ */
+static inline void list_add_tail(struct list_head *new, struct list_head *head)
+{
+ __list_add(new, head->prev, head);
+}
+
+/*
+ * Delete a list entry by making the prev/next entries
+ * point to each other.
+ *
+ * This is only for internal list manipulation where we know
+ * the prev/next entries already!
+ */
+static inline void __list_del(struct list_head *prev, struct list_head *next)
+{
+ next->prev = prev;
+ prev->next = next;
+}
+
+/**
+ * list_del - deletes entry from list.
+ * @entry: the element to delete from the list.
+ * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
+ */
+static inline void list_del(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ entry->next = (void *) 0;
+ entry->prev = (void *) 0;
+}
+
+/**
+ * list_del_init - deletes entry from list and reinitialize it.
+ * @entry: the element to delete from the list.
+ */
+static inline void list_del_init(struct list_head *entry)
+{
+ __list_del(entry->prev, entry->next);
+ INIT_LIST_HEAD(entry);
+}
+
+/**
+ * list_move - delete from one list and add as another's head
+ * @list: the entry to move
+ * @head: the head that will precede our entry
+ */
+static inline void list_move(struct list_head *list, struct list_head *head)
+{
+ __list_del(list->prev, list->next);
+ list_add(list, head);
+}
+
+/**
+ * list_move_tail - delete from one list and add as another's tail
+ * @list: the entry to move
+ * @head: the head that will follow our entry
+ */
+static inline void list_move_tail(struct list_head *list,
+ struct list_head *head)
+{
+ __list_del(list->prev, list->next);
+ list_add_tail(list, head);
+}
+
+/**
+ * list_empty - tests whether a list is empty
+ * @head: the list to test.
+ */
+static inline int list_empty(struct list_head *head)
+{
+ return head->next == head;
+}
+
+static inline void __list_splice(struct list_head *list,
+ struct list_head *head)
+{
+ struct list_head *first = list->next;
+ struct list_head *last = list->prev;
+ struct list_head *at = head->next;
+
+ first->prev = head;
+ head->next = first;
+
+ last->next = at;
+ at->prev = last;
+}
+
+/**
+ * list_splice - join two lists
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ */
+static inline void list_splice(struct list_head *list, struct list_head *head)
+{
+ if (!list_empty(list))
+ __list_splice(list, head);
+}
+
+/**
+ * list_splice_init - join two lists and reinitialise the emptied list.
+ * @list: the new list to add.
+ * @head: the place to add it in the first list.
+ *
+ * The list at @list is reinitialised
+ */
+static inline void list_splice_init(struct list_head *list,
+ struct list_head *head)
+{
+ if (!list_empty(list)) {
+ __list_splice(list, head);
+ INIT_LIST_HEAD(list);
+ }
+}
+
+/**
+ * list_entry - get the struct for this entry
+ * @ptr: the &struct list_head pointer.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_entry(ptr, type, member) \
+ ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
+
+/**
+ * list_for_each - iterate over a list
+ * @pos: the &struct list_head to use as a loop counter.
+ * @head: the head for your list.
+ */
+#define list_for_each(pos, head) \
+ for (pos = (head)->next; pos != (head); \
+ pos = pos->next)
+/**
+ * list_for_each_prev - iterate over a list backwards
+ * @pos: the &struct list_head to use as a loop counter.
+ * @head: the head for your list.
+ */
+#define list_for_each_prev(pos, head) \
+ for (pos = (head)->prev; pos != (head); \
+ pos = pos->prev)
+
+/**
+ * list_for_each_safe - iterate over a list safe against removal of list entry
+ * @pos: the &struct list_head to use as a loop counter.
+ * @n: another &struct list_head to use as temporary storage
+ * @head: the head for your list.
+ */
+#define list_for_each_safe(pos, n, head) \
+ for (pos = (head)->next, n = pos->next; pos != (head); \
+ pos = n, n = pos->next)
+
+/**
+ * list_for_each_entry - iterate over list of given type
+ * @pos: the type * to use as a loop counter.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry(pos, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * list_for_each_entry_prev - iterate over list of given type backwards
+ * @pos: the type * to use as a loop counter.
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_prev(pos, head, member) \
+ for (pos = list_entry((head)->prev, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = list_entry(pos->member.prev, typeof(*pos), member))
+
+
+/**
+ * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
+ * @pos: the type * to use as a loop counter.
+ * @n: another type * to use as temporary storage
+ * @head: the head for your list.
+ * @member: the name of the list_struct within the struct.
+ */
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+
+#endif
diff --git a/src/stage.c b/src/stage.c
new file mode 100644
index 0000000..20a1fc0
--- /dev/null
+++ b/src/stage.c
@@ -0,0 +1,453 @@
+/*
+ * 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 "list.h"
+#include "stage.h"
+
+typedef struct v2f_t {
+ float x, y;
+} v2f_t;
+
+typedef struct aabb_t {
+ v2f_t min, max;
+} aabb_t;
+
+typedef struct stage_cached_t {
+ int width, height;
+ SDL_Texture *texture;
+} stage_cached_t;
+
+struct stage_node_t {
+ list_head_t nodes;
+ char name[STAGE_NODE_NAME_MAX];
+ stage_cached_t cached;
+ aabb_t aabb; /* node coordinates expressed in the space of -1.0...1.0 */
+ float alpha; /* alpha for the texture when composited */
+ double angle; /* angle for the texture when composited */
+ unsigned active:1; /* node is active */
+ unsigned locked:1; /* node is locked */
+
+ stage_render_func_t render; /* render object into a texture */
+ stage_free_func_t free; /* free object */
+ void *object; /* object */
+};
+
+struct stage_t {
+ list_head_t layers[STAGE_LAYERS_MAX];
+ SDL_Renderer *renderer; /* target renderer */
+ float aspect_ratio; /* width/height ratio of stage, -1.0...1.0 range of node coordinates map to this ratio. */
+};
+
+
+/* minimally initialize a node to carry an object */
+static void node_init(stage_node_t *node, const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func)
+{
+ strncpy(node->name, name, sizeof(node->name));
+ node->render = render_func;
+ node->free = free_func;
+ node->object = object;
+}
+
+
+/* allocate a node, no list manipulation occurs, this purely creates a node in isolation and assigns its associated name, object and functions */
+static stage_node_t * node_new(const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func)
+{
+ stage_node_t *node;
+
+ node = calloc(1, sizeof(stage_node_t));
+ if (!node)
+ return NULL;
+
+ node_init(node, name, object, render_func, free_func);
+
+ return node;
+}
+
+
+/* free a node, no list manipulation occurs, this is purely cleanup of the node and its associated texture and object */
+static void node_free(stage_node_t *node)
+{
+ if (node->free)
+ node->free(node->object);
+ if (node->cached.texture)
+ SDL_DestroyTexture(node->cached.texture);
+ free(node);
+}
+
+
+/* returns a new node installed at the specified layer */
+stage_node_t * stage_node_new(stage_t *stage, int layer, const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func)
+{
+ stage_node_t *node;
+
+ node = node_new(name, object, render_func, free_func);
+ if (!node)
+ return NULL;
+
+ list_add_tail(&node->nodes, &stage->layers[layer]);
+
+ return node;
+}
+
+
+/* replace a given node's object only - retain render/free hooks and everything else, invalidates cache - DOES NOT FREE OLD OBJECT */
+/* FIXME: this is being used as a kludge to swap frames in an svg -
+ * eliminate this bullshit and add support for animations or at least frames,
+ * the stage really shouldn't have anything to do with it, we can dick with
+ * the svg behind its back. I just don't currently have the helpers for creating
+ * svg nodes actually giving out the bare svg handle currently, so the stage_node_t
+ * is all I have to dick with at the moment. Next time! This really dicks with
+ * the resource lifecycle - if the stage frees the node, which object was active
+ * at the time and which object still needs to be freed because it wasn't in
+ * the stage when the node was killed? It's totally shitty, and I'm aware.
+ */
+void stage_node_set_object(const stage_t *stage, stage_node_t *node, void *object)
+{
+ if (node->cached.texture) {
+ SDL_DestroyTexture(node->cached.texture);
+ node->cached.texture = NULL;
+ }
+
+ node->object = object;
+}
+
+/* FIXME: GROSS fuck me */
+void stage_node_get_object(const stage_t *stage, stage_node_t *node, void **res_object)
+{
+ *res_object = node->object;
+}
+
+
+/* replaces a given node's object-related properties but otherwise keeping the existing state */
+void stage_node_replace(const stage_t *stage, stage_node_t *node, const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func)
+{
+ if (node->free)
+ node->free(node->object);
+
+ if (node->cached.texture) {
+ SDL_DestroyTexture(node->cached.texture);
+ node->cached.texture = NULL;
+ }
+
+ node_init(node, name, object, render_func, free_func);
+}
+
+
+/* frees a given node removing it from the stage */
+stage_node_t * stage_node_free(stage_t *stage, stage_node_t *node)
+{
+ if (node) {
+ list_del(&node->nodes);
+ node_free(node);
+ }
+
+ return NULL;
+}
+
+
+/* set the alpha on a node */
+void stage_node_set_alpha(const stage_t *stage, stage_node_t *node, float alpha)
+{
+ /* TODO: this should probably mark something dirty for when we stop always recomposing everything */
+ node->alpha = alpha;
+}
+
+
+/* set the aabb for a node */
+void stage_node_set_aabb(const stage_t *stage, stage_node_t *node, const aabb_t *aabb)
+{
+ node->aabb = *aabb;
+}
+
+
+/* get the aabb for a node */
+void stage_node_get_aabb(const stage_t *stage, stage_node_t *node, aabb_t *res_aabb)
+{
+ *res_aabb = node->aabb;
+}
+
+
+/* set a node to active (participates in rendering) */
+void stage_node_set_active(const stage_t *stage, stage_node_t *node)
+{
+ node->active = 1;
+}
+
+
+/* set a node to inactive (doesn't participate in rendering) */
+void stage_node_set_inactive(const stage_t *stage, stage_node_t *node)
+{
+ /* TODO: should this discard the potentially cached texture? */
+ node->active = 0;
+}
+
+
+/* set a node to locked (doesn't get freed by clears) */
+void stage_node_set_locked(const stage_t *stage, stage_node_t *node)
+{
+ node->locked = 1;
+}
+
+
+/* set a node to unlocked (default, gets freed by clears) */
+void stage_node_set_unlocked(const stage_t *stage, stage_node_t *node)
+{
+ node->locked = 0;
+}
+
+
+/* set a node's layer */
+void stage_node_set_layer(stage_t *stage, stage_node_t *node, int layer)
+{
+ /* TODO: assert layer sanity */
+ list_del(&node->nodes);
+ list_add_tail(&node->nodes, &stage->layers[layer]);
+}
+
+
+void stage_node_set_angle(const stage_t *stage, stage_node_t *node, double angle)
+{
+ node->angle = angle;
+}
+
+
+void stage_node_get_angle(const stage_t *stage, stage_node_t *node, double *res_angle)
+{
+ *res_angle = node->angle;
+}
+
+
+/* create a new stage with the given aspect ratio */
+stage_t * stage_new(SDL_Renderer *renderer, float aspect_ratio)
+{
+ stage_t *stage;
+ int i;
+
+ stage = calloc(1, sizeof(stage_t));
+ if (!stage)
+ return NULL;
+
+ for (i = 0; i < STAGE_LAYERS_MAX; i++)
+ INIT_LIST_HEAD(&stage->layers[i]);
+
+ stage->renderer = renderer;
+ stage->aspect_ratio = aspect_ratio;
+
+ return stage;
+}
+
+static void _stage_clear(stage_t *stage, int force)
+{
+ stage_node_t *node, *_node;
+ int i;
+
+ for (i = 0; i < STAGE_LAYERS_MAX; i++) {
+ list_for_each_entry_safe(node, _node, &stage->layers[i], nodes) {
+ if (force || !node->locked)
+ stage_node_free(stage, node);
+ }
+ }
+}
+
+/* free everything in the stage, but keep the stage around */
+/* probably good hygiene to clear everything when there's no intention of
+ * reusing nodes at the start of a new context, in case something is left
+ * around unintentionally. */
+void stage_clear(stage_t *stage)
+{
+ _stage_clear(stage, 0);
+}
+
+
+/* free the supplied stage and all associated textures and objects */
+stage_t * stage_free(stage_t *stage)
+{
+
+ if (stage) {
+ _stage_clear(stage, 1);
+ free(stage);
+ }
+
+ return NULL;
+}
+
+
+/* fit a stage to the supplied dimensions, returns the fitted dimensions in the result pointers */
+/* the idea is this can be used in a window resize hook to enforce the stage's aspect ratio */
+void stage_fit(const stage_t *stage, int width, int height, int *res_width, int *res_height)
+{
+ float full_width = stage->aspect_ratio * ((float)height);
+
+ if (full_width == width) {
+ /* perfect fit */
+ *res_width = width;
+ *res_height = height;
+ } else if (full_width > width) {
+ /* height is too large */
+ *res_height = (1.0f / stage->aspect_ratio) * ((float)width);
+ *res_width = width;
+ } else {
+ /* width is too large */
+ *res_width = full_width;
+ *res_height = height;
+ }
+}
+
+
+static void aabb_to_rect(const aabb_t *aabb, const SDL_Rect *stage_rect, SDL_Rect *res_rect)
+{
+ float half_w = ((float)stage_rect->w) * .5f, half_h = ((float)stage_rect->h) * .5f; /* FIXME silly to recompute this repeatedly... */
+
+ res_rect->x = stage_rect->x;
+ res_rect->y = stage_rect->y;
+ res_rect->x += ((float)aabb->min.x) * half_w + half_w;
+ res_rect->y += stage_rect->h - (((float)aabb->max.y) * half_h + half_h);
+
+ res_rect->w = (aabb->max.x - aabb->min.x) * half_w;
+ res_rect->h = (aabb->max.y - aabb->min.y) * half_h;
+}
+
+
+static void render_nodes(list_head_t *head, SDL_Renderer *renderer, SDL_Rect *dest_rect)
+{
+ stage_node_t *node;
+
+ list_for_each_entry(node, head, nodes) {
+ SDL_Rect node_rect;
+
+ if (!node->active)
+ continue;
+
+ /* scale the node's aabb stage coordinates to destination renderer coordinates */
+ aabb_to_rect(&node->aabb, dest_rect, &node_rect);
+
+ /* if we have a cached texture, see if the dimensions changed */
+ if (node->cached.texture &&
+ (node_rect.w != node->cached.width ||
+ node_rect.h != node->cached.height)) {
+ SDL_DestroyTexture(node->cached.texture);
+ node->cached.texture = NULL;
+ }
+
+ /* Tell the node to draw its cached texture */
+ /* The draw function may either use the existing texture when non-NULL or destroy and create a new one,
+ * whatever is most convenient for it.
+ * If the texture is NULL, a create & render must be performed, and 1 returned.
+ * If the texture is non-NULL, and there's no animation or anything to be done, 0 may simply be returned.
+ * Otherwise, if supplied a non-NULL texture and it's modified, 1 must be returned.
+ */
+ /* XXX: at this time, the return value is ignored (it always composites the stage even when unchanged) */
+ node->render(renderer, node->object, node_rect.w, node_rect.h, &node->cached.texture);
+ node->cached.width = node_rect.w;
+ node->cached.height = node_rect.h;
+ SDL_SetTextureAlphaMod(node->cached.texture, ((float)node->alpha * 255.0f));
+
+ /* copy the texture to renderer */
+ if (node->angle == 0)
+ SDL_RenderCopy(renderer, node->cached.texture, NULL, &node_rect);
+ else
+ SDL_RenderCopyEx(renderer, node->cached.texture, NULL, &node_rect, node->angle, NULL, SDL_FLIP_NONE);
+ }
+}
+
+
+/* XXX: ANOTHER KLUDGE FIXME */
+/* we shouldn't need to let this out of the stage, but for the mouse aiming
+ * it was easier this way for now.
+ */
+void stage_get_output_size(stage_t *stage, int *res_width, int *res_height)
+{
+ SDL_GetRendererOutputSize(stage->renderer, res_width, res_height);
+}
+
+
+/* render the supplied stage into the supplied renderer */
+/* the aspect_ratio of the stage will be enforced in filling the logical size
+ * of the renderer. Unless the aspect ratio matches precisely, stage_render()
+ * will center the rendering and leave blank borders on the edges left empty.
+ * The renderer may be sized precisely by first calling stage_fit().
+ */
+void stage_render(stage_t *stage)
+{
+ SDL_Rect rect;
+ int i, width, height;
+
+ /* XXX TODO: investigate renderer viewports and scale factors */
+ SDL_GetRendererOutputSize(stage->renderer, &width, &height);
+
+ stage_fit(stage, width, height, &rect.w, &rect.h);
+ rect.x = (width - rect.w) / 2;
+ rect.y = (height - rect.h) / 2;
+
+ SDL_RenderClear(stage->renderer); /* TODO: handle -1 on error */
+
+ for (i = 0; i < STAGE_LAYERS_MAX; i++)
+ render_nodes(&stage->layers[i], stage->renderer, &rect);
+}
+
+
+/* lookup a node from a name */
+stage_node_t * stage_node_lookup_name(const stage_t *stage, const char *name)
+{
+ stage_node_t *node;
+ int i;
+
+ for (i = 0; i < STAGE_LAYERS_MAX; i++) {
+ list_for_each_entry(node, &stage->layers[i], nodes) {
+ if (!strncmp(node->name, name, sizeof(node->name)))
+ return node;
+ }
+ }
+
+ return NULL;
+}
+
+
+/* lookup a node from a cartesian renderer coordinate */
+stage_node_t * stage_node_lookup_cartesian(const stage_t *stage, int x, int y)
+{
+ stage_node_t *node;
+ SDL_Rect rect;
+ int i, width, height;
+
+ /* FIXME: copy-pasta with render, factor out */
+ SDL_GetRendererOutputSize(stage->renderer, &width, &height);
+
+ stage_fit(stage, width, height, &rect.w, &rect.h);
+ rect.x = (width - rect.w) / 2;
+ rect.y = (height - rect.h) / 2;
+
+ /* this is currently very simple: find the first top-most node
+ * including the specified coordinate.
+ */
+ for (i = STAGE_LAYERS_MAX - 1; i >= 0; i--) {
+ list_for_each_entry(node, &stage->layers[i], nodes) {
+ SDL_Rect node_rect;
+
+ if (!node->active)
+ continue;
+
+ aabb_to_rect(&node->aabb, &rect, &node_rect);
+ if (x >= node_rect.x && x < node_rect.x + node_rect.w &&
+ y >= node_rect.y && y < node_rect.y + node_rect.h)
+ return node;
+ }
+ }
+
+ return NULL;
+}
diff --git a/src/stage.h b/src/stage.h
new file mode 100644
index 0000000..05ee3ec
--- /dev/null
+++ b/src/stage.h
@@ -0,0 +1,57 @@
+/*
+ * 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/>.
+ */
+
+#ifndef _STAGE_H
+#define _STAGE_H
+
+#include <SDL.h>
+
+#define STAGE_NODE_NAME_MAX 16
+#define STAGE_LAYERS_MAX 10
+
+typedef struct aabb_t aabb_t;
+typedef struct stage_t stage_t;
+typedef struct stage_node_t stage_node_t;
+
+typedef int (*stage_render_func_t)(SDL_Renderer *renderer, void *object, int width, int height, SDL_Texture **texture);
+typedef void (*stage_free_func_t)(void *object);
+
+stage_t * stage_new(SDL_Renderer *renderer, float aspect_ratio);
+void stage_clear(stage_t *stage);
+stage_t * stage_free(stage_t *stage);
+void stage_fit(const stage_t *stage, int width, int height, int *res_width, int *res_height);
+void stage_render(stage_t *stage);
+stage_node_t * stage_node_new(stage_t *stage, int layer, const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func);
+
+void stage_get_output_size(stage_t *stage, int *res_width, int *res_height);
+void stage_node_set_object(const stage_t *stage, stage_node_t *node, void *object);
+void stage_node_get_object(const stage_t *stage, stage_node_t *node, void **res_object);
+void stage_node_replace(const stage_t *stage, stage_node_t *node, const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func);
+stage_node_t * stage_node_free(stage_t *stage, stage_node_t *node);
+void stage_node_set_alpha(const stage_t *stage, stage_node_t *node, float alpha);
+void stage_node_set_aabb(const stage_t *stage, stage_node_t *node, const aabb_t *aabb);
+void stage_node_get_aabb(const stage_t *stage, stage_node_t *node, aabb_t *res_aabb);
+void stage_node_set_active(const stage_t *stage, stage_node_t *node);
+void stage_node_set_inactive(const stage_t *stage, stage_node_t *node);
+void stage_node_set_locked(const stage_t *stage, stage_node_t *node);
+void stage_node_set_unlocked(const stage_t *stage, stage_node_t *node);
+void stage_node_set_layer(stage_t *stage, stage_node_t *node, int layer);
+void stage_node_set_angle(const stage_t *stage, stage_node_t *node, double angle);
+void stage_node_get_angle(const stage_t *stage, stage_node_t *node, double *res_angle);
+stage_node_t * stage_node_lookup_name(const stage_t *stage, const char *name);
+stage_node_t * stage_node_lookup_cartesian(const stage_t *stage, int x, int y);
+
+#endif
© All Rights Reserved