--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scene_node.c Thu Apr 24 19:53:40 2025 +0200 @@ -0,0 +1,134 @@ +/* +* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * Copyright 2023 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ascension/scene_node.h" +#include "ascension/context.h" + +#include <cx/tree.h> +#include <cx/linked_list.h> + +static CxTreeIterator asc_scene_node_iterator( + AscSceneNode *node, + bool visit_on_exit +) { + return cx_tree_iterator( + node, visit_on_exit, + offsetof(AscSceneNode, children), + offsetof(AscSceneNode, next) + ); +} + +AscSceneNode *asc_scene_node_empty(void) { + AscSceneNode *node = calloc(1, sizeof(AscSceneNode)); + node->render_group = ASC_RENDER_GROUP_NONE; + node->free_func = (asc_scene_free_func) free; + node->scale.x = node->scale.y = node->scale.z = 1; + asc_transform_identity(node->transform); + asc_transform_identity(node->world_transform); + return node; +} + +void asc_scene_node_free(AscSceneNode *node) { + if (node == NULL) return; + + // remove this node from its parent + asc_scene_node_unlink(node); + + // free the entire subtree + CxTreeIterator iter = asc_scene_node_iterator(node, true); + cx_foreach(AscSceneNode*, child, iter) { + if (!iter.exiting) continue; + if (child->behaviors != NULL) { + cxListFree(child->behaviors); + } + if (child->free_func != NULL) { + child->free_func(child); + } else { + free(child); + } + } +} + +void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) { + cx_tree_link( + parent, node, + offsetof(AscSceneNode, parent), + offsetof(AscSceneNode, children), + offsetof(AscSceneNode, last_child), + offsetof(AscSceneNode, prev), + offsetof(AscSceneNode, next) + ); +} + +void asc_scene_node_unlink(AscSceneNode *node) { + cx_tree_unlink( + node, + offsetof(AscSceneNode, parent), + offsetof(AscSceneNode, children), + offsetof(AscSceneNode, last_child), + offsetof(AscSceneNode, prev), + offsetof(AscSceneNode, next) + ); +} + +void asc_scene_add_behavior( + AscSceneNode *node, + asc_scene_update_func behavior +) { + if (node->behaviors == NULL) { + node->behaviors = cxLinkedListCreateSimple(CX_STORE_POINTERS); + } + cxListAdd(node->behaviors, behavior); +} + +void asc_scene_remove_behavior( + AscSceneNode *node, + asc_scene_update_func behavior +) { + if (node->behaviors != NULL) { + cxListFindRemove(node->behaviors, behavior); + } +} + +void asc_node_update(AscSceneNode *node) { + asc_set_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS); +} + +void asc_node_update_transform(AscSceneNode *node) { + // fast skip if node is already marked + if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) { + return; + } + + CxTreeIterator iter = asc_scene_node_iterator(node, false); + cx_foreach(AscSceneNode*, n, iter) { + if (asc_test_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) { + cxTreeIteratorContinue(iter); + } + asc_set_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM); + } +}