Page 1 of 1

Enabling drawing in love.update() - Solved!

Posted: Mon Mar 14, 2016 12:18 am
by Taehl
Hi all! In my current project, I'd like to treat rendering as part of the standard game update loop. But since love.run has a love.graphics.clear() between calling love.update and love.draw, anything drawn will be immediately lost.

I know I can very easily change this behaviour by moving the offending love.graphics.clear() up a few lines in love.run. However, changing love.run seems terribly intrusive for a library, so I'm wondering if there are more elegant ways of going about it? ("Elegant" discounting solutions such as running all updates in love.draw - eww)

Re: Enabling drawing in love.update()

Posted: Mon Mar 14, 2016 12:20 am
by Nixola
You could draw to a canvas, then draw the canvas in love.draw

Re: Enabling drawing in love.update()

Posted: Mon Mar 14, 2016 12:30 am
by Taehl
Usually a great approach, but I don't have canvas support. :(

Re: Enabling drawing in love.update()

Posted: Mon Mar 14, 2016 1:22 am
by Taehl
love.graphics.clear - LOVE wiki wrote:Note that the scissor area bounds the cleared region.
Wait, that's it!

Code: Select all

function love.update( dt )
	local perfLength = 128
	local timer = love.timer.getTime

	-- clear any scissor region (like the one used below) and the screen
	love.graphics.setScissor()
	love.graphics.clear()
	
	-- the following is what I'm using right now, as an example
	for sysN,system in ipairs( ECS.systems ) do
		if not system.__skipUpdate then
			-- run each system and profile their performance
			local start = timer()
			TECS.runSystem( sysN, dt )		-- some systems use love.graphics to draw stuff
			table.insert( ECS.perf[sysN], 1, timer()-start )
			ECS.perf[sysN][perfLength] = nil		-- only keep the most recent updates
		end
	end
	
	-- prevent Love2D from clearing the screen between here and love.draw
	love.graphics.setScissor( -1,-1, 1,1 )
end
Kind of a hack, but it works well. Nicer than having to replace love.run, too.