src/scene.c

Mon, 26 Feb 2024 21:16:00 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 26 Feb 2024 21:16:00 +0100
changeset 31
8324037e0148
parent 30
fceda550ebcb
child 32
86468a71dd73
permissions
-rw-r--r--

use tree iterator to free scene nodes

     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 }
    57 void asc_scene_draw(AscScene const *scene) {
    58     // TODO: don't visit the tree, visit the render groups
    59     CxTreeIterator iter = cx_tree_iterator(scene->root, false, child_list_off_);
    60     cx_foreach(AscSceneNode*, node, iter) {
    61         if (node->draw_func != NULL) {
    62             node->draw_func(node);
    63         }
    64     }
    65 }
    67 AscSceneNode *asc_scene_node_empty(void) {
    68     // TODO: check if this can remain a calloc or if it's too expensive
    69     AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
    70     assert(node != NULL);
    71     node->free_func = (asc_scene_free_func) free;
    72     return node;
    73 }
    75 void asc_scene_node_free(AscSceneNode *node) {
    76     if (node == NULL) return;
    78     // remove this node from its parent
    79     asc_scene_node_unlink(node);
    81     // free the entire subtree
    82     CxTreeIterator iter = cx_tree_iterator(node, true, child_list_off_);
    83     cx_foreach(AscSceneNode*, child, iter) {
    84         if (!iter.exiting) continue;
    85         if (child->free_func != NULL) {
    86             child->free_func(child);
    87         } else {
    88             free(child);
    89         }
    90     }
    91 }
    93 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
    94     cx_tree_link(parent, node, node_layout_);
    95 }
    97 void asc_scene_node_unlink(AscSceneNode *node) {
    98     cx_tree_unlink(node, node_layout_);
    99 }

mercurial