Skip to content

Commit c1733f3

Browse files
committed
love11
1 parent 5de9509 commit c1733f3

File tree

23 files changed

+897
-408
lines changed

23 files changed

+897
-408
lines changed

character0/main.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ function love.load()
5353
-- amount by which we'll translate the scene to emulate a camera
5454
cameraScroll = 0
5555

56-
backgroundR = math.random(255)
57-
backgroundG = math.random(255)
58-
backgroundB = math.random(255)
56+
backgroundR = math.random(255) / 255
57+
backgroundG = math.random(255) / 255
58+
backgroundB = math.random(255) / 255
5959

6060
for y = 1, mapHeight do
6161
table.insert(tiles, {})
@@ -104,7 +104,7 @@ function love.draw()
104104
-- fractional camera offsets with a virtual resolution will result in weird pixelation and artifacting
105105
-- as things are attempted to be drawn fractionally and then forced onto a small virtual canvas
106106
love.graphics.translate(-math.floor(cameraScroll), 0)
107-
love.graphics.clear(backgroundR, backgroundG, backgroundB, 255)
107+
love.graphics.clear(backgroundR, backgroundG, backgroundB, 1)
108108

109109
for y = 1, mapHeight do
110110
for x = 1, mapWidth do

character0/push.lua

+83-35
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
-- push.lua v0.2
1+
-- push.lua v0.4
22

3-
-- Copyright (c) 2017 Ulysse Ramage
3+
-- Copyright (c) 2020 Ulysse Ramage
44
-- 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:
55
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
66
-- 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.
77

8+
local love11 = love.getVersion() == 11
9+
local getDPI = love11 and love.window.getDPIScale or love.window.getPixelScale
10+
local windowUpdateMode = love11 and love.window.updateMode or function(width, height, settings)
11+
local _, _, flags = love.window.getMode()
12+
for k, v in pairs(settings) do flags[k] = v end
13+
love.window.setMode(width, height, flags)
14+
end
15+
816
local push = {
917

1018
defaults = {
1119
fullscreen = false,
1220
resizable = false,
1321
pixelperfect = false,
1422
highdpi = true,
15-
canvas = true
23+
canvas = true,
24+
stencil = true
1625
}
1726

1827
}
1928
setmetatable(push, push)
2029

21-
--TODO: rendering resolution?
22-
--TODO: clean up code
23-
2430
function push:applySettings(settings)
2531
for k, v in pairs(settings) do
2632
self["_" .. k] = v
@@ -39,11 +45,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
3945
self:applySettings(self.defaults) --set defaults first
4046
self:applySettings(settings) --then fill with custom settings
4147

42-
love.window.setMode( self._RWIDTH, self._RHEIGHT, {
48+
windowUpdateMode(self._RWIDTH, self._RHEIGHT, {
4349
fullscreen = self._fullscreen,
4450
resizable = self._resizable,
4551
highdpi = self._highdpi
46-
} )
52+
})
4753

4854
self:initValues()
4955

@@ -62,25 +68,31 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
6268
end
6369

6470
function push:setupCanvas(canvases)
65-
table.insert(canvases, { name = "_render" }) --final render
71+
table.insert(canvases, { name = "_render", private = true }) --final render
6672

6773
self._canvas = true
6874
self.canvases = {}
6975

7076
for i = 1, #canvases do
71-
self.canvases[i] = {
72-
name = canvases[i].name,
73-
shader = canvases[i].shader,
74-
canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT)
75-
}
77+
push:addCanvas(canvases[i])
7678
end
7779

7880
return self
7981
end
82+
function push:addCanvas(params)
83+
table.insert(self.canvases, {
84+
name = params.name,
85+
private = params.private,
86+
shader = params.shader,
87+
canvas = love.graphics.newCanvas(self._WWIDTH, self._WHEIGHT),
88+
stencil = params.stencil or self._stencil
89+
})
90+
end
8091

8192
function push:setCanvas(name)
8293
if not self._canvas then return true end
83-
return love.graphics.setCanvas( self:getCanvasTable(name).canvas )
94+
local canvasTable = self:getCanvasTable(name)
95+
return love.graphics.setCanvas({ canvasTable.canvas, stencil = canvasTable.stencil })
8496
end
8597
function push:getCanvasTable(name)
8698
for i = 1, #self.canvases do
@@ -98,7 +110,7 @@ function push:setShader(name, shader)
98110
end
99111

100112
function push:initValues()
101-
self._PSCALE = self._highdpi and love.window.getPixelScale() or 1
113+
self._PSCALE = (not love11 and self._highdpi) and getDPI() or 1
102114

