2014-10-22 02:48:19 +00:00
|
|
|
local util = {}
|
2016-02-03 14:45:22 +00:00
|
|
|
--TODO: the whole stencil/canvas system should be reviewed since it has been changed in a naive way
|
2014-10-22 02:48:19 +00:00
|
|
|
|
2014-12-20 03:50:42 +00:00
|
|
|
function util.process(canvas, options)
|
2017-07-17 21:01:13 +00:00
|
|
|
--TODO: now you cannot draw a canvas to itself
|
2016-04-24 13:38:59 +00:00
|
|
|
temp = love.graphics.newCanvas()
|
|
|
|
util.drawCanvasToCanvas(canvas, temp, options)
|
|
|
|
util.drawCanvasToCanvas(temp, canvas, options)
|
2014-12-20 03:50:42 +00:00
|
|
|
end
|
|
|
|
|
2014-10-22 02:48:19 +00:00
|
|
|
function util.drawCanvasToCanvas(canvas, other_canvas, options)
|
|
|
|
options = options or {}
|
|
|
|
|
2014-12-20 03:50:42 +00:00
|
|
|
util.drawto(other_canvas, 0, 0, 1, function()
|
|
|
|
if options["blendmode"] then
|
|
|
|
love.graphics.setBlendMode(options["blendmode"])
|
|
|
|
end
|
|
|
|
if options["shader"] then
|
|
|
|
love.graphics.setShader(options["shader"])
|
|
|
|
end
|
|
|
|
if options["stencil"] then
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.stencil(options["stencil"])
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setStencilTest("greater",0)
|
2014-12-20 03:50:42 +00:00
|
|
|
end
|
|
|
|
if options["istencil"] then
|
2016-02-03 14:45:22 +00:00
|
|
|
love.graphics.stencil(options["istencil"])
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setStencilTest("equal", 0)
|
2014-12-20 03:50:42 +00:00
|
|
|
end
|
|
|
|
if options["color"] then
|
|
|
|
love.graphics.setColor(unpack(options["color"]))
|
|
|
|
else
|
2014-10-22 02:48:19 +00:00
|
|
|
love.graphics.setColor(255,255,255)
|
2014-12-20 03:50:42 +00:00
|
|
|
end
|
2016-02-04 16:33:22 +00:00
|
|
|
if love.graphics.getCanvas() ~= canvas then
|
|
|
|
love.graphics.draw(canvas,0,0)
|
|
|
|
end
|
2014-10-22 02:48:19 +00:00
|
|
|
if options["blendmode"] then
|
|
|
|
love.graphics.setBlendMode("alpha")
|
|
|
|
end
|
|
|
|
if options["shader"] then
|
|
|
|
love.graphics.setShader()
|
|
|
|
end
|
2016-02-04 16:33:22 +00:00
|
|
|
if options["stencil"] or options["istencil"] then
|
2016-02-03 14:45:22 +00:00
|
|
|
--love.graphics.setInvertedStencil()
|
2016-02-04 16:33:22 +00:00
|
|
|
love.graphics.setStencilTest()
|
2014-12-20 03:50:42 +00:00
|
|
|
end
|
|
|
|
end)
|
2014-10-22 02:48:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function util.drawto(canvas, x, y, scale, cb)
|
|
|
|
local last_buffer = love.graphics.getCanvas()
|
|
|
|
love.graphics.push()
|
|
|
|
love.graphics.origin()
|
2018-05-30 03:45:58 +00:00
|
|
|
love.graphics.setCanvas({canvas,stencil = true})
|
2014-10-22 02:48:19 +00:00
|
|
|
love.graphics.translate(x, y)
|
|
|
|
love.graphics.scale(scale)
|
|
|
|
cb()
|
|
|
|
love.graphics.setCanvas(last_buffer)
|
|
|
|
love.graphics.pop()
|
|
|
|
end
|
|
|
|
|
2017-07-17 21:01:13 +00:00
|
|
|
function util.loadShader(name)
|
|
|
|
local shader = ""
|
|
|
|
local externInit = {}
|
|
|
|
for line in love.filesystem.lines(name) do
|
|
|
|
|
|
|
|
if line:sub(1,6) == "extern" then
|
|
|
|
local type, name = line:match("extern (%w+) (%w+)")
|
|
|
|
local value = line:match("=(.*);")
|
|
|
|
if value then
|
|
|
|
externInit[name] = {type=type, val=value}
|
|
|
|
line = line:match("extern %w+ %w+")..";"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
shader = shader.."\n"..line
|
|
|
|
end
|
|
|
|
|
|
|
|
local effect = love.graphics.newShader(shader)
|
|
|
|
for k, v in pairs(externInit) do
|
|
|
|
if v.type == "bool" then
|
|
|
|
effect:send(k, v.val)
|
|
|
|
elseif v.type == "int" or v.type == "uint" then
|
|
|
|
effect:sendInt(k, tonumber(v.val))
|
|
|
|
elseif v.type == "float" or v.type == "double" or v.type == "number" then
|
|
|
|
effect:send(k, tonumber(v.val))
|
|
|
|
elseif v.type:sub(1,3) == "vec" then
|
|
|
|
v.val = v.val:gsub(" ", ""):sub(6):sub(1, -2)
|
|
|
|
local next = v.val:gmatch("([^,]+)")
|
|
|
|
local values = {}
|
|
|
|
for n in next do
|
|
|
|
table.insert(values, tonumber(n))
|
|
|
|
end
|
|
|
|
effect:send(k, values)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return effect
|
|
|
|
end
|
|
|
|
|
2014-10-22 02:48:19 +00:00
|
|
|
return util
|