add first draft of a scene graph structure

Mon, 18 Dec 2023 19:04:44 +0100

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Dec 2023 19:04:44 +0100
changeset 21
1a47c57666f5
parent 20
b101c1ef13c7
child 22
30b12e88fd84

add first draft of a scene graph structure

src/Makefile file | annotate | diff | comparison | revisions
src/ascension/scene.h file | annotate | diff | comparison | revisions
src/scene.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/Makefile	Mon Dec 18 13:04:04 2023 +0100
     1.2 +++ b/src/Makefile	Mon Dec 18 19:04:44 2023 +0100
     1.3 @@ -27,7 +27,8 @@
     1.4  
     1.5  BUILD_DIR=../build/lib
     1.6  
     1.7 -SRC  = context.c error.c window.c files.c shader.c font.c text.c primitives.c
     1.8 +SRC = context.c error.c window.c files.c shader.c font.c text.c scene.c \
     1.9 +	  primitives.c
    1.10  
    1.11  OBJ = $(SRC:%.c=$(BUILD_DIR)/%.o)
    1.12  
    1.13 @@ -70,6 +71,10 @@
    1.14  	@echo "Compiling $<"
    1.15  	$(CC) -o $@ $(CFLAGS) -c $<
    1.16  
    1.17 +$(BUILD_DIR)/scene.o: scene.c ascension/scene.h ascension/error.h
    1.18 +	@echo "Compiling $<"
    1.19 +	$(CC) -o $@ $(CFLAGS) -c $<
    1.20 +
    1.21  $(BUILD_DIR)/shader.o: shader.c ascension/shader.h ascension/files.h \
    1.22   ascension/error.h
    1.23  	@echo "Compiling $<"
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/ascension/scene.h	Mon Dec 18 19:04:44 2023 +0100
     2.3 @@ -0,0 +1,57 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + * Copyright 2023 Mike Becker. All rights reserved.
     2.7 + *
     2.8 + * Redistribution and use in source and binary forms, with or without
     2.9 + * modification, are permitted provided that the following conditions are met:
    2.10 + *
    2.11 + *   1. Redistributions of source code must retain the above copyright
    2.12 + *      notice, this list of conditions and the following disclaimer.
    2.13 + *
    2.14 + *   2. Redistributions in binary form must reproduce the above copyright
    2.15 + *      notice, this list of conditions and the following disclaimer in the
    2.16 + *      documentation and/or other materials provided with the distribution.
    2.17 + *
    2.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.28 + * POSSIBILITY OF SUCH DAMAGE.
    2.29 + */
    2.30 +
    2.31 +#ifndef ASCENSION_SCENE_H
    2.32 +#define ASCENSION_SCENE_H
    2.33 +
    2.34 +#include <cx/list.h>
    2.35 +
    2.36 +typedef struct AscSceneNode {
    2.37 +    struct AscSceneNode *parent;
    2.38 +    CxList *children;
    2.39 +    // TODO: add node contents
    2.40 +} AscSceneNode;
    2.41 +
    2.42 +typedef struct AscScene {
    2.43 +    AscSceneNode *root;
    2.44 +    // TODO: add render groups for batching
    2.45 +} AscScene;
    2.46 +
    2.47 +void asc_scene_init(AscScene *scene);
    2.48 +
    2.49 +void asc_scene_destroy(AscScene *scene);
    2.50 +
    2.51 +AscSceneNode *asc_scene_node_create(AscSceneNode *parent);
    2.52 +
    2.53 +void asc_scene_node_free(AscSceneNode *node);
    2.54 +
    2.55 +void asc_scene_node_link(AscSceneNode *node, AscSceneNode *parent);
    2.56 +
    2.57 +void asc_scene_node_unlink(AscSceneNode *node);
    2.58 +
    2.59 +#endif // ASCENSION_SCENE_H
    2.60 +
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/scene.c	Mon Dec 18 19:04:44 2023 +0100
     3.3 @@ -0,0 +1,84 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + * Copyright 2023 Mike Becker. All rights reserved.
     3.7 + *
     3.8 + * Redistribution and use in source and binary forms, with or without
     3.9 + * modification, are permitted provided that the following conditions are met:
    3.10 + *
    3.11 + *   1. Redistributions of source code must retain the above copyright
    3.12 + *      notice, this list of conditions and the following disclaimer.
    3.13 + *
    3.14 + *   2. Redistributions in binary form must reproduce the above copyright
    3.15 + *      notice, this list of conditions and the following disclaimer in the
    3.16 + *      documentation and/or other materials provided with the distribution.
    3.17 + *
    3.18 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.19 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.20 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.21 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.22 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.23 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.24 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.25 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.26 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.27 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.28 + * POSSIBILITY OF SUCH DAMAGE.
    3.29 + */
    3.30 +
    3.31 +#include "ascension/scene.h"
    3.32 +#include "ascension/error.h"
    3.33 +
    3.34 +#include <cx/linked_list.h>
    3.35 +#include <assert.h>
    3.36 +
    3.37 +void asc_scene_init(AscScene *scene) {
    3.38 +    if (scene->root != NULL) {
    3.39 +        asc_error("Scene is already initialized.");
    3.40 +        return;
    3.41 +    }
    3.42 +    scene->root = asc_scene_node_create(NULL);
    3.43 +}
    3.44 +
    3.45 +void asc_scene_destroy(AscScene *scene) {
    3.46 +    asc_scene_node_free(scene->root);
    3.47 +}
    3.48 +
    3.49 +AscSceneNode *asc_scene_node_create(AscSceneNode *parent) {
    3.50 +    AscSceneNode *node = malloc(sizeof(AscSceneNode));
    3.51 +    assert(node != NULL);
    3.52 +    node->children = cxLinkedListCreateSimple(CX_STORE_POINTERS);
    3.53 +    assert(node->children != NULL);
    3.54 +    node->parent = NULL;
    3.55 +    asc_scene_node_link(node, parent);
    3.56 +    return node;
    3.57 +}
    3.58 +
    3.59 +void asc_scene_node_free(AscSceneNode *node) {
    3.60 +    if (node == NULL) return;
    3.61 +    while (cxListSize(node->children) > 0) {
    3.62 +        AscSceneNode *child = cxListAt(node->children, 0);
    3.63 +        asc_scene_node_free(child);
    3.64 +    }
    3.65 +    if (node->parent != NULL) {
    3.66 +        cxListFindRemove(node->parent->children, node);
    3.67 +    }
    3.68 +    free(node);
    3.69 +}
    3.70 +
    3.71 +void asc_scene_node_link(
    3.72 +        AscSceneNode *node,
    3.73 +        AscSceneNode *parent
    3.74 +) {
    3.75 +    if (node->parent == parent) return;
    3.76 +    if (node->parent != NULL || parent == NULL) asc_scene_node_unlink(node);
    3.77 +    if (parent != NULL) {
    3.78 +        cxListAdd(parent->children, node);
    3.79 +        node->parent = parent;
    3.80 +    }
    3.81 +}
    3.82 +
    3.83 +void asc_scene_node_unlink(AscSceneNode *node) {
    3.84 +    if (node->parent == NULL) return;
    3.85 +    cxListFindRemove(node->parent->children, node);
    3.86 +    node->parent = NULL;
    3.87 +}
    3.88 \ No newline at end of file

mercurial