Drawing to a canvas with a closed window.

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
HellzoneByron
Prole
Posts: 5
Joined: Sat Jul 01, 2017 5:58 pm
Contact:

Drawing to a canvas with a closed window.

Post 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")

Code: Select all

room = {"Saskatoon","Poulsbo","Benettsville","Palmyra"}
Chicago = not Chicago
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Drawing to a canvas with a closed window.

Post 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.
User avatar
Sir_Silver
Party member
Posts: 286
Joined: Mon Aug 22, 2016 2:25 pm
Contact:

Re: Drawing to a canvas with a closed window.

Post 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.
alloyed
Citizen
Posts: 80
Joined: Thu May 28, 2015 8:45 pm
Contact:

Re: Drawing to a canvas with a closed window.

Post 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
HellzoneByron
Prole
Posts: 5
Joined: Sat Jul 01, 2017 5:58 pm
Contact:

Re: Drawing to a canvas with a closed window.

Post 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! :-)

Code: Select all

room = {"Saskatoon","Poulsbo","Benettsville","Palmyra"}
Chicago = not Chicago
Post Reply

Who is online

Users browsing this forum: No registered users and 97 guests