定时 支持函数回调 和AddTimer行为一致

This commit is contained in:
baidwwy 2021-04-14 07:51:08 +08:00
parent 10a186f7f4
commit e7b3315559
1 changed files with 23 additions and 5 deletions

View File

@ -1,7 +1,7 @@
--[[
@Author : GGELUA
@Date : 2020-11-27 10:05:41
@LastEditTime : 2021-04-14 07:08:21
@LastEditTime : 2021-04-14 07:50:03
--]]
require("GGE")
@ -101,6 +101,7 @@ function SDL窗口:SDL窗口(t)
self._reg = setmetatable({}, {__mode='v'})--注册消息
self._tick = {}
self._timer = {}--定时器
end
local function _Sendreg(self,k,...)
@ -171,7 +172,20 @@ function SDL窗口:_Event(t,...)
end
end
end
if next(self._timer) then
local oc = SDL.GetTicks()
for i,t in ipairs(self._timer) do
if oc>=t.time then
t.ms = ggexpcall(t.fun,t.ms)
if t.ms==0 or type(t.ms)~='number' then
table.remove(self._timer,i)
break
else
t.time = t.ms+oc
end
end
end
end
self.dt = ...
_Sendmsg(self,"更新事件",...)
_Sendmsg(self,"渲染事件",...)
@ -218,15 +232,19 @@ function SDL窗口:注册事件(t)
return true
end
end
function SDL窗口:定时(ms)
--SDL.AddTimer
function SDL窗口:定时(ms,fun)
if type(fun)=='function' then
table.insert(self._timer, {ms=ms,time=SDL.GetTicks()+ms,fun=fun})
return
end
local co,main = coroutine.running()
if not main then
self._tick[co] = SDL.GetTicks()+ms
coroutine.yield()
self._tick[co] = nil
return true
end
return true
end
function SDL窗口:关闭()