universe@21: /* universe@21: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@21: * Copyright 2023 Mike Becker. All rights reserved. universe@21: * universe@21: * Redistribution and use in source and binary forms, with or without universe@21: * modification, are permitted provided that the following conditions are met: universe@21: * universe@21: * 1. Redistributions of source code must retain the above copyright universe@21: * notice, this list of conditions and the following disclaimer. universe@21: * universe@21: * 2. Redistributions in binary form must reproduce the above copyright universe@21: * notice, this list of conditions and the following disclaimer in the universe@21: * documentation and/or other materials provided with the distribution. universe@21: * universe@21: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@21: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@21: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@21: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@21: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@21: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@21: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@21: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@21: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@21: * POSSIBILITY OF SUCH DAMAGE. universe@21: */ universe@21: universe@21: #include "ascension/scene.h" universe@21: #include "ascension/error.h" universe@21: universe@29: #include universe@29: universe@21: #include universe@21: universe@30: #define node_layout_ \ universe@29: offsetof(AscSceneNode, parent), offsetof(AscSceneNode, children), \ universe@29: offsetof(AscSceneNode, prev), offsetof(AscSceneNode, next) universe@30: #define child_list_off_ \ universe@30: offsetof(AscSceneNode, children), offsetof(AscSceneNode, next) universe@29: universe@21: void asc_scene_init(AscScene *scene) { universe@21: if (scene->root != NULL) { universe@21: asc_error("Scene is already initialized."); universe@21: return; universe@21: } universe@29: scene->root = asc_scene_node_empty(); universe@21: } universe@21: universe@21: void asc_scene_destroy(AscScene *scene) { universe@21: asc_scene_node_free(scene->root); universe@21: } universe@21: universe@29: void asc_scene_add(AscScene *scene, AscSceneNode *node) { universe@29: asc_scene_node_link(scene->root, node); universe@29: } universe@29: universe@30: void asc_scene_draw(AscScene const *scene) { universe@30: // TODO: don't visit the tree, visit the render groups universe@30: CxTreeIterator iter = cx_tree_iterator(scene->root, false, child_list_off_); universe@30: cx_foreach(AscSceneNode*, node, iter) { universe@30: if (node->draw_func != NULL) { universe@30: node->draw_func(node); universe@30: } universe@29: } universe@29: } universe@29: universe@29: AscSceneNode *asc_scene_node_empty(void) { universe@27: // TODO: check if this can remain a calloc or if it's too expensive universe@27: AscSceneNode *node = calloc(1, sizeof(AscSceneNode)); universe@21: assert(node != NULL); universe@29: node->free_func = (asc_scene_free_func) free; universe@21: return node; universe@21: } universe@21: universe@21: void asc_scene_node_free(AscSceneNode *node) { universe@21: if (node == NULL) return; universe@27: universe@27: // remove this node from its parent universe@27: asc_scene_node_unlink(node); universe@27: universe@31: // free the entire subtree universe@31: CxTreeIterator iter = cx_tree_iterator(node, true, child_list_off_); universe@31: cx_foreach(AscSceneNode*, child, iter) { universe@31: if (!iter.exiting) continue; universe@31: if (child->free_func != NULL) { universe@31: child->free_func(child); universe@31: } else { universe@31: free(child); universe@31: } universe@21: } universe@21: } universe@21: universe@29: void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) { universe@30: cx_tree_link(parent, node, node_layout_); universe@29: } universe@29: universe@21: void asc_scene_node_unlink(AscSceneNode *node) { universe@30: cx_tree_unlink(node, node_layout_); universe@21: }