Lua_Play now works in coroutines

This commit is contained in:
Krzosa Karol
2024-08-14 15:08:27 +02:00
parent 8cd754c923
commit a84a7073dc
4 changed files with 55 additions and 39 deletions

View File

@@ -331,6 +331,11 @@ function AddCo(f)
return Coroutines[i] return Coroutines[i]
end end
function AddTest(f)
local ff = AddCo(f)
coroutine.resume(ff)
end
function OnUpdate() function OnUpdate()
local new_co_list = {} local new_co_list = {}
for key, co in pairs(Coroutines) do for key, co in pairs(Coroutines) do
@@ -343,6 +348,7 @@ function OnUpdate()
Coroutines = new_co_list Coroutines = new_co_list
end end
)=="; )==";
void GenerateConfig() { void GenerateConfig() {

View File

@@ -220,6 +220,11 @@ function AddCo(f)
return Coroutines[i] return Coroutines[i]
end end
function AddTest(f)
local ff = AddCo(f)
coroutine.resume(ff)
end
function OnUpdate() function OnUpdate()
local new_co_list = {} local new_co_list = {}
for key, co in pairs(Coroutines) do for key, co in pairs(Coroutines) do
@@ -233,6 +238,7 @@ function OnUpdate()
end end
)=="; )==";
void ReloadStyle() { void ReloadStyle() {
ColorText = GetColor("Text", ColorText); ColorText = GetColor("Text", ColorText);

View File

@@ -160,19 +160,19 @@ int Lua_FileExists(lua_State *L) {
} }
int Lua_GetWorkingDir(lua_State *L) { int Lua_GetWorkingDir(lua_State *L) {
lua_pushlstring(LuaState, WorkingDir.data, WorkingDir.len); lua_pushlstring(L, WorkingDir.data, WorkingDir.len);
return 1; return 1;
} }
int Lua_GetActiveMainWindowBufferName(lua_State *L) { int Lua_GetActiveMainWindowBufferName(lua_State *L) {
String name = GetActiveMainWindowBufferName(); String name = GetActiveMainWindowBufferName();
lua_pushlstring(LuaState, name.data, name.len); lua_pushlstring(L, name.data, name.len);
return 1; return 1;
} }
int Lua_GetActiveMainWindowBufferDir(lua_State *L) { int Lua_GetActiveMainWindowBufferDir(lua_State *L) {
String name = GetActiveMainWindowBufferDir(); String name = GetActiveMainWindowBufferDir();
lua_pushlstring(LuaState, name.data, name.len); lua_pushlstring(L, name.data, name.len);
return 1; return 1;
} }
@@ -287,57 +287,62 @@ Color GetColor(String name, Color default_color) {
return result; return result;
} }
Int GetInt(const char *name) { Int GetInt(lua_State *L, const char *name) {
lua_getfield(LuaState, -1, name); lua_getfield(L, -1, name);
lua_Integer num = lua_tointeger(LuaState, -1); lua_Integer num = lua_tointeger(L, -1);
lua_pop(LuaState, 1); lua_pop(L, 1);
return (Int)num; return (Int)num;
} }
const char *GetString(const char *name) { const char *GetString(lua_State *L, const char *name) {
lua_getfield(LuaState, -1, name); lua_getfield(L, -1, name);
const char *result = lua_tostring(LuaState, -1); const char *result = lua_tostring(L, -1);
lua_pop(LuaState, 1); lua_pop(L, 1);
return result; return result;
} }
Vec2 GetVec2(const char *name) { Vec2 GetVec2(lua_State *L, const char *name) {
Vec2 result = {}; Vec2 result = {};
lua_getfield(LuaState, -1, name); lua_getfield(L, -1, name);
defer { lua_pop(LuaState, 1); }; defer { lua_pop(L, 1); };
lua_rawgeti(LuaState, -1, 1); {
result.x = (float)lua_tonumber(LuaState, -1); lua_getfield(L, -1, "1");
lua_pop(LuaState, 1); defer { lua_pop(L, 1); };
result.x = (float)lua_tonumber(L, -1);
}
lua_rawgeti(LuaState, -1, 2); {
result.x = (float)lua_tonumber(LuaState, -1); lua_getfield(L, -1, "2");
lua_pop(LuaState, 1); defer { lua_pop(L, 1); };
result.y = (float)lua_tonumber(L, -1);
}
lua_pop(LuaState, 3);
return result; return result;
} }
// :Event // :Event
int Lua_Play(lua_State *L) { int Lua_Play(lua_State *L) {
if (!lua_istable(L, -1)) luaL_error(LuaState, "expected a table of events"); if (!lua_istable(L, -1)) luaL_error(L, "expected a table of events");
defer { lua_pop(L, 1); };
size_t size = lua_rawlen(L, -1); int size = (int)lua_rawlen(L, -1);
for (size_t i = 0; i < size; i += 1) { for (int i = 0; i < size; i += 1) {
lua_rawgeti(L, -1, i + 1); lua_geti(L, -1, i + 1);
if (!lua_istable(L, -1)) luaL_error(L, "expected a table of events");
defer { lua_pop(L, 1); }; defer { lua_pop(L, 1); };
Event event = {}; Event event = {};
event.kind = (EventKind)GetInt("kind"); event.kind = (EventKind)GetInt(L, "kind");
event.key = (SDL_Keycode)GetInt("key"); event.key = (SDL_Keycode)GetInt(L, "key");
event.xmouse = (int16_t)GetInt("xmouse"); event.xmouse = (int16_t)GetInt(L, "xmouse");
event.ymouse = (int16_t)GetInt("ymouse"); event.ymouse = (int16_t)GetInt(L, "ymouse");
event.xwindow = (int16_t)GetInt("xwindow"); event.xwindow = (int16_t)GetInt(L, "xwindow");
event.ywindow = (int16_t)GetInt("ywindow"); event.ywindow = (int16_t)GetInt(L, "ywindow");
event.clicks = (uint8_t)GetInt("clicks"); event.clicks = (uint8_t)GetInt(L, "clicks");
event.flags = (uint8_t)GetInt("flags"); event.flags = (uint8_t)GetInt(L, "flags");
event.text = (char *)GetString("text"); event.text = (char *)GetString(L, "text");
event.wheel = GetVec2("wheel"); event.wheel = GetVec2(L, "wheel");
Add(&EventPlayback, event); Add(&EventPlayback, event);
} }
@@ -384,10 +389,9 @@ void UpdateLua() {
} }
} }
lua_getglobal(LuaState, "OnUpdate"); if (luaL_dostring(LuaState, "OnUpdate()") != 0) {
if (lua_pcall(LuaState, 0, 0, 0) != 0) {
const char *error_message = lua_tostring(LuaState, -1); const char *error_message = lua_tostring(LuaState, -1);
ReportWarningf("Failed the call to OnUpdate! %s", error_message); ReportWarningf("OnUpdate failed! %s", error_message);
lua_pop(LuaState, 1); lua_pop(LuaState, 1);
return; return;
} }

View File

@@ -30,7 +30,7 @@ void Serialize(Serializer *s, String name, Vec2 *datum) {
void Serialize(Serializer *s, String name, char **text) { void Serialize(Serializer *s, String name, char **text) {
String str = *text; String str = *text;
IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"(%.*s)\", ", FmtString(name), FmtString(str)); IKnowWhatImDoing_Appendf(s->buffer, "%.*s = \"%.*s\", ", FmtString(name), FmtString(str));
} }
void SerializeBegin(Serializer *s) { void SerializeBegin(Serializer *s) {