Skip to content

Cannot use stencils while effect is active. #47

@RuairiD

Description

@RuairiD

The following code crashes:

local moonshine = require('moonshine')
local effect = moonshine(moonshine.effects.crt)
local stencilFunction = (function ()
    love.graphics.rectangle('fill', 100, 100, 100, 100)
end)


function love.draw()
    effect(function ()
        love.graphics.setColor(1, 1, 1, 1)
        love.graphics.stencil(stencilFunction, "replace", 1)
        love.graphics.setStencilTest("greater", 0)
        love.graphics.rectangle('fill', 0, 0, 300, 300)
        love.graphics.setStencilTest()
    end)
end

...with the following error:

Error: main.lua:11: Drawing to the stencil buffer with a Canvas active requires either stencil=true or a custom stencil-type Canvas to be used, in setCanvas.
stack traceback:
	[string "boot.lua"]:637: in function <[string "boot.lua"]:633>
	[C]: in function 'stencil'
	main.lua:11: in function 'func'
	moonshine/init.lua:68: in function 'effect'
	main.lua:9: in function 'draw'
	[string "boot.lua"]:513: in function <[string "boot.lua"]:493>
	[C]: in function 'xpcall'

I was able to fix this by adding 'stencil=true' when calling love.graphics.setCanvas within chain.draw i.e.

diff --git a/init.lua b/init.lua
index 431c6c0..cb58a12 100644
--- a/init.lua
+++ b/init.lua
@@ -63,7 +63,7 @@ moonshine.chain = function(w,h,effect)
     local fg_r, fg_g, fg_b, fg_a = love.graphics.getColor()

     -- draw scene to front buffer
-    love.graphics.setCanvas((buffer())) -- parens are needed: take only front buffer
+    love.graphics.setCanvas{(buffer()), stencil=true} -- parens are needed: take only front buffer
     love.graphics.clear(love.graphics.getBackgroundColor())
     func(...)

Shall I make a PR for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions