Wed, 06 Mar 2024 23:12:56 +0100
fix const-warning
universe@21 | 1 | /* |
universe@21 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@21 | 3 | * Copyright 2023 Mike Becker. All rights reserved. |
universe@21 | 4 | * |
universe@21 | 5 | * Redistribution and use in source and binary forms, with or without |
universe@21 | 6 | * modification, are permitted provided that the following conditions are met: |
universe@21 | 7 | * |
universe@21 | 8 | * 1. Redistributions of source code must retain the above copyright |
universe@21 | 9 | * notice, this list of conditions and the following disclaimer. |
universe@21 | 10 | * |
universe@21 | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@21 | 12 | * notice, this list of conditions and the following disclaimer in the |
universe@21 | 13 | * documentation and/or other materials provided with the distribution. |
universe@21 | 14 | * |
universe@21 | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@21 | 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@21 | 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@21 | 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@21 | 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@21 | 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@21 | 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@21 | 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@21 | 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@21 | 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@21 | 25 | * POSSIBILITY OF SUCH DAMAGE. |
universe@21 | 26 | */ |
universe@21 | 27 | |
universe@21 | 28 | #include "ascension/scene.h" |
universe@21 | 29 | #include "ascension/error.h" |
universe@21 | 30 | |
universe@29 | 31 | #include <cx/tree.h> |
universe@29 | 32 | |
universe@21 | 33 | #include <assert.h> |
universe@21 | 34 | |
universe@30 | 35 | #define child_list_off_ \ |
universe@30 | 36 | offsetof(AscSceneNode, children), offsetof(AscSceneNode, next) |
universe@29 | 37 | |
universe@21 | 38 | void asc_scene_init(AscScene *scene) { |
universe@21 | 39 | if (scene->root != NULL) { |
universe@21 | 40 | asc_error("Scene is already initialized."); |
universe@21 | 41 | return; |
universe@21 | 42 | } |
universe@29 | 43 | scene->root = asc_scene_node_empty(); |
universe@21 | 44 | } |
universe@21 | 45 | |
universe@21 | 46 | void asc_scene_destroy(AscScene *scene) { |
universe@21 | 47 | asc_scene_node_free(scene->root); |
universe@21 | 48 | } |
universe@21 | 49 | |
universe@29 | 50 | void asc_scene_add(AscScene *scene, AscSceneNode *node) { |
universe@29 | 51 | asc_scene_node_link(scene->root, node); |
universe@32 | 52 | asc_node_update(node); |
universe@29 | 53 | } |
universe@29 | 54 | |
universe@30 | 55 | void asc_scene_draw(AscScene const *scene) { |
universe@30 | 56 | CxTreeIterator iter = cx_tree_iterator(scene->root, false, child_list_off_); |
universe@32 | 57 | |
universe@32 | 58 | // skip the root node deliberately, we know it's just the container |
universe@32 | 59 | cxIteratorNext(iter); |
universe@32 | 60 | |
universe@33 | 61 | // update the children |
universe@30 | 62 | cx_foreach(AscSceneNode*, node, iter) { |
universe@33 | 63 | // execute behaviors, first |
universe@33 | 64 | AscBehaviorNode *behavior = node->behaviors; |
universe@33 | 65 | while (behavior) { |
universe@33 | 66 | behavior->func(node); |
universe@33 | 67 | behavior = behavior->next; |
universe@33 | 68 | } |
universe@33 | 69 | |
universe@33 | 70 | // check if geometry needs update |
universe@32 | 71 | if (node->need_update && node->update_func != NULL) { |
universe@32 | 72 | node->need_update = false; |
universe@32 | 73 | asc_transform_copy(node->transform, node->parent->transform); |
universe@32 | 74 | node->update_func(node); |
universe@32 | 75 | } |
universe@33 | 76 | |
universe@32 | 77 | // TODO: don't visit the tree for drawing, visit the render groups |
universe@30 | 78 | if (node->draw_func != NULL) { |
universe@30 | 79 | node->draw_func(node); |
universe@30 | 80 | } |
universe@29 | 81 | } |
universe@29 | 82 | } |
universe@29 | 83 | |
universe@29 | 84 | AscSceneNode *asc_scene_node_empty(void) { |
universe@27 | 85 | AscSceneNode *node = calloc(1, sizeof(AscSceneNode)); |
universe@21 | 86 | assert(node != NULL); |
universe@29 | 87 | node->free_func = (asc_scene_free_func) free; |
universe@32 | 88 | asc_transform_identity(node->transform); |
universe@21 | 89 | return node; |
universe@21 | 90 | } |
universe@21 | 91 | |
universe@21 | 92 | void asc_scene_node_free(AscSceneNode *node) { |
universe@21 | 93 | if (node == NULL) return; |
universe@27 | 94 | |
universe@27 | 95 | // remove this node from its parent |
universe@27 | 96 | asc_scene_node_unlink(node); |
universe@27 | 97 | |
universe@31 | 98 | // free the entire subtree |
universe@31 | 99 | CxTreeIterator iter = cx_tree_iterator(node, true, child_list_off_); |
universe@31 | 100 | cx_foreach(AscSceneNode*, child, iter) { |
universe@31 | 101 | if (!iter.exiting) continue; |
universe@31 | 102 | if (child->free_func != NULL) { |
universe@31 | 103 | child->free_func(child); |
universe@31 | 104 | } else { |
universe@31 | 105 | free(child); |
universe@31 | 106 | } |
universe@21 | 107 | } |
universe@21 | 108 | } |
universe@21 | 109 | |
universe@29 | 110 | void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) { |
universe@33 | 111 | cx_tree_link( |
universe@33 | 112 | parent, node, |
universe@33 | 113 | offsetof(AscSceneNode, parent), |
universe@33 | 114 | offsetof(AscSceneNode, children), |
universe@33 | 115 | offsetof(AscSceneNode, prev), |
universe@33 | 116 | offsetof(AscSceneNode, next) |
universe@33 | 117 | ); |
universe@29 | 118 | } |
universe@29 | 119 | |
universe@21 | 120 | void asc_scene_node_unlink(AscSceneNode *node) { |
universe@33 | 121 | cx_tree_unlink( |
universe@33 | 122 | node, |
universe@33 | 123 | offsetof(AscSceneNode, parent), |
universe@33 | 124 | offsetof(AscSceneNode, children), |
universe@33 | 125 | offsetof(AscSceneNode, prev), |
universe@33 | 126 | offsetof(AscSceneNode, next) |
universe@33 | 127 | ); |
universe@33 | 128 | } |
universe@33 | 129 | |
universe@33 | 130 | AscBehaviorNode *asc_scene_add_behavior(AscSceneNode *node, asc_scene_update_func behavior) { |
universe@33 | 131 | AscBehaviorNode *behavior_node = calloc(1, sizeof(AscBehaviorNode)); |
universe@33 | 132 | behavior_node->func = behavior; |
universe@33 | 133 | cx_tree_link( |
universe@33 | 134 | node, |
universe@33 | 135 | behavior_node, |
universe@33 | 136 | offsetof(AscBehaviorNode, parent), |
universe@33 | 137 | offsetof(AscSceneNode, behaviors), |
universe@33 | 138 | offsetof(AscBehaviorNode, prev), |
universe@33 | 139 | offsetof(AscBehaviorNode, next) |
universe@33 | 140 | ); |
universe@33 | 141 | return behavior_node; |
universe@33 | 142 | } |
universe@33 | 143 | |
universe@33 | 144 | void asc_scene_remove_behavior(AscBehaviorNode *node) { |
universe@33 | 145 | cx_tree_unlink( |
universe@33 | 146 | node, |
universe@33 | 147 | offsetof(AscBehaviorNode, parent), |
universe@33 | 148 | offsetof(AscSceneNode, behaviors), |
universe@33 | 149 | offsetof(AscBehaviorNode, prev), |
universe@33 | 150 | offsetof(AscBehaviorNode, next) |
universe@33 | 151 | ); |
universe@33 | 152 | } |