[0.9.2] Clearing canvases

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
giann
Prole
Posts: 42
Joined: Fri Jun 28, 2013 11:38 am

[0.9.2] Clearing canvases

Post by giann »

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.

NB: I'm still using love 0.9.2

Here is the code:

Code: Select all

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
Am I missing something ?
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [0.9.2] Clearing canvases

Post by zorg »

A question, why exactly do you need to do this? Use 200 canvases?
Me and my stuff :3True 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.
giann
Prole
Posts: 42
Joined: Fri Jun 28, 2013 11:38 am

Re: [0.9.2] Clearing canvases

Post by giann »

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.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: [0.9.2] Clearing canvases

Post by zorg »

And you're using the improved version of that, right? Not the first, but the second version?

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 :3True 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.
giann
Prole
Posts: 42
Joined: Fri Jun 28, 2013 11:38 am

Re: [0.9.2] Clearing canvases

Post by giann »

Yes second version and it needs a canvas for the body image and normal: https://github.com/tanema/light_world.l ... y.lua#L423
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 42 guests