summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2018-08-22 14:08:20 +0800
committerchai <chaifix@163.com>2018-08-22 14:08:20 +0800
commit5cafb38a6fde6b786ba9b45b8faba02bc8f887cf (patch)
treeaeb453792cc0361bbe9504c7a54de0f770790214
parentac0de268b46b136a6cdd6bbf096d93c1ff126329 (diff)
*update
-rw-r--r--gameloop/init.lua42
-rw-r--r--log/init.lua (renamed from loghelper/init.lua)8
-rw-r--r--main.lua27
3 files changed, 65 insertions, 12 deletions
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/loghelper/init.lua b/log/init.lua
index b227795..bfa2e4e 100644
--- a/loghelper/init.lua
+++ b/log/init.lua
@@ -3,6 +3,11 @@
local log = {}
io.stdout:setvbuf("no")
+local _format = "%c"
+log.dateFormat = function(fmt)
+ _format = fmt
+end
+
log.LEVEL = {
INFO = 4,
DEBUG = 3,
@@ -26,7 +31,8 @@ end
log.log = function(level, msg)
if level <= log.level then
- print(logTag[level] .. ":" .. msg)
+ local time = os.date(_format, os.time())
+ print(time .. logTag[level] .. ":" .. msg)
end
end
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")