From 38a9808460890769976b56c26931bc60b5d796dc Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 7 Aug 2019 18:35:28 +0800 Subject: [PATCH] update test script --- code/game.lua | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/code/game.lua b/code/game.lua index cf915a1..6bd0141 100644 --- a/code/game.lua +++ b/code/game.lua @@ -4,6 +4,14 @@ font = Font("asserts/msyh.ttf", 18) musicPlayer = MusicPlayer() music = Music("asserts/mp3/bgm1.mp3") +function Point(x, y) + return {x=x,y=y,type="point"} +end + +function Rect(x,y,w,h) + return {x=x,y=y,w=w,h=h,type="rect"} +end + all_bqb = fs.listdir("asserts/bqb_all") @@ -16,6 +24,43 @@ setmetatable(texture_track, { __mode = "k" }) +texture_gc_limit_min = 20 +texture_gc_limit = 20 +texture_gc_activate = false + +-- Gloabl gc timer +local function global_texture_cleaner() + local cnt = 0 + for k in pairs(texture_track) do + cnt = cnt + 1 + end + print ("Tracked texture: ", cnt, texture_gc_limit) + if cnt > texture_gc_limit then + collectgarbage("collect") + if texture_gc_activate then + texture_gc_limit = texture_gc_limit * 2 + else + texture_gc_activate = true + end + else + if not texture_gc_activate then + texture_gc_limit = (texture_gc_limit - 1) > texture_gc_limit_min and (texture_gc_limit - 1) or texture_gc_limit_min + else + texture_gc_activate = false + end + end + wnd:setTimeout(global_texture_cleaner, 1000) +end +wnd:setTimeout(global_texture_cleaner, 1000) + +-- Global garbage maker +local function global_garbage_maker() + texture_track[rnd:loadTexture("asserts/bqb_all/" .. all_bqb[math.random(#all_bqb)].name)] = true + + wnd:setTimeout(global_garbage_maker, 1) +end +wnd:setTimeout(global_garbage_maker, 1) + wnd:setTimeout(function() print("Playing music") musicPlayer:play(music) @@ -27,7 +72,6 @@ wnd:on('click', function(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"}) - t:close() rnd:update() end) wnd:on('quit', function() @@ -54,13 +98,13 @@ wnd:on('keydown', function(key) 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"}) - t:close() rnd:update() end) xpcall(function() wnd:start() -end, function() +end, function(err) + print("LuaMain Exception: ", err) print(debug.traceback()) end)