Files
dusk/src/scene/scenemanager.c
2025-10-08 06:53:37 -05:00

115 lines
2.7 KiB
C

/**
* Copyright (c) 2025 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "scenemanager.h"
#include "util/memory.h"
#include "assert/assert.h"
#include "console/console.h"
#include "display/framebuffer.h"
#include "util/string.h"
scenemanager_t SCENE_MANAGER;
errorret_t sceneManagerInit(void) {
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
sceneManagerRegisterScene(&SCENE_TEST);
errorOk();
}
scene_t * sceneManagerGetSceneByName(const char_t *name) {
assertNotNull(name, "Name is null");
for(uint8_t i = 0; i < SCENE_MANAGER.sceneCount; i++) {
if(strcmp(SCENE_MANAGER.scenes[i]->name, name) != 0) continue;
return SCENE_MANAGER.scenes[i];
}
return NULL;
}
void sceneManagerRegisterScene(scene_t *scene) {
assertNotNull(scene, "Scene is null");
assertTrue(
SCENE_MANAGER.sceneCount < SCENE_MANAGER_SCENE_COUNT_MAX,
"Scene count exceeded max"
);
assertNotNull(scene->name, "Scene name is null");
assertNull(
sceneManagerGetSceneByName(scene->name), "Scene name already registered"
);
SCENE_MANAGER.scenes[SCENE_MANAGER.sceneCount++] = scene;
}
void sceneManagerSetScene(scene_t *scene) {
if(SCENE_MANAGER.current) {
// TODO: Should dispose?
assertTrue(
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
"Current scene not initialized"
);
SCENE_MANAGER.current->flags &= ~SCENE_FLAG_INITIALIZED;
if(SCENE_MANAGER.current->dispose) {
SCENE_MANAGER.current->dispose(&SCENE_MANAGER.sceneData);
}
}
SCENE_MANAGER.current = scene;
if(scene) {
assertTrue(
scene->flags & SCENE_FLAG_INITIALIZED,
"Scene not initialized"
);
}
}
void sceneManagerUpdate(void) {
if(!SCENE_MANAGER.current) return;
assertTrue(
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
"Current scene not initialized"
);
if(SCENE_MANAGER.current->update) {
SCENE_MANAGER.current->update(&SCENE_MANAGER.sceneData);
}
}
void sceneManagerRender(void) {
if(!SCENE_MANAGER.current) return;
assertTrue(
SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED,
"Current scene not initialized"
);
frameBufferClear(
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
SCENE_MANAGER.current->background
);
if(SCENE_MANAGER.current->render) {
SCENE_MANAGER.current->render(&SCENE_MANAGER.sceneData);
}
}
void sceneManagerDispose(void) {
for(uint8_t i = 0; i < SCENE_MANAGER.sceneCount; i++) {
scene_t *scene = SCENE_MANAGER.scenes[i];
if(scene->flags & SCENE_FLAG_INITIALIZED) {
scene->dispose(&SCENE_MANAGER.sceneData);
}
}
SCENE_MANAGER.sceneCount = 0;
}