Efficent way to Draw a screenshot as a Background

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
Jaston
Prole
Posts: 18
Joined: Sun Nov 25, 2018 5:43 pm

Efficent way to Draw a screenshot as a Background

Post by Jaston »

Hi Everyone,

I have game where I want a pause menu to display over my game. So I run render with a dt of 0 to draw my game paused. Then I run draw calls after to display my menu.

I use the push library to handle scaling and resizing of my game. Thus after rendering my game the scaling aspects are all gone :(. This results in my pause menu moving all over the screen when it is resized.

I was thinking of drawing a screenshot of the game to a file. Loading that file as a image. Then drawing that image to the screen before i used the oush library.

This approach is very laggy as it has to create a image. Save an image then load and draw an image.

How can I get around this issue? What is the best practice when trying to make a menu that overlaps your game?

Note I use the push library to process my shaders. So i have to run push:finish() before I draw my menu if I go with the render the game at 0 dt technique. Hence my image screen shot method.
MissDanish
Citizen
Posts: 65
Joined: Wed Mar 07, 2018 11:21 pm

Re: Efficent way to Draw a screenshot as a Background

Post by MissDanish »

The best way to do this is just to call the same draw functions that your other gamestate uses, that's what I do. There's no need to over complicate things

something like this:

Code: Select all

if gamestate == "playing" then
  level_draw()
elseif gamestate == "menu" then
  level_draw()
end
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Efficent way to Draw a screenshot as a Background

Post by MrFariator »

You don't have to save and load the screenshot from a file. Instead, you can simply just create a new image from the imageData object created by the screenshot function during runtime, and thus avoid having to do unnecessary IO operations. If you're on 11.x, you'll have to use a callback structure as outlined on the wiki, otherwise you can just use the imageData object returned by love.graphics.newScreenshot.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Efficent way to Draw a screenshot as a Background

Post by ivan »

Jaston wrote: Tue Nov 27, 2018 7:34 pm This approach is very laggy as it has to create a image. Save an image then load and draw an image.
Why not just draw the screen to canvas?
Jaston
Prole
Posts: 18
Joined: Sun Nov 25, 2018 5:43 pm

Re: Efficent way to Draw a screenshot as a Background

Post by Jaston »

Re the call back method, could you show me a code example of that new function and how to get it to return the image data object. Prior to my post I wasnt able to get it to work hence I relied on writing to the file name and reading the image back in.

I tried to draw it to a seperate canvas too before I posted. Problem there was I couldnt grab the right canvas as I appear to be drawing to the default canvas which is the screen?

I put a get canvas functiom to grab the canvas used between the push:start() and push:finish() functions. Then drew that canvas to a new canvas. Then drew that canvas behind the pause menu. For some reason it just makes a black screen. So either the push library clears the canvas after push:finish(shaders) tbat it uses or something else wonky is happening.

Re first solution. Cant do as I need the shaders to be applied in push:finish(shaders) function before I draw the pause menu on top.
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Efficent way to Draw a screenshot as a Background

Post by MrFariator »

Jaston wrote: Wed Nov 28, 2018 5:56 pm Re the call back method, could you show me a code example of that new function and how to get it to return the image data object. Prior to my post I wasnt able to get it to work hence I relied on writing to the file name and reading the image back in.

Code: Select all

-- define the function to be used as a callback
local myCallbackFunc = function ( imgData )
  local img = love.graphics.newImage(imgData)
  -- do whatever you want with the image
end
...
-- later use the callback function as a parameter
love.graphics.captureScreenshot( myCallbackFunc )
or even just

Code: Select all

love.graphics.captureScreenshot( 
  function ( imgData )
    local img = love.graphics.newImage(imgData)
    -- do whatever you want with the image
  end 
)
Of course, I'd prefer the former so you're not creating a new function every time you're creating a screenshot.
Jaston
Prole
Posts: 18
Joined: Sun Nov 25, 2018 5:43 pm

Re: Efficent way to Draw a screenshot as a Background

Post by Jaston »

Thank you for the example. I will give it a shot and report back :)
Jaston
Prole
Posts: 18
Joined: Sun Nov 25, 2018 5:43 pm

Re: Efficent way to Draw a screenshot as a Background

Post by Jaston »

It worked and is not laggy! For those doing this, make sure you cover the condition where you capture screenshot hasnt finished yet to avoid no object being found.

Also make sure to clear your image to nil before making a new screenshot.

Thank you everyone for your help :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 47 guests