-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
235 lines (188 loc) · 6.4 KB
/
main.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
LoveLightning = require "lovelightning"
baton = require "lib/baton/baton"
class = require "lib/middleclass/middleclass"
profi = require "profi"
-------------------------------------------------------------------------------
local DEBUG = true
local run_profiler = false
-------------------------------------------------------------------------------
local controls = baton.new({
controls = {
primary = {'mouse:1'},
secondary = {'mouse:2'},
fire = {'key:space'},
generate = {'key:lshift'},
increase = {'key:kp+','key:+','key:='},
decrease = {'key:kp-','key:-'},
capture = {'key:v'},
border_toggle = {'key:b'},
continuous_toggle = {'key:c'},
quit = {'key:q'},
}
})
-------------------------------------------------------------------------------
local Target = class("Target")
local SEC_TARG_DEF_COLOR = {255,50,50,127}
local target_id_counter = 0
function Target:initialize(vector)
target_id_counter = target_id_counter + 1
self.id = target_id_counter
self.x = vector.x
self.y = vector.y
self.r, self.g, self.b, self.a = unpack(SEC_TARG_DEF_COLOR)
end
function Target:setColor(r, g, b, a)
self.r = r
self.g = g
self.b = b
self.a = a
end
function Target:draw()
love.graphics.setColor(self.r,self.g,self.b,self.a)
love.graphics.circle('fill', self.x, self.y, 3)
love.graphics.circle('line', self.x, self.y, 10)
love.graphics.setColor(255,255,255,255)
end
-------------------------------------------------------------------------------
local DEFAULT_MARGIN = 20
local margin = DEFAULT_MARGIN
local source_targ = Target:new({x=margin, y=love.graphics.getHeight()/2})
source_targ:setColor(0,255,0)
local prim_targ = Target:new({x=love.graphics.getWidth()-margin,
y=love.graphics.getHeight()/2})
prim_targ:setColor(255,0,0)
local sec_targs = {}
local n_sec_targs = 20
local continuous = false
local function generate_sec_targs()
sec_targs = {}
for _ = 1 , n_sec_targs do
local tx = margin+math.random()*(love.graphics.getWidth()-margin*2)
local ty = margin+math.random()*(love.graphics.getHeight()-margin*2)
table.insert(sec_targs,Target:new({x=tx,y=ty}))
end
end
function love.load(args)
print(unpack(args))
for _, arg in ipairs(args) do
if arg == '--profiler' then
run_profiler = true
end
end
bolt = LoveLightning:new(255,255,255)
bolt:setSource(source_targ)
bolt:setPrimaryTarget(prim_targ)
for _ = 1 , n_sec_targs do
local tx = margin+math.random()*(love.graphics.getWidth()-margin*2)
local ty = margin+math.random()*(love.graphics.getHeight()-margin*2)
table.insert(sec_targs,Target:new({x=tx,y=ty}))
end
print("bolt vertex pool size: "..#bolt.vpool)
if run_profiler then
profi:start()
end
end
function love.quit()
if run_profiler then
profi:stop()
profi:writeReport('profile.txt')
end
end
function love.resize(w, h)
source_targ.x = margin
source_targ.y = h/2
prim_targ.x = w-margin
prim_targ.y = h/2
bolt:setSource(source_targ)
bolt:setPrimaryTarget(prim_targ)
bolt:setForkTargets()
bolt:clear()
generate_sec_targs()
end
local create_time = 0
function love.update(dt)
local st = love.timer.getTime()
local force_gen_sec_targs = false
controls:update()
if controls:pressed('quit') then
love.event.push('quit')
end
if controls:pressed('continuous_toggle') then
continuous = not continuous
end
if controls:pressed('border_toggle') then
if margin == DEFAULT_MARGIN then
margin = 0
else
margin = DEFAULT_MARGIN
end
love.resize(love.graphics.getWidth(),love.graphics.getHeight())
end
if controls:pressed('increase') then
n_sec_targs = n_sec_targs + 1
force_gen_sec_targs = true
end
if controls:pressed('decrease') then
n_sec_targs = math.max(0, n_sec_targs - 1)
force_gen_sec_targs = true
end
if controls:pressed('generate') or force_gen_sec_targs then
generate_sec_targs()
force_gen_sec_targs = true
end
if controls:pressed('fire') or force_gen_sec_targs or continuous then
-- reset the color on the targets
for _, t in ipairs(sec_targs) do
t:setColor(unpack(SEC_TARG_DEF_COLOR))
end
local st = love.timer.getTime()
bolt:setForkTargets(sec_targs)
bolt:generate(function(t,level)
t:setColor(255,255,25,255)
-- print("Target "..t.id.." hit!")
end)
create_time = love.timer.getTime() - st
print("")
print("bolt iterations: "..bolt.last_iteration_count)
print("bolt vertice count: "..bolt:verticeCount())
print("bolt generate time: "..create_time*1000)
print("bolt vertex pool size: "..#bolt.vpool)
end
bolt:update(dt)
if bolt.canvas and controls:pressed('capture') then
love.filesystem.remove('capture.png')
bolt.canvas:newImageData():encode('png', 'capture.png')
end
end
function love.draw()
local st = love.timer.getTime()
source_targ:draw()
prim_targ:draw()
for _, t in ipairs(sec_targs) do
t:draw()
end
bolt:draw()
love.graphics.setColor(50, 200, 100, 200)
love.graphics.print(
"V: Capture Image || B: Toggle Border",
20, love.graphics.getHeight()-80)
love.graphics.print(
"plus: Increase Targets || minus: Decrease Targets",
20, love.graphics.getHeight()-60)
love.graphics.print(
"Space: Generate Lightning || L Shift: Generate Targets",
20, love.graphics.getHeight()-40)
love.graphics.print(
"Q: Quit || C: Continuous",
20, love.graphics.getHeight()-20)
-- debug
love.graphics.setFont(love.graphics.newFont())
love.graphics.setColor(50, 200, 100, 200)
love.graphics.print("FPS: "..love.timer.getFPS(), 20, 20)
local dc = love.graphics.getStats()
love.graphics.print("draws: "..dc.drawcalls, 20, 40)
love.graphics.print("switches: "..dc.canvasswitches.." / "..dc.shaderswitches, 20, 60)
love.graphics.print(math.floor((love.timer.getTime() - st) * 1000000) / 1000 .. " ms", 20, 80)
love.graphics.print(math.floor(create_time * 1000000) / 1000 .. " ms", 20, 100)
love.graphics.setColor(255,255,255,255)
end