Screenshot just a specific area?

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
User avatar
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Screenshot just a specific area?

Post by clofresh »

I'm trying to develop a game where you can take photos of your environment. Is it possible to take a screenshot of just a specific area of the screen and work with it as an image object? Could I submit a patch to add optional screen area parameters to love.graphics.newScreenshot() or will it be too slow for use in the game?

Would I be better off drawing everything to a canvas first, draw the canvas to the screen, and when a picture is taken, save a scissored version of that canvas as the picture object?
----------------------------------------
Sluicer Games
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Screenshot just a specific area?

Post by slime »

You could use love.graphics.newScreenshot and [wiki]ImageData:paste[/wiki], although that might not be as ideal as a native solution in LÖVE.
Keep in mind that such a solution won't improve performance though, because the main cost of newScreenshot (and Canvas:getImageData) is the fact that they have to cause the GPU and CPU to synchronize (the GPU is normally one or more whole frames 'behind' the CPU.)
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Screenshot just a specific area?

Post by dusoft »

clofresh wrote:I'm trying to develop a game where you can take photos of your environment. Is it possible to take a screenshot of just a specific area of the screen and work with it as an image object? Could I submit a patch to add optional screen area parameters to love.graphics.newScreenshot() or will it be too slow for use in the game?

Would I be better off drawing everything to a canvas first, draw the canvas to the screen, and when a picture is taken, save a scissored version of that canvas as the picture object?
Does the love.graphics.setScissor work with screenshots? It says it restricts all drawing calls, so possibly also screenshot?
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Screenshot just a specific area?

Post by slime »

The scissor affects love.graphics.clear / Canvas:clear, but not love.graphics.newScreenshot / Canvas:getImageData.
User avatar
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Re: Screenshot just a specific area?

Post by clofresh »

Ah, didn't even need scissor. I just created 2 canvases, one the size of the screen and the other the size of the screenshot area you want, then draw a translated version of the screen canvas onto the screenshot canvas and it will do the clipping for you.

Code: Select all

function love.load()
    screen = love.graphics.newCanvas(love.graphics.getDimensions())
end

function love.draw()
    love.graphics.setCanvas(screen)
    drawStuff()
    love.graphics.setCanvas()
    love.graphics.draw(screen)

    if snap == nil then
        local snapCanvas = love.graphics.newCanvas(snapWidth, snapHeight)
        love.graphics.setCanvas(snapCanvas)
        love.graphics.translate(-snapX, -snapY)
        love.graphics.draw(screen)
        love.graphics.setCanvas()
        snap = love.graphics.newImage(snapCanvas:getImageData())
    else
        love.graphics.draw(snap)
    end

    screen:clear()
end
I'm assuming that it's cheaper to keep around image objects than canvas objects, though I'm not sure if that's true. Also if the conversion from canvas to image becomes slow, it might be worth doing it in a background thread.
----------------------------------------
Sluicer Games
User avatar
slime
Solid Snayke
Posts: 3132
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Screenshot just a specific area?

Post by slime »

clofresh wrote:I'm assuming that it's cheaper to keep around image objects than canvas objects, though I'm not sure if that's true.
Images and Canvases are both OpenGL textures under the hood. Keeping one OpenGL texture around is pretty much always cheaper than creating multiple.

Drawing the scene to a canvas and then keeping that canvas around and drawing it when you need is better than creating ImageData from the canvas (which stalls the GPU) and then creating an Image from that ImageData (an unnecessary operation because the Canvas already has the exact same data.)
clofresh wrote:Also if the conversion from canvas to image becomes slow, it might be worth doing it in a background thread.
Unfortunately you can't use love.graphics functions or objects in background threads.
User avatar
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Re: Screenshot just a specific area?

Post by clofresh »

Ah ok, good to know. Thanks slime!
----------------------------------------
Sluicer Games
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 240 guests