diff --git a/lua.cpp b/lua.cpp index eee3519..6e79b3a 100644 --- a/lua.cpp +++ b/lua.cpp @@ -513,6 +513,67 @@ extern "C" { lua_pushinteger(L, strlen(str)); return 1; } + + static int cpp_fopen(lua_State *L) { + const char* str = luaL_checkstring(L, 1); + uint8_t mode = luaL_optinteger(L, 2, 0); + fopen(str, mode); + return 0; + } + static int cpp_fclose(lua_State *L) { + fclose(); + return 0; + } + + static int cpp_fwritei(lua_State *L) { + int value = luaL_checkinteger(L, 1); + fwritei(value); + return 0; + } + + static int cpp_fwrited(lua_State *L) { + float value = luaL_checknumber(L, 1); + fwrited(value); + return 0; + } + + static int cpp_fwrites(lua_State *L) { + const char* str = luaL_checkstring(L, 1); + fwrites(str); + return 0; + } + + static int cpp_fwriteb(lua_State *L) { + bool value = lua_toboolean(L, 1); + fwriteb(value); + return 0; + } + + static int cpp_fwriteln(lua_State *L) { + void fwriteln(); + return 0; + } + + static int cpp_freadi(lua_State *L) { + lua_pushinteger(L,freadi()); + return 1; + } + + static int cpp_freadd(lua_State *L) { + lua_pushnumber(L,freadd()); + return 1; + } + + static int cpp_freads(lua_State *L) { + lua_pushstring(L,freads()); + return 1; + } + + static int cpp_freadb(lua_State *L) { + lua_pushboolean(L,freadb()); + return 1; + } + } lua_State *L; @@ -602,6 +663,18 @@ void lua_init() { lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii"); lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen"); + lua_pushcfunction(L,cpp_fopen); lua_setglobal(L, "fopen"); + lua_pushcfunction(L,cpp_fclose); lua_setglobal(L, "fclose"); + lua_pushcfunction(L,cpp_fwritei); lua_setglobal(L, "fwritei"); + lua_pushcfunction(L,cpp_fwrited); lua_setglobal(L, "fwrited"); + lua_pushcfunction(L,cpp_fwrites); lua_setglobal(L, "fwrites"); + lua_pushcfunction(L,cpp_fwriteb); lua_setglobal(L, "fwriteb"); + lua_pushcfunction(L,cpp_fwriteln); lua_setglobal(L, "fwriteln"); + lua_pushcfunction(L,cpp_freadi); lua_setglobal(L, "freadi"); + lua_pushcfunction(L,cpp_freadd); lua_setglobal(L, "freadd"); + lua_pushcfunction(L,cpp_freads); lua_setglobal(L, "freads"); + lua_pushcfunction(L,cpp_freadb); lua_setglobal(L, "freadb"); + lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); lua_pushinteger(L, 5); lua_setglobal(L, "KEY_B"); @@ -710,6 +783,9 @@ void lua_init() { lua_pushinteger(L, 230); lua_setglobal(L, "KEY_RALT"); lua_pushinteger(L, 231); lua_setglobal(L, "KEY_RGUI"); + lua_pushinteger(L, 0); lua_setglobal(L, "FILE_READ"); + lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE"); + if (lua_pcall(L,0, LUA_MULTRET, 0)) { debug("runtime error"); debug(lua_tostring(L, -1)); diff --git a/mini.cpp b/mini.cpp index d4be1ce..782b82e 100644 --- a/mini.cpp +++ b/mini.cpp @@ -13,15 +13,19 @@ struct surface_t { #define swap(a, b) {auto tmp=a;a=b;b=tmp;} -char window_title[256]; -uint16_t screen_width = 160; -uint16_t screen_height = 120; -uint8_t screen_zoom = 4; -surface_t surfaces[10]; -surface_t *screen_surface = &surfaces[0]; -surface_t *dest_surface = screen_surface; -surface_t *source_surface = NULL; -surface_t *map_surface = NULL; +char window_title[256]; +uint16_t screen_width = 160; +uint16_t screen_height = 120; +uint8_t screen_zoom = 4; + +surface_t surfaces[10]; +surface_t *screen_surface = &surfaces[0]; +surface_t *dest_surface = screen_surface; +surface_t *source_surface = NULL; +surface_t *map_surface = NULL; + +FILE *file = NULL; +bool file_ignore_comma=true; #define DEST(x, y) dest_surface->p[x+y*dest_surface->w] #define SOURCE(x, y) source_surface->p[x+y*source_surface->w] @@ -89,6 +93,7 @@ struct bmp_header_t { uint32_t num_important_colors; // ignore }; + char* get_value_from_line(char* line) { char* equal_character = strchr(line, '='); if (equal_character == NULL) return NULL; @@ -127,6 +132,8 @@ void reinit() { if (surfaces[i].p != NULL) free(surfaces[i].p); surfaces[i].p = NULL; } + if (file!=NULL) fclose(file); + file = NULL; } uint8_t newsurf(int w, int h) { @@ -902,3 +909,63 @@ void pdebug() { uint8_t ascii(const char *str, uint8_t index) { return str[index]; } + + +char fstr[255]; + +void fopen(const char *filename, uint8_t mode) { + if (file != NULL) fclose(file); + file = fopen(filename, mode==0?"r":"w"); +} + +void fclose() { + fclose(file); +} + +void fwritei(int value) { + sprintf(fstr, "%i ", value); + fwrite(fstr, strlen(fstr), 1, file); +} + +void fwrited(float value) { + char str[255]; + sprintf(fstr, "%f ", value); + fwrite(fstr, strlen(fstr), 1, file); +} +void fwrites(const char *value) { + char str[255]; + sprintf(fstr, "\"%s\" ", value); + fwrite(fstr, strlen(fstr), 1, file); +} + +void fwriteb(bool value) { + char str[255]; + sprintf(fstr, value?"true ":"false ", value); + fwrite(fstr, strlen(fstr), 1, file); +} + +void fwriteln() { + fwrite("\n", 1, 1, file); +} + +int freadi() { + int value; + fscanf(file, "%i", &value); + return value; +} + +float freadd() { + float value; + fscanf(file, "%f", &value); + return value; +} +const char *freads() { + fscanf(file, " \"%[^\"]\"", &fstr); + //fscanf(file, "\"%[^\"]\"", &fstr); + return fstr; +} + +bool freadb() { + fscanf(file, "%s", &fstr); + return strcmp(fstr, "true")==0?true:false; +} diff --git a/mini.h b/mini.h index 3abda33..b43c8dc 100644 --- a/mini.h +++ b/mini.h @@ -228,4 +228,18 @@ const char* tostr(int val); void debug(const char *str); void pdebug(); -uint8_t ascii(const char *str, uint8_t index); \ No newline at end of file +uint8_t ascii(const char *str, uint8_t index); + +void fopen(const char *filename, uint8_t mode=0); +void fclose(); + +void fwritei(int value); +void fwrited(float value); +void fwrites(const char *value); +void fwriteb(bool value); +void fwriteln(); + +int freadi(); +float freadd(); +const char *freads(); +bool freadb();