summaryrefslogtreecommitdiff
path: root/src/01-coroutine
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-09-17 08:07:08 +0800
committerchai <chaifix@163.com>2019-09-17 08:07:08 +0800
commitea55f90b49fbfb740a9aec66b4c40060542dc444 (patch)
tree6d98b3f198ed63947de9b3d42b54e93abfc43c61 /src/01-coroutine
+init
Diffstat (limited to 'src/01-coroutine')
-rw-r--r--src/01-coroutine/main.cpp70
-rw-r--r--src/01-coroutine/test.lua112
2 files changed, 182 insertions, 0 deletions
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 <windows.h>
+#include <time.h>
+#include <conio.h>
+
+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
diff --git a/src/01-coroutine/test.lua b/src/01-coroutine/test.lua
new file mode 100644
index 0000000..8735082
--- /dev/null
+++ b/src/01-coroutine/test.lua
@@ -0,0 +1,112 @@
+
+local watchdog = {
+ m_ThreadsWaitOnTime = {}, -- [coroutine] = second
+ m_ThreadsWaitOnSignal = {}, -- [signal] = {coroutines}
+ m_Time = 0,
+
+ Update = function(dog, dt)
+ dog.m_Time = dog.m_Time + dt
+
+ local threadsToWake = {}
+
+ for co, wakeupTime in pairs(dog.m_ThreadsWaitOnTime) do
+ if wakeupTime < dog.m_Time then
+ dog.m_ThreadsWaitOnTime[co] = nil
+ table.insert(threadsToWake, co)
+ end
+ end
+
+ for _, co in pairs(threadsToWake) do
+ coroutine.resume(co)
+ end
+ end ,
+ GetTime = function(dog)
+ return dog.m_Time
+ end ,
+ AddWaitOnTime = function(dog, co, sec)
+ dog.m_ThreadsWaitOnTime[co] = sec
+ end ,
+ AddWaitOnSignal = function(dog, co, cmd)
+ dog.m_ThreadsWaitOnSignal[cmd] = dog.m_ThreadsWaitOnSignal[cmd] or {}
+ table.insert(dog.m_ThreadsWaitOnSignal[cmd], co)
+ end ,
+ Signal = function(dog, command)
+ print("Signal " .. command)
+ local thread = dog.m_ThreadsWaitOnSignal[command]
+ if thread == nil then
+ print("Invalid signal")
+ return nil
+ end
+ dog.m_ThreadsWaitOnSignal[command] = nil
+ for _, co in pairs(thread) do
+ coroutine.resume(co)
+ end
+ end
+}
+
+local function wait(param)
+ local co = coroutine.running()
+ assert(co ~= nil, "the main coroutine cant wait")
+ if type(param) == "number" then
+ local wakeupTime = watchdog:GetTime() + param
+ print("Wait " .. param .." Sec, until " .. string.format("%.2f", wakeupTime).. " Sec")
+ watchdog:AddWaitOnTime(co, wakeupTime)
+ return coroutine.yield()
+ elseif type(param) == "string" then
+ print("Wait Signal " .. param)
+ watchdog:AddWaitOnSignal(co, param)
+ return coroutine.yield()
+ end
+end
+
+local function speaker()
+ print("Speaker: Start")
+ wait(2) --等2秒
+ print("Speaker: I'm speaker.")
+ wait(2) --等2秒
+ print("Speaker: And I like game and programming.")
+ wait(2) --等2秒
+ print("Speaker: Nice to meet you!")
+ wait("kick") --等kick信号
+ print("Speaker: You hurt me!")
+ wait("sorry") --等sorry信号
+ print("Speaker: That's ok.")
+end
+
+local signal = function(cmd)
+ watchdog:Signal(cmd)
+end
+
+local command = ""
+local GetInput = function()
+ if Kbhit() then
+ local c = GetChar()
+ if c == '\r' then
+ signal(command)
+ command = ""
+ else
+ print("Input: " .. c)
+ command = command .. c
+ end
+ end
+end
+
+local function main()
+ local pre = 0
+ local dt = 0
+ local co = coroutine.create(speaker)
+ coroutine.resume(co)
+ while true do
+ local cur = GetTime()
+ dt = cur - pre
+ pre = cur
+
+ GetInput()
+
+ watchdog:Update(dt)
+
+ Sleep(0.016)
+ end
+end
+
+xpcall(main, debug.traceback, 10)