-
Notifications
You must be signed in to change notification settings - Fork 0
/
readout.go
690 lines (636 loc) · 13 KB
/
readout.go
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
package main
import (
"bufio"
"image"
"image/color"
"image/draw"
_ "image/png"
"io"
"os"
"strings"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func checkF(f func() error) {
check(f())
}
const (
key_z = 1 << iota
key_x
key_c
key_return
key_shift
key_ctrl
key_up
key_down
key_left
key_right
key_a
key_d
key_i
key_l
key_n
key_o
key_r
key_s
key_u
key_space
)
var mask = loadPNG("tas-mask.png")
var invMask = invertMask(mask)
var splash1 = removeBlack(loadPNG("tas-splash-1.png"))
var splash2 = removeBlack(loadPNG("tas-splash-2.png"))
func loadPNG(name string) *image.RGBA {
f, err := os.Open(name)
check(err)
defer checkF(f.Close)
img, _, err := image.Decode(f)
check(err)
// Ensure the image has premultiplied alpha and normalized bounds.
rect := img.Bounds()
min := rect.Min
rect = rect.Sub(rect.Min)
rgba := image.NewRGBA(rect)
draw.Draw(rgba, rect, img, min, draw.Src)
return rgba
}
func invertMask(mask *image.RGBA) *image.Alpha {
inv := image.NewAlpha(mask.Rect)
for i := range inv.Pix {
inv.Pix[i] = ^mask.Pix[3+4*i]
}
return inv
}
func isBlack(c color.RGBA) bool {
return c.R == 0 && c.G == 0 && c.B == 0
}
func removeBlack(src *image.RGBA) *image.RGBA {
dst := image.NewRGBA(src.Rect)
copy(dst.Pix, src.Pix)
for y := dst.Rect.Min.Y; y < dst.Rect.Max.Y; y++ {
for x := dst.Rect.Min.X; x < dst.Rect.Max.X; x++ {
allBlack := true
for dy := -1; allBlack && dy <= 1; dy++ {
for dx := -1; allBlack && dx <= 1; dx++ {
if !isBlack(dst.RGBAAt(x+dx, y+dy)) {
allBlack = false
}
}
}
if allBlack {
dst.SetRGBA(x, y, color.RGBA{})
}
}
}
return dst
}
func main() {
inputs, err := os.Open("tas/inputs")
check(err)
defer checkF(inputs.Close)
bits := parse(inputs)
render(bits)
}
func parse(r io.Reader) []uint32 {
s := bufio.NewScanner(r)
defer checkF(s.Err)
var bits []uint32
for s.Scan() {
line := s.Text()
line = strings.Split(line, "|")[1]
line = strings.TrimPrefix(line, "K")
if line == "" {
bits = append(bits, 0)
continue
}
active := strings.Split(line, ":")
var b uint32
for _, key := range active {
switch key {
case "20":
b |= key_space
case "61":
b |= key_a
case "63":
b |= key_c
case "64":
b |= key_d
case "69":
b |= key_i
case "6c":
b |= key_l
case "6e":
b |= key_n
case "6f":
b |= key_o
case "72":
b |= key_r
case "73":
b |= key_s
case "75":
b |= key_u
case "78":
b |= key_x
case "7a":
b |= key_z
case "ff0d":
b |= key_return
case "ff51":
b |= key_left
case "ff52":
b |= key_up
case "ff53":
b |= key_right
case "ff54":
b |= key_down
case "ffe1":
b |= key_shift
case "ffe3":
b |= key_ctrl
default:
panic(key)
}
}
bits = append(bits, b)
}
return bits
}
// Remainder of this file is based on https://web.archive.org/web/20120619043838/http://code.google.com/p/brandon-evans-tas/source/browse/Lua/ddrinput.lua
var buttons = [...]string{
`................
....xxxxxxxx....
...xOOOOOOOOx...
....xxxxxOOOx...
.........OOOx...
........xOOOx...
.......xOOOx....
......xOOOx.....
.....xOOOx......
....xOOOx.......
...xOOOx........
...xOOO.........
...xOOOxxxxx....
...xOOOOOOOOx...
....xxxxxxxx....
................`,
`................
....xxx..xxx....
...xOOO..OOOx...
...xOOO..OOOx...
...xOOO..OOOx...
...xOOOxxOOOx...
....xOOOOOOx....
.....xOOOOx.....
.....xOOOOx.....
....xOOOOOOx....
...xOOOxxOOOx...
...xOOO..OOOx...
...xOOO..OOOx...
...xOOO..OOOx...
....xxx..xxx....
................`,
`................
.....xxxxxx.....
....xOOOOOOx....
...xOOOxxOOOx...
...xOOO..OOOx...
...xOOO..OOOx...
...xOOO..xxx....
...xOOO.........
...xOOO.........
...xOOO..xxx....
...xOOO..OOOx...
...xOOO..OOOx...
...xOOOxxOOOx...
....xOOOOOOx....
.....xxxxxx.....
................`,
`......xxx.......
.....xxOxx......
....xxOxOxx.....
...xxOxxxOxx....
..xxOxx.xxOxx...
.xxOxx...xxOxx..
xxOxxx...xxxOxx.
xOOOOx...xOOOOx.
xxxxOx...xOxxxx.
...xOx...xOx....
...xOx...xOx....
...xOx...xOx....
...xOx...xOx....
...xOxxxxxOx....
...xOOOOOOOx....
...xxxxxxxxx....`,
`...xxxxxxxxx....
...xOOOOOOOx....
...xOxxxxxOx....
...xOx...xOx....
...xOx...xOx....
...xOx...xOx....
...xOx...xOx....
xxxxOx...xOxxxx.
xOOOOx...xOOOOx.
xxOxxx...xxxOxx.
.xxOxx...xxOxx..
..xxOxx.xxOxx...
...xxOxxxOxx....
....xxOxOxx.....
.....xxOxx......
......xxx.......`,
`................
......xxx.......
.....xxOx.......
....xxOOx.......
...xxOxOxxxxxxxx
..xxOxxOOOOOOOOx
.xxOxxxxxxxxxxOx
xxOxx........xOx
xOxx.........xOx
xxOxx........xOx
.xxOxxxxxxxxxxOx
..xxOxxOOOOOOOOx
...xxOxOxxxxxxxx
....xxOOx.......
.....xxOx.......
......xxx.......`,
`................
.......xxx......
.......xOxx.....
.......xOOxx....
xxxxxxxxOxOxx...
xOOOOOOOOxxOxx..
xOxxxxxxxxxxOxx.
xOx........xxOxx
xOx.........xxOx
xOx........xxOxx
xOxxxxxxxxxxOxx.
xOOOOOOOOxxOxx..
xxxxxxxxOxOxx...
.......xOOxx....
.......xOxx.....
.......xxx......`,
`................
.....xxxxxx.....
....xOOOOOOx....
...xOOxxxxOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOxxxxOOx...
...xOOOOOOOOx...
...xOOOxxOOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xxxx..xxxx...`,
`................
...xxxxxxxx.....
...xOOOOOOOx....
...xOOxxxxOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOOxxOOxx...
...xOOOOOOOx....
...xxxxxxxx.....`,
`................
....xxxxxxx.....
....xOOOOOx.....
....xxOOOxx.....
.....xOOOx......
.....xOOOx......
.....xOOOx......
.....xOOOx......
.....xOOOx......
.....xOOOx......
.....xOOOx......
.....xOOOx......
.....xOOOx......
....xxOOOxx.....
....xOOOOOx.....
....xxxxxxx.....`,
`................
...xxxx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOx.........
...xOOOxxxxxx...
...xOOOOOOOOx...
...xxxxxxxxxx...`,
`................
...xxxx..xxxx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOOx.xOOx...
...xOOOO.xOOx...
...xOOOOOOOOx...
...xOOOxOOOOx...
...xOOx.OOOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xxxx..xxxx...`,
`................
.....xxxxxx.....
....xOOOOOOx....
...xOOxxxxOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOxxxxOOx...
...xxOOxxOOxx...
....xOOOOOOx....
.....xxxxxx.....`,
`................
...xxxxxxxx.....
...xOOOOOOOx....
...xOOxxxxOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOOxxOOxx...
...xOOOOOOOx....
...xOOxxxxOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xxxx..xxxx...`,
`................
.....xxxxxx.....
....xOOOOOOx....
...xOOxxxxOOx...
...xOOx..xOOx...
...xOOx..xOOx...
....xOOOx.xx....
.....xOOxx......
......xOOxx.....
.......xOOOx....
...xxxx.xOOxx...
...xOOx..xOOx...
...xOOx..xOOx...
...xxOOxxOOxx...
....xOOOOOOx....
.....xxxxxx.....`,
`................
...xxxx..xxxx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xOOx..xOOx...
...xxOOxxOOxx...
....xOOOOOOx....
.....xxxxxx.....`,
`................
................
................
................
................
................
................
................
................
...xxx....xxx...
..xOOOxxxxOOOx..
..xOxOOOOOOxOx..
..xOxxxxxxxxOx..
..xOOOOOOOOOOx..
...xxxxxxxxxx...
................`,
}
var buttonMasks = [17][2]*image.Alpha{
makeMask(buttons[0]),
makeMask(buttons[1]),
makeMask(buttons[2]),
makeMask(buttons[3]),
makeMask(buttons[4]),
makeMask(buttons[5]),
makeMask(buttons[6]),
makeMask(buttons[7]),
makeMask(buttons[8]),
makeMask(buttons[9]),
makeMask(buttons[10]),
makeMask(buttons[11]),
makeMask(buttons[12]),
makeMask(buttons[13]),
makeMask(buttons[14]),
makeMask(buttons[15]),
makeMask(buttons[16]),
}
func makeMask(str string) [2]*image.Alpha {
oimg := image.NewAlpha(image.Rect(0, 0, buttonSize, buttonSize))
ximg := image.NewAlpha(image.Rect(0, 0, buttonSize, buttonSize))
i, j := 0, 0
for y := 0; y < buttonSize; y++ {
for x := 0; x < buttonSize; x++ {
switch str[i] {
case 'O':
oimg.Pix[j] = 255
case 'x':
ximg.Pix[j] = 255
}
i++
j++
}
i++
}
return [2]*image.Alpha{oimg, ximg}
}
const (
preButtonCount = 3
buttonCount = 7
extraButtonCount = len(buttons) - buttonCount
width, height = 640, 120
// button size in pixels (square)
buttonSize = 16
// pixels per frame moved by inputs.
step = 3
// time in frames to go through the rainbow; must be divisible by 6
period = 600
// glow time in frames
glowHold = 10
// buttons are pressed when they reach this x position
target = (width - buttonSize) / 2
// top of first button
top = (height - buttonCount*buttonSize) / 2
// number of inputs on the screen (in both directions)
display = (target + buttonSize + step) / step
)
func render(bits []uint32) {
img := image.NewRGBA(image.Rect(0, 0, width, height))
minLeft := width - mask.Rect.Max.X
maxLeft := target + 12*30*step
left := maxLeft
splashStep := 0
for trueFrame := range bits {
draw.Draw(img, img.Rect, image.Black, image.ZP, draw.Src)
for i := -preButtonCount; i < buttonCount+extraButtonCount; i++ {
var (
// detect holds by going forward, then draw the symbols backward
backString [display*2 + 1]bool
glowLevel [display*2 + 1]int
any bool
)
isPre := i < 0
btn := i
btn2 := btn + preButtonCount
if isPre {
btn = btn2
}
xoff := 0
y := top + buttonSize*btn
if btn >= buttonCount {
y = (height - buttonSize) * (btn - buttonCount) / (extraButtonCount - 1)
xoff = (btn%2)*buttonSize - buttonSize/2
}
for i := -display; i <= display; i++ {
offset := display + i
refFrame := trueFrame + i
if refFrame < 0 || refFrame >= len(bits) {
backString[offset] = false
glowLevel[offset] = 0
} else {
isHeld := bits[refFrame]&(1<<btn2) != 0
isGlow := isHeld || (btn < preButtonCount && bits[refFrame]&(1<<btn) != 0)
if isHeld {
any = true
backString[offset] = true
}
if isGlow {
if i == -display {
glowLevel[offset] = 1
} else {
glowLevel[offset] = glowHold
}
} else {
if i == -display || glowLevel[offset-1] <= 0 {
glowLevel[offset] = 0
} else {
glowLevel[offset] = glowLevel[offset-1] - 1
}
}
}
}
for i := display; i >= 0; i-- {
offset := display + i
if backString[offset] {
fg := retrieveColor(trueFrame + i)
if backString[offset-1] {
for k := 0; k < step; k++ {
overlay(img, buttonMasks[btn], image.Pt(target+i*step-k+xoff, y), fg, color.RGBA{})
}
} else {
overlay(img, buttonMasks[btn], image.Pt(target+i*step+xoff, y), fg, color.RGBA{0, 0, 0, 255})
}
}
}
for i := -1; i >= -display; i-- {
offset := display + i
if backString[offset] {
fg := retrieveColor(trueFrame + i)
fg.R = fg.R/8 + 16
fg.G = fg.G/8 + 16
fg.B = fg.B/8 + 16
if offset > 0 && backString[offset-1] {
for k := 0; k < step; k++ {
overlay(img, buttonMasks[btn], image.Pt(target+i*step-k+xoff, y), fg, color.RGBA{})
}
} else {
overlay(img, buttonMasks[btn], image.Pt(target+i*step+xoff, y), fg, color.RGBA{0, 0, 0, 255})
}
}
}
if any || btn < buttonCount {
mh := uint8(255 * glowLevel[display] / glowHold)
overlay(img, buttonMasks[btn], image.Pt(target-1+xoff, y), color.RGBA{192, 192, 192, 255}, color.RGBA{mh, mh, mh, 255})
}
}
if splashStep < 2 {
draw.Draw(img, image.Rect(0, 0, left, height), splash1, image.ZP, draw.Over)
draw.DrawMask(img, image.Rect(left, 0, width, height), splash1, image.Pt(left, 0), invMask, image.ZP, draw.Over)
if splashStep == 0 {
draw.DrawMask(img, image.Rect(left, 0, width, height), splash2, image.Pt(left, 0), mask, image.ZP, draw.Over)
}
left -= step
if left < minLeft {
left = maxLeft
splashStep++
splash1 = splash2
}
}
_, err := os.Stdout.Write(img.Pix)
check(err)
}
}
func overlay(img *image.RGBA, mask [2]*image.Alpha, pt image.Point, col, xcol color.RGBA) {
rect := image.Rect(0, 0, buttonSize, buttonSize).Add(pt)
draw.DrawMask(img, rect, image.NewUniform(col), image.ZP, mask[0], image.ZP, draw.Over)
draw.DrawMask(img, rect, image.NewUniform(xcol), image.ZP, mask[1], image.ZP, draw.Over)
}
func retrieveColor(refFrame int) color.RGBA {
phase := refFrame % period
if phase < 0 {
phase += period
}
sixth := period / 6
i := phase / sixth
part := phase % sixth
frac := uint8(255 * part / (sixth - 1))
c := color.RGBA{A: 255}
switch i {
case 0:
c.R = 255
c.G = frac
case 1:
c.R = 255 - frac
c.G = 255
case 2:
c.G = 255
c.B = frac
case 3:
c.G = 255 - frac
c.B = 255
case 4:
c.R = frac
c.B = 255
case 5:
c.R = 255
c.B = 255 - frac
}
return c
}