Since newCanvas is quite slow, I'm trying to implement a cache where I take and put back canvases.
In order to be reused, the canvas needs to be cleared to transparent.
However Canvas:clear doesn't seems to work: canvas are not cleared.
canvasCache = {}
canvasCache['32x32'] = {}
-- Put some canvases in the cache
for i = 1, 200 do
table.insert(canvasCache['32x32'], love.graphics.newCanvas(32, 32))
end
local newCanvas = love.graphics.newCanvas
function love.graphics.newCanvas(width, height)
if canvasCache[width .. 'x' .. height] and #canvasCache[width .. 'x' .. height] > 0 then
local canvas = table.remove(canvasCache[width .. 'x' .. height])
canvas:clear()
return canvas
end
return newCanvas(width, height)
end
function love.graphics.cacheCanvas(canvas)
if not canvasCache[canvas:getWidth() .. 'x' .. canvas:getHeight()] then
canvasCache[canvas:getWidth() .. 'x' .. canvas:getHeight()] = {}
end
print('Put one to cache')
table.insert(canvasCache[canvas:getWidth() .. 'x' .. canvas:getHeight()], canvas)
end
A question, why exactly do you need to do this? Use 200 canvases?
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Well I use the Light & Shadow engine and it needs a canvas for each body. I considered modifying it to not do that. But that would require a significant amount of rewriting.
I looked at it, and i don't see where on the user end would it require anyone to create that many canvases... for whatever reason.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.