[NEW] Multiple lua files
This commit is contained in:
1
game.ini
1
game.ini
@@ -2,3 +2,4 @@ title=HOLA MINI
|
|||||||
width=160
|
width=160
|
||||||
height=120
|
height=120
|
||||||
zoom=3
|
zoom=3
|
||||||
|
files=game.lua
|
||||||
|
|||||||
46
lua.cpp
46
lua.cpp
@@ -585,16 +585,7 @@ bool lua_is_playing() {
|
|||||||
return is_playing;
|
return is_playing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lua_init() {
|
void push_lua_funcs() {
|
||||||
L = luaL_newstate();
|
|
||||||
|
|
||||||
if (luaL_loadfile(L, "game.lua")) {
|
|
||||||
debug("error loading game");
|
|
||||||
debug(lua_tostring(L, -1));
|
|
||||||
lua_pop(L,1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
lua_pushcfunction(L,cpp_newsurf); lua_setglobal(L, "newsurf");
|
lua_pushcfunction(L,cpp_newsurf); lua_setglobal(L, "newsurf");
|
||||||
lua_pushcfunction(L,cpp_loadsurf); lua_setglobal(L, "loadsurf");
|
lua_pushcfunction(L,cpp_loadsurf); lua_setglobal(L, "loadsurf");
|
||||||
lua_pushcfunction(L,cpp_freesurf); lua_setglobal(L, "freesurf");
|
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, 0); lua_setglobal(L, "FILE_READ");
|
||||||
lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE");
|
lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE");
|
||||||
|
}
|
||||||
|
|
||||||
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
|
void lua_init(char* filenames) {
|
||||||
debug("runtime error");
|
L = luaL_newstate();
|
||||||
debug(lua_tostring(L, -1));
|
|
||||||
lua_pop(L,1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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");
|
lua_getglobal(L, "_init");
|
||||||
if (lua_isfunction(L,-1)) init_exists = true;
|
if (lua_isfunction(L,-1)) init_exists = true;
|
||||||
lua_pop(L,1);
|
lua_pop(L,1);
|
||||||
|
|||||||
2
lua.h
2
lua.h
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
bool lua_is_playing();
|
bool lua_is_playing();
|
||||||
void lua_init();
|
void lua_init(char* filenames);
|
||||||
void lua_call_init();
|
void lua_call_init();
|
||||||
void lua_call_update();
|
void lua_call_update();
|
||||||
void lua_quit();
|
void lua_quit();
|
||||||
|
|||||||
12
mini.cpp
12
mini.cpp
@@ -27,6 +27,8 @@ surface_t *map_surface = NULL;
|
|||||||
FILE *file = NULL;
|
FILE *file = NULL;
|
||||||
bool file_ignore_comma=true;
|
bool file_ignore_comma=true;
|
||||||
|
|
||||||
|
char *lua_files=NULL;
|
||||||
|
|
||||||
#define DEST(x, y) dest_surface->p[x+y*dest_surface->w]
|
#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 SOURCE(x, y) source_surface->p[x+y*source_surface->w]
|
||||||
#define TILES(x, y) map_surface->p[x+y*map_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, "width") == 0) screen_width = atoi(value);
|
||||||
else if (strcmp(line, "height") == 0) screen_height = 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, "zoom") == 0) screen_zoom = atoi(value);
|
||||||
|
else if (strcmp(line, "files") == 0) {
|
||||||
|
lua_files = (char*)malloc(strlen(value));
|
||||||
|
strcpy(lua_files, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(f);
|
fclose(f);
|
||||||
@@ -267,7 +273,7 @@ int main(int argc,char*argv[]){
|
|||||||
|
|
||||||
reinit();
|
reinit();
|
||||||
debug("MINI SYSTEM BOOTING...");
|
debug("MINI SYSTEM BOOTING...");
|
||||||
lua_init();
|
lua_init(lua_files);
|
||||||
lua_call_init();
|
lua_call_init();
|
||||||
|
|
||||||
while(!exit) {
|
while(!exit) {
|
||||||
@@ -281,13 +287,13 @@ int main(int argc,char*argv[]){
|
|||||||
lua_quit();
|
lua_quit();
|
||||||
reinit();
|
reinit();
|
||||||
} else {
|
} else {
|
||||||
lua_init();
|
lua_init(lua_files);
|
||||||
lua_call_init();
|
lua_call_init();
|
||||||
}
|
}
|
||||||
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
|
||||||
lua_quit();
|
lua_quit();
|
||||||
reinit();
|
reinit();
|
||||||
lua_init();
|
lua_init(lua_files);
|
||||||
lua_call_init();
|
lua_call_init();
|
||||||
} else {
|
} else {
|
||||||
key_just_pressed = mini_eve.key.keysym.scancode;
|
key_just_pressed = mini_eve.key.keysym.scancode;
|
||||||
|
|||||||
Reference in New Issue
Block a user