117 lines
2.7 KiB
C
117 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"
|
|
|
|
#include "scene/scene/scenetest.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.current->data);
|
|
}
|
|
}
|
|
|
|
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.current->data);
|
|
}
|
|
}
|
|
|
|
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.current->data);
|
|
}
|
|
}
|
|
|
|
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->data);
|
|
}
|
|
}
|
|
|
|
SCENE_MANAGER.sceneCount = 0;
|
|
}
|