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