-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam11.lua
215 lines (189 loc) · 5.11 KB
/
cam11.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
-- Camera library using the new Transform features in love2d 11.0+
--
-- Copyright © 2019 Pedro Gimeno Fortea
--
-- You can do whatever you want with this software, under the sole condition
-- that this notice and any copyright notices are preserved. It is offered
-- with no warrany, not even implied.
-- Cache some functions into locals
local newTransform = love.math.newTransform
local replaceTransform, applyTransform, push, pop, getWidth, getHeight
local getScissor, intersectScissor, setScissor
local xfSetXf, xfGetMatrix
do
local lg = love.graphics
replaceTransform = lg.replaceTransform
applyTransform = lg.applyTransform
push = lg.push
pop = lg.pop
getWidth = lg.getWidth
getHeight = lg.getHeight
getScissor = lg.getScissor
intersectScissor = lg.intersectScissor
setScissor = lg.setScissor
local Xf = debug.getregistry().Transform
xfSetXf = Xf.setTransformation
xfGetMatrix = Xf.getMatrix
end
local Camera = {}
local CameraClassMT = {__call = function (c, ...) return c.new(...) end}
local CameraInstanceMT = {__index = Camera}
local function lazyUpdateXf(self)
if self.dirty then
self.dirty = false
local vp = self.vp
self.matdirty = true
self.invmatdirty = true
return xfSetXf(self.xf,
vp[1] + (vp[3] or getWidth()) * vp[5],
vp[2] + (vp[4] or getHeight()) * vp[6],
self.angle, self.zoom, self.zoom, self.x, self.y)
end
end
local function lazyUpdateMat(self)
lazyUpdateXf(self)
if self.matdirty then
self.matdirty = false
local mat = self.mat
local t
mat[1], mat[2], t, mat[5], mat[3], mat[4], t, mat[6] = xfGetMatrix(self.xf)
end
end
local function lazyUpdateInvMat(self)
lazyUpdateMat(self)
if self.invmatdirty then
self.invmatdirty = false
local imat = self.invmat
local mat = self.mat
local a11, a12, a21, a22 = mat[1], mat[2], mat[3], mat[4]
local det = a11*a22 - a12*a21
imat[1], imat[2], imat[3], imat[4] = a22/det, a12/-det, a21/-det, a11/det
imat[5] = mat[5]
imat[6] = mat[6]
end
end
function Camera:setDirty(dirty)
self.dirty = dirty ~= false and true or false
end
function Camera:attach(clip)
lazyUpdateXf(self)
push()
local vp = self.vp
if clip or clip == nil and (vp[1] ~= 0 or vp[2] ~= 0 or vp[3] or vp[4]) then
local x, y, w, h = getScissor()
local scissor = self.scissor
scissor[1] = x
scissor[2] = y
scissor[3] = w
scissor[4] = h
intersectScissor(vp[1], vp[2], vp[3] or getWidth(), vp[4] or getHeight())
end
return replaceTransform(self.xf)
end
function Camera:detach()
local scissor = self.scissor
if scissor[1] ~= false then
setScissor(scissor[1], scissor[2], scissor[3], scissor[4])
scissor[1] = false
end
return pop()
end
function Camera:setPos(x, y)
self.dirty = self.x ~= x or self.y ~= y or self.dirty
self.x = x
self.y = y
end
function Camera:setZoom(zoom)
self.dirty = self.zoom ~= zoom or self.dirty
self.zoom = zoom
end
function Camera:setAngle(angle)
self.dirty = self.angle ~= angle or self.dirty
self.angle = angle
end
function Camera:setViewport(x, y, w, h, cx, cy)
x, y = x or 0, y or 0
w, h = w or false, h or false
cx, cy = cx or 0.5, cy or 0.5
if x ~= self.vp[1] or y ~= self.vp[2] or w ~= self.vp[3] or h ~= self.vp[4]
or cx ~= self.vp[5] or cy ~= self.vp[6]
then
self.dirty = true
end
local vp = self.vp
vp[1] = x
vp[2] = y
vp[3] = w
vp[4] = h
vp[5] = cx
vp[6] = cy
end
function Camera:toScreen(x, y)
lazyUpdateMat(self)
local mat = self.mat
return mat[1] * x + mat[2] * y + mat[5], mat[3] * x + mat[4] * y + mat[6]
end
function Camera:toWorld(x, y)
lazyUpdateInvMat(self)
local imat = self.invmat
x = x - imat[5]
y = y - imat[6]
return imat[1] * x + imat[2] * y, imat[3] * x + imat[4] * y
end
function Camera:getTransform()
lazyUpdateXf(self)
return self.xf
end
function Camera:getPos()
return self.x, self.y
end
function Camera:getX()
return self.x
end
function Camera:getY()
return self.y
end
function Camera:getZoom()
return self.zoom
end
function Camera:getAngle()
return self.angle
end
function Camera:getViewport()
local vp = self.vp
return vp[1], vp[2], vp[3], vp[4], vp[5], vp[6]
end
function Camera:getVPTopLeft()
local vp = self.vp
return vp[1], vp[2]
end
function Camera:getVPBottomRight()
local vp = self.vp
return vp[1] + (vp[3] or getWidth()), vp[2] + (vp[4] or getHeight())
end
function Camera:getVPFocusPoint()
local vp = self.vp
return vp[1] + (vp[3] or getWidth()) * vp[5],
vp[2] + (vp[4] or getHeight()) * vp[6]
end
function Camera.new(x, y, zoom, angle, vpx, vpy, vpw, vph, cx, cy)
vpx, vpy = vpx or 0, vpy or 0
vpw, vph = vpw or false, vph or false
cx, cy = cx or 0.5, cy or 0.5
local self = {
x = x or 0;
y = y or 0;
zoom = zoom or 1;
angle = angle or 0;
vp = {vpx, vpy, vpw, vph, cx, cy};
xf = newTransform();
dirty = true;
matdirty = true;
invmatdirty = true;
scissor = {false,false,false,false};
mat = {0, 0, 0, 0, 0, 0};
invmat = {0, 0, 0, 0, 0, 0};
}
return setmetatable(self, CameraInstanceMT)
end
return setmetatable(Camera, CameraClassMT)