Only you know the organization of your game, but the functionality is as follows.
You can see the stencil as an invisible mask, similar to an alpha layer, but only yes/no. When the stencil is active, a pixel is either drawn or not drawn, even if the values of the mask can be in 0-255; there's no opacity. You control the mask value threshold for a pixel to be drawn.
You draw to that mask (the stencil) with regular draw functions. What gets drawn in the stencil and what gets drawn in the screen, is decided by whether you're drawing inside the stencil function or outside. For example:
Code: Select all
love.graphics.stencil(function ()
love.graphics.rectangle("fill", 10, 10, 20, 20)
end,
"replace", 1, false)
love.graphics.setStencilTest("greater", 0)
love.graphics.circle("fill", 30, 30, 20)
The "replace" mode means that the pixels that are drawn will all be set in the mask to the value we supply (1 in this case). The colour and alpha you choose doesn't matter; everything you draw, be it a circle, a rectangle, lines, or whatever, will have the value 1. Since we're setting 'keepvalues' to false, the mask will be cleared before drawing anything to it (similar to a love.graphics.clear of the mask).
The rectangle will be drawn to the stencil; the circle will be drawn to the screen.
Note that once the stencil is drawn to, you have to specify the threshold value for a pixel to be drawable. That's what setStencilTest does. In this case, we're saying that any value greater than zero is drawable. We have drawn the rectangle with the value 1, so all the pixels in the rectangle will be drawable.
Now, as for how to adapt it to your game, exactly how is up to you, but it boils down to this. You would draw the tiles to the stencil as well as to the screen. Assuming you don't need the stencil for other uses, you can just draw the tiles to the screen and to the stencil at the same point of the code. For example:
Code: Select all
local function myStencilFunction()
drawTiles()
end
...
drawTiles()
love.graphics.stencil(myStencilFunction) -- default values for the rest are OK
That prepares the stencil. Then before drawing the objects to spawn, you do:
Code: Select all
love.graphics.setStencilTest("greater", 0)
and when you finish drawing the objects to spawn, you do:
That should be all.