src/scene.c

Sun, 21 Jan 2024 13:34:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 21 Jan 2024 13:34:49 +0100
changeset 26
139dac8ef9ee
parent 21
1a47c57666f5
child 27
4ccf4703999e
permissions
-rw-r--r--

make AscText immutable when drawing

21
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 * Copyright 2023 Mike Becker. All rights reserved.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 * Redistribution and use in source and binary forms, with or without
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * modification, are permitted provided that the following conditions are met:
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 * 1. Redistributions of source code must retain the above copyright
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * notice, this list of conditions and the following disclaimer.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 * 2. Redistributions in binary form must reproduce the above copyright
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer in the
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * documentation and/or other materials provided with the distribution.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 *
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * POSSIBILITY OF SUCH DAMAGE.
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 */
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 #include "ascension/scene.h"
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29 #include "ascension/error.h"
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #include <cx/linked_list.h>
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32 #include <assert.h>
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 void asc_scene_init(AscScene *scene) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 if (scene->root != NULL) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 asc_error("Scene is already initialized.");
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 return;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 scene->root = asc_scene_node_create(NULL);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 void asc_scene_destroy(AscScene *scene) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 asc_scene_node_free(scene->root);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 AscSceneNode *asc_scene_node_create(AscSceneNode *parent) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 AscSceneNode *node = malloc(sizeof(AscSceneNode));
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 assert(node != NULL);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 node->children = cxLinkedListCreateSimple(CX_STORE_POINTERS);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 assert(node->children != NULL);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 node->parent = NULL;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 asc_scene_node_link(node, parent);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 return node;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 void asc_scene_node_free(AscSceneNode *node) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 if (node == NULL) return;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 while (cxListSize(node->children) > 0) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 AscSceneNode *child = cxListAt(node->children, 0);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 asc_scene_node_free(child);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 if (node->parent != NULL) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 cxListFindRemove(node->parent->children, node);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 free(node);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 void asc_scene_node_link(
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 AscSceneNode *node,
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 AscSceneNode *parent
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 ) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 if (node->parent == parent) return;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 if (node->parent != NULL || parent == NULL) asc_scene_node_unlink(node);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 if (parent != NULL) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75 cxListAdd(parent->children, node);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76 node->parent = parent;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 }
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80 void asc_scene_node_unlink(AscSceneNode *node) {
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 if (node->parent == NULL) return;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 cxListFindRemove(node->parent->children, node);
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 node->parent = NULL;
1a47c57666f5 add first draft of a scene graph structure
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 }

mercurial