Scene update
This commit is contained in:
+3
-1
@@ -72,4 +72,6 @@ else
|
|||||||
print("Unknown platform, no default input bindings set.")
|
print("Unknown platform, no default input bindings set.")
|
||||||
end
|
end
|
||||||
|
|
||||||
print('Inited')
|
|
||||||
|
local test = 'test'
|
||||||
|
Scene.set('test/scene.lua')
|
||||||
+58
-50
@@ -17,12 +17,33 @@
|
|||||||
#include "display/screen/screen.h"
|
#include "display/screen/screen.h"
|
||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
#include "util/string.h"
|
#include "util/string.h"
|
||||||
|
#include "script/scriptmanager.h"
|
||||||
|
|
||||||
scene_t SCENE;
|
scene_t SCENE;
|
||||||
|
|
||||||
errorret_t sceneInit(void) {
|
errorret_t sceneInit(void) {
|
||||||
memoryZero(&SCENE, sizeof(scene_t));
|
memoryZero(&SCENE, sizeof(scene_t));
|
||||||
eventInit(&SCENE.eventUpdate, SCENE.updateListeners, SCENE_EVENT_UPDATE_MAX);
|
|
||||||
|
errorChain(scriptContextExec(
|
||||||
|
&SCRIPT_MANAGER.mainContext,
|
||||||
|
"SceneBase = {}\n"
|
||||||
|
"function SceneBase:callTest()\n"
|
||||||
|
" print('SceneBase:callTest called with: ' .. tostring(self.test))\n"
|
||||||
|
"end\n"
|
||||||
|
"Scene = {}\n"
|
||||||
|
"Scene.__index = Scene\n"
|
||||||
|
"setmetatable(Scene, { __index = SceneBase })\n"
|
||||||
|
"Scene.current = nil\n"
|
||||||
|
));
|
||||||
|
|
||||||
|
lua_State *L = SCRIPT_MANAGER.mainContext.luaState;
|
||||||
|
lua_getglobal(L, "Scene");
|
||||||
|
|
||||||
|
assertTrue(lua_istable(L, -1), "Scene must be a table");
|
||||||
|
lua_pushcfunction(L, sceneSetLua);
|
||||||
|
lua_setfield(L, -2, "set");
|
||||||
|
|
||||||
|
lua_pop(L, 1);
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,6 +54,16 @@ errorret_t sceneUpdate(void) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Call scene update on active scene
|
||||||
|
if(SCENE.scriptActive) {
|
||||||
|
errorChain(scriptContextExec(
|
||||||
|
&SCRIPT_MANAGER.mainContext,
|
||||||
|
"if Scene.current ~= nil and Scene.current.update ~= nil then\n"
|
||||||
|
" Scene.current:update()\n"
|
||||||
|
"end\n"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
// Is the scene changing?
|
// Is the scene changing?
|
||||||
if(stringCompare(SCENE.sceneNext, SCENE.sceneCurrent) != 0) {
|
if(stringCompare(SCENE.sceneNext, SCENE.sceneCurrent) != 0) {
|
||||||
errorChain(sceneSetImmediate(SCENE.sceneNext));
|
errorChain(sceneSetImmediate(SCENE.sceneNext));
|
||||||
@@ -42,19 +73,6 @@ errorret_t sceneUpdate(void) {
|
|||||||
if(!SCENE.scriptActive) {
|
if(!SCENE.scriptActive) {
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|
||||||
eventInvoke(&SCENE.eventUpdate, NULL, NULL);
|
|
||||||
|
|
||||||
assertNotNull(SCENE.script.luaState, "Script context null?");
|
|
||||||
lua_getglobal(SCENE.script.luaState, "sceneUpdate");
|
|
||||||
if(lua_isfunction(SCENE.script.luaState, -1)) {
|
|
||||||
if(lua_pcall(SCENE.script.luaState, 0, 0, 0) != LUA_OK) {
|
|
||||||
errorThrow(
|
|
||||||
"Failed to call sceneUpdate function in script: %s\n",
|
|
||||||
lua_tostring(SCENE.script.luaState, -1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
@@ -178,31 +196,15 @@ errorret_t sceneSetImmediate(const char_t *script) {
|
|||||||
|
|
||||||
// If there's currently a scene active, dispose of it first.
|
// If there's currently a scene active, dispose of it first.
|
||||||
if(SCENE.scriptActive) {
|
if(SCENE.scriptActive) {
|
||||||
assertNotNull(SCENE.script.luaState, "Script context null?");
|
// Unload script context. I do this with Lua code but I can probably make
|
||||||
|
// it optimized in future and call from C.
|
||||||
// Call sceneDispose function
|
errorChain(scriptContextExec(
|
||||||
lua_getglobal(SCENE.script.luaState, "sceneDispose");
|
&SCRIPT_MANAGER.mainContext,
|
||||||
if(lua_isfunction(SCENE.script.luaState, -1)) {
|
"if Scene.current ~= nil and Scene.current.dispose ~= nil then\n"
|
||||||
if(lua_pcall(SCENE.script.luaState, 0, 0, 0) != LUA_OK) {
|
" Scene.current:dispose()\n"
|
||||||
errorThrow(
|
"end\n"
|
||||||
"Failed to call sceneDispose function in script: %s\n",
|
"Scene.current = nil\n"
|
||||||
lua_tostring(SCENE.script.luaState, -1)
|
));
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scenes should ideally clean themselves up, this is just a warning because
|
|
||||||
// you can technically move entities between scenes, but it's messy.
|
|
||||||
for(entityid_t i = 0; i < ENTITY_COUNT_MAX; i++) {
|
|
||||||
if(ENTITY_MANAGER.entities[i].state & ENTITY_STATE_ACTIVE) {
|
|
||||||
consolePrint(
|
|
||||||
"Entity %d is active after scene dispose\n", i
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unload script context.
|
|
||||||
scriptContextDispose(&SCENE.script);
|
|
||||||
SCENE.scriptActive = false;
|
SCENE.scriptActive = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,20 +217,14 @@ errorret_t sceneSetImmediate(const char_t *script) {
|
|||||||
|
|
||||||
// If we have a new scene, execute it.
|
// If we have a new scene, execute it.
|
||||||
if(script != NULL) {
|
if(script != NULL) {
|
||||||
errorChain(scriptContextInit(&SCENE.script));
|
|
||||||
SCENE.scriptActive = true;
|
SCENE.scriptActive = true;
|
||||||
errorChain(scriptContextExecFile(&SCENE.script, script));
|
errorChain(scriptContextExecFile(&SCRIPT_MANAGER.mainContext, script));
|
||||||
|
|
||||||
// Call the init function.
|
// Call the init function.
|
||||||
lua_getglobal(SCENE.script.luaState, "sceneInit");
|
errorChain(scriptContextExec(
|
||||||
if(lua_isfunction(SCENE.script.luaState, -1)) {
|
&SCRIPT_MANAGER.mainContext,
|
||||||
if(lua_pcall(SCENE.script.luaState, 0, 0, 0) != LUA_OK) {
|
"Scene.current = Scene.new()\n"
|
||||||
errorThrow(
|
));
|
||||||
"Failed to call sceneInit function in script: %s\n",
|
|
||||||
lua_tostring(SCENE.script.luaState, -1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
@@ -247,4 +243,16 @@ void sceneSet(const char_t *script) {
|
|||||||
errorret_t sceneDispose(void) {
|
errorret_t sceneDispose(void) {
|
||||||
errorChain(sceneSetImmediate(NULL));
|
errorChain(sceneSetImmediate(NULL));
|
||||||
errorOk();
|
errorOk();
|
||||||
|
}
|
||||||
|
|
||||||
|
int sceneSetLua(lua_State *L) {
|
||||||
|
assertNotNull(L, "Lua state cannot be NULL");
|
||||||
|
|
||||||
|
if(!lua_isstring(L, 1)) {
|
||||||
|
luaL_error(L, "Scene.set requires a string argument");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
sceneSet(lua_tostring(L, 1));
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -13,13 +13,9 @@
|
|||||||
#define SCENE_EVENT_UPDATE_MAX 16
|
#define SCENE_EVENT_UPDATE_MAX 16
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
scriptcontext_t script;
|
|
||||||
bool_t scriptActive;
|
bool_t scriptActive;
|
||||||
char_t sceneCurrent[ASSET_FILE_PATH_MAX];
|
char_t sceneCurrent[ASSET_FILE_PATH_MAX];
|
||||||
char_t sceneNext[ASSET_FILE_PATH_MAX];
|
char_t sceneNext[ASSET_FILE_PATH_MAX];
|
||||||
|
|
||||||
eventlistener_t updateListeners[SCENE_EVENT_UPDATE_MAX];
|
|
||||||
event_t eventUpdate;
|
|
||||||
} scene_t;
|
} scene_t;
|
||||||
|
|
||||||
extern scene_t SCENE;
|
extern scene_t SCENE;
|
||||||
@@ -64,4 +60,11 @@ void sceneSet(const char_t *script);
|
|||||||
*
|
*
|
||||||
* @return The error return value.
|
* @return The error return value.
|
||||||
*/
|
*/
|
||||||
errorret_t sceneDispose(void);
|
errorret_t sceneDispose(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lua method for setting the scene.
|
||||||
|
* @param L The Lua state.
|
||||||
|
* @return The number of return values on the Lua stack.
|
||||||
|
*/
|
||||||
|
int sceneSetLua(lua_State *L);
|
||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "script/module/entity/moduleentity.h"
|
#include "script/module/entity/moduleentity.h"
|
||||||
#include "script/module/input/moduleinput.h"
|
#include "script/module/input/moduleinput.h"
|
||||||
#include "script/module/moduleplatform.h"
|
#include "script/module/moduleplatform.h"
|
||||||
#include "script/module/scene/modulescene.h"
|
|
||||||
#include "script/module/locale/modulelocale.h"
|
#include "script/module/locale/modulelocale.h"
|
||||||
#include "script/module/time/moduletime.h"
|
#include "script/module/time/moduletime.h"
|
||||||
#include "script/module/event/moduleevent.h"
|
#include "script/module/event/moduleevent.h"
|
||||||
@@ -31,7 +30,6 @@ void moduleRegister(lua_State *L) {
|
|||||||
moduleEntity(L);
|
moduleEntity(L);
|
||||||
moduleInput(L);
|
moduleInput(L);
|
||||||
modulePlatform(L);
|
modulePlatform(L);
|
||||||
moduleScene(L);
|
|
||||||
moduleLocale(L);
|
moduleLocale(L);
|
||||||
moduleTime(L);
|
moduleTime(L);
|
||||||
moduleEvent(L);
|
moduleEvent(L);
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
/**
|
|
||||||
* Copyright (c) 2025 Dominic Masters
|
|
||||||
*
|
|
||||||
* This software is released under the MIT License.
|
|
||||||
* https://opensource.org/licenses/MIT
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include "script/module/modulebase.h"
|
|
||||||
#include "scene/scene.h"
|
|
||||||
|
|
||||||
static int moduleSceneSet(lua_State *L) {
|
|
||||||
assertNotNull(L, "Lua state cannot be NULL");
|
|
||||||
|
|
||||||
if(!lua_isstring(L, 1)) {
|
|
||||||
luaL_error(L, "sceneSet requires a string argument");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
sceneSet(lua_tostring(L, 1));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void moduleScene(lua_State *L) {
|
|
||||||
assertNotNull(L, "Lua state cannot be NULL");
|
|
||||||
|
|
||||||
lua_register(L, "sceneSet", moduleSceneSet);
|
|
||||||
|
|
||||||
lua_pushlightuserdata(L, &SCENE.eventUpdate);
|
|
||||||
lua_setglobal(L, "SCENE_EVENT_UPDATE");
|
|
||||||
}
|
|
||||||
@@ -16,10 +16,6 @@ errorret_t scriptManagerInit() {
|
|||||||
memoryZero(&SCRIPT_MANAGER, sizeof(scriptmanager_t));
|
memoryZero(&SCRIPT_MANAGER, sizeof(scriptmanager_t));
|
||||||
|
|
||||||
errorChain(scriptContextInit(&SCRIPT_MANAGER.mainContext));
|
errorChain(scriptContextInit(&SCRIPT_MANAGER.mainContext));
|
||||||
errorChain(scriptContextExec(
|
|
||||||
&SCRIPT_MANAGER.mainContext,
|
|
||||||
"print('Main Script Context Ready!')"
|
|
||||||
));
|
|
||||||
|
|
||||||
errorOk();
|
errorOk();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "script/scriptcontext.h"
|
#include "script/scriptcontext.h"
|
||||||
|
|
||||||
void modulePlatformDolphin(scriptcontext_t *ctx) {
|
static void modulePlatformDolphin(lua_State *L) {
|
||||||
scriptContextExec(ctx, "DOLPHIN = true\n");
|
luaL_dostring(L, "DOLPHIN = true\n");
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "script/scriptcontext.h"
|
#include "script/scriptcontext.h"
|
||||||
|
|
||||||
void modulePlatformPSP(scriptcontext_t *ctx) {
|
void modulePlatformPSP(lua_State *L) {
|
||||||
scriptContextExec(ctx, "PSP = true\n");
|
luaL_dostring(L, "PSP = true\n");
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user