LuaYard/code/game.lua

103 lines
2.7 KiB
Lua
Raw Normal View History

local wnd = Window("Hello", 1024, 768)
local rnd = Renderer(wnd)
local font = Font("asserts/msyh.ttf", 18)
local musicPlayer = MusicPlayer()
2019-08-07 14:15:01 +08:00
require("code/helper")
2019-08-07 18:35:28 +08:00
local all_bqb = fs.listdir("asserts/bqb_all")
2019-08-07 14:15:01 +08:00
for i, info in ipairs(all_bqb) do
2019-08-10 15:29:42 +08:00
-- print (i, info.name, info.type, info.size)
i = i
2019-08-07 14:15:01 +08:00
end
2019-08-10 15:29:42 +08:00
local all_music = fs.listdir("asserts/mp3")
table.remove(all_music, 1)
table.remove(all_music, 1)
2019-08-07 03:35:00 +08:00
2019-08-10 15:29:42 +08:00
local music_table = {}
local last_channel = 0
local next_channel = 1
local next_music = 1
2019-08-10 15:29:42 +08:00
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()
2019-08-10 15:29:42 +08:00
print("Start loading: ", filename)
local c = Chunk(filename)
2019-08-10 15:29:42 +08:00
table.insert(music_table, c)
end, 1000)
end)
2019-08-10 15:29:42 +08:00
end
2019-08-10 15:29:42 +08:00
local texture_track = {}
setmetatable(texture_track, {
__mode = "k"
})
wnd:show()
2019-08-07 03:35:00 +08:00
wnd:on('mousedown', function(x, y)
2019-08-06 20:36:04 +08:00
print("Clicked", x, y)
2019-08-07 14:15:01 +08:00
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
2019-08-07 03:35:00 +08:00
rnd:copyTo(t, {x=x,y=y,type="point"})
rnd:update()
2019-08-06 20:36:04 +08:00
end)
wnd:on('mousemove', function(x, y, xrel, yrel)
-- print("Mousemove", x, y, xrel, yrel)
end)
2019-08-06 20:36:04 +08:00
wnd:on('quit', function()
print("before quit")
end)
2019-08-06 20:36:04 +08:00
wnd:on('keydown', function(key)
2019-08-07 14:15:01 +08:00
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
2019-08-10 15:29:42 +08:00
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
2019-08-07 14:15:01 +08:00
end
2019-08-06 20:36:04 +08:00
print("Keydown", key)
2019-08-07 03:35:00 +08:00
rnd:clear()
2019-08-07 14:15:01 +08:00
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"})
2019-08-07 03:35:00 +08:00
rnd:update()
2019-08-06 20:36:04 +08:00
end)
xpcall(function()
wnd:start()
2019-08-07 18:35:28 +08:00
end, function(err)
print("LuaMain Exception: ", err)
2019-08-06 20:36:04 +08:00
print(debug.traceback())
end)
2019-08-07 14:15:01 +08:00