src/scene.c

changeset 21
1a47c57666f5
child 27
4ccf4703999e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/scene.c	Mon Dec 18 19:04:44 2023 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + * Copyright 2023 Mike Becker. All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions are met:
    1.10 + *
    1.11 + *   1. Redistributions of source code must retain the above copyright
    1.12 + *      notice, this list of conditions and the following disclaimer.
    1.13 + *
    1.14 + *   2. Redistributions in binary form must reproduce the above copyright
    1.15 + *      notice, this list of conditions and the following disclaimer in the
    1.16 + *      documentation and/or other materials provided with the distribution.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 + * POSSIBILITY OF SUCH DAMAGE.
    1.29 + */
    1.30 +
    1.31 +#include "ascension/scene.h"
    1.32 +#include "ascension/error.h"
    1.33 +
    1.34 +#include <cx/linked_list.h>
    1.35 +#include <assert.h>
    1.36 +
    1.37 +void asc_scene_init(AscScene *scene) {
    1.38 +    if (scene->root != NULL) {
    1.39 +        asc_error("Scene is already initialized.");
    1.40 +        return;
    1.41 +    }
    1.42 +    scene->root = asc_scene_node_create(NULL);
    1.43 +}
    1.44 +
    1.45 +void asc_scene_destroy(AscScene *scene) {
    1.46 +    asc_scene_node_free(scene->root);
    1.47 +}
    1.48 +
    1.49 +AscSceneNode *asc_scene_node_create(AscSceneNode *parent) {
    1.50 +    AscSceneNode *node = malloc(sizeof(AscSceneNode));
    1.51 +    assert(node != NULL);
    1.52 +    node->children = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    1.53 +    assert(node->children != NULL);
    1.54 +    node->parent = NULL;
    1.55 +    asc_scene_node_link(node, parent);
    1.56 +    return node;
    1.57 +}
    1.58 +
    1.59 +void asc_scene_node_free(AscSceneNode *node) {
    1.60 +    if (node == NULL) return;
    1.61 +    while (cxListSize(node->children) > 0) {
    1.62 +        AscSceneNode *child = cxListAt(node->children, 0);
    1.63 +        asc_scene_node_free(child);
    1.64 +    }
    1.65 +    if (node->parent != NULL) {
    1.66 +        cxListFindRemove(node->parent->children, node);
    1.67 +    }
    1.68 +    free(node);
    1.69 +}
    1.70 +
    1.71 +void asc_scene_node_link(
    1.72 +        AscSceneNode *node,
    1.73 +        AscSceneNode *parent
    1.74 +) {
    1.75 +    if (node->parent == parent) return;
    1.76 +    if (node->parent != NULL || parent == NULL) asc_scene_node_unlink(node);
    1.77 +    if (parent != NULL) {
    1.78 +        cxListAdd(parent->children, node);
    1.79 +        node->parent = parent;
    1.80 +    }
    1.81 +}
    1.82 +
    1.83 +void asc_scene_node_unlink(AscSceneNode *node) {
    1.84 +    if (node->parent == NULL) return;
    1.85 +    cxListFindRemove(node->parent->children, node);
    1.86 +    node->parent = NULL;
    1.87 +}
    1.88 \ No newline at end of file

mercurial