Some slightly noisier tiny debugging.

Fully optional and/or only on rarely-called functions
This commit is contained in:
Sage Vaillancourt 2025-03-06 20:30:03 -05:00
parent cedf7cb945
commit a278e30c74
5 changed files with 37 additions and 9 deletions

6
lib/tiny-debug.lua Normal file
View File

@ -0,0 +1,6 @@
getCurrentTimeMilliseconds = playdate.getCurrentTimeMilliseconds
tinyTrackEntityAges = true
ENTITY_INIT_MS = { "ENTITY_INIT_MS" }
tinyLogSystemUpdateTime = false

View File

@ -484,9 +484,20 @@ function tiny.addEntity(world, entity)
end end
tiny_addEntity = tiny.addEntity tiny_addEntity = tiny.addEntity
if tinyTrackEntityAges then
local wrapped = tiny.addEntity
function tiny.addEntity(world, entity)
local added = wrapped(world, entity)
added[ENTITY_INIT_MS] = getCurrentTimeMilliseconds()
end
tiny_addEntity = tiny.addEntity
end
--- Adds a System to the world. Returns the System. --- Adds a System to the world. Returns the System.
function tiny.addSystem(world, system) function tiny.addSystem(world, system)
assert(system.world == nil, "System already belongs to a World.") if system.world ~= nil then
error("System " .. system.name .. " already belongs to a World.")
end
local s2a = world.systemsToAdd local s2a = world.systemsToAdd
s2a[#s2a + 1] = system s2a[#s2a + 1] = system
system.world = world system.world = world
@ -521,7 +532,9 @@ tiny_removeEntity = tiny.removeEntity
--- Removes a System from the world. Returns the System. --- Removes a System from the world. Returns the System.
function tiny.removeSystem(world, system) function tiny.removeSystem(world, system)
assert(system.world == world, "System does not belong to this World.") if system.world ~= world then
error("System " .. system.name .. " does not belong to this World.")
end
local s2r = world.systemsToRemove local s2r = world.systemsToRemove
s2r[#s2r + 1] = system s2r[#s2r + 1] = system
return system return system
@ -778,15 +791,15 @@ function tiny.update(world, dt, filter)
end end
end end
local tinyLogSystemUpdateTime = tinyLogSystemUpdateTime
-- Iterate through Systems IN ORDER -- Iterate through Systems IN ORDER
for i = 1, #systems do for i = 1, #systems do
local system = systems[i] local system = systems[i]
if system.active and ((not filter) or filter(world, system)) then if system.active and ((not filter) or filter(world, system)) then
-- TODO: Track how long each system runs for during any given frame.
-- Update Systems that have an update method (most Systems) -- Update Systems that have an update method (most Systems)
local update = system.update local update = system.update
if update then if update then
--local currentMs = playdate.getCurrentTimeMilliseconds() local currentMs = tinyLogSystemUpdateTime and getCurrentTimeMilliseconds()
local interval = system.interval local interval = system.interval
if interval then if interval then
local bufferedTime = (system.bufferedTime or 0) + dt local bufferedTime = (system.bufferedTime or 0) + dt
@ -798,14 +811,18 @@ function tiny.update(world, dt, filter)
else else
update(system, dt) update(system, dt)
end end
--local endTimeMs = playdate.getCurrentTimeMilliseconds() if tinyLogSystemUpdateTime then
--print(tostring(endTimeMs - currentMs) .. "ms taken to update " .. system.name) local endTimeMs = getCurrentTimeMilliseconds()
print(tostring(endTimeMs - currentMs) .. "ms taken to update system '" .. system.name .. "'")
end
end end
system.modified = false system.modified = false
end end
end end
--print("") if tinyLogSystemUpdateTime then
print("")
end
-- Iterate through Systems IN ORDER AGAIN -- Iterate through Systems IN ORDER AGAIN
for i = 1, #systems do for i = 1, #systems do

View File

@ -7,6 +7,7 @@ import("CoreLibs/timer.lua")
import("CoreLibs/ui.lua") import("CoreLibs/ui.lua")
import("CoreLibs/utilities/where.lua") import("CoreLibs/utilities/where.lua")
import("../lib/tiny-debug.lua")
import("../lib/tiny.lua") import("../lib/tiny.lua")
local tiny <const> = tiny local tiny <const> = tiny

View File

@ -1,7 +1,5 @@
local gfx <const> = playdate.graphics local gfx <const> = playdate.graphics
local focusPriority = {}
Camera = { Camera = {
pan = { pan = {
x = 0, x = 0,
@ -11,6 +9,8 @@ Camera = {
expireBelowScreenSystem = filteredSystem("expireBelowScreen", { position = T.XyPair, expireBelowScreenBy = T.number }) expireBelowScreenSystem = filteredSystem("expireBelowScreen", { position = T.XyPair, expireBelowScreenBy = T.number })
local focusPriority = {}
cameraPanSystem = filteredSystem("cameraPan", { focusPriority = T.number, position = T.XyPair }, function(e, dt) cameraPanSystem = filteredSystem("cameraPan", { focusPriority = T.number, position = T.XyPair }, function(e, dt)
if e.focusPriority >= focusPriority.priority then if e.focusPriority >= focusPriority.priority then
focusPriority.position = e.position focusPriority.position = e.position

View File

@ -3,6 +3,10 @@
---@param process fun(entity: T, dt: number, system: System) ---@param process fun(entity: T, dt: number, system: System)
---@return System | { entities: T[] } ---@return System | { entities: T[] }
function filteredSystem(name, shape, process) function filteredSystem(name, shape, process)
assert(type(name) == "string")
assert(type(shape) == "table")
assert(process == nil or type(process) == "function")
local system = tiny.processingSystem() local system = tiny.processingSystem()
system.name = name system.name = name
local keys = {} local keys = {}