diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/libjin/jin.h | 1 | ||||
-rw-r--r-- | src/lua/common/error.h | 2 | ||||
-rw-r--r-- | src/lua/libraries/luax/luax.h | 71 | ||||
-rw-r--r-- | src/lua/modules/_embed/boot.lua.h | 6 | ||||
-rw-r--r-- | src/lua/modules/event/event.cpp | 46 | ||||
-rw-r--r-- | src/lua/modules/filesystem/filesystem.cpp | 2 | ||||
-rw-r--r-- | src/lua/modules/graphics/graphics.cpp | 12 | ||||
-rw-r--r-- | src/lua/modules/jin.cpp | 28 | ||||
-rw-r--r-- | src/lua/modules/jin.h | 3 | ||||
-rw-r--r-- | src/main.cpp | 25 |
10 files changed, 112 insertions, 84 deletions
diff --git a/src/libjin/jin.h b/src/libjin/jin.h index 6651783..a4bb37f 100644 --- a/src/libjin/jin.h +++ b/src/libjin/jin.h @@ -9,6 +9,7 @@ #endif // LIBJIN_MODULES_AUDIO && LIBJIN_AUDIO_SDLAUDIO #include "Core/Core.h" #include "Filesystem/Filesystem.h" +#include "Filesystem/Buffer.h" #include "Input/Input.h" #include "Net/Net.h" #include "Graphics/Graphics.h" diff --git a/src/lua/common/error.h b/src/lua/common/error.h index f8b62f2..2b2c176 100644 --- a/src/lua/common/error.h +++ b/src/lua/common/error.h @@ -19,7 +19,7 @@ namespace lua vsnprintf(err + strlen(err), FORMAT_MSG_BUFFER_SIZE, fmt, args); va_end(args); luax_getglobal(L, MODULE_NAME); - luax_setfield_string(L, "error", err); + luax_setfieldstring(L, "error", err); } } diff --git a/src/lua/libraries/luax/luax.h b/src/lua/libraries/luax/luax.h index 9b283bd..b6b3e80 100644 --- a/src/lua/libraries/luax/luax.h +++ b/src/lua/libraries/luax/luax.h @@ -39,13 +39,18 @@ #define luax_dostring luaL_dostring #define luax_pcall lua_pcall #define luax_setglobal lua_setglobal +#define luax_setglobali(L, i, name)\ +lua_pushvalue(L, i);\ +lua_setglobal(L, name); #define luax_pop lua_pop #define luax_newtable lua_newtable #define luax_getglobal lua_getglobal + +#define luax_clear(L) lua_settop(L, 0) /** * */ -#define luax_setglobal_string(L, n, v) (lua_pushstring(L, v), lua_setglobal(L, n)) +#define luax_setglobalstring(L, n, v) (lua_pushstring(L, v), lua_setglobal(L, n)) /** * Get number of args @@ -125,37 +130,37 @@ inline bool luax_checkbool(lua_State *L, int numArg) #define luax_setfield_(T, L, k, v)\ do { lua_push##T(L, v); lua_setfield(L, -2, k); } while (0) -#define luax_setfield_number(L, k, v) luax_setfield_(number, L, k, v) -#define luax_setfield_string(L, k, v) luax_setfield_(string, L, k, v) -#define luax_setfield_bool(L, k, v) luax_setfield_(boolean, L, k, v) -#define luax_setfield_udata(L, k, v) luax_setfield_(lightuserdata, L, k, v) -#define luax_setfield_cfunc(L, k, v) luax_setfield_(cfunction, L, k, v) -#define luax_setfield_fstring(L, k, ...)\ +#define luax_setfieldnumber(L, k, v) luax_setfield_(number, L, k, v) +#define luax_setfieldstring(L, k, v) luax_setfield_(string, L, k, v) +#define luax_setfieldbool(L, k, v) luax_setfield_(boolean, L, k, v) +#define luax_setfieldudata(L, k, v) luax_setfield_(lightuserdata, L, k, v) +#define luax_setfieldcfunc(L, k, v) luax_setfield_(cfunction, L, k, v) +#define luax_setfieldfstring(L, k, ...)\ do { lua_pushfstring(L, __VA_ARGS__); lua_setfield(L, -2, k); } while (0) /** * If nosuch field push a nil at the top of stack. */ #define luax_getfield(L, I, N) lua_getfield(L, I, N) -inline float luax_getfield_number(lua_State* L, int I, const char* N) +inline float luax_getfieldnumber(lua_State* L, int I, const char* N) { luax_getfield(L, I, N); float n = luax_checknumber(L, -1); return n; } -inline int luax_getfield_integer(lua_State* L, int I, const char* N) +inline int luax_getfieldinteger(lua_State* L, int I, const char* N) { luax_getfield(L, I, N); int bin = luax_checkinteger(L, -1); return bin; } -inline const char* luax_getfield_string(lua_State* L, int I, const char* N) +inline const char* luax_getfieldstring(lua_State* L, int I, const char* N) { luax_getfield(L, I, N); const char* str = luax_checkstring(L, -1); return str; } -inline char luax_getfield_bool(lua_State* L, int I, const char* N) +inline char luax_getfieldbool(lua_State* L, int I, const char* N) { luax_getfield(L, I, N); char bin = lua_toboolean(L, -1); @@ -165,12 +170,12 @@ inline char luax_getfield_bool(lua_State* L, int I, const char* N) /** * Set raw */ -#define luax_setraw_(T, L, idx, i, v)\ +#define luax_setraw(T, L, idx, i, v)\ (lua_push##T(L, v), lua_rawseti(L, idx, i)) -#define luax_setraw_string(L, idx, i, v) luax_setraw_(string, L, idx, i, v) -#define luax_setraw_number(L, idx, i, v) luax_setraw_(number, L, idx, i, v) -#define luax_setraw_bool(L, idx, i, v) luax_setraw_(boolean, L, idx, i, v) +#define luax_setrawstring(L, idx, i, v) luax_setraw(string, L, idx, i, v) +#define luax_setrawnumber(L, idx, i, v) luax_setraw(number, L, idx, i, v) +#define luax_setrawbool(L, idx, i, v) luax_setraw(boolean, L, idx, i, v) /** * @@ -316,7 +321,7 @@ inline int luax_tableidxlen(lua_State* L, int i) } /** -* Get table hash size +* Get table hash size inline int luax_tbalehashlen(lua_State* L, int i) { @@ -324,20 +329,20 @@ inline int luax_tbalehashlen(lua_State* L, int i) */ /** -* Get table hash and index size +* Get table hash and index size inline int luax_tablelen(lua_State* L, int i) { } */ -/** -* Set value i in stack a global value called v, and -* don't pop it. -*/ -#define luax_justglobal(L, i, v) (lua_pushvalue(L, i), lua_setglobal(L, v)) +/* create a global tbale and stay it on the top of stack */ +#define luax_globaltable(L, name)\ +lua_newtable(L);\ +lua_pushvalue(L, 1);\ +lua_setglobal(L, name); -inline int luax_table_insert(lua_State * L, int tindex, int vindex, int pos) +inline int luax_tableinsert(lua_State * L, int tindex, int vindex, int pos) { if (tindex < 0) tindex = lua_gettop(L) + 1 + tindex; @@ -364,7 +369,7 @@ inline int luax_table_insert(lua_State * L, int tindex, int vindex, int pos) /** * Add the package loader to the package.loaders table. */ -inline int luax_register_searcher(lua_State * L, lua_CFunction f, int pos) +inline int luax_registersearcher(lua_State * L, lua_CFunction f, int pos) { lua_getglobal(L, "package"); @@ -377,11 +382,27 @@ inline int luax_register_searcher(lua_State * L, lua_CFunction f, int pos) return luaL_error(L, "Can't register searcher: package.loaders table does not exist."); lua_pushcfunction(L, f); - luax_table_insert(L, -2, -1, pos); + luax_tableinsert(L, -2, -1, pos); lua_pop(L, 3); return 0; } +typedef struct luax_Str +{ + const char* name; + const char* value; +} luax_Str; + +inline void luax_setfieldstrings(lua_State* L, const luax_Str* strs) +{ + for (int i = 0; strs[i].name != 0; ++i) + { + luax_setfieldstring(L, strs[i].name, strs[i].value); + } +} + +typedef luaL_Reg luax_Ref; + #endif // #if LUA_VERSION_NUM == 501 #endif // __LUAX_H
\ No newline at end of file diff --git a/src/lua/modules/_embed/boot.lua.h b/src/lua/modules/_embed/boot.lua.h index 8ff89ee..f7ffc43 100644 --- a/src/lua/modules/_embed/boot.lua.h +++ b/src/lua/modules/_embed/boot.lua.h @@ -1,8 +1,8 @@ /* boot.lua */ static const char* boot_lua = R"( -jin._argv[2] = jin._argv[2] or '.' +jin.args[2] = jin.args[2] or '.' jin.filesystem.init() -jin.filesystem.mount(jin._argv[2]) +jin.filesystem.mount(jin.args[2]) ------------------------------------------------------------------------- -- Config game @@ -15,7 +15,7 @@ end jin.config.width = jin.config.width or 576 jin.config.height = jin.config.height or 448 jin.config.vsync = jin.config.vsync or true -jin.config.title = jin.config.title or ("jin v" .. jin.version()) +jin.config.title = jin.config.title or ("jin v" .. jin.version) jin.config.resizable = jin.config.resizable or false jin.config.fullscreen = jin.config.fullscreen or false jin.config.fps = jin.config.fps or 60 diff --git a/src/lua/modules/event/event.cpp b/src/lua/modules/event/event.cpp index 08d52e0..0fb438f 100644 --- a/src/lua/modules/event/event.cpp +++ b/src/lua/modules/event/event.cpp @@ -31,53 +31,53 @@ namespace lua switch (e.type) { case EventType::QUIT: - luax_setfield_string(L, "type", "Quit"); + luax_setfieldstring(L, "type", "Quit"); break; case EventType::KEY_DOWN: - luax_setfield_string(L, "type", "KeyDown"); - luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); + luax_setfieldstring(L, "type", "KeyDown"); + luax_setfieldstring(L, "key", getKeyName(e.key.keysym.sym)); break; case EventType::KEY_UP: - luax_setfield_string(L, "type", "KeyUp"); - luax_setfield_string(L, "key", getKeyName(e.key.keysym.sym)); + luax_setfieldstring(L, "type", "KeyUp"); + luax_setfieldstring(L, "key", getKeyName(e.key.keysym.sym)); break; case EventType::MOUSE_MOTION: - luax_setfield_string(L, "type", "MouseMotion"); - luax_setfield_number(L, "x", e.motion.x); - luax_setfield_number(L, "y", e.motion.y); + luax_setfieldstring(L, "type", "MouseMotion"); + luax_setfieldnumber(L, "x", e.motion.x); + luax_setfieldnumber(L, "y", e.motion.y); break; case EventType::MOUSE_BUTTON_DOWN: - luax_setfield_string(L, "type", "MouseButtonDown"); - luax_setfield_string(L, "button", getButtonName(e.button.button)); - luax_setfield_number(L, "x", e.button.x); - luax_setfield_number(L, "y", e.button.y); + luax_setfieldstring(L, "type", "MouseButtonDown"); + luax_setfieldstring(L, "button", getButtonName(e.button.button)); + luax_setfieldnumber(L, "x", e.button.x); + luax_setfieldnumber(L, "y", e.button.y); break; case EventType::MOUSE_BUTTON_UP: - luax_setfield_string(L, "type", "MouseButtonUp"); - luax_setfield_string(L, "button", getButtonName(e.button.button)); - luax_setfield_number(L, "x", e.button.x); - luax_setfield_number(L, "y", e.button.y); + luax_setfieldstring(L, "type", "MouseButtonUp"); + luax_setfieldstring(L, "button", getButtonName(e.button.button)); + luax_setfieldnumber(L, "x", e.button.x); + luax_setfieldnumber(L, "y", e.button.y); break; case EventType::MOUSE_WHEEL: - luax_setfield_string(L, "type", "Wheel"); + luax_setfieldstring(L, "type", "Wheel"); if(e.wheel.x == -1) - luax_setfield_string(L, "x", "Left"); + luax_setfieldstring(L, "x", "Left"); else if(e.wheel.x == 1) - luax_setfield_string(L, "x", "Right"); + luax_setfieldstring(L, "x", "Right"); else - luax_setfield_string(L, "x", "None"); + luax_setfieldstring(L, "x", "None"); if (e.wheel.y == -1) - luax_setfield_string(L, "y", "Near"); + luax_setfieldstring(L, "y", "Near"); else if (e.wheel.y == 1) - luax_setfield_string(L, "y", "Far"); + luax_setfieldstring(L, "y", "Far"); else - luax_setfield_string(L, "y", "None"); + luax_setfieldstring(L, "y", "None"); break; default: diff --git a/src/lua/modules/filesystem/filesystem.cpp b/src/lua/modules/filesystem/filesystem.cpp index 1fc0d7c..c3dabad 100644 --- a/src/lua/modules/filesystem/filesystem.cpp +++ b/src/lua/modules/filesystem/filesystem.cpp @@ -132,7 +132,7 @@ namespace lua int luaopen_filesystem(lua_State* L) { luax_newlib(L, f); - luax_register_searcher(L, loader, 1); + luax_registersearcher(L, loader, 1); return 0; } diff --git a/src/lua/modules/graphics/graphics.cpp b/src/lua/modules/graphics/graphics.cpp index a8c54c2..d662cc7 100644 --- a/src/lua/modules/graphics/graphics.cpp +++ b/src/lua/modules/graphics/graphics.cpp @@ -25,12 +25,12 @@ namespace lua { Window* wnd = Window::get(); Window::Setting setting; - setting.width = luax_getfield_integer(L, 1, "width"); - setting.height = luax_getfield_integer(L, 1, "height"); - setting.title = luax_getfield_string(L, 1, "title"); - setting.vsync = luax_getfield_bool(L, 1, "vsync"); - setting.fullscreen = luax_getfield_bool(L, 1, "fullscreen"); - setting.resizable = luax_getfield_bool(L, 1, "resizable"); + setting.width = luax_getfieldinteger(L, 1, "width"); + setting.height = luax_getfieldinteger(L, 1, "height"); + setting.title = luax_getfieldstring(L, 1, "title"); + setting.vsync = luax_getfieldbool(L, 1, "vsync"); + setting.fullscreen = luax_getfieldbool(L, 1, "fullscreen"); + setting.resizable = luax_getfieldbool(L, 1, "resizable"); if (!wnd->init(&setting)) { luax_pushboolean(L, false); diff --git a/src/lua/modules/jin.cpp b/src/lua/modules/jin.cpp index a287218..de49846 100644 --- a/src/lua/modules/jin.cpp +++ b/src/lua/modules/jin.cpp @@ -51,16 +51,16 @@ namespace lua return 1; } - static const luaL_Reg f[] = { - { "version", l_getversion }, - { "revision", l_revision }, - { "author", l_getAuthor }, - { "os", l_getOS }, - { 0, 0 } + static const luax_Str s[] = { + { "version", VERSION }, + { "revision", REVISION_S }, + { "author", AUTHOR }, + { "codename", CODE_NAME }, + { 0, 0 } }; - - // submodules - static const luaL_Reg mods[] = { + + /* sub modules */ + static const luax_Ref mods[] = { { "core", luaopen_core }, { "event", luaopen_event }, { "graphics", luaopen_graphics }, @@ -77,15 +77,15 @@ namespace lua { 0, 0 } }; + /* register jin module, keep it on the top of stack */ int luaopen_jin(lua_State* L) { - // jin module is on top of the stack - luax_newlib(L, f); + luax_globaltable(L, MODULE_NAME); - // set to global field - luax_justglobal(L, -1, MODULE_NAME); + /* register strings */ + luax_setfieldstrings(L, s); - // register submodules + /* register submodules */ for (int i = 0; mods[i].name; ++i) { mods[i].func(L); diff --git a/src/lua/modules/jin.h b/src/lua/modules/jin.h index 2cc01f0..fe8d6dd 100644 --- a/src/lua/modules/jin.h +++ b/src/lua/modules/jin.h @@ -9,7 +9,8 @@ #define MODULE_NAME "jin" #define CODE_NAME "Side Part" #define VERSION "0.1.1" -#define REVISION 101 +#define REVISION_S "101" +#define REVISION 101 #define AUTHOR "chai" namespace jin diff --git a/src/main.cpp b/src/main.cpp index 5b6309a..18d873d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,30 +9,35 @@ #include <Windows.h> using namespace jin::lua; +using namespace jin::filesystem; int main(int argc, char* argv[]) { lua_State* L = luax_newstate(); + /* open lua standard module */ luax_openlibs(L); + /* open jin module */ luaopen_jin(L); - - // add args to global field + /* add args to field */ luax_newtable(L); for (int i = 0; i < argc; ++i) - luax_setraw_string(L, -2, i + 1, argv[i]); - luax_setfield(L, -2, "_argv"); - - const int BUFFER_SIZE = 512; - char buffer[BUFFER_SIZE]; + luax_setrawstring(L, -2, i + 1, argv[i]); + luax_setfield(L, -2, "args"); + /* push current working directory */ + /* absolute directory */ + Buffer cwd = Buffer(1024); #ifdef _WIN32 - _getcwd(buffer, BUFFER_SIZE); + _getcwd((char*)cwd.data, cwd.size); #elif defined __unix__ #elif defined __APPLE__ #endif - luax_setfield_string(L, "_dir", buffer); + luax_setfieldstring(L, "cwd", (char*)cwd.data); + luax_clear(L); + + /* boot jin and run it */ boot(L); - luax_close(L); + luax_close(L); return 0; }
\ No newline at end of file |