From 5cafb38a6fde6b786ba9b45b8faba02bc8f887cf Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 22 Aug 2018 14:08:20 +0800 Subject: *update --- gameloop/init.lua | 42 +++++++++++++++++++++++++++++++++++++++++ log/init.lua | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ loghelper/init.lua | 49 ------------------------------------------------ main.lua | 27 ++++++++++++++++----------- 4 files changed, 113 insertions(+), 60 deletions(-) create mode 100644 gameloop/init.lua create mode 100644 log/init.lua delete mode 100644 loghelper/init.lua diff --git a/gameloop/init.lua b/gameloop/init.lua new file mode 100644 index 0000000..030a151 --- /dev/null +++ b/gameloop/init.lua @@ -0,0 +1,42 @@ +local function call(func, ...) + if func then + return func(...) + end +end + +function jin.core.run() + call(jin.core.onLoad) + local dt = 0 + local previous = jin.time.second() + local current = previous + -- TODO: 重写,事件处理事件应该高频率调用 + while jin.core.running() do + for _, e in pairs(jin.event.poll()) do + if e.type == "keydown" then + jin.keyboard.set(e.key, true) + elseif e.type == "keyup" then + jin.keyboard.set(e.key, false) + end + call(jin.core.onEvent, e) + end + + previous = current + current = jin.time.second() + dt = current - previous + + call(jin.core.onUpdate, dt) + + if jin.graphics then + jin.graphics.unbindCanvas() + jin.graphics.clear() + jin.graphics.setColor() + jin.graphics.setFont() + call(jin.core.onDraw) + jin.graphics.present() + end + + if jin.time then + jin.time.sleep(0.001) + end + end +end diff --git a/log/init.lua b/log/init.lua new file mode 100644 index 0000000..bfa2e4e --- /dev/null +++ b/log/init.lua @@ -0,0 +1,55 @@ +-- 不能使用 debug 命名模块,会冲突, +-- 要使用其余名字比如 loghelper +local log = {} +io.stdout:setvbuf("no") + +local _format = "%c" +log.dateFormat = function(fmt) + _format = fmt +end + +log.LEVEL = { + INFO = 4, + DEBUG = 3, + WARN = 2, + ERROR = 1, + NONE = 0 +} + +local logTag = { + [log.LEVEL.INFO] = "[Info]", + [log.LEVEL.DEBUG] = "[Debug]", + [log.LEVEL.WARN] = "[Warn]", + [log.LEVEL.ERROR] = "[Error]", +} + +log.level = log.LEVEL.INFO + +log.strict = function(level) + log.level = level +end + +log.log = function(level, msg) + if level <= log.level then + local time = os.date(_format, os.time()) + print(time .. logTag[level] .. ":" .. msg) + end +end + +log.info = function(msg) + log.log(log.LEVEL.INFO, msg) +end + +log.debug = function(msg) + log.log(log.LEVEL.DEBUG, msg) +end + +log.warn = function(msg) + log.log(log.LEVEL.WARN, msg) +end + +log.error = function(msg) + log.log(log.LEVEL.ERROR, msg) +end + +return log \ No newline at end of file diff --git a/loghelper/init.lua b/loghelper/init.lua deleted file mode 100644 index b227795..0000000 --- a/loghelper/init.lua +++ /dev/null @@ -1,49 +0,0 @@ --- 不能使用 debug 命名模块,会冲突, --- 要使用其余名字比如 loghelper -local log = {} -io.stdout:setvbuf("no") - -log.LEVEL = { - INFO = 4, - DEBUG = 3, - WARN = 2, - ERROR = 1, - NONE = 0 -} - -local logTag = { - [log.LEVEL.INFO] = "[Info]", - [log.LEVEL.DEBUG] = "[Debug]", - [log.LEVEL.WARN] = "[Warn]", - [log.LEVEL.ERROR] = "[Error]", -} - -log.level = log.LEVEL.INFO - -log.strict = function(level) - log.level = level -end - -log.log = function(level, msg) - if level <= log.level then - print(logTag[level] .. ":" .. msg) - end -end - -log.info = function(msg) - log.log(log.LEVEL.INFO, msg) -end - -log.debug = function(msg) - log.log(log.LEVEL.DEBUG, msg) -end - -log.warn = function(msg) - log.log(log.LEVEL.WARN, msg) -end - -log.error = function(msg) - log.log(log.LEVEL.ERROR, msg) -end - -return log \ No newline at end of file diff --git a/main.lua b/main.lua index f1cd109..5e1f26a 100644 --- a/main.lua +++ b/main.lua @@ -1,13 +1,15 @@ -_require = require -require = function(name) - return _require("jin-modules." .. name) -end +-- _require = require +-- require = function(name) +-- return _require("jin-modules." .. name) +-- end local jnet = require("jnet") -local log = require("loghelper") +local log = require("log") +log.dateFormat("") log.strict(log.LEVEL.INFO) local EventMsgCenter = require("EventMsgCenter.EventMsgCenter") local Events = require("EventMsgCenter.Events") local timer = require("timer.timer") +require("gameloop") _G["frame"] = 0 local thread = nil @@ -15,7 +17,7 @@ local socket = nil jin.core.onLoad = function() ---- 网络测试 jin.net.init() - socket = jin.net.Socket("TCP", 8807) + socket = jin.net.newSocket("TCP", 8807) local Skill = { id = jnet.DataType.INT, damage = jnet.DataType.FLOAT, @@ -28,7 +30,7 @@ jin.core.onLoad = function() -- } cd = jnet.DataType.BOOL } - local buf = jin.net.Buffer(0) + local buf = jin.net.newBuffer(0) local mySkill = { id = 12, name = "Hell fire!!!", @@ -41,11 +43,14 @@ jin.core.onLoad = function() -- } cd = true } + local len = buf:append("Skill") jnet.serialize(mySkill, buf) - local msg = jnet.deserialize(Skill, buf, 0) - print(msg.name) + local msgName = buf:grabString(0) + log.error(msgName) + local msg = jnet.deserialize(Skill, buf, len) + log.info(msg.id) - thread = jin.thread.Thread("Test", [[ + thread = jin.thread.newThread("Test", [[ local thread = jin.thread.getThread() local socket = thread:demand(1) while true do @@ -58,7 +63,7 @@ jin.core.onLoad = function() thread:start() thread:send(1, socket) EventMsgCenter.registerMsg(Events.Player_Move, function(msg) - print(msg) + -- print(msg) end) timer.every(1.0, function() log.info(_G["frame"] .. "fps") -- cgit v1.1-26-g67d0