更新启动代码

This commit is contained in:
Kirigaya Kazuto 2019-08-10 21:37:33 +08:00
parent f1ef4bfc9d
commit 080c8fef6b
2 changed files with 114 additions and 14 deletions

View File

@ -25,11 +25,14 @@ for i, info in ipairs(all_music) do
local filename = string.format("tmp\\tmp_%d.wav", i)
ConvertMusic(string.format("asserts\\mp3\\%s",info.name), filename, function()
print(filename, "convert finished.")
wnd:setTimeout(function()
print("Start loading: ", filename)
local c = Chunk(filename)
table.insert(music_table, c)
end, 1000)
print("Start loading: ", filename)
table.insert(music_table, Chunk(filename))
if #music_table == #all_music then
print("All music loaded.")
rnd:clear()
rnd:copyTo(font:renderText(rnd, "All music loaded.", RGBA(0, 0, 255, 255)), Point(0, 0))
rnd:update()
end
end)
end
@ -38,8 +41,6 @@ setmetatable(texture_track, {
__mode = "k"
})
wnd:show()
wnd:on('mousedown', function(x, y)
print("Clicked", x, y)
local t = font:renderText(rnd, string.format("%.0f,%.0f", x, y), {r=255,g=255,b=255,a=0,type="color"})
@ -54,9 +55,11 @@ end)
wnd:on('quit', function()
print("before quit")
return false
end)
wnd:on('keydown', function(key)
print("keydown", key)
if key == string.byte('q') then
local current = collectgarbage("count")
collectgarbage("collect")
@ -93,10 +96,4 @@ wnd:on('keydown', function(key)
rnd:update()
end)
xpcall(function()
wnd:start()
end, function(err)
print("LuaMain Exception: ", err)
print(debug.traceback())
end)
wnd:show()

103
code/init.lua Normal file
View File

@ -0,0 +1,103 @@
-- Tweaks
local plainRenderer = Renderer
Renderer = function(...)
local r = plainRenderer(...)
local mt = getmetatable(r)
local oldFunc = mt.__index.loadTexture
local textureWatcher = setmetatable({}, {__mode="k"})
local lastCallTime = os.time()
mt.__index.loadTexture = function(...)
print("loadTexture invoked.")
local text = oldFunc(...)
textureWatcher[text] = true
if os.time() - lastCallTime > 3 then
local cnt = 0
for t, b in pairs(textureWatcher) do
cnt = cnt + 1
end
print("Total weak texture: ", cnt)
if cnt > 100 then
collectgarbage()
end
lastCallTime = os.time()
end
return text
end
Renderer = plainRenderer
return r
end
-- Promise, Async/Await Helpers
function Promise(callback)
local p = {
__waiting = {},
__status = 0
}
p.resolve = function(...)
p.__ret = table.pack(...)
p.__status = 1
while #p.__waiting > 0 do
local co = p.__waiting[1]
table.remove(p.__waiting, 1)
coroutine.resume(co)
end
end
p.reject = function(err)
p.__ret = err
p.__status = 2
while #p.__waiting > 0 do
local co = p.__waiting[1]
table.remove(p.__waiting, 1)
coroutine.resume(co)
end
end
callback(p.resolve, p.reject)
return p
end
function await(p)
if p.__status ~= 0 then
if p.__status == 1 then
return table.unpack(p.__ret)
else
error(p.__ret)
end
else
local current = coroutine.running()
table.insert(p.__waiting, current)
coroutine.yield(p)
end
end
function async(fn)
return function()
local co = coroutine.create(fn)
local ret = table.pack(coroutine.resume(fn))
if coroutine.status(co) == "dead" then
local success = table.remove(ret, 1)
ret.n = ret.n - 1
local p = {}
if success then
p.__status = 1 -- resolved
p.__ret = ret
else
p.__status = 2 -- rejected
p.__ret = ret[1]
end
return p
else
return ret[2]
end
end
end
-- Load & run game
local gameMain = loadfile("code/game.lua")
xpcall(gameMain, function(err)
print("LuaMain Exception: ", err)
print(debug.traceback())
end)