Working on the terminal...

This commit is contained in:
2021-12-05 10:16:37 +01:00
parent 9e547e7292
commit fe9220bb39
6 changed files with 99 additions and 28 deletions

47
lua.cpp
View File

@@ -214,20 +214,25 @@ extern "C" {
}
}
#define STATE_STOPPED 0
#define STATE_PAUSED 1
#define STATE_PLAYING 2
lua_State *L;
bool is_playing = false;
uint8_t lua_state = STATE_STOPPED;
bool init_exists = false;
bool update_exists = false;
bool lua_is_playing() {
return is_playing;
return lua_state == STATE_PLAYING;
}
void lua_init() {
void lua_init(const bool start_playing) {
if (lua_state != STATE_STOPPED) lua_quit();
L = luaL_newstate();
if (luaL_loadfile(L, "game.lua")) {
debug("error loading game");
debug("ERROR LOADING GAME");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
@@ -400,7 +405,7 @@ void lua_init() {
lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE");
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
@@ -415,18 +420,28 @@ void lua_init() {
if (lua_isfunction(L,-1)) update_exists = true;
lua_pop(L,1);
is_playing = true;
lua_state = start_playing ? STATE_PLAYING : STATE_PAUSED;
}
void lua_call_cmd(const char* str) {
if (luaL_dostring(L, str)) {
debug(" ");
debug("ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
}
debug(" ");
}
void lua_call_init() {
if (!init_exists) return;
lua_getglobal(L, "init");
if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
is_playing = false;
lua_state = STATE_STOPPED;
}
}
@@ -434,16 +449,24 @@ void lua_call_update() {
if (!update_exists) return;
lua_getglobal(L, "update");
if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
is_playing = false;
lua_state = STATE_STOPPED;
}
}
void lua_quit() {
if (!is_playing) return;
is_playing = false;
if (lua_state == STATE_STOPPED) return;
lua_state = STATE_STOPPED;
lua_close(L);
}
void lua_pause() {
lua_state = STATE_PAUSED;
}
void lua_resume() {
lua_state = STATE_PLAYING;
}