[NEW] Multiple lua files

This commit is contained in:
2022-09-19 17:28:43 +02:00
parent 664710d59a
commit aad3ba639a
4 changed files with 41 additions and 20 deletions

View File

@@ -2,3 +2,4 @@ title=HOLA MINI
width=160
height=120
zoom=3
files=game.lua

34
lua.cpp
View File

@@ -585,16 +585,7 @@ bool lua_is_playing() {
return is_playing;
}
void lua_init() {
L = luaL_newstate();
if (luaL_loadfile(L, "game.lua")) {
debug("error loading game");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
void push_lua_funcs() {
lua_pushcfunction(L,cpp_newsurf); lua_setglobal(L, "newsurf");
lua_pushcfunction(L,cpp_loadsurf); lua_setglobal(L, "loadsurf");
lua_pushcfunction(L,cpp_freesurf); lua_setglobal(L, "freesurf");
@@ -785,14 +776,37 @@ void lua_init() {
lua_pushinteger(L, 0); lua_setglobal(L, "FILE_READ");
lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE");
}
void lua_init(char* filenames) {
L = luaL_newstate();
push_lua_funcs();
char *pointer = filenames;
do {
// Get next filename...
char *file_start=pointer;
while (*pointer!=',' && *pointer!=0) pointer++;
if (*pointer!=0) *(pointer++)=0;
// Load and execute file
if (luaL_loadfile(L, file_start)) { // "game.lua"
debug("error loading game");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug(lua_tostring(L, -1));
lua_pop(L,1);
return;
}
if (*pointer!=0) *(pointer-1)=',';
} while (*pointer!=0);
// Check if _init and _update exist
lua_getglobal(L, "_init");
if (lua_isfunction(L,-1)) init_exists = true;
lua_pop(L,1);

2
lua.h
View File

@@ -1,7 +1,7 @@
#pragma once
bool lua_is_playing();
void lua_init();
void lua_init(char* filenames);
void lua_call_init();
void lua_call_update();
void lua_quit();

View File

@@ -27,6 +27,8 @@ surface_t *map_surface = NULL;
FILE *file = NULL;
bool file_ignore_comma=true;
char *lua_files=NULL;
#define DEST(x, y) dest_surface->p[x+y*dest_surface->w]
#define SOURCE(x, y) source_surface->p[x+y*source_surface->w]
#define TILES(x, y) map_surface->p[x+y*map_surface->w]
@@ -113,6 +115,10 @@ void read_ini() {
else if (strcmp(line, "width") == 0) screen_width = atoi(value);
else if (strcmp(line, "height") == 0) screen_height = atoi(value);
else if (strcmp(line, "zoom") == 0) screen_zoom = atoi(value);
else if (strcmp(line, "files") == 0) {
lua_files = (char*)malloc(strlen(value));
strcpy(lua_files, value);
}
}
}
fclose(f);
@@ -267,7 +273,7 @@ int main(int argc,char*argv[]){
reinit();
debug("MINI SYSTEM BOOTING...");
lua_init();
lua_init(lua_files);
lua_call_init();
while(!exit) {
@@ -281,13 +287,13 @@ int main(int argc,char*argv[]){
lua_quit();
reinit();
} else {
lua_init();
lua_init(lua_files);
lua_call_init();
}
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
lua_quit();
reinit();
lua_init();
lua_init(lua_files);
lua_call_init();
} else {
key_just_pressed = mini_eve.key.keysym.scancode;