Canvas & Stencil in Love 11.0

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.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Canvas & Stencil in Love 11.0

Post by Ref »

Error message:
Error
physics.lua:435: Drawing to the stencil buffer with a Canvas active requires either stencil=true or a custom stencil-type Canvas to be used, in setCanvas.
Code referenced:

Code: Select all

texCanvas = function( canW, canH, image, x, y, a, sx, sy, wd, ht  )
	local canvas = love.graphics.newCanvas( canW, canH )
	local stencilFunction = function()
		gr.polygon( 'fill', polymask )
	end
	gr.setCanvas( canvas )
		gr.stencil( stencilFunction, 'replace', 1 )
		gr.setStencilTest( 'greater',  0 )
			gr.setColor( 1, 1, 1 )
			gr.draw( image, x, y, a, sx, sy, image:getWidth()/2, image:getHeight()/2 )
		gr.setStencilTest()
	gr.setCanvas()
	return canvas
end
OK!
Works on Love 10 but not on Love 11.
Where do I stick 'stencil=true' or get a "custom stencil-type Canvas" from?
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Canvas & Stencil in Love 11.0

Post by raidho36 »

Try

Code: Select all

gr.setCanvas( canvas. { stencil = true } )
or try browsing source code and hopefully work it out that way.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Canvas & Stencil in Love 11.0

Post by zorg »

That dot should be a comma though.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Countzero
Prole
Posts: 3
Joined: Wed Feb 07, 2018 9:07 am

Re: Canvas & Stencil in Love 11.0

Post by Countzero »

So (just got it from the wiki, thought) the right way (or at least a way that works) is

Code: Select all

love.graphics.setCanvas({canvas, stencil=true})
or just withouth the round brackets

Code: Select all

love.graphics.setCanvas{canvas, stencil=true}
Tested and working.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Canvas & Stencil in Love 11.0

Post by Ref »

Glad to hear you got it to work.
Used your suggestion and got the same error message for the line mentioned above.
Must be something else in the code that is causing this Love 10 script to fail in Love 11.
More head scratching required!
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: Canvas & Stencil in Love 11.0

Post by pgimeno »

Ref wrote: Wed Apr 04, 2018 1:55 pm Glad to hear you got it to work.
Used your suggestion and got the same error message for the line mentioned above.
Must be something else in the code that is causing this Love 10 script to fail in Love 11.
More head scratching required!
Really? Does this code gives an error for you? (texCanvas is a direct copy/paste of your function, with the change suggested by Countzero)

Code: Select all

local gr = love.graphics
local polymask = {0,0,1,1,0,1}

texCanvas = function( canW, canH, image, x, y, a, sx, sy, wd, ht  )
	local canvas = love.graphics.newCanvas( canW, canH )
	local stencilFunction = function()
		gr.polygon( 'fill', polymask )
	end
	gr.setCanvas( {canvas, stencil=true} )
		gr.stencil( stencilFunction, 'replace', 1 )
		gr.setStencilTest( 'greater',  0 )
			gr.setColor( 1, 1, 1 )
			gr.draw( image, x, y, a, sx, sy, image:getWidth()/2, image:getHeight()/2 )
		gr.setStencilTest()
	gr.setCanvas()
	return canvas
end

texCanvas(100,100,love.graphics.newImage(love.image.newImageData(1,1)), 10, 10, 1, 1, 1, 1, 1)

function love.keypressed(k) if k == "escape" then love.event.quit() end end
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: Canvas & Stencil in Love 11.0

Post by Ref »

Bingo!

Code: Select all

gr.setCanvas( {canvas, stencil=true} )
does work.
Just have to get the brackets in the right places.
Thanks for the help!
MissDanish
Citizen
Posts: 65
Joined: Wed Mar 07, 2018 11:21 pm

Re: Canvas & Stencil in Love 11.0

Post by MissDanish »

btw canvases are broken in 11.0 unless you use the fix smile linked in the 11.0 release thread. Sometimes they refuse to draw on screen
User avatar
Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

Re: Canvas & Stencil in Love 11.0

Post by Jack Dandy »

Hey fellas, I ran into the same problem.

https://www.reddit.com/r/love2d/comment ... th_ingame/
Hi.

I made a game that uses a Stencil to create a 'Fog of War' style effect. It worked great.

However, when I tried using the 'Scrale' scaling library to also have a nice fullscreen option, it gave me this error:

(image on reddit thread)

Scrale uses the canvas mechanic to work.

(I understand that this is a problem Love2D version 11.0 and up are having- canvases and stencils colliding.)

(image on reddit thread)

I checked out the implementation of scrale, found that it only uses 'setCanvas' in one place, and changed it from 'love.graphics.setCanvas(scrale.canvas)'

to 'love.graphics.setCanvas({scrale.canvas, stencil=true}' ).

But then, it breaks the Fog of War effect. Things that were previously hidden by it, are now always displayed (Even when they're supposed to be hidden). See below - every sprite in the greyed-out and blacked-out zone is not supposed to appear. It worked just fine before.


(image on reddit thread)


In short- anyone have a clue on how to combine Scrale with using stencils?
Can anyone help please?
User avatar
Jack Dandy
Prole
Posts: 49
Joined: Mon Sep 08, 2014 4:26 pm

Re: Canvas & Stencil in Love 11.0

Post by Jack Dandy »

Bump? All it involves is a canvas and a stencil that is drawn & applied inside it.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests