-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.lua
386 lines (296 loc) · 10.7 KB
/
test1.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
--[[
Tests QuadSlice features.
--]]
--love.window.setVSync(0)
love.keyboard.setKeyRepeat(true)
local quadSlice = require("quad_slice")
local image1 = love.graphics.newImage("demo_res/9s_test1.png")
image1:setFilter("nearest", "nearest")
local slice1 = quadSlice.newSlice(8,8, 3,3, 2,2, 3,3, image1:getDimensions())
local image2 = love.graphics.newImage("demo_res/9s_test2.png")
image2:setFilter("linear", "linear")
local slice2 = quadSlice.newSlice(8,8, 3,3, 2,2, 3,3, image2:getDimensions())
local image3 = love.graphics.newImage("demo_res/9s_test3.png")
image3:setFilter("nearest", "nearest")
local slice3 = quadSlice.newSlice(0,0, 32,32, 8,8, 32,32, image3:getDimensions())
slice3:setTileEnabled(5, false) -- hide center tile
local image_mir = love.graphics.newImage("demo_res/9s_mir.png")
image_mir:setFilter("nearest", "nearest")
image_mir:setWrap("mirroredrepeat", "mirroredrepeat") -- <- Important
local image_par = love.graphics.newImage("demo_res/9s_partial.png")
local slice_mir_none = quadSlice.newSlice(0,0, 32,32, 32,32, 32,32, image_mir:getDimensions())
local slice_mir_h = quadSlice.newSlice(0,0, 32,32, 32,32, 32,32, image_mir:getDimensions())
slice_mir_h:setMirroring(true, false)
local slice_mir_v = quadSlice.newSlice(0,0, 32,32, 32,32, 32,32, image_mir:getDimensions())
slice_mir_v:setMirroring(false, true)
local slice_mir_hv = quadSlice.newSlice(0,0, 32,32, 32,32, 32,32, image_mir:getDimensions())
slice_mir_hv:setMirroring(true, true)
-- 1.2.1: Test unmirroring
--[[
do
local t = true
slice_mir_h:setMirroring()
slice_mir_v:setMirroring()
slice_mir_hv:setMirroring()
end
--]]
local batch1 = love.graphics.newSpriteBatch(image_mir) -- tests batch:add()
local batch2 = love.graphics.newSpriteBatch(image_mir)
local batch3 = love.graphics.newSpriteBatch(image_mir)
local page = 1
local n_pages = 5
-- Page 3 setup
local p3_start = 1
local partial_slices = {}
-- Make Slices with every combination of zero-sized columns and rows.
for w1 = 0, 1 do
for w2 = 0, 1 do
for w3 = 0, 1 do
for h1 = 0, 1 do
for h2 = 0, 1 do
for h3 = 0, 1 do
table.insert(partial_slices,
quadSlice.newSlice(
0,0,
w1*32,h1*32,
w2*32,h2*32,
w3*32,h3*32,
image_par:getDimensions()
)
)
end
end
end
end
end
end
-- Page 4 setup
local slice_enable = quadSlice.newSlice(0,0, 32,32, 32,32, 32,32, image_mir:getDimensions())
local p4_timer = 0
local p4_timer_max = 0.25
local p4_tick = 0
-- Page 5 setup
local p5_index = 1
local p5_draw_funcs = {}
if quadSlice.draw_functions then
for k, v in pairs(quadSlice.draw_functions) do
table.insert(p5_draw_funcs, {id = k, fn = v})
end
table.sort(p5_draw_funcs, function(a, b) return a.id < b.id end)
end
function love.keypressed(kc, sc, rep)
local num = tonumber(sc)
if sc == "escape" then
love.event.quit()
return
-- Page selection.
elseif num and num >= 1 and num <= n_pages then
page = num
-- Toggle VSync.
elseif sc == "0" then
love.window.setVSync(love.window.getVSync() == 0 and 1 or 0)
end
-- Step through page 3 partial slices
if page == 3 then
if sc == "left" then
p3_start = math.max(1, p3_start - 1)
elseif sc == "right" then
p3_start = math.min(#partial_slices, p3_start + 1)
end
-- Step through page 5 aux draw functions
elseif page == 5 then
if #p5_draw_funcs > 0 then
if sc == "up" then
p5_index = math.max(1, p5_index - 1)
elseif sc == "down" then
p5_index = math.min(#p5_draw_funcs, p5_index + 1)
end
end
end
end
function love.update(dt)
if page == 4 then
p4_timer = p4_timer + dt
end
end
-- Helper for printing text to the display.
local function label(x, y, text)
love.graphics.push("all")
love.graphics.origin()
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print(text, x, y)
love.graphics.pop()
end
function love.draw()
local demo_time = love.timer.getTime()
local demo_w, demo_h
local mx, my = love.mouse.getPosition()
if page == 1 then
love.graphics.push("all")
label(8, 0, "scaled, nearest")
demo_w = math.floor(32*2 + math.cos(demo_time) * 32)
demo_h = math.floor(32*2 + math.sin(demo_time / 1.1) * 32)
love.graphics.scale(3, 3)
slice1:draw(image1, 8, 8, demo_w, demo_h)
love.graphics.pop()
love.graphics.push("all")
label(384, 0, "bilinear")
demo_w = math.floor(128*2 + math.cos(demo_time) * 64)
demo_h = math.floor(128*2 + math.sin(demo_time / 1.1) * 64)
slice2:draw(image2, 384, 24, demo_w, demo_h)
love.graphics.pop()
love.graphics.push("all")
label(mx - 24, my - 24, "hollow center tile")
demo_w = math.floor(128*2 + math.cos(demo_time + math.pi/4) * 64)
demo_h = math.floor(128*2 + math.sin((demo_time / 1.1) + math.pi/4) * 64)
love.graphics.setColor(0, 0, 1, 0.5)
love.graphics.setColor(1, 1, 1, 1)
slice3:draw(image3, mx - 64, my - 64, demo_w, demo_h)
love.graphics.pop()
love.graphics.push("all")
label(8, 400, "mirrored tile tests")
label(8 + 196*0, 424, "normal")
label(8 + 196*1, 424, "horizontal")
label(8 + 196*2, 424, "vertical")
label(8 + 196*3, 424, "horizontal+vertical")
demo_w = math.floor(96 + math.cos(demo_time + math.pi/4) * 32)
demo_h = math.floor(96 + math.sin((demo_time / 1.1) + math.pi/4) * 32)
slice_mir_none:draw(image_mir, 8 + 196*0, 448, demo_w, demo_h)
slice_mir_h:draw(image_mir, 8 + 196*1, 448, demo_w, demo_h)
slice_mir_v:draw(image_mir, 8 + 196*2, 448, demo_w, demo_h)
slice_mir_hv:draw(image_mir, 8 + 196*3, 448, demo_w, demo_h)
love.graphics.pop()
-- Spritebatch tests.
elseif page == 2 then
demo_w = math.floor(96 + math.cos(demo_time + math.pi/4) * 32)
demo_h = math.floor(96 + math.sin((demo_time / 1.1) + math.pi/4) * 32)
batch1:clear()
slice_mir_none:batchAdd(batch1, 0, 0, demo_w, demo_h)
label(mx, my - 24, "batch:clear(); batch:add()")
love.graphics.draw(batch1, mx, my)
-- Zero-column, zero-row tests
elseif page == 3 then
love.graphics.printf("Press left/right to scroll.", 0, 4, love.graphics.getWidth(), "center")
love.graphics.translate(0, 32)
demo_w = math.floor(96 + math.cos(demo_time + math.pi/4) * 32)
demo_h = math.floor(96 + math.sin((demo_time / 1.1) + math.pi/4) * 32)
local xx, yy = 0, 0
local y_dash = math.floor(love.graphics.getFont():getHeight() * 2.5)
local x_add, y_add = 48, 96
for i = p3_start, #partial_slices do
local slice = partial_slices[i]
slice:draw(image_par, xx, yy + y_dash, demo_w, demo_h)
love.graphics.print(
"w1: " .. slice.w1 ..
", w2: " .. slice.w2 ..
", w3: " .. slice.w3 ..
"\nh1: " .. slice.h1 ..
", h2: " .. slice.h2 ..
", h3: " .. slice.h3,
xx, yy
)
xx = xx + x_add + math.floor(image_par:getWidth())
if xx > love.graphics.getWidth() then
xx = 0
yy = yy + y_add + math.floor(image_par:getHeight())
end
if yy > love.graphics.getHeight() then
break
end
end
love.graphics.origin()
-- Enabling and disabling individual tiles.
elseif page == 4 then
local draw_w, draw_h = 160, 160
local mid_x = (love.graphics.getWidth() - draw_w) / 2
local mid_y = (love.graphics.getHeight() - draw_h) / 2
-- First, draw a dim version of the slice with all tiles enabled so that it isn't so jarring.
love.graphics.setColor(1, 1, 1, 0.34)
slice_enable:resetTiles()
slice_enable:draw(image_mir, mid_x, mid_y, draw_w, draw_h)
love.graphics.setColor(1, 1, 1, 1)
-- Set each tile based on elapsed time.
slice_enable:setTileEnabled(1, p4_tick % 2 == 1)
slice_enable:setTileEnabled(2, math.floor(p4_tick / 2) % 2 == 1)
slice_enable:setTileEnabled(3, math.floor(p4_tick / 4) % 2 == 1)
slice_enable:setTileEnabled(4, math.floor(p4_tick / 8) % 2 == 1)
slice_enable:setTileEnabled(5, math.floor(p4_tick / 16) % 2 == 1)
slice_enable:setTileEnabled(6, math.floor(p4_tick / 32) % 2 == 1)
slice_enable:setTileEnabled(7, math.floor(p4_tick / 64) % 2 == 1)
slice_enable:setTileEnabled(8, math.floor(p4_tick / 128) % 2 == 1)
slice_enable:setTileEnabled(9, math.floor(p4_tick / 256) % 2 == 1)
slice_enable:draw(image_mir, mid_x, mid_y, draw_w, draw_h)
-- (p4_timer is incremented in love.update().)
if p4_timer >= p4_timer_max then
p4_timer = p4_timer - p4_timer_max
p4_tick = p4_tick + 1
p4_tick = p4_tick % 512
end
-- Test alternative draw functions
elseif page == 5 then
if #p5_draw_funcs == 0 then
love.graphics.print("[!] quadSlice.draw_functions is empty, renamed, or missing.")
else
love.graphics.printf("Press up/down to select a draw function.", 0, 4, love.graphics.getWidth(), "center")
-- For demo purposes, we will change the slice's draw function. The slice is shared
-- with other test pages, so we need to change it back after drawing.
local old_draw_fn = slice_enable.drawFromParams
local draw_w, draw_h = 160, 160
local mid_x = (love.graphics.getWidth() - draw_w) / 2
local mid_y = (love.graphics.getHeight() - draw_h) / 2
love.graphics.setColor(1, 1, 1, 0.34)
slice_enable.drawFromParams = nil
slice_enable:resetTiles()
slice_enable:draw(image_mir, mid_x, mid_y, draw_w, draw_h)
love.graphics.setColor(1, 1, 1, 1)
local draw_fn_tbl = p5_draw_funcs[p5_index]
slice_enable.drawFromParams = draw_fn_tbl.fn
-- Uncomment to run a simple stress test.
--[[
love.graphics.setColor(1,1,1,0.1)
for i = 1, 1000 do
slice_enable:draw(
image_mir,
math.floor(0.5 + mid_x + math.cos(demo_time) * i/4),
math.floor(0.5 + mid_y + math.sin(demo_time) * i/5),
draw_w,
draw_h
)
end
--]]
slice_enable:draw(image_mir, mid_x, mid_y, draw_w, draw_h)
local x_right = love.graphics.getWidth() - 160
local yy = 16
local yy_add = math.floor(love.graphics.getFont():getHeight()) * 1.3
for i, fn_tbl in ipairs(p5_draw_funcs) do
if p5_index == i then
love.graphics.setColor(1, 1, 1, 1)
else
love.graphics.setColor(0.75, 0.75, 0.75, 1)
end
love.graphics.print(fn_tbl.id, x_right, yy)
yy = yy + yy_add
-- [XXX] This ad hoc list component doesn't handle scrolling.
end
love.graphics.setColor(1, 1, 1, 1)
slice_enable.drawFromParams = old_draw_fn
end
end
local bottom_dash_y_margin = 6
local bottom_text_h = love.graphics.getFont():getHeight()
local bottom_dash_y = love.graphics.getHeight() - bottom_text_h
love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle(
"fill",
0, bottom_dash_y - bottom_dash_y_margin,
love.graphics.getWidth(), bottom_text_h + bottom_dash_y_margin
)
love.graphics.setColor(1, 1, 1, 1)
love.graphics.print(
"PAGE " .. page .. "/" .. n_pages .. "\t(1: lg.draw, 2: spritebatch, 3: zero column/row, 4: tile enable, 5: aux draw)" ..
"\t0: VSync (" .. tostring(love.window.getVSync() == 1) .. ")",
8,
bottom_dash_y
)
love.graphics.print("FPS: " .. love.timer.getFPS(), love.graphics.getWidth() - 72, bottom_dash_y)
end