Scene methods
Some checks failed
Build Dusk / run-tests (push) Failing after 2m14s
Build Dusk / build-linux (push) Successful in 2m12s
Build Dusk / build-psp (push) Failing after 1m30s

This commit is contained in:
2026-01-28 10:31:31 -06:00
parent 25dc97e3cc
commit 6bdb4ae30d
20 changed files with 215 additions and 151 deletions

View File

@@ -5,80 +5,63 @@
#include "scene.h"
#include "assert/assert.h"
#include "util/string.h"
#include "util/memory.h"
const scene_t SCENES[] = {
};
uint_fast8_t SCENE_CURRENT = 0xFF;
scene_t SCENE;
errorret_t sceneInit(void) {
// Initialize the scene subsystem here
memoryZero(&SCENE, sizeof(scene_t));
errorChain(scriptContextInit(&SCENE.scriptContext));
errorOk();
}
void sceneUpdate(void) {
const scene_t *current = sceneGetCurrent();
if(current && current->update) {
current->update();
if(!scriptContextHasFunc(&SCENE.scriptContext, "sceneUpdate")) {
return;
}
errorret_t err = scriptContextCallFunc(
&SCENE.scriptContext, "sceneUpdate", NULL, 0, NULL
);
errorCatch(errorPrint(err));
}
void sceneRender(void) {
const scene_t *current = sceneGetCurrent();
if(current && current->render) {
current->render();
if(!scriptContextHasFunc(&SCENE.scriptContext, "sceneRender")) {
return;
}
errorret_t err = scriptContextCallFunc(
&SCENE.scriptContext, "sceneRender", NULL, 0, NULL
);
errorCatch(errorPrint(err));
}
void sceneDispose(void) {
const scene_t *current = sceneGetCurrent();
if(current && current->dispose) {
current->dispose();
}
}
errorret_t sceneSet(const scene_t *scene) {
sceneDispose();
if(scene) {
SCENE_CURRENT = (uint_fast8_t)(scene - SCENES);
assertTrue(
SCENE_CURRENT < sizeof(SCENES) / sizeof(scene_t),
"Invalid scene index."
errorret_t sceneSet(const char_t *script) {
// Cleanup old script context.
if(scriptContextHasFunc(&SCENE.scriptContext, "sceneDispose")) {
errorret_t err = scriptContextCallFunc(
&SCENE.scriptContext, "sceneDispose", NULL, 0, NULL
);
if(scene->init) {
errorret_t err = scene->init();
if(err.code != ERROR_OK) SCENE_CURRENT = 0xFF;
errorChain(err);
}
} else {
SCENE_CURRENT = 0xFF;
errorCatch(errorPrint(err));
}
scriptContextDispose(&SCENE.scriptContext);
// Create a new script context.
errorChain(scriptContextInit(&SCENE.scriptContext));
errorChain(scriptContextExecFile(&SCENE.scriptContext, script));
errorOk();
}
const scene_t* sceneGetCurrent(void) {
if(SCENE_CURRENT == 0xFF) return NULL;
assertTrue(
SCENE_CURRENT < sizeof(SCENES) / sizeof(scene_t),
"Invalid current scene index."
);
return &SCENES[SCENE_CURRENT];
}
const scene_t* sceneGetByName(const char_t *name) {
for(uint_fast8_t i = 0; i < sizeof(SCENES) / sizeof(scene_t); i++) {
if(stringCompare(SCENES[i].name, name) == 0) {
return &SCENES[i];
}
void sceneDispose(void) {
if(scriptContextHasFunc(&SCENE.scriptContext, "sceneDispose")) {
errorret_t err = scriptContextCallFunc(
&SCENE.scriptContext, "sceneDispose", NULL, 0, NULL
);
errorCatch(errorPrint(err));
}
return NULL;
scriptContextDispose(&SCENE.scriptContext);
}