LuaYard/code/game.lua
Kiritow bc189b4951 完整重构
分离各SDL类的包装代码.
事件分发处理全部转移至Lua Init层. C层只提供获取事件的方法.
这样有助于提高性能,以及今后Coroutine scheduler的添加.
2019-08-25 00:09:28 +08:00

110 lines
3.0 KiB
Lua

local Window = require("window")
local Renderer = require("renderer")
local Font = require("font")
local MusicPlayer = require("musicplayer")
local Chunk = require("chunk")
local Music = require("music")
local fs = require('fs')
local utils = require("utils")
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)
utils.ConvertMusic(string.format("asserts\\mp3\\%s",info.name), filename, function()
print(filename, "convert finished.")
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
local texture_track = {}
setmetatable(texture_track, {
__mode = "k"
})
wnd:on('mousedown', function(e)
print("Clicked", e.x, e.y)
local t = font:renderText(rnd, string.format("%.0f,%.0f", e.x, e.y), RGBA(255, 255, 255, 0))
texture_track[t] = true
rnd:copyTo(t, Point(e.x, e.y))
rnd:update()
end)
wnd:on('mousemove', function(e)
-- print("Mousemove", e.x, e.y, e.xrel, e.yrel)
end)
wnd:on('quit', function()
print("before quit")
end)
wnd:on('keydown', function(e)
local key = e.key
print("keydown", 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, Point(0, 0))
rnd:update()
end)
wnd:show()