From ea55f90b49fbfb740a9aec66b4c40060542dc444 Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 17 Sep 2019 08:07:08 +0800 Subject: +init --- src/01-coroutine/main.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/01-coroutine/main.cpp (limited to 'src/01-coroutine/main.cpp') diff --git a/src/01-coroutine/main.cpp b/src/01-coroutine/main.cpp new file mode 100644 index 0000000..7e711be --- /dev/null +++ b/src/01-coroutine/main.cpp @@ -0,0 +1,70 @@ +extern "C" { +#include "../lua51/lua.h" +#include "../lua51/lualib.h" +#include "../lua51/lauxlib.h" +} + +#include +#include +#include + +static int l_GetTime(lua_State* L) +{ + float second = (double)clock() / CLOCKS_PER_SEC; + lua_pushnumber(L, second); + return 1; +} + +static int l_Sleep(lua_State* L) +{ + float t = lua_tonumber(L, 1); + Sleep(t * 1000); + return 0; +} + +static int l_Kbhit(lua_State* L) +{ + int c = _kbhit(); + lua_pushboolean(L, c); + return 1; +} + +static int l_GetChar(lua_State* L) +{ + char str[2] = { 0,0 }; + str[0] = _getch(); + lua_pushstring(L, str); + return 1; +} + +luaL_reg fns[] = { + {"GetTime", l_GetTime } , + {"Sleep", l_Sleep } , + {"Kbhit", l_Kbhit }, + {"GetChar", l_GetChar }, + {0, 0} +}; + +void openlibs(lua_State* L) +{ + luaL_openlibs(L); + //luaL_register(L, NULL, fns); + luaL_reg* fn; + int i = 0; + for (fn = &fns[0]; fn->name != 0; fn = &fns[++i]) + { + lua_pushcfunction(L, fn->func); + lua_setglobal(L, fn->name); + } +} + +int main(int args, char* argv[]) +{ + lua_State* L = luaL_newstate(); + openlibs(L); + + luaL_dofile(L, "01-coroutine/test.lua"); + + lua_close(L); + return 0; +} \ No newline at end of file -- cgit v1.1-26-g67d0