Still working on the console...
This commit is contained in:
23
lua.cpp
23
lua.cpp
@@ -162,6 +162,11 @@ extern "C" {
|
||||
lua_pushinteger(L, ascii(str, index));
|
||||
return 1;
|
||||
}
|
||||
static int cpp_chr(lua_State *L) {
|
||||
int val = luaL_checkinteger(L, 1);
|
||||
lua_pushstring(L, chr(val));
|
||||
return 1;
|
||||
}
|
||||
static int cpp_strlen(lua_State *L) {
|
||||
const char* str = luaL_checkstring(L, 1);
|
||||
lua_pushinteger(L, strlen(str));
|
||||
@@ -212,6 +217,12 @@ extern "C" {
|
||||
setmode(val);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cpp_load(lua_State *L) {
|
||||
const char* str = luaL_checkstring(L, 1);
|
||||
load(str);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
#define STATE_STOPPED 0
|
||||
@@ -227,16 +238,18 @@ bool lua_is_playing() {
|
||||
return lua_state == STATE_PLAYING;
|
||||
}
|
||||
|
||||
void lua_init(const bool start_playing) {
|
||||
void lua_init(const char* filename, const bool start_playing) {
|
||||
if (lua_state != STATE_STOPPED) lua_quit();
|
||||
L = luaL_newstate();
|
||||
init_exists = update_exists = false;
|
||||
bool file_loaded = true;
|
||||
|
||||
if (luaL_loadfile(L, "game.lua")) {
|
||||
if (luaL_loadfile(L, filename)) {
|
||||
debug("ERROR LOADING GAME");
|
||||
debug(lua_tostring(L, -1));
|
||||
lua_pop(L,1);
|
||||
setmode(0);
|
||||
return;
|
||||
file_loaded = false;
|
||||
}
|
||||
|
||||
lua_pushcfunction(L,cpp_cls); lua_setglobal(L, "cls");
|
||||
@@ -270,6 +283,7 @@ void lua_init(const bool start_playing) {
|
||||
lua_pushcfunction(L,cpp_srand); lua_setglobal(L, "srand");
|
||||
lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr");
|
||||
lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii");
|
||||
lua_pushcfunction(L,cpp_chr); lua_setglobal(L, "chr");
|
||||
lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen");
|
||||
|
||||
lua_pushcfunction(L,cpp_setchar); lua_setglobal(L, "setchar");
|
||||
@@ -278,6 +292,7 @@ void lua_init(const bool start_playing) {
|
||||
lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound");
|
||||
lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound");
|
||||
lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode");
|
||||
lua_pushcfunction(L,cpp_load); lua_setglobal(L, "load");
|
||||
|
||||
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
|
||||
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A");
|
||||
@@ -404,6 +419,8 @@ void lua_init(const bool start_playing) {
|
||||
lua_pushinteger(L, 14); lua_setglobal(L, "COLOR_YELLOW");
|
||||
lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE");
|
||||
|
||||
if (!file_loaded) return;
|
||||
|
||||
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
|
||||
debug("RUNTIME ERROR");
|
||||
debug(lua_tostring(L, -1));
|
||||
|
||||
Reference in New Issue
Block a user