@fglette same here, can’t seem to access the page.
@ruilov: afaict there is little or no cost to setting the rect’s size to 0.
@frosty If you don’t need z-ordering on your sprites based on draw order, then a simple free list (actually a stack) will suffice for removed rects. If you need z-ordering based on draw order, life gets a little more…complicated (I did this the other day, it was a bit of a pain, but if you need to do it lemme know and I’ll show you how I did it). Here’s some simple code for implementing meshes with a free list. It keeps a hash table where each mesh is a key, and the value is another table that is the stack of free indices. The hash table has weak keys, so if no other references exist to the mesh it will be collected and won’t hang around forever.
meshFreeListHash = setmetatable({}, {__mode = "k"})
function meshNewRect(m, x, y, w, h, r)
local stack = meshFreeListHash[m]
if stack and #stack > 0 then
local index = table.remove(stack)
m:setRect(index, x, y, w, h, r or 0)
return index
end
return m:addRect(x, y, w, h, r or 0)
end
function meshFreeRect(m, index)
local stack = meshFreeListHash[m]
if not stack then
stack = {}
meshFreeListHash[m] = stack
end
table.insert(stack, index)
m:setRect(index, 0, 0, 0, 0, 0)
end
Just use meshNewRect(myMesh, x, y, w, h, [r]) instead of myMesh:addRect(x, y, w, h, [r]), and when you are done with a rect use meshFreeRect(myMesh, index), and everything should work as you’d expect.