Mon, 04 Mar 2024 21:16:46 +0100
add transformation matrix
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 asc_node_update(node);
56 }
58 void asc_scene_draw(AscScene const *scene) {
59 CxTreeIterator iter = cx_tree_iterator(scene->root, false, child_list_off_);
61 // skip the root node deliberately, we know it's just the container
62 cxIteratorNext(iter);
64 // draw the children
65 cx_foreach(AscSceneNode*, node, iter) {
66 if (node->need_update && node->update_func != NULL) {
67 node->need_update = false;
68 asc_transform_copy(node->transform, node->parent->transform);
69 node->update_func(node);
70 }
71 // TODO: don't visit the tree for drawing, visit the render groups
72 if (node->draw_func != NULL) {
73 node->draw_func(node);
74 }
75 }
76 }
78 AscSceneNode *asc_scene_node_empty(void) {
79 AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
80 assert(node != NULL);
81 node->free_func = (asc_scene_free_func) free;
82 asc_transform_identity(node->transform);
83 return node;
84 }
86 void asc_scene_node_free(AscSceneNode *node) {
87 if (node == NULL) return;
89 // remove this node from its parent
90 asc_scene_node_unlink(node);
92 // free the entire subtree
93 CxTreeIterator iter = cx_tree_iterator(node, true, child_list_off_);
94 cx_foreach(AscSceneNode*, child, iter) {
95 if (!iter.exiting) continue;
96 if (child->free_func != NULL) {
97 child->free_func(child);
98 } else {
99 free(child);
100 }
101 }
102 }
104 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
105 cx_tree_link(parent, node, node_layout_);
106 }
108 void asc_scene_node_unlink(AscSceneNode *node) {
109 cx_tree_unlink(node, node_layout_);
110 }