1
- -- push.lua v0.2
1
+ -- push.lua v0.3
2
2
3
- -- Copyright (c) 2017 Ulysse Ramage
3
+ -- Copyright (c) 2018 Ulysse Ramage
4
4
-- 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:
5
5
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
6
-- 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.
7
7
8
+ local love11 = love .getVersion () == 11
9
+ local getDPI = love11 and love .window .getDPIScale or love .window .getPixelScale
10
+
8
11
local push = {
9
12
10
13
defaults = {
@@ -18,9 +21,6 @@ local push = {
18
21
}
19
22
setmetatable (push , push )
20
23
21
- -- TODO: rendering resolution?
22
- -- TODO: clean up code
23
-
24
24
function push :applySettings (settings )
25
25
for k , v in pairs (settings ) do
26
26
self [" _" .. k ] = v
@@ -39,11 +39,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
39
39
self :applySettings (self .defaults ) -- set defaults first
40
40
self :applySettings (settings ) -- then fill with custom settings
41
41
42
- love .window .setMode ( self ._RWIDTH , self ._RHEIGHT , {
42
+ love .window .setMode (self ._RWIDTH , self ._RHEIGHT , {
43
43
fullscreen = self ._fullscreen ,
44
44
resizable = self ._resizable ,
45
45
highdpi = self ._highdpi
46
- } )
46
+ })
47
47
48
48
self :initValues ()
49
49
@@ -62,25 +62,29 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
62
62
end
63
63
64
64
function push :setupCanvas (canvases )
65
- table.insert (canvases , { name = " _render" }) -- final render
65
+ table.insert (canvases , { name = " _render" , private = true }) -- final render
66
66
67
67
self ._canvas = true
68
68
self .canvases = {}
69
69
70
70
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
- }
71
+ push :addCanvas (canvases [i ])
76
72
end
77
73
78
74
return self
79
75
end
76
+ function push :addCanvas (params )
77
+ table.insert (self .canvases , {
78
+ name = params .name ,
79
+ private = params .private ,
80
+ shader = params .shader ,
81
+ canvas = love .graphics .newCanvas (self ._WWIDTH , self ._WHEIGHT )
82
+ })
83
+ end
80
84
81
85
function push :setCanvas (name )
82
86
if not self ._canvas then return true end
83
- return love .graphics .setCanvas ( self :getCanvasTable (name ).canvas )
87
+ return love .graphics .setCanvas (self :getCanvasTable (name ).canvas )
84
88
end
85
89
function push :getCanvasTable (name )
86
90
for i = 1 , # self .canvases do
@@ -98,7 +102,7 @@ function push:setShader(name, shader)
98
102
end
99
103
100
104
function push :initValues ()
101
- self ._PSCALE = self ._highdpi and love . window . getPixelScale () or 1
105
+ self ._PSCALE = ( not love11 and self ._highdpi ) and getDPI () or 1
102
106
103
107
self ._SCALE = {
104
108
x = self ._RWIDTH / self ._WWIDTH * self ._PSCALE ,
@@ -119,13 +123,8 @@ function push:initValues()
119
123
self ._GHEIGHT = self ._RHEIGHT * self ._PSCALE - self ._OFFSET .y * 2
120
124
end
121
125
122
- --[[ DEPRECATED ]] --
123
126
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
127
+ self ._drawFunctions [operation ](self , shader )
129
128
end
130
129
131
130
function push :start ()
@@ -140,32 +139,73 @@ function push:start()
140
139
end
141
140
end
142
141
142
+ function push :applyShaders (canvas , shaders )
143
+ local _shader = love .graphics .getShader ()
144
+ if # shaders <= 1 then
145
+ love .graphics .setShader (shaders [1 ])
146
+ love .graphics .draw (canvas )
147
+ else
148
+ local _canvas = love .graphics .getCanvas ()
149
+
150
+ local _tmp = self :getCanvasTable (" _tmp" )
151
+ if not _tmp then -- create temp canvas only if needed
152
+ self :addCanvas ({ name = " _tmp" , private = true , shader = nil })
153
+ _tmp = self :getCanvasTable (" _tmp" )
154
+ end
155
+
156
+ love .graphics .push ()
157
+ love .graphics .origin ()
158
+ local outputCanvas
159
+ for i = 1 , # shaders do
160
+ local inputCanvas = i % 2 == 1 and canvas or _tmp .canvas
161
+ outputCanvas = i % 2 == 0 and canvas or _tmp .canvas
162
+ love .graphics .setCanvas (outputCanvas )
163
+ love .graphics .clear ()
164
+ love .graphics .setShader (shaders [i ])
165
+ love .graphics .draw (inputCanvas )
166
+ love .graphics .setCanvas (inputCanvas )
167
+ end
168
+ love .graphics .pop ()
169
+
170
+ love .graphics .setCanvas (_canvas )
171
+ love .graphics .draw (outputCanvas )
172
+ end
173
+ love .graphics .setShader (_shader )
174
+ end
175
+
143
176
function push :finish (shader )
144
177
love .graphics .setBackgroundColor (unpack (self ._borderColor ))
145
178
if self ._canvas then
146
179
local _render = self :getCanvasTable (" _render" )
147
180
148
181
love .graphics .pop ()
149
182
150
- love .graphics .setColor (255 , 255 , 255 )
183
+ local white = love11 and 1 or 255
184
+ love .graphics .setColor (white , white , white )
151
185
152
186
-- draw canvas
153
187
love .graphics .setCanvas (_render .canvas )
154
- for i = 1 , # self .canvases - 1 do -- do not draw _render yet
188
+ for i = 1 , # self .canvases do -- do not draw _render yet
155
189
local _table = self .canvases [i ]
156
- love .graphics .setShader (_table .shader )
157
- love .graphics .draw (_table .canvas )
190
+ if not _table .private then
191
+ local _canvas = _table .canvas
192
+ local _shader = _table .shader
193
+ self :applyShaders (_canvas , type (_shader ) == " table" and _shader or { _shader })
194
+ end
158
195
end
159
196
love .graphics .setCanvas ()
160
-
197
+
161
198
-- draw render
162
199
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 )
200
+ local shader = shader or _render .shader
201
+ love .graphics .push ()
202
+ love .graphics .scale (self ._SCALE .x , self ._SCALE .y )
203
+ self :applyShaders (_render .canvas , type (shader ) == " table" and shader or { shader })
204
+ love .graphics .pop ()
165
205
166
206
-- clear canvas
167
207
for i = 1 , # self .canvases do
168
- love .graphics .setCanvas ( self .canvases [i ].canvas )
208
+ love .graphics .setCanvas (self .canvases [i ].canvas )
169
209
love .graphics .clear ()
170
210
end
171
211
193
233
194
234
-- doesn't work - TODO
195
235
function push :toReal (x , y )
196
- return x + self ._OFFSET .x , y + self ._OFFSET .y
236
+ return x + self ._OFFSET .x , y + self ._OFFSET .y
197
237
end
198
238
199
239
function push :switchFullscreen (winw , winh )
@@ -218,8 +258,7 @@ function push:switchFullscreen(winw, winh)
218
258
end
219
259
220
260
function push :resize (w , h )
221
- local pixelScale = love .window .getPixelScale ()
222
- if self ._highdpi then w , h = w / pixelScale , h / pixelScale end
261
+ if self ._highdpi then w , h = w / self ._PSCALE , h / self ._PSCALE end
223
262
self ._RWIDTH = w
224
263
self ._RHEIGHT = h
225
264
self :initValues ()
0 commit comments