Skip to content

Commit e01bc83

Browse files
committed
lua demo
1 parent 5fd2077 commit e01bc83

File tree

4 files changed

+381
-35
lines changed

4 files changed

+381
-35
lines changed

lua_demo.lua

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
--
2+
-- Variables
3+
--
4+
5+
-- global (accessible from other Lua modules)
6+
hello = 'hello'
7+
8+
-- local (accessible only in this scope)
9+
local world = ' world!'
10+
11+
--
12+
-- Functions
13+
--
14+
15+
-- declaring our function
16+
function say(text)
17+
print(text)
18+
end
19+
20+
-- calling our function (note the .. operator to concatenate strings!)
21+
say(hello .. world)
22+
23+
--
24+
-- If statements
25+
--
26+
if world == 'world' then
27+
print('world!')
28+
else
29+
print('hello!')
30+
end
31+
32+
--
33+
-- Loops
34+
--
35+
36+
-- while loop with counter
37+
local i = 10
38+
while i > 0 do
39+
-- note the lack of -= and +=
40+
i = i - 1
41+
print(i)
42+
end
43+
44+
-- for loop, decrements from 10 to 1
45+
for j = 10, 1, -1 do
46+
print(j)
47+
end
48+
49+
-- repeat (do-while) loop
50+
i = 10
51+
repeat
52+
i = i - 1
53+
print(i)
54+
until i == 0
55+
56+
--
57+
-- Tables
58+
--
59+
60+
-- sort of like structs or hash tables in C, and like Python dictionaries
61+
local person = {}
62+
person.name = 'Colton Ogden'
63+
person.age = 26
64+
person.height = 69.5
65+
66+
-- bracket and dot syntax to access table fields
67+
print(person['name'])
68+
print(person.name)
69+
70+
-- iterate through table's key-value pairs and print both of each
71+
for key, value in pairs(person) do
72+
print(key, value)
73+
end

pong-final/main.lua

+5-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ function love.load()
8585
push:setupScreen(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
8686
fullscreen = false,
8787
resizable = true,
88-
vsync = true
88+
vsync = true,
89+
canvas = false
8990
})
9091

9192
-- initialize our player paddles; make them global so that they can be
@@ -300,7 +301,7 @@ end
300301
]]
301302
function love.draw()
302303
-- begin drawing with push, in our virtual resolution
303-
push:apply('start')
304+
push:start()
304305

305306
love.graphics.clear(40, 45, 52, 255)
306307

@@ -338,7 +339,7 @@ function love.draw()
338339
displayFPS()
339340

340341
-- end our drawing to push
341-
push:apply('end')
342+
push:finish()
342343
end
343344

344345
--[[
@@ -361,4 +362,5 @@ function displayFPS()
361362
love.graphics.setFont(smallFont)
362363
love.graphics.setColor(0, 255, 0, 255)
363364
love.graphics.print('FPS: ' .. tostring(love.timer.getFPS()), 10, 10)
365+
love.graphics.setColor(255, 255, 255, 255)
364366
end

pong-final/push.lua

+71-32
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
-- push.lua v0.2
1+
-- push.lua v0.3
22

3-
-- Copyright (c) 2017 Ulysse Ramage
3+
-- Copyright (c) 2018 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+
811
local push = {
912

1013
defaults = {
@@ -18,9 +21,6 @@ local push = {
1821
}
1922
setmetatable(push, push)
2023

21-
--TODO: rendering resolution?
22-
--TODO: clean up code
23-
2424
function push:applySettings(settings)
2525
for k, v in pairs(settings) do
2626
self["_" .. k] = v
@@ -39,11 +39,11 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
3939
self:applySettings(self.defaults) --set defaults first
4040
self:applySettings(settings) --then fill with custom settings
4141

42-
love.window.setMode( self._RWIDTH, self._RHEIGHT, {
42+
love.window.setMode(self._RWIDTH, self._RHEIGHT, {
4343
fullscreen = self._fullscreen,
4444
resizable = self._resizable,
4545
highdpi = self._highdpi
46-
} )
46+
})
4747

4848
self:initValues()
4949

@@ -62,25 +62,29 @@ function push:setupScreen(WWIDTH, WHEIGHT, RWIDTH, RHEIGHT, settings)
6262
end
6363

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

6767
self._canvas = true
6868
self.canvases = {}
6969

7070
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])
7672
end
7773

7874
return self
7975
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
8084

8185
function push:setCanvas(name)
8286
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)
8488
end
8589
function push:getCanvasTable(name)
8690
for i = 1, #self.canvases do
@@ -98,7 +102,7 @@ function push:setShader(name, shader)
98102
end
99103

100104
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
102106

103107
self._SCALE = {
104108
x = self._RWIDTH/self._WWIDTH * self._PSCALE,
@@ -119,13 +123,8 @@ function push:initValues()
119123
self._GHEIGHT = self._RHEIGHT * self._PSCALE - self._OFFSET.y * 2
120124
end
121125

122-
--[[ DEPRECATED ]]--
123126
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)
129128
end
130129

131130
function push:start()
@@ -140,32 +139,73 @@ function push:start()
140139
end
141140
end
142141

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+
143176
function push:finish(shader)
144177
love.graphics.setBackgroundColor(unpack(self._borderColor))
145178
if self._canvas then
146179
local _render = self:getCanvasTable("_render")
147180

148181
love.graphics.pop()
149182

150-
love.graphics.setColor(255, 255, 255)
183+
local white = love11 and 1 or 255
184+
love.graphics.setColor(white, white, white)
151185

152186
--draw canvas
153187
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
155189
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
158195
end
159196
love.graphics.setCanvas()
160-
197+
161198
--draw render
162199
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()
165205

166206
--clear canvas
167207
for i = 1, #self.canvases do
168-
love.graphics.setCanvas( self.canvases[i].canvas )
208+
love.graphics.setCanvas(self.canvases[i].canvas)
169209
love.graphics.clear()
170210
end
171211

@@ -193,7 +233,7 @@ end
193233

194234
--doesn't work - TODO
195235
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
197237
end
198238

199239
function push:switchFullscreen(winw, winh)
@@ -218,8 +258,7 @@ function push:switchFullscreen(winw, winh)
218258
end
219259

220260
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
223262
self._RWIDTH = w
224263
self._RHEIGHT = h
225264
self:initValues()

0 commit comments

Comments
 (0)