From 7e1e365133b655195517cf32cedde82e6a1a99d6 Mon Sep 17 00:00:00 2001 From: Victor Escudero Date: Fri, 3 Apr 2020 17:58:10 +0200 Subject: [PATCH] Upgraded to push v0.4 and updated pong examples --- pong-1/main.lua | 4 +- pong-1/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-10/main.lua | 6 +-- pong-10/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-11/main.lua | 6 +-- pong-11/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-12/main.lua | 6 +-- pong-12/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-2/main.lua | 6 +-- pong-2/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-3/main.lua | 6 +-- pong-3/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-4/main.lua | 6 +-- pong-4/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-5/main.lua | 6 +-- pong-5/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-6/main.lua | 6 +-- pong-6/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-7/main.lua | 6 +-- pong-7/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-8/main.lua | 6 +-- pong-8/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-9/main.lua | 6 +-- pong-9/push.lua | 120 +++++++++++++++++++++++++++++++------------- pong-final/main.lua | 4 +- pong-final/push.lua | 27 ++++++---- 26 files changed, 1063 insertions(+), 478 deletions(-) diff --git a/pong-1/main.lua b/pong-1/main.lua index 4029596..b0ab316 100644 --- a/pong-1/main.lua +++ b/pong-1/main.lua @@ -69,12 +69,12 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- condensed onto one line from last example -- note we are now using virtual width and height now for text placement love.graphics.printf('Hello Pong!', 0, VIRTUAL_HEIGHT / 2 - 6, VIRTUAL_WIDTH, 'center') -- end rendering at virtual resolution - push:apply('end') + push:finish() end diff --git a/pong-1/push.lua b/pong-1/push.lua index 0e0709a..5360ff0 100644 --- a/pong-1/push.lua +++ b/pong-1/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-10/main.lua b/pong-10/main.lua index 6f19e49..e1fe767 100644 --- a/pong-10/main.lua +++ b/pong-10/main.lua @@ -251,11 +251,11 @@ end ]] function love.draw() - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) love.graphics.setFont(smallFont) @@ -287,7 +287,7 @@ function love.draw() displayFPS() - push:apply('end') + push:finish() end --[[ diff --git a/pong-10/push.lua b/pong-10/push.lua index 0e0709a..5360ff0 100644 --- a/pong-10/push.lua +++ b/pong-10/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-11/main.lua b/pong-11/main.lua index 58956e1..01aa607 100644 --- a/pong-11/main.lua +++ b/pong-11/main.lua @@ -267,11 +267,11 @@ end ]] function love.draw() - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) love.graphics.setFont(smallFont) @@ -303,7 +303,7 @@ function love.draw() displayFPS() - push:apply('end') + push:finish() end --[[ diff --git a/pong-11/push.lua b/pong-11/push.lua index 0e0709a..5360ff0 100644 --- a/pong-11/push.lua +++ b/pong-11/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-12/main.lua b/pong-12/main.lua index b06a998..1fbc988 100644 --- a/pong-12/main.lua +++ b/pong-12/main.lua @@ -275,11 +275,11 @@ end ]] function love.draw() - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) love.graphics.setFont(smallFont) @@ -311,7 +311,7 @@ function love.draw() displayFPS() - push:apply('end') + push:finish() end --[[ diff --git a/pong-12/push.lua b/pong-12/push.lua index 0e0709a..5360ff0 100644 --- a/pong-12/push.lua +++ b/pong-12/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-2/main.lua b/pong-2/main.lua index aa4b0bf..9c87370 100644 --- a/pong-2/main.lua +++ b/pong-2/main.lua @@ -71,11 +71,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw welcome text toward the top of the screen love.graphics.printf('Hello Pong!', 0, 20, VIRTUAL_WIDTH, 'center') @@ -95,5 +95,5 @@ function love.draw() love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4) -- end rendering at virtual resolution - push:apply('end') + push:finish() end diff --git a/pong-2/push.lua b/pong-2/push.lua index 0e0709a..5360ff0 100644 --- a/pong-2/push.lua +++ b/pong-2/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-3/main.lua b/pong-3/main.lua index c4aa3c8..7e6a6f0 100644 --- a/pong-3/main.lua +++ b/pong-3/main.lua @@ -110,11 +110,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw welcome text toward the top of the screen love.graphics.setFont(smallFont) @@ -138,5 +138,5 @@ function love.draw() love.graphics.rectangle('fill', VIRTUAL_WIDTH / 2 - 2, VIRTUAL_HEIGHT / 2 - 2, 4, 4) -- end rendering at virtual resolution - push:apply('end') + push:finish() end diff --git a/pong-3/push.lua b/pong-3/push.lua index 0e0709a..5360ff0 100644 --- a/pong-3/push.lua +++ b/pong-3/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-4/main.lua b/pong-4/main.lua index 8d8bbcb..22db147 100644 --- a/pong-4/main.lua +++ b/pong-4/main.lua @@ -149,11 +149,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw different things based on the state of the game love.graphics.setFont(smallFont) @@ -174,5 +174,5 @@ function love.draw() love.graphics.rectangle('fill', ballX, ballY, 4, 4) -- end rendering at virtual resolution - push:apply('end') + push:finish() end diff --git a/pong-4/push.lua b/pong-4/push.lua index 0e0709a..5360ff0 100644 --- a/pong-4/push.lua +++ b/pong-4/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-5/main.lua b/pong-5/main.lua index 62222eb..1988bdd 100644 --- a/pong-5/main.lua +++ b/pong-5/main.lua @@ -150,11 +150,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw different things based on the state of the game love.graphics.setFont(smallFont) @@ -173,5 +173,5 @@ function love.draw() ball:render() -- end rendering at virtual resolution - push:apply('end') + push:finish() end diff --git a/pong-5/push.lua b/pong-5/push.lua index 0e0709a..5360ff0 100644 --- a/pong-5/push.lua +++ b/pong-5/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-6/main.lua b/pong-6/main.lua index f86f891..a91a714 100644 --- a/pong-6/main.lua +++ b/pong-6/main.lua @@ -164,11 +164,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw different things based on the state of the game love.graphics.setFont(smallFont) @@ -198,7 +198,7 @@ function love.draw() displayFPS() -- end rendering at virtual resolution - push:apply('end') + push:finish() end --[[ diff --git a/pong-6/push.lua b/pong-6/push.lua index 0e0709a..5360ff0 100644 --- a/pong-6/push.lua +++ b/pong-6/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-7/main.lua b/pong-7/main.lua index 0c940ee..33f5bfa 100644 --- a/pong-7/main.lua +++ b/pong-7/main.lua @@ -203,11 +203,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw different things based on the state of the game love.graphics.setFont(smallFont) @@ -237,7 +237,7 @@ function love.draw() displayFPS() -- end rendering at virtual resolution - push:apply('end') + push:finish() end --[[ diff --git a/pong-7/push.lua b/pong-7/push.lua index 0e0709a..5360ff0 100644 --- a/pong-7/push.lua +++ b/pong-7/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-8/main.lua b/pong-8/main.lua index 2d4873f..c662090 100644 --- a/pong-8/main.lua +++ b/pong-8/main.lua @@ -219,11 +219,11 @@ end ]] function love.draw() -- begin rendering at virtual resolution - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- draw different things based on the state of the game love.graphics.setFont(smallFont) @@ -247,7 +247,7 @@ function love.draw() displayFPS() -- end rendering at virtual resolution - push:apply('end') + push:finish() end --[[ diff --git a/pong-8/push.lua b/pong-8/push.lua index 0e0709a..5360ff0 100644 --- a/pong-8/push.lua +++ b/pong-8/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-9/main.lua b/pong-9/main.lua index d7f1b58..a79f976 100644 --- a/pong-9/main.lua +++ b/pong-9/main.lua @@ -222,11 +222,11 @@ end ]] function love.draw() - push:apply('start') + push:start() -- clear the screen with a specific color; in this case, a color similar -- to some versions of the original Pong - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) love.graphics.setFont(smallFont) @@ -251,7 +251,7 @@ function love.draw() displayFPS() - push:apply('end') + push:finish() end --[[ diff --git a/pong-9/push.lua b/pong-9/push.lua index 0e0709a..5360ff0 100644 --- a/pong-9/push.lua +++ b/pong-9/push.lua @@ -1,10 +1,18 @@ --- push.lua v0.2 +-- push.lua v0.4 --- Copyright (c) 2017 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +local love11 = love.getVersion() == 11 +local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end + local push = { defaults = { @@ -12,15 +20,13 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } setmetatable(push, push) ---TODO: rendering resolution? ---TODO: clean up code - function push:applySettings(settings) for k, v in pairs(settings) do self["_" .. k] = v @@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode( self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi - } ) + }) self:initValues() @@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) end function push:setupCanvas(canvases) - table.insert(canvases, { name = "_render" }) --final render + table.insert(canvases, { name = "_render", private = true }) --final render self._canvas = true self.canvases = {} for i = 1, #canvases do - self.canvases[i] = { - name = canvases[i].name, - shader = canvases[i].shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) - } + push:addCanvas(canvases[i]) end return self end +function push:addCanvas(params) + table.insert(self.canvases, { + name = params.name, + private = params.private, + shader = params.shader, + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil + }) +end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas( self:getCanvasTable(name).canvas ) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -98,7 +110,7 @@ function push:setShader(name, shader) end function push:initValues() - self._PSCALE = self._highdpi and love.window.getPixelScale() or 1 + self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1 self._SCALE = { x = self._RWIDTH/self._WWIDTH * self._PSCALE, @@ -119,19 +131,15 @@ function push:initValues() self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2 end ---[[ DEPRECATED ]]-- function push:apply(operation, shader) - if operation == "start" then - self:start() - elseif operation == "finish" or operation == "end" then - self:finish(shader) - end + self._drawFunctions[operation](self, shader) end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -140,6 +148,40 @@ function push:start() end end +function push:applyShaders(canvas, shaders) + local _shader = love.graphics.getShader() + if #shaders <= 1 then + love.graphics.setShader(shaders[1]) + love.graphics.draw(canvas) + else + local _canvas = love.graphics.getCanvas() + + local _tmp = self:getCanvasTable("_tmp") + if not _tmp then --create temp canvas only if needed + self:addCanvas({ name = "_tmp", private = true, shader = nil }) + _tmp = self:getCanvasTable("_tmp") + end + + love.graphics.push() + love.graphics.origin() + local outputCanvas + for i = 1, #shaders do + local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas + outputCanvas = i % 2 == 0 and canvas or _tmp.canvas + love.graphics.setCanvas(outputCanvas) + love.graphics.clear() + love.graphics.setShader(shaders[i]) + love.graphics.draw(inputCanvas) + love.graphics.setCanvas(inputCanvas) + end + love.graphics.pop() + + love.graphics.setCanvas(_canvas) + love.graphics.draw(outputCanvas) + end + love.graphics.setShader(_shader) +end + function push:finish(shader) love.graphics.setBackgroundColor(unpack(self._borderColor)) if self._canvas then @@ -147,25 +189,32 @@ function push:finish(shader) love.graphics.pop() - love.graphics.setColor(255, 255, 255) + local white = love11 and 1 or 255 + love.graphics.setColor(white, white, white) --draw canvas love.graphics.setCanvas(_render.canvas) - for i = 1, #self.canvases - 1 do --do not draw _render yet + for i = 1, #self.canvases do --do not draw _render yet local _table = self.canvases[i] - love.graphics.setShader(_table.shader) - love.graphics.draw(_table.canvas) + if not _table.private then + local _canvas = _table.canvas + local _shader = _table.shader + self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader }) + end end love.graphics.setCanvas() - + --draw render love.graphics.translate(self._OFFSET.x, self._OFFSET.y) - love.graphics.setShader(shader or self:getCanvasTable("_render").shader) - love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y) + local shader = shader or _render.shader + love.graphics.push() + love.graphics.scale(self._SCALE.x, self._SCALE.y) + self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader }) + love.graphics.pop() --clear canvas for i = 1, #self.canvases do - love.graphics.setCanvas( self.canvases[i].canvas ) + love.graphics.setCanvas(self.canvases[i].canvas) love.graphics.clear() end @@ -193,7 +242,7 @@ end --doesn't work - TODO function push:toReal(x, y) - return x+self._OFFSET.x, y+self._OFFSET.y + return x + self._OFFSET.x, y + self._OFFSET.y end function push:switchFullscreen(winw, winh) @@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end function push:resize(w, h) - local pixelScale = love.window.getPixelScale() - if self._highdpi then w, h = w / pixelScale, h / pixelScale end + if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end self._RWIDTH = w self._RHEIGHT = h self:initValues() @@ -229,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file diff --git a/pong-final/main.lua b/pong-final/main.lua index c8df5fc..994636b 100644 --- a/pong-final/main.lua +++ b/pong-final/main.lua @@ -86,7 +86,7 @@ function love.load() fullscreen = false, resizable = true, vsync = true, - canvas = false + --canvas = false }) -- initialize our player paddles; make them global so that they can be @@ -303,7 +303,7 @@ function love.draw() -- begin drawing with push, in our virtual resolution push:start() - love.graphics.clear(40, 45, 52, 255) + love.graphics.clear(40, 45, 52, 0.35) -- render different things depending on which part of the game we're in if gameState == 'start' then diff --git a/pong-final/push.lua b/pong-final/push.lua index 723eb26..5360ff0 100644 --- a/pong-final/push.lua +++ b/pong-final/push.lua @@ -1,12 +1,17 @@ --- push.lua v0.3 +-- push.lua v0.4 --- Copyright (c) 2018 Ulysse Ramage +-- Copyright (c) 2020 Ulysse Ramage -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. local love11 = love.getVersion() == 11 local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale +local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings) + local _, _, flags = love.window.getMode() + for k, v in pairs(settings) do flags[k] = v end + love.window.setMode(width, height, flags) +end local push = { @@ -15,7 +20,8 @@ local push = { resizable = false, pixelperfect = false, highdpi = true, - canvas = true + canvas = true, + stencil = true } } @@ -39,7 +45,7 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings) self:applySettings(self.defaults) --set defaults first self:applySettings(settings) --then fill with custom settings - love.window.setMode(self._RWIDTH, self._RHEIGHT, { + windowUpdateMode(self._RWIDTH, self._RHEIGHT, { fullscreen = self._fullscreen, resizable = self._resizable, highdpi = self._highdpi @@ -78,13 +84,15 @@ function push:addCanvas(params) name = params.name, private = params.private, shader = params.shader, - canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT) + canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT), + stencil = params.stencil or self._stencil }) end function push:setCanvas(name) if not self._canvas then return true end - return love.graphics.setCanvas(self:getCanvasTable(name).canvas) + local canvasTable = self:getCanvasTable(name) + return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil }) end function push:getCanvasTable(name) for i = 1, #self.canvases do @@ -130,7 +138,8 @@ end function push:start() if self._canvas then love.graphics.push() - love.graphics.setCanvas(self.canvases[1].canvas) + love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil }) + else love.graphics.translate(self._OFFSET.x, self._OFFSET.y) love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y) @@ -253,7 +262,7 @@ function push:switchFullscreen(winw, winh) love.window.setFullscreen(self._fullscreen, "desktop") if not self._fullscreen and (winw or winh) then - love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions + windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions end end @@ -268,4 +277,4 @@ function push:getWidth() return self._WWIDTH end function push:getHeight() return self._WHEIGHT end function push:getDimensions() return self._WWIDTH, self._WHEIGHT end -return push +return push \ No newline at end of file