Wed, 02 Jul 2025 23:21:17 +0200
resolve TODOs regarding input.h
a) mouse position must be integer, because it can be negative (though rarely)
b) we should not trade "access complexity" for space in the scancodes array
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * Copyright 2023 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include "ascension/scene_node.h" #include "ascension/context.h" #include <cx/tree.h> #include <cx/linked_list.h> #include <cx/printf.h> #include "ascension/error.h" static CxTreeIterator asc_scene_node_iterator( AscSceneNode *node, bool visit_on_exit ) { return cx_tree_iterator( node, visit_on_exit, offsetof(AscSceneNode, children), offsetof(AscSceneNode, next) ); } AscSceneNode *asc_scene_node_empty(void) { AscSceneNode *node = cxZallocDefault(sizeof(AscSceneNode)); node->render_group = ASC_RENDER_GROUP_NONE; node->scale = asc_vec3f_one; asc_transform_identity(node->transform); asc_transform_identity(node->world_transform); return node; } static void asc_scene_node_destroy(AscSceneNode *node) { cxListFree(node->behaviors); if (node->destroy_func != NULL) { node->destroy_func(node); } if (node->name.ptr != NULL) { asc_dprintf("Destroy node: %"CX_PRIstr, CX_SFMT(node->name)); cx_strfree(&node->name); } } void asc_scene_node_free(AscSceneNode *node) { if (node == NULL) return; // remove this node from its parent asc_scene_node_unlink(node); // free the entire subtree CxTreeIterator iter = asc_scene_node_iterator(node, true); cx_foreach(AscSceneNode*, child, iter) { if (!iter.exiting) continue; asc_scene_node_destroy(child); cxFreeDefault(child); } } void asc_scene_node_name(AscSceneNode *node, const char *name) { cx_strfree(&node->name); if (name == NULL) { node->name.ptr = NULL; node->name.length = 0; } else { node->name.ptr = strdup(name); node->name.length = strlen(name); } } cxstring asc_scene_node_get_name(AscSceneNode *node) { if (node->name.ptr != NULL) return cx_strcast(node->name); AscSceneNode *parent = node->parent; while (parent != NULL && parent->name.ptr == NULL) { parent = parent->parent; } if (parent == NULL) { cx_sprintf(&node->name.ptr, &node->name.length, "%"PRIuPTR " - w/o named parent", (uintptr_t)node); } else { cx_sprintf(&node->name.ptr, &node->name.length, "%"PRIuPTR " - child of %"CX_PRIstr, (uintptr_t)node, CX_SFMT(parent->name)); } return cx_strcast(node->name); } void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) { cx_tree_link( parent, node, offsetof(AscSceneNode, parent), offsetof(AscSceneNode, children), offsetof(AscSceneNode, last_child), offsetof(AscSceneNode, prev), offsetof(AscSceneNode, next) ); asc_node_update_transform(node); } void asc_scene_node_unlink(AscSceneNode *node) { cx_tree_unlink( node, offsetof(AscSceneNode, parent), offsetof(AscSceneNode, children), offsetof(AscSceneNode, last_child), offsetof(AscSceneNode, prev), offsetof(AscSceneNode, next) ); asc_node_update_transform(node); } void asc_node_update(AscSceneNode *node) { asc_set_flag(node->flags, ASC_SCENE_NODE_UPDATE_GRAPHICS); } void asc_node_update_transform(AscSceneNode *node) { // fast skip if node is already marked if (asc_test_flag(node->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) { return; } CxTreeIterator iter = asc_scene_node_iterator(node, false); cx_foreach(AscSceneNode*, n, iter) { if (asc_test_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM)) { cxTreeIteratorContinue(iter); } asc_set_flag(n->flags, ASC_SCENE_NODE_UPDATE_TRANSFORM); } }