LuaYard/code/game.lua
2019-08-10 15:29:42 +08:00

103 lines
2.7 KiB
Lua

local wnd = Window("Hello", 1024, 768)
local rnd = Renderer(wnd)
local font = Font("asserts/msyh.ttf", 18)
local musicPlayer = MusicPlayer()
require("code/helper")
local all_bqb = fs.listdir("asserts/bqb_all")
for i, info in ipairs(all_bqb) do
-- print (i, info.name, info.type, info.size)
i = i
end
local all_music = fs.listdir("asserts/mp3")
table.remove(all_music, 1)
table.remove(all_music, 1)
local music_table = {}
local last_channel = 0
local next_channel = 1
local next_music = 1
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)
end)
end
local texture_track = {}
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"})
texture_track[t] = true
rnd:copyTo(t, {x=x,y=y,type="point"})
rnd:update()
end)
wnd:on('mousemove', function(x, y, xrel, yrel)
-- print("Mousemove", x, y, xrel, yrel)
end)
wnd:on('quit', function()
print("before quit")
end)
wnd:on('keydown', function(key)
if key == string.byte('q') then
local current = collectgarbage("count")
collectgarbage("collect")
print("Before GC: ", current)
print("After GC: ", collectgarbage("count"))
return
elseif key == string.byte("w") then
local cnt = 0
for k in pairs(texture_track) do
cnt = cnt + 1
end
print ("Tracked texture: ", cnt)
return
elseif key == string.byte('p') then
musicPlayer:fadeOutChannel(last_channel, 5000)
if not music_table[next_music] then
next_music = 1
end
print("Playing", next_music, next_channel)
musicPlayer:fadeInChannel(music_table[next_music], next_channel, 1, 3000)
next_music = next_music + 1
last_channel = next_channel
next_channel = next_channel + 1
if next_channel >= 16 then
next_channel = 0
end
end
print("Keydown", key)
rnd:clear()
local t = rnd:loadTexture("asserts/bqb_all/" .. all_bqb[math.random(#all_bqb)].name)
texture_track[t] = true
rnd:copyTo(t, {x=0,y=0,type="point"})
rnd:update()
end)
xpcall(function()
wnd:start()
end, function(err)
print("LuaMain Exception: ", err)
print(debug.traceback())
end)