Compare commits

...

2 commits

Author SHA1 Message Date
e77c75aaf8 add gc utils and bind them to f12 2025-11-11 05:20:00 -03:00
8d49bc70e5 add room destroy function 2025-11-11 05:11:10 -03:00
2 changed files with 71 additions and 2 deletions

View file

@ -53,7 +53,7 @@ function love.load()
up = {'key:up', 'key:w'}, up = {'key:up', 'key:w'},
down = {'key:down', 'key:s'}, down = {'key:down', 'key:s'},
action = {'key:z', 'key:space'}, action = {'key:z', 'key:space'},
f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'} f1 = {'key:f1'}, f2 = {'key:f2'}, f3 = {'key:f3'}, f12 = {'key:f12'}
}, },
pairs = { pairs = {
move = {'left', 'right', 'up', 'down'} move = {'left', 'right', 'up', 'down'}
@ -69,7 +69,16 @@ function love.update(dt)
input:update(dt) input:update(dt)
if input:pressed('f1') then gotoRoom('CircleRoom') end if input:pressed('f1') then gotoRoom('CircleRoom') end
if input:pressed('f3') then camera:shake(4, 60, 1) end if input:pressed('f3') then camera:shake(4, 60, 1) end
if input:pressed('f12') then
print("-------------------------------------")
print("Before collection: " .. collectgarbage("count")/1024)
collectgarbage()
print("After collection: " .. collectgarbage("count")/1024)
print("Object count: ")
local counts = type_count()
for k, v in pairs(counts) do print(k, v) end
print("-------------------------------------")
end
camera:update(dt) camera:update(dt)
end end
@ -121,6 +130,7 @@ function gotoRoom(room_type, ...)
if type(Class) ~= "table" and type(Class) ~= "function" then if type(Class) ~= "table" and type(Class) ~= "function" then
error("room '"..room_type.."' is not callable (got "..type(Class)..", does room return itself?)") error("room '"..room_type.."' is not callable (got "..type(Class)..", does room return itself?)")
end end
if current_room and current_room.destroy then current_room:destroy() end
current_room = _G[room_type](...) current_room = _G[room_type](...)
end end
@ -150,3 +160,56 @@ function random(min, max)
local min, max = min or 0, max or 1 local min, max = min or 0, max or 1
return (min > max and (love.math.random()*(min - max) + max)) or (love.math.random()*(max - min) + min) return (min > max and (love.math.random()*(min - max) + max)) or (love.math.random()*(max - min) + min)
end end
------------------------
-- garbage collection --
------------------------
---Counts all globals and applies function
---@param f function
function count_all(f)
local seen = {}
local count_table
count_table = function(t)
if seen[t] then return end
f(t)
seen[t] = true
for k,v in pairs(t) do
if type(v) == "table" then
count_table(v)
elseif type(v) == "userdata" then
f(v)
end
end
end
count_table(_G)
end
---Count all types of globals
---@return table
function type_count()
local counts = {}
local enumerate = function (o)
local t = type_name(o)
counts[t] = (counts[t] or 0) + 1
end
count_all(enumerate)
return counts
end
global_type_table = nil
---Resolves an object's true class name via metatable lookup.
---Caches results in `global_type_table` for speed.
---Fallbacks: "table" (no metatable), "Unknown" (uncached).
---@param o any Any object (table, userdata, etc.)
---@return string Class name (e.g., "Player", "Camera", "ImageData")
function type_name(o)
if global_type_table == nil then
global_type_table = {}
for k,v in pairs(_G) do
global_type_table[v] = k
end
global_type_table[0] = "table"
end
return global_type_table[getmetatable(o) or 0] or "Unknown"
end

View file

@ -46,4 +46,10 @@ function Room:canvasDraw()
self.area:draw() self.area:draw()
end end
---Destroys room
function Room:destroy()
self.area:destroy()
self.area = nil
end
return Room return Room