103115
self._SCALE = {
104116
x = self._RWIDTH/self._WWIDTH * self._PSCALE,
@@ -119,19 +131,15 @@ function push:initValues()
119131
self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2
120132
end
121133

122-
--[[ DEPRECATED ]]--
123134
function push:apply(operation, shader)
124-
if operation == "start" then
125-
self:start()
126-
elseif operation == "finish" or operation == "end" then
127-
self:finish(shader)
128-
end
135+
self._drawFunctions[operation](self, shader)
129136
end
130137

131138
function push:start()
132139
if self._canvas then
133140
love.graphics.push()
134-
love.graphics.setCanvas(self.canvases[1].canvas)
141+
love.graphics.setCanvas({ self.canvases[1].canvas, stencil = self.canvases[1].stencil })
142+
135143
else
136144
love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
137145
love.graphics.setScissor(self._OFFSET.x, self._OFFSET.y, self._WWIDTH*self._SCALE.x, self._WHEIGHT*self._SCALE.y)
@@ -140,32 +148,73 @@ function push:start()
140148
end
141149
end
142150

151+
function push:applyShaders(canvas, shaders)
152+
local _shader = love.graphics.getShader()
153+
if #shaders <= 1 then
154+
love.graphics.setShader(shaders[1])
155+
love.graphics.draw(canvas)
156+
else
157+
local _canvas = love.graphics.getCanvas()
158+
159+
local _tmp = self:getCanvasTable("_tmp")
160+
if not _tmp then --create temp canvas only if needed
161+
self:addCanvas({ name = "_tmp", private = true, shader = nil })
162+
_tmp = self:getCanvasTable("_tmp")
163+
end
164+
165+
love.graphics.push()
166+
love.graphics.origin()
167+
local outputCanvas
168+
for i = 1, #shaders do
169+
local inputCanvas = i % 2 == 1 and canvas or _tmp.canvas
170+
outputCanvas = i % 2 == 0 and canvas or _tmp.canvas
171+
love.graphics.setCanvas(outputCanvas)
172+
love.graphics.clear()
173+
love.graphics.setShader(shaders[i])
174+
love.graphics.draw(inputCanvas)
175+
love.graphics.setCanvas(inputCanvas)
176+
end
177+
love.graphics.pop()
178+
179+
love.graphics.setCanvas(_canvas)
180+
love.graphics.draw(outputCanvas)
181+
end
182+
love.graphics.setShader(_shader)
183+
end
184+
143185
function push:finish(shader)
144186
love.graphics.setBackgroundColor(unpack(self._borderColor))
145187
if self._canvas then
146188
local _render = self:getCanvasTable("_render")
147189

148190
love.graphics.pop()
149191

150-
love.graphics.setColor(255, 255, 255)
192+
local white = love11 and 1 or 255
193+
love.graphics.setColor(white, white, white)
151194

152195
--draw canvas
153196
love.graphics.setCanvas(_render.canvas)
154-
for i = 1, #self.canvases - 1 do --do not draw _render yet
197+
for i = 1, #self.canvases do --do not draw _render yet
155198
local _table = self.canvases[i]
156-
love.graphics.setShader(_table.shader)
157-
love.graphics.draw(_table.canvas)
199+
if not _table.private then
200+
local _canvas = _table.canvas
201+
local _shader = _table.shader
202+
self:applyShaders(_canvas, type(_shader) == "table" and _shader or { _shader })
203+
end
158204
end
159205
love.graphics.setCanvas()
160-
206+
161207
--draw render
162208
love.graphics.translate(self._OFFSET.x, self._OFFSET.y)
163-
love.graphics.setShader(shader or self:getCanvasTable("_render").shader)
164-
love.graphics.draw(self:getCanvasTable("_render").canvas, 0, 0, 0, self._SCALE.x, self._SCALE.y)
209+
local shader = shader or _render.shader
210+
love.graphics.push()
211+
love.graphics.scale(self._SCALE.x, self._SCALE.y)
212+
self:applyShaders(_render.canvas, type(shader) == "table" and shader or { shader })
213+
love.graphics.pop()
165214

166215
--clear canvas
167216
for i = 1, #self.canvases do
168-
love.graphics.setCanvas( self.canvases[i].canvas )
217+
love.graphics.setCanvas(self.canvases[i].canvas)
169218
love.graphics.clear()
170219
end
171220

@@ -193,7 +242,7 @@ end
193242

194243
--doesn't work - TODO
195244
function push:toReal(x, y)
196-
return x+self._OFFSET.x, y+self._OFFSET.y
245+
return x + self._OFFSET.x, y + self._OFFSET.y
197246
end
198247

199248
function push:switchFullscreen(winw, winh)
@@ -213,13 +262,12 @@ function push:switchFullscreen(winw, winh)
213262

214263
love.window.setFullscreen(self._fullscreen, "desktop")
215264
if not self._fullscreen and (winw or winh) then
216-
love.window.setMode(self._RWIDTH, self._RHEIGHT) --set window dimensions
265+
windowUpdateMode(self._RWIDTH, self._RHEIGHT) --set window dimensions
217266
end
218267
end
219268

220269
function push:resize(w, h)
221-
local pixelScale = love.window.getPixelScale()
222-
if self._highdpi then w, h = w / pixelScale, h / pixelScale end
270+
if self._highdpi then w, h = w / self._PSCALE, h / self._PSCALE end
223271
self._RWIDTH = w
224272
self._RHEIGHT = h
225273
self:initValues()

character1/main.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ function love.load()
5555
-- amount by which we'll translate the scene to emulate a camera
5656
cameraScroll = 0
5757

58-
backgroundR = math.random(255)
59-
backgroundG = math.random(255)
60-
backgroundB = math.random(255)
58+
backgroundR = math.random(255) / 255
59+
backgroundG = math.random(255) / 255
60+
backgroundB = math.random(255) / 255
6161

6262
for y = 1, mapHeight do
6363
table.insert(tiles, {})
@@ -106,7 +106,7 @@ function love.draw()
106106
-- fractional camera offsets with a virtual resolution will result in weird pixelation and artifacting
107107
-- as things are attempted to be drawn fractionally and then forced onto a small virtual canvas
108108
love.graphics.translate(-math.floor(cameraScroll), 0)
109-
love.graphics.clear(backgroundR, backgroundG, backgroundB, 255)
109+
love.graphics.clear(backgroundR, backgroundG, backgroundB, 1)
110110

111111
for y = 1, mapHeight do
112112
for x = 1, mapWidth do

0 commit comments

Comments
 (0)