src/scene.c

Mon, 26 Feb 2024 21:16:00 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 26 Feb 2024 21:16:00 +0100
changeset 31
8324037e0148
parent 30
fceda550ebcb
child 32
86468a71dd73
permissions
-rw-r--r--

use tree iterator to free scene nodes

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

mercurial