Sun, 21 Jan 2024 14:01:27 +0100
remove unused cx/list.h include
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 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include <assert.h> |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | void asc_scene_init(AscScene *scene) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | if (scene->root != NULL) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
35 | asc_error("Scene is already initialized."); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | return; |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
38 | scene->root = asc_scene_node_create(NULL); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | void asc_scene_destroy(AscScene *scene) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | asc_scene_node_free(scene->root); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
44 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | AscSceneNode *asc_scene_node_create(AscSceneNode *parent) { |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
46 | // 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
|
47 | 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
|
48 | assert(node != NULL); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | asc_scene_node_link(node, parent); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
50 | return node; |
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 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | 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
|
54 | if (node == NULL) return; |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
55 | |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
56 | // free the children recursively |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
57 | while (node->children != NULL) { |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
58 | asc_scene_node_free(node->children); |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | } |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
60 | |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
61 | // remove this node from its parent |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
62 | asc_scene_node_unlink(node); |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
63 | |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
64 | // free the node |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | free(node); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
67 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
68 | void asc_scene_node_link( |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
69 | AscSceneNode *node, |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
70 | AscSceneNode *parent |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
71 | ) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
72 | if (node->parent == parent) return; |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
73 | if (node->parent != NULL || parent == NULL) asc_scene_node_unlink(node); |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | if (parent != NULL) { |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
75 | if (parent->children == NULL) { |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
76 | parent->children = node; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
77 | } else { |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
78 | parent->children->prev = node; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
79 | node->next = parent->children; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
80 | } |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | node->parent = parent; |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | } |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | void asc_scene_node_unlink(AscSceneNode *node) { |
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | if (node->parent == NULL) return; |
27
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
87 | AscSceneNode *left = node->prev; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
88 | AscSceneNode *right = node->next; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
89 | assert(left == NULL || node->parent->children != node); |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
90 | if (left != NULL) { |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
91 | left->next = right; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
92 | } else { |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
93 | node->parent->children = right; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
94 | } |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
95 | if (right != NULL) right->prev = left; |
4ccf4703999e
improve implementation of scene graph tree
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
96 | node->parent = node->prev = node->next = NULL; |
21
1a47c57666f5
add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | } |