Scene methods
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -6,18 +6,13 @@
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "error/error.h"
|
||||
#include "script/scriptcontext.h"
|
||||
|
||||
typedef struct {
|
||||
const char_t *name;
|
||||
errorret_t (*init)(void);
|
||||
void (*update)(void);
|
||||
void (*render)(void);
|
||||
void (*dispose)(void);
|
||||
scriptcontext_t scriptContext;
|
||||
} scene_t;
|
||||
|
||||
extern const scene_t SCENES[];
|
||||
extern uint_fast8_t SCENE_CURRENT;
|
||||
extern scene_t SCENE;
|
||||
|
||||
/**
|
||||
* Initialize the scene subsystem.
|
||||
@@ -34,30 +29,14 @@ void sceneUpdate(void);
|
||||
*/
|
||||
void sceneRender(void);
|
||||
|
||||
/**
|
||||
* Set the current scene by script name.
|
||||
*
|
||||
* @param script The script name of the scene to set.
|
||||
*/
|
||||
errorret_t sceneSet(const char_t *script);
|
||||
|
||||
/**
|
||||
* Dispose of the scene subsystem.
|
||||
*/
|
||||
void sceneDispose(void);
|
||||
|
||||
/**
|
||||
* Set the current scene.
|
||||
*
|
||||
* @param sceneIndex The index of the scene to set.
|
||||
* @return An error code indicating success or failure.
|
||||
*/
|
||||
errorret_t sceneSet(const scene_t *scene);
|
||||
|
||||
/**
|
||||
* Get the current scene.
|
||||
*
|
||||
* @return The current scene.
|
||||
*/
|
||||
const scene_t* sceneGetCurrent(void);
|
||||
|
||||
/**
|
||||
* Get a scene by its name.
|
||||
*
|
||||
* @param name The name of the scene.
|
||||
* @return The scene with the given name, or NULL if not found.
|
||||
*/
|
||||
const scene_t* sceneGetByName(const char_t *name);
|
||||
void sceneDispose(void);
|
||||
Reference in New Issue
Block a user