diff options
Diffstat (limited to 'sdl/luatools.c')
-rw-r--r-- | sdl/luatools.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/sdl/luatools.c b/sdl/luatools.c new file mode 100644 index 0000000..5116df1 --- /dev/null +++ b/sdl/luatools.c @@ -0,0 +1,163 @@ +/* luatools.c + * + * Lua Tools + * + * + */ + +#include <lauxlib.h> +#include <lualib.h> +#include <stdio.h> +#include <string.h> +#include "luatools.h" + +const char *luaGetFieldString(lua_State *L, const char *key) +{ + if (!L || !key) + return NULL; + + const char *ret = NULL; + + /* push the key string onto stack */ + lua_pushstring(L, key); + + lua_gettable(L, -2); + if (!lua_isstring(L, -1)) + { + fprintf(stderr, "Lua: \"%s\" field within the table is not a string\n", key); + lua_pop(L, 1); + + return ret; + } + + ret = lua_tostring(L, -1); + lua_pop(L, 1); + + return ret; +} + +double luaGetFieldNumber(lua_State *L, const char *key) +{ + if (!L || !key) + return -1.0; + + double ret = -1.0; + + /* push the key string onto stack */ + lua_pushstring(L, key); + + /* remember, -1 points to top of the stack, our table should a second + * item from the top, this call effectively accesses t[key] and + * pushes that value on the stack */ + lua_gettable(L, -2); + if (!lua_isnumber(L, -1)) + { + fprintf(stderr, "Lua: \"%s\" field within the table is NaN\n", key); + lua_pop(L, 1); + + return ret; + } + + /* finally retrieve our value */ + ret = lua_tonumber(L, -1); + + /* value is no longer needed, pop it */ + lua_pop(L, 1); + + return ret; +} + +int luaLoadConfig(lua_State *L, const char *fname) +{ + if (!L || !fname) + return -1; + + int status; + + /* load the file containing the config */ + /* call a function, what function? dry run the script? */ + status = (luaL_loadfile(L, fname) || lua_pcall(L, 0, 0, 0)); + if (status) + { + fprintf(stderr, "Lua: %s\n", lua_tostring(L, -1)); + return -1; + } + + return 0; +} + +int luaFillTablePlatform(lua_State *L, struct luat_table_platform *p) +{ + if (!L || !p) + return -1; + + double ret; + const char *ret_str = NULL; + + /* pushes the table on the stack */ + lua_getglobal(L, "platform"); + + if (!lua_istable(L, -1)) + { + fprintf(stderr, "Lua: failed to parse \"platform\" table\n"); + lua_pop(L, 1); + return -1; + } + + if ((ret_str = luaGetFieldString(L, "name")) != NULL) + p->name = ret_str; + + if ((ret_str = luaGetFieldString(L, "icon")) != NULL) + p->icon = ret_str; + + if ((ret = luaGetFieldNumber(L, "xres")) != -1.0) + p->xres = (unsigned int) ret; + + if ((ret = luaGetFieldNumber(L, "yres")) != -1.0) + p->yres = (unsigned int) ret; + + if ((ret = luaGetFieldNumber(L, "bpp")) != -1.0) + p->bpp = (unsigned int) ret; + + if ((ret = luaGetFieldNumber(L, "af")) != -1.0) + p->af = (unsigned int) ret; + + if ((ret = luaGetFieldNumber(L, "fovy")) != -1.0) + p->fovy = (float) ret; + + if ((ret = luaGetFieldNumber(L, "znear")) != -1.0) + p->znear = (float) ret; + + if ((ret = luaGetFieldNumber(L, "zfar")) != -1.0) + p->zfar = (float) ret; + + if ((ret = luaGetFieldNumber(L, "maxfps")) != -1.0) + p->maxfps = (unsigned int) ret; + + /* pop the table */ + lua_pop(L, 1); + + return 0; +} + +void luaPrintTablePlatform(struct luat_table_platform *p) +{ + if (!p) + return; + + printf("-- Lua platform config file --\n" + " name : %s\n" + " icon : %s\n" + " xres : %u\n" + " yres : %u\n" + " bpp : %u\n" + " af : %u\n" + " fovy : %f\n" + " znear : %f\n" + " zfar : %f\n" + " maxfps: %u\n", + p->name, p->icon, p->xres, p->yres, p->bpp, p->af, + p->fovy, p->znear, p->zfar, p->maxfps); + puts(""); +} + |