Enabling drawing in love.update() - Solved!

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
Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Enabling drawing in love.update() - Solved!

Post by Taehl » Mon Mar 14, 2016 12:18 am

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)
Last edited by Taehl on Mon Mar 14, 2016 1:23 am, edited 1 time in total.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.

User avatar
Nixola
Inner party member
Posts: 1941
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Enabling drawing in love.update()

Post by Nixola » Mon Mar 14, 2016 12:20 am

You could draw to a canvas, then draw the canvas in love.draw
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Enabling drawing in love.update()

Post by Taehl » Mon Mar 14, 2016 12:30 am

Usually a great approach, but I don't have canvas support. :(
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.

User avatar
Taehl
Dreaming in associative arrays
Posts: 1024
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Enabling drawing in love.update()

Post by Taehl » Mon Mar 14, 2016 1:22 am

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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.

Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests