Page 1 of 1

Drawing to a canvas with a closed window.

Posted: Fri Aug 25, 2017 6:57 pm
by HellzoneByron
I've been implementing a headless server for my game and I'm implementing a feature that effectively draws a simple approximation of the world and draws it to a canvas, sending it to the server admin over HTTP. However, I'm running into an issue: when running it in a completely headless operation by using love.window.close(), it disables the love.graphics() library with it. As a result, the server crashes when attempt to operate the canvas if love.window.close() is invoked. Is there a way to use graphics library functionality whilst the window is disabled?

Code: Select all

local canvas = love.graphics.newCanvas(800, 600)
canvas:renderTo(function()
    for i, v in pairs(objects) do
    	love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
    end
end)
local rawimg = canvas:newImageData()
local data = rawimg:encode("png")
rawimg:encode("png", "test.png")

Re: Drawing to a canvas with a closed window.

Posted: Fri Aug 25, 2017 8:06 pm
by MrFariator
This is essentially an OpenGL thing, where it requires a context where to draw the stuff at hand (check the footnotes on this wiki page). Now, there are ways to handle OpenGL without opening a window, but you probably would have to write changes to löve's source code for it to do this exact thing. Additionally, the approach you'd have to take is also going to depend on your operating system.

Re: Drawing to a canvas with a closed window.

Posted: Fri Aug 25, 2017 9:29 pm
by Sir_Silver
Perhaps you want to use https://love2d.org/wiki/love.window.setMode instead of https://love2d.org/wiki/love.window.close which mentions that graphics calls will crash it.

Re: Drawing to a canvas with a closed window.

Posted: Sat Aug 26, 2017 6:09 am
by alloyed
depending on how much you control the OS you can use something like xvfb to make a "virtual" window.

That said, with your example maybe software rendering could also work? Instead of using a canvas just edit your ImageData directly like so

Code: Select all

for iy = y, y + w - 1 do
  for ix = x, x + h - 1 do
    image:setPixel(ix, iy, r, g, b)
  end
end

Re: Drawing to a canvas with a closed window.

Posted: Fri Sep 01, 2017 6:43 pm
by HellzoneByron
Apologies for such a belated response. I've decided to go with the software rendering approach -- something I wasn't aware of beforehand. Granted it's not going to be as fast but speed is not a large concern in practice considering this image is only called upon once every 10 seconds. I am tempted to change it all over to some sort of client-side HTML5 canvas type thing down the road, but for now this will do. Thank you to everyone that responded! :-)