2 * Copyright 2020 Iwo 'Outfrost' Bujkiewicz
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 #include "engine/logger.h"
15 Scene* currentScene = NULL;
20 Scene* scene = malloc(sizeof(Scene));
21 *scene = (Scene) { .parent = NULL,
24 .transform = identity(),
29 void insertChildScene(Scene* scene, Scene* newChild) {
30 if (!scene || !newChild) {
33 if (newChild->parent) {
35 "Cannot insert Scene 0x%p as child of 0x%p, because it is already child of 0x%p",
42 scene->children = realloc(scene->children, ++(scene->numChildren) * sizeof(Scene*));
43 scene->children[scene->numChildren - 1] = newChild;
45 newChild->parent = scene;
48 void reparentScene(Scene* scene, Scene* newParent) {
54 Scene* parent = scene->parent;
55 size_t indexInParent = parent->numChildren;
56 for (size_t i = 0; i < parent->numChildren; ++i) {
57 if (parent->children[i] == scene) {
62 // This includes the hopefully impossible case where parent->numChildren == 0
63 if (indexInParent == parent->numChildren) {
64 logError("Scene %p not found in children of parent %p", scene, parent);
67 for (size_t i = indexInParent; i < parent->numChildren - 1; ++i) {
68 parent->children[i] = parent->children[i + 1];
70 parent->children = realloc(parent->children, --(parent->numChildren) * sizeof(Scene*));
76 insertChildScene(newParent, scene);
79 Transform worldTransform(const Scene* scene) {
81 logError("Cannot compute world space Transform of null Scene");
86 return multiply(scene->transform, worldTransform(scene->parent));
89 return scene->transform;