Add inventory.
This commit is contained in:
119
archive/scene/scenemanager.c
Normal file
119
archive/scene/scenemanager.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* 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 "display/framebuffer.h"
|
||||
#include "util/string.h"
|
||||
#include "asset/asset.h"
|
||||
#include "script/scriptmanager.h"
|
||||
|
||||
scenemanager_t SCENE_MANAGER;
|
||||
|
||||
errorret_t sceneManagerInit(void) {
|
||||
memoryZero(&SCENE_MANAGER, sizeof(scenemanager_t));
|
||||
|
||||
sceneManagerRegisterScene(&SCENE_TEST);
|
||||
sceneManagerRegisterScene(&SCENE_MAP);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
errorret_t sceneManagerSetScene(scene_t *scene) {
|
||||
if(
|
||||
SCENE_MANAGER.current &&
|
||||
(SCENE_MANAGER.current->flags & SCENE_FLAG_INITIALIZED) != 0
|
||||
) {
|
||||
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 && (scene->flags & SCENE_FLAG_INITIALIZED) == 0) {
|
||||
scene->flags |= SCENE_FLAG_INITIALIZED;
|
||||
|
||||
if(scene->init) errorChain(scene->init(&SCENE_MANAGER.sceneData));
|
||||
|
||||
// Execute scene script if it exists
|
||||
char_t buffer[256];
|
||||
snprintf(buffer, sizeof(buffer), "scene/%s.dsf", scene->name);
|
||||
if(assetFileExists(buffer)) {
|
||||
scriptcontext_t ctx;
|
||||
scriptContextInit(&ctx);
|
||||
errorChain(scriptContextExecFile(&ctx, buffer));
|
||||
scriptContextDispose(&ctx);
|
||||
}
|
||||
}
|
||||
|
||||
errorOk();
|
||||
}
|
||||
|
||||
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"
|
||||
);
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user