Thu, 22 Feb 2024 22:34:50 +0100
apply new CxTreeIterator from ucx 3.1 preview
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 * Copyright 2023 Mike Becker. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "ascension/scene.h"
29 #include "ascension/error.h"
31 #include <cx/tree.h>
33 #include <assert.h>
35 #define node_layout_ \
36 offsetof(AscSceneNode, parent), offsetof(AscSceneNode, children), \
37 offsetof(AscSceneNode, prev), offsetof(AscSceneNode, next)
38 #define child_list_off_ \
39 offsetof(AscSceneNode, children), offsetof(AscSceneNode, next)
41 void asc_scene_init(AscScene *scene) {
42 if (scene->root != NULL) {
43 asc_error("Scene is already initialized.");
44 return;
45 }
46 scene->root = asc_scene_node_empty();
47 }
49 void asc_scene_destroy(AscScene *scene) {
50 asc_scene_node_free(scene->root);
51 }
53 void asc_scene_add(AscScene *scene, AscSceneNode *node) {
54 asc_scene_node_link(scene->root, node);
55 }
57 void asc_scene_draw(AscScene const *scene) {
58 // TODO: don't visit the tree, visit the render groups
59 CxTreeIterator iter = cx_tree_iterator(scene->root, false, child_list_off_);
60 cx_foreach(AscSceneNode*, node, iter) {
61 if (node->draw_func != NULL) {
62 node->draw_func(node);
63 }
64 }
65 }
67 AscSceneNode *asc_scene_node_empty(void) {
68 // TODO: check if this can remain a calloc or if it's too expensive
69 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
70 assert(node != NULL);
71 node->free_func = (asc_scene_free_func) free;
72 return node;
73 }
75 void asc_scene_node_free(AscSceneNode *node) {
76 if (node == NULL) return;
78 // TODO: replace with UCX tree visitor
79 // TODO: avoid recursion
81 // free the children recursively
82 while (node->children != NULL) {
83 asc_scene_node_free(node->children);
84 }
86 // remove this node from its parent
87 asc_scene_node_unlink(node);
89 // free the node
90 if (node->free_func != NULL) {
91 node->free_func(node);
92 }
93 }
95 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
96 cx_tree_link(parent, node, node_layout_);
97 }
99 void asc_scene_node_unlink(AscSceneNode *node) {
100 cx_tree_unlink(node, node_layout_);
101 }