Files
dusk/src/scene/scene.c

96 lines
2.8 KiB
C

// Copyright (c) 2026 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "scene.h"
#include "assert/assert.h"
#include "util/memory.h"
#include "debug/debug.h"
#include "time/time.h"
#include "display/camera/camera.h"
#include "display/screen.h"
scene_t SCENE;
errorret_t sceneInit(void) {
memoryZero(&SCENE, sizeof(scene_t));
errorChain(scriptContextInit(&SCENE.scriptContext));
errorOk();
}
errorret_t sceneUpdate(void) {
#if TIME_FIXED == 0
if(!TIME.dynamicUpdate) {
errorOk();
}
#endif
lua_getglobal(SCENE.scriptContext.luaState, "sceneUpdate");
if(!lua_isfunction(SCENE.scriptContext.luaState, -1)) {
lua_pop(SCENE.scriptContext.luaState, 1);
errorOk();
}
if(lua_pcall(SCENE.scriptContext.luaState, 0, 0, 0) != LUA_OK) {
const char_t *strErr = lua_tostring(SCENE.scriptContext.luaState, -1);
lua_pop(SCENE.scriptContext.luaState, 1);
errorThrow("Failed to call function '%s': %s", "sceneUpdate", strErr);
}
errorOk();
}
errorret_t sceneRender(void) {
lua_getglobal(SCENE.scriptContext.luaState, "sceneRender");
if(!lua_isfunction(SCENE.scriptContext.luaState, -1)) {
lua_pop(SCENE.scriptContext.luaState, 1);
errorOk();
}
if(lua_pcall(SCENE.scriptContext.luaState, 0, 0, 0) != LUA_OK) {
const char_t *strErr = lua_tostring(SCENE.scriptContext.luaState, -1);
lua_pop(SCENE.scriptContext.luaState, 1);
errorThrow("Failed to call function '%s': %s", "sceneRender", strErr);
}
errorOk();
}
errorret_t sceneSet(const char_t *script) {
// Cleanup old script context.
lua_getglobal(SCENE.scriptContext.luaState, "sceneDispose");
if(lua_isfunction(SCENE.scriptContext.luaState, -1)) {
if(lua_pcall(SCENE.scriptContext.luaState, 0, 0, 0) != LUA_OK) {
const char_t *strErr = lua_tostring(SCENE.scriptContext.luaState, -1);
lua_pop(SCENE.scriptContext.luaState, 1);
errorThrow("Failed to call function '%s': %s", "sceneDispose", strErr);
}
} else {
lua_pop(SCENE.scriptContext.luaState, 1);
}
scriptContextDispose(&SCENE.scriptContext);
// Create a new script context.
errorChain(scriptContextInit(&SCENE.scriptContext));
errorChain(scriptContextExecFile(&SCENE.scriptContext, script));
errorOk();
}
void sceneDispose(void) {
lua_getglobal(SCENE.scriptContext.luaState, "sceneDispose");
if(lua_isfunction(SCENE.scriptContext.luaState, -1)) {
if(lua_pcall(SCENE.scriptContext.luaState, 0, 0, 0) != LUA_OK) {
const char_t *strErr = lua_tostring(SCENE.scriptContext.luaState, -1);
lua_pop(SCENE.scriptContext.luaState, 1);
debugPrint("Failed to call function '%s': %s\n", "sceneDispose", strErr);
debugFlush();
}
} else {
lua_pop(SCENE.scriptContext.luaState, 1);
}
scriptContextDispose(&SCENE.scriptContext);
}