src/scene.c

Tue, 23 Jan 2024 21:34:12 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Jan 2024 21:34:12 +0100
changeset 29
1d001eb694dc
parent 27
4ccf4703999e
child 30
fceda550ebcb
permissions
-rw-r--r--

bring first scene graph to live

     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 asc_scene_node_layout \
    36     offsetof(AscSceneNode, parent), offsetof(AscSceneNode, children), \
    37     offsetof(AscSceneNode, prev), offsetof(AscSceneNode, next)
    39 void asc_scene_init(AscScene *scene) {
    40     if (scene->root != NULL) {
    41         asc_error("Scene is already initialized.");
    42         return;
    43     }
    44     scene->root = asc_scene_node_empty();
    45 }
    47 void asc_scene_destroy(AscScene *scene) {
    48     asc_scene_node_free(scene->root);
    49 }
    51 void asc_scene_add(AscScene *scene, AscSceneNode *node) {
    52     asc_scene_node_link(scene->root, node);
    53 }
    55 static void asc_scene_draw_node(AscSceneNode *node) {
    56     if (node->draw_func != NULL) {
    57         node->draw_func(node);
    58     }
    59     if (node->children != NULL) {
    60         asc_scene_draw_node(node->children);
    61     }
    62     if (node->next != NULL) {
    63         asc_scene_draw_node(node->next);
    64     }
    65 }
    67 void asc_scene_draw(AscScene const *scene) {
    68     // TODO: replace with UCX tree visitor
    69     // TODO: don't visit the tree, visit the render groups
    70     // TODO: avoid recursion
    71     asc_scene_draw_node(scene->root);
    72 }
    74 AscSceneNode *asc_scene_node_empty(void) {
    75     // TODO: check if this can remain a calloc or if it's too expensive
    76     AscSceneNode *node = calloc(1, sizeof(AscSceneNode));
    77     assert(node != NULL);
    78     node->free_func = (asc_scene_free_func) free;
    79     return node;
    80 }
    82 void asc_scene_node_free(AscSceneNode *node) {
    83     if (node == NULL) return;
    85     // TODO: replace with UCX tree visitor
    86     // TODO: avoid recursion
    88     // free the children recursively
    89     while (node->children != NULL) {
    90         asc_scene_node_free(node->children);
    91     }
    93     // remove this node from its parent
    94     asc_scene_node_unlink(node);
    96     // free the node
    97     if (node->free_func != NULL) {
    98         node->free_func(node);
    99     }
   100 }
   102 void asc_scene_node_link(AscSceneNode * restrict parent, AscSceneNode * restrict node) {
   103     cx_tree_link(parent, node, asc_scene_node_layout);
   104 }
   106 void asc_scene_node_unlink(AscSceneNode *node) {
   107     cx_tree_unlink(node, asc_scene_node_layout);
   108 }

mercurial