Scene tree
This commit is contained in:
10
src/scene/CMakeLists.txt
Normal file
10
src/scene/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DUSK_TARGET_NAME}
|
||||
PRIVATE
|
||||
scenetree.c
|
||||
)
|
||||
@@ -1,15 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dusk.h"
|
||||
|
||||
typedef struct {
|
||||
int padding;
|
||||
} scene_t;
|
||||
|
||||
extern scene_t *SCENE;
|
||||
117
src/scene/scenetree.c
Normal file
117
src/scene/scenetree.c
Normal file
@@ -0,0 +1,117 @@
|
||||
// Copyright (c) 2025 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "scenetree.h"
|
||||
#include "util/memory.h"
|
||||
#include "assert/assert.h"
|
||||
|
||||
scenetree_t SCENE_TREE;
|
||||
|
||||
void sceneTreeInit(void) {
|
||||
memoryZero(&SCENE_TREE, sizeof(scenetree_t));
|
||||
|
||||
// Default all items to have no parent
|
||||
for (ecsentityid_t i = 0; i < ECS_ENTITY_COUNT_MAX; i++) {
|
||||
SCENE_TREE.items[i].parent = -1;
|
||||
}
|
||||
}
|
||||
|
||||
ecsentityid_t sceneTreeParentGet(const ecsentityid_t child) {
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
return SCENE_TREE.items[child].parent;
|
||||
}
|
||||
|
||||
uint8_t sceneTreeChildGetAll(const ecsentityid_t id, ecsentityid_t *children) {
|
||||
assertTrue(id >= 0 && id < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
|
||||
const scenetreenode_t *node = &SCENE_TREE.items[id];
|
||||
if(children == NULL) return node->childCount;
|
||||
|
||||
return node->childCount;
|
||||
}
|
||||
|
||||
void sceneTreeChildAdd(const ecsentityid_t parent, const ecsentityid_t child) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
assertTrue(parent != child, "Child cannot be a child of itself.");
|
||||
assertFalse(
|
||||
sceneTreeChildInTree(parent, child),
|
||||
"Child is already in the parent's tree"
|
||||
);
|
||||
assertFalse(
|
||||
sceneTreeChildInTree(child, parent),
|
||||
"Child cannot be a deep child of itself."
|
||||
);
|
||||
|
||||
scenetreenode_t *parentNode = &SCENE_TREE.items[parent];
|
||||
assertTrue(parentNode->childCount < SCENE_ITEM_CHILD_MAX, "Parent full.");
|
||||
|
||||
// Add child to parent's children array
|
||||
parentNode->children[parentNode->childCount] = child;
|
||||
parentNode->childCount++;
|
||||
|
||||
// Set child's parent
|
||||
SCENE_TREE.items[child].parent = parent;
|
||||
}
|
||||
|
||||
void sceneTreeChildRemove(
|
||||
const ecsentityid_t parent,
|
||||
const ecsentityid_t child
|
||||
) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
assertTrue(parent != child, "Child cannot be a child of itself.");
|
||||
|
||||
scenetreenode_t *childNode = &SCENE_TREE.items[child];
|
||||
scenetreenode_t *parentNode = &SCENE_TREE.items[parent];
|
||||
assertTrue(childNode->parent == parent, "Child does not belong to parent");
|
||||
assertTrue(parentNode->childCount > 0, "Parent has no children");
|
||||
|
||||
// Remove child from parent's children array
|
||||
for (uint8_t i = 0; i < parentNode->childCount; i++) {
|
||||
if (parentNode->children[i] == child) {
|
||||
// Shift remaining children down
|
||||
for (uint8_t j = i; j < parentNode->childCount - 1; j++) {
|
||||
parentNode->children[j] = parentNode->children[j + 1];
|
||||
}
|
||||
parentNode->childCount--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Clear child's parent
|
||||
childNode->parent = -1;
|
||||
}
|
||||
|
||||
void sceneTreeChildRemoveAll(const ecsentityid_t parent) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
|
||||
scenetreenode_t *parentNode = &SCENE_TREE.items[parent];
|
||||
for(uint8_t i = 0; i < parentNode->childCount; i++) {
|
||||
ecsentityid_t child = parentNode->children[i];
|
||||
SCENE_TREE.items[child].parent = -1;
|
||||
}
|
||||
|
||||
// Reset child count
|
||||
parentNode->childCount = 0;
|
||||
}
|
||||
|
||||
bool_t sceneTreeChildInTree(
|
||||
const ecsentityid_t parent,
|
||||
const ecsentityid_t child
|
||||
) {
|
||||
assertTrue(parent >= 0 && parent < ECS_ENTITY_COUNT_MAX, "Invalid parent ID");
|
||||
assertTrue(child >= 0 && child < ECS_ENTITY_COUNT_MAX, "Invalid child ID");
|
||||
assertTrue(parent != child, "Child cannot be a child of itself.");
|
||||
|
||||
scenetreenode_t *childNode = &SCENE_TREE.items[child];
|
||||
while(childNode) {
|
||||
if(childNode->parent == parent) return true;
|
||||
if(childNode->parent == -1) break; // No parent means we've reached the root
|
||||
childNode = &SCENE_TREE.items[childNode->parent];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
91
src/scene/scenetree.h
Normal file
91
src/scene/scenetree.h
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Copyright (c) 2025 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "ecs/ecs.h"
|
||||
|
||||
#define SCENE_ITEM_CHILD_MAX 16
|
||||
|
||||
typedef struct {
|
||||
ecsentityid_t children[SCENE_ITEM_CHILD_MAX];
|
||||
uint8_t childCount;
|
||||
ecsentityid_t parent;
|
||||
} scenetreenode_t;
|
||||
|
||||
typedef struct scene_s {
|
||||
scenetreenode_t items[ECS_ENTITY_COUNT_MAX];
|
||||
} scenetree_t;
|
||||
|
||||
extern scenetree_t SCENE_TREE;
|
||||
|
||||
/**
|
||||
* Initialize the scene tree.
|
||||
*
|
||||
* This will initialize the ECS system and prepare the scene tree for use.
|
||||
*/
|
||||
void sceneTreeInit(void);
|
||||
|
||||
/**
|
||||
* Create a new scene item in the scene tree.
|
||||
* This will create a new entity in the ECS and return its ID.
|
||||
*
|
||||
* @return The ID of the newly created scene item.
|
||||
*/
|
||||
ecsentityid_t sceneTreeCreate(void);
|
||||
|
||||
/**
|
||||
* Get the parent of a given scene item.
|
||||
*
|
||||
* @param id The ID of the scene item.
|
||||
* @return The ID of the parent scene item, or -1 if it has no parent.
|
||||
*/
|
||||
ecsentityid_t sceneTreeParentGet(const ecsentityid_t child);
|
||||
|
||||
/**
|
||||
* Get the children of a given scene item. If children is NULL only the count
|
||||
* will be returned.
|
||||
*
|
||||
* @param id The ID of the scene item.
|
||||
* @param children Pointer to an array where the children IDs will be stored.
|
||||
* @return The number of children found.
|
||||
*/
|
||||
uint8_t sceneTreeChildGetAll(const ecsentityid_t id, ecsentityid_t *children);
|
||||
|
||||
/**
|
||||
* Add a child to a parent in the scene tree.
|
||||
*
|
||||
* @param parent The ID of the parent scene item.
|
||||
* @param child The ID of the child scene item to add.
|
||||
*/
|
||||
void sceneTreeChildAdd(const ecsentityid_t parent, const ecsentityid_t child);
|
||||
|
||||
/**
|
||||
* Remove a child from a parent in the scene tree.
|
||||
*
|
||||
* @param prnt The ID of the parent scene item.
|
||||
* @param child The ID of the child scene item to remove.
|
||||
*/
|
||||
void sceneTreeChildRemove(const ecsentityid_t prnt, const ecsentityid_t child);
|
||||
|
||||
/**
|
||||
* Remove all children from a parent in the scene tree.
|
||||
*
|
||||
* @param parent The ID of the parent scene item.
|
||||
*/
|
||||
void sceneTreeChildRemoveAll(const ecsentityid_t parent);
|
||||
|
||||
/**
|
||||
* Returns true if the child is within the parent's children, recursively.
|
||||
*
|
||||
* @param parent The ID of the parent scene item.
|
||||
* @param child The ID of the child scene item to check.
|
||||
* @return True if the child is in the parent's children, false otherwise.
|
||||
*/
|
||||
bool_t sceneTreeChildInTree(
|
||||
const ecsentityid_t parent,
|
||||
const ecsentityid_t child
|
||||
);
|
||||
Reference in New Issue
Block a user