forked from ikemen-engine/Ikemen-GO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanim.go
More file actions
2298 lines (2046 loc) · 57.9 KB
/
Copy pathanim.go
File metadata and controls
2298 lines (2046 loc) · 57.9 KB
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"math"
"sort"
"strconv"
"strings"
)
// AnimFrame holds frame data, used in animation tables.
type AnimFrame struct {
Time int32
Group int32
Number int32
Xoffset int32
Yoffset int32
TransType TransType
SrcAlpha byte
DstAlpha byte
Hscale int8
Vscale int8
Xscale float32
Yscale float32
Angle float32
Clsn1 [][4]float32
Clsn2 [][4]float32
}
func newAnimFrame() *AnimFrame {
return &AnimFrame{
Time: -1,
Group: -1,
TransType: TT_none,
SrcAlpha: 255,
DstAlpha: 0,
Hscale: 1, // These two are technically flags but are coded like scale for simplicity
Vscale: 1,
Xscale: 1,
Yscale: 1,
Angle: 0,
}
}
func ReadAnimFrame(line string) *AnimFrame {
if len(line) == 0 || (line[0] < '0' || '9' < line[0]) && line[0] != '-' {
return nil
}
ary := strings.SplitN(line, ",", 10)
if len(ary) < 5 {
return nil
}
// Read required parameters
af := newAnimFrame()
af.Group = int32(Atoi(ary[0]))
af.Number = int32(Atoi(ary[1]))
af.Xoffset = int32(Atoi(ary[2]))
af.Yoffset = int32(Atoi(ary[3]))
af.Time = Atoi(ary[4])
// Read H and V flags
if len(ary) >= 6 {
for i := range ary[5] {
switch ary[5][i] {
case 'H', 'h':
af.Hscale = -1
af.Xoffset *= -1
case 'V', 'v':
af.Vscale = -1
af.Yoffset *= -1
}
}
}
// Read alpha
if len(ary) >= 7 {
// Helper to read source and destination
parseAlphaNumbers := func(a string, start int, defSrc, defDst byte) (src, dst byte) {
src, dst = defSrc, defDst
i, alp := start, 0
for ; i < len(a) && a[i] >= '0' && a[i] <= '9'; i++ {
alp = alp*10 + int(a[i]-'0')
}
alp &= 0x3fff
if alp < 255 {
src = byte(alp)
}
if i < len(a) && a[i] == 'd' {
i++
if i < len(a) && a[i] >= '0' && a[i] <= '9' {
alp = 0
for ; i < len(a) && a[i] >= '0' && a[i] <= '9'; i++ {
alp = alp*10 + int(a[i]-'0')
}
alp &= 0x3fff
if alp < 255 {
dst = byte(alp)
}
}
}
return
}
ia := strings.IndexAny(ary[6], "ASas")
if ia >= 0 {
ary[6] = ary[6][ia:]
}
a := strings.ToLower(SplitAndTrim(ary[6], ",")[0])
switch {
case a == "a1":
af.TransType = TT_add
af.SrcAlpha = 255
af.DstAlpha = 128
case len(a) >= 2 && a[:2] == "as": // Add with alpha
af.TransType = TT_add
af.SrcAlpha, af.DstAlpha = parseAlphaNumbers(a, 2, 255, 255)
case len(a) > 0 && a[0] == 'a': // Plain Add
af.TransType = TT_add
af.SrcAlpha = 255
af.DstAlpha = 255
case len(a) >= 2 && a[:2] == "ss": // Sub with alpha
af.TransType = TT_sub
af.SrcAlpha, af.DstAlpha = parseAlphaNumbers(a, 2, 255, 255)
case len(a) == 1 && a[0] == 's': // Plain sub
af.TransType = TT_sub
af.SrcAlpha = 255
af.DstAlpha = 255
}
}
// Read X scale
// In Mugen 1.1 a blank parameter means 0
// In Ikemen it means no change, like the other optional parameters
if len(ary) >= 8 {
if IsNumeric(ary[7]) {
af.Xscale = float32(Atof(ary[7]))
}
}
// Read Y scale
if len(ary) >= 9 {
if IsNumeric(ary[8]) {
af.Yscale = float32(Atof(ary[8]))
}
}
// Read angle
if len(ary) >= 10 {
if IsNumeric(ary[9]) {
af.Angle = float32(Atof(ary[9]))
}
}
return af
}
type Animation struct {
sff *Sff
palettedata *PaletteList
spr *Sprite
frames []AnimFrame
tile Tiling
loopstart int32
interpolate_offset []int32
interpolate_scale []int32
interpolate_angle []int32
interpolate_blend []int32
curtime int32
curelem int32
curelemtime int32
lastActionFrame int32
drawidx int32
totaltime int32
looptime int32
prelooptime int32
mask int16
transType TransType
srcAlpha int16
dstAlpha int16
newframe bool
loopend bool
interpolate_offset_x float32
interpolate_offset_y float32
scale_x float32
scale_y float32
rot Rotation
curtrans TransType
interpolate_blend_srcalpha float32
interpolate_blend_dstalpha float32
remap RemapPreset
start_scale [2]float32
isParallax bool
isVideo bool // Because videos are rendered through Animation.Draw()
phantomPixel bool
copyAction int32
}
func newAnimation(sff *Sff, pal *PaletteList) *Animation {
return &Animation{
sff: sff,
palettedata: pal,
mask: -1,
transType: TT_default,
srcAlpha: 255,
dstAlpha: 0,
curtrans: TT_none,
interpolate_blend_srcalpha: 255,
interpolate_blend_dstalpha: 0,
newframe: true,
remap: make(RemapPreset),
start_scale: [...]float32{1, 1},
lastActionFrame: -1,
copyAction: -1,
}
}
func ReadAnimation(sff *Sff, pal *PaletteList, lines []string, i *int) *Animation {
a := newAnimation(sff, pal)
a.mask = 0
ols := int32(0)
var clsn1, clsn1d, clsn2, clsn2d [][4]float32
def1, def2 := true, true
for ; *i < len(lines); (*i)++ {
if len(lines[*i]) > 0 && lines[*i][0] == '[' {
break
}
line := strings.ToLower(strings.TrimSpace(
strings.SplitN(lines[*i], ";", 2)[0]))
// Copy Action
if len(line) >= 12 && line[:12] == "copy action " { // Trailing space here
numStr := strings.TrimSpace(line[12:])
id, err := strconv.ParseInt(numStr, 10, 32)
if err != nil || numStr == "" {
// Mark for deletion if number is invalid
a.copyAction = -1
} else {
a.copyAction = int32(id)
}
return a
}
af := ReadAnimFrame(line)
switch {
case af != nil:
ols = a.loopstart
if def1 {
clsn1 = clsn1d
}
if def2 {
clsn2 = clsn2d
}
af.Clsn1 = clsn1
af.Clsn2 = clsn2
a.frames = append(a.frames, *af)
def1, def2 = true, true
case len(line) >= 9 && line[:9] == "loopstart":
a.loopstart = int32(len(a.frames))
case len(line) >= 18 && line[:18] == "interpolate offset":
a.interpolate_offset = append(a.interpolate_offset, int32(len(a.frames)))
case len(line) >= 17 && line[:17] == "interpolate scale":
a.interpolate_scale = append(a.interpolate_scale, int32(len(a.frames)))
case len(line) >= 17 && line[:17] == "interpolate angle":
a.interpolate_angle = append(a.interpolate_angle, int32(len(a.frames)))
case len(line) >= 17 && line[:17] == "interpolate blend":
a.interpolate_blend = append(a.interpolate_blend, int32(len(a.frames)))
case len(line) >= 5 && line[:4] == "clsn":
ii := strings.Index(line, ":")
if ii < 0 {
break
}
size := Atoi(line[ii+1:])
if size < 0 {
break
}
var clsn [][4]float32
if line[4] == '1' {
clsn1 = make([][4]float32, size)
clsn = clsn1
if len(line) >= 12 && line[5:12] == "default" {
clsn1d = clsn1
}
def1 = false
} else if line[4] == '2' {
clsn2 = make([][4]float32, size)
clsn = clsn2
if len(line) >= 12 && line[5:12] == "default" {
clsn2d = clsn2
}
def2 = false
} else {
break
}
if size == 0 {
break
}
(*i)++
for n := int32(0); n < size && *i < len(lines); {
line := strings.ToLower(strings.TrimSpace(
strings.SplitN(lines[*i], ";", 2)[0]))
if len(line) == 0 {
(*i)++
continue
}
if len(line) < 4 || line[:4] != "clsn" {
break
}
ii := strings.Index(line, "=")
if ii < 0 {
break
}
ary := strings.Split(line[ii+1:], ",")
if len(ary) < 4 {
break
}
l, t, r, b := Atoi(ary[0]), Atoi(ary[1]), Atoi(ary[2]), Atoi(ary[3])
if l > r {
l, r = r, l
}
if t > b {
t, b = b, t
}
clsn[n][0], clsn[n][1], clsn[n][2], clsn[n][3] =
float32(l), float32(t), float32(r), float32(b)
n++
(*i)++
}
(*i)--
}
}
if int(a.loopstart) >= len(a.frames) {
a.loopstart = ols
}
if len(a.frames) == 0 {
} else if a.frames[len(a.frames)-1].Time == -1 {
a.totaltime = -1
} else {
tmp := int32(0)
for i, f := range a.frames {
if f.Time == -1 {
a.totaltime = 0
a.looptime = -tmp
a.prelooptime = 0
}
a.totaltime += f.Time
if i < int(a.loopstart) {
a.prelooptime += f.Time
tmp += f.Time
} else {
a.looptime += f.Time
}
}
if a.totaltime == -1 {
a.prelooptime = 0
}
}
return a
}
func ReadAction(sff *Sff, pal *PaletteList, lines []string, i *int) (no int32, a *Animation) {
var name, subname string
for ; *i < len(lines); (*i)++ {
name, subname = SectionName(lines[*i])
if len(name) > 0 {
break
}
}
if name != "begin " {
return
}
spi := strings.Index(subname, " ")
if spi < 0 {
return
}
if strings.ToLower(subname[:spi+1]) != "action " {
return
}
(*i)++
return Atoi(subname[spi+1:]), ReadAnimation(sff, pal, lines, i)
}
func (a *Animation) Reset() {
a.curelem, a.drawidx = 0, 0
a.curelemtime, a.curtime = 0, 0
a.newframe, a.loopend = true, false
a.spr = nil
}
func (a *Animation) isBlank() bool {
return a.scale_x == 0 || a.scale_y == 0 || a.spr == nil || a.spr.isBlank()
}
func (a *Animation) isCommonFX() bool {
for _, fx := range sys.ffx {
if fx.sff == a.sff { // TODO: This may be a problem in the very unlikely scenario that the same SFF is used in a char and in common FX
return true
}
}
return false
}
func (a *Animation) AnimTime() int32 {
return a.curtime - a.totaltime
}
func (a *Animation) AnimElemTime(elem int32) int32 {
if int(elem) > len(a.frames) {
t := a.AnimTime()
if t > 0 {
t = 0
}
return t
}
e, t := Max(0, elem)-1, a.curtime
for i := int32(0); i < e; i++ {
t -= Max(0, a.frames[i].Time)
}
return t
}
func (a *Animation) AnimElemNo(time int32) int32 {
if len(a.frames) > 0 {
i, oldt := a.curelem, int32(0)
if time <= 0 {
time += a.curelemtime
loop := false
for {
if time >= 0 {
return i + 1
}
i--
if i < 0 || a.curelem >= a.loopstart && i < a.loopstart {
if time == oldt {
break
}
oldt = time
loop = true
i = int32(len(a.frames)) - 1
}
time += Max(0, a.frames[i].Time)
if loop && i == int32(len(a.frames))-1 && a.frames[i].Time == -1 {
return i + 1
}
}
} else {
time += a.curelemtime
for {
time -= Max(0, a.frames[i].Time)
if time < 0 || i == int32(len(a.frames))-1 && a.frames[i].Time == -1 {
return i + 1
}
i++
if i >= int32(len(a.frames)) {
if time == oldt {
break
}
oldt = time
i = a.loopstart
}
}
}
}
return int32(len(a.frames))
}
func (a *Animation) GetLength() int32 {
if a == nil || len(a.frames) == 0 {
return 0
}
var length int32
for _, f := range a.frames {
var t int32
switch {
case f.Time == -1: // Special case: infinite frame, count as 1 tick
t = 1
case f.Time > 0: // Normal positive duration
t = f.Time
default: // Time <= 0 (except -1) doesn't contribute
t = 0
}
length += t
}
return length
}
func (a *Animation) curFrame() *AnimFrame {
return &a.frames[a.curelem]
}
func (a *Animation) CurrentFrame() *AnimFrame {
if len(a.frames) == 0 {
return nil
}
return a.curFrame()
}
func (a *Animation) drawFrame() *AnimFrame {
if len(a.frames) == 0 {
return nil
}
return &a.frames[a.drawidx]
}
func (a *Animation) SetAnimElem(elem, elemtime int32) {
a.curelem = Max(0, elem-1)
// If trying to set an element higher than the last one in the animation
if int(a.curelem) >= len(a.frames) {
//if a.totaltime == -1 {
// a.current = int32(len(a.frames)) - 1
//} else if int32(len(a.frames))-a.loopstart > 0 { // Prevent division by zero crash
// a.current = a.loopstart +
// (a.current-a.loopstart)%(int32(len(a.frames))-a.loopstart)
//}
// Mugen merely sets the element to 1
a.curelem = 0
}
a.drawidx = a.curelem
// Shortcut the most common elemtime
// Out of range elemtime is also set to 0, as with elem
if elemtime != 0 {
frametime := a.frames[a.curelem].Time
if elemtime < 0 || (frametime != -1 && elemtime >= frametime) {
elemtime = 0
}
}
a.curelemtime = elemtime
a.newframe = true
a.loopend = false
a.UpdateSprite()
a.curtime = 0 // Used within AnimElemTime, so must be set to 0 first
a.curtime = -a.AnimElemTime(a.curelem+1) + a.curelemtime
}
func (a *Animation) animSeek(elem int32) {
if elem < 0 {
elem = 0
}
foo := true
for {
a.curelem = elem
for int(a.curelem) < len(a.frames) && a.curFrame().Time <= 0 {
if int(a.curelem) == len(a.frames)-1 && a.curFrame().Time == -1 {
break
}
a.curelem++
}
if int(a.curelem) < len(a.frames) {
break
}
foo = !foo
if foo {
a.curelem = int32(len(a.frames) - 1)
break
}
}
if a.curelem < 0 {
a.curelem = 0
} else if int(a.curelem) >= len(a.frames) {
a.curelem = int32(len(a.frames) - 1)
}
}
func (a *Animation) UpdateSprite() {
if len(a.frames) == 0 {
return
}
// Handle frame timing and looping
if a.totaltime > 0 {
// Change into the loop start frame when animation ends
if a.curtime >= a.totaltime {
a.curelemtime, a.newframe, a.curelem = 0, true, a.loopstart
}
a.animSeek(a.curelem)
// Handle negative prelooptime
if a.prelooptime < 0 && a.curtime >= a.totaltime+a.prelooptime &&
a.curtime >= a.totaltime-a.looptime &&
(a.curtime == a.totaltime+a.prelooptime ||
a.curtime == a.totaltime-a.looptime) {
a.curelemtime = 0
a.newframe = true
a.curelem = 0
}
}
// Change to a new sprite if necessary
if a.newframe && a.sff != nil && a.frames[a.curelem].Time != 0 {
group, number := a.curFrame().Group, a.curFrame().Number
if mg, ok := a.remap[group]; ok {
if mn, ok := mg[number]; ok {
group, number = mn[0], mn[1]
}
}
//Mugen only nulls out -1, other negatives wrap around
if group != -1 && number != -1 {
a.spr = a.sff.GetSprite(uint16(group), uint16(number))
if a.spr == nil {
// Log missing sprites
// We will save the history in the SFF itself so that each sprite is only mentioned once
key := [2]uint16{uint16(group), uint16(number)}
if a.sff.debugMissing == nil {
a.sff.debugMissing = make(map[[2]uint16]bool)
}
if !a.sff.debugMissing[key] {
LogMessage("WARNING: Animation missing sprite %v,%v from %v", group, number, a.sff.filename)
a.sff.debugMissing[key] = true
}
}
} else {
a.spr = nil
}
}
a.newframe, a.drawidx = false, a.curelem
a.curtrans = a.frames[a.drawidx].TransType
a.scale_x = a.frames[a.drawidx].Xscale
a.scale_y = a.frames[a.drawidx].Yscale
a.rot.angle = a.frames[a.drawidx].Angle
a.UpdateInterpolation()
// Apply scale correction only after interpolation runs
a.scale_x *= a.start_scale[0]
a.scale_y *= a.start_scale[1]
}
func (a *Animation) UpdateInterpolation() {
// Reset values
a.interpolate_offset_x = 0
a.interpolate_offset_y = 0
a.interpolate_blend_srcalpha = float32(a.frames[a.drawidx].SrcAlpha)
a.interpolate_blend_dstalpha = float32(a.frames[a.drawidx].DstAlpha)
// Determine which frame to interpolate into
nextDrawidx := a.drawidx + 1
if int(a.drawidx) >= len(a.frames)-1 {
nextDrawidx = a.loopstart
}
// Determine current interpolation progress
var progress float32
if a.frames[a.drawidx].Time > 0 {
progress = float32(a.curelemtime) / float32(a.curFrame().Time)
}
// Invalid progress
if progress <= 0 {
return
}
// Offset
for _, i := range a.interpolate_offset {
if nextDrawidx != i {
continue
}
a.interpolate_offset_x = float32(a.frames[nextDrawidx].Xoffset-a.frames[a.drawidx].Xoffset) * progress
a.interpolate_offset_y = float32(a.frames[nextDrawidx].Yoffset-a.frames[a.drawidx].Yoffset) * progress
break
}
// Scale
for _, i := range a.interpolate_scale {
if nextDrawidx != i {
continue
}
a.scale_x += (a.frames[nextDrawidx].Xscale - a.frames[a.drawidx].Xscale) * progress
a.scale_y += (a.frames[nextDrawidx].Yscale - a.frames[a.drawidx].Yscale) * progress
break
}
// Angle
for _, i := range a.interpolate_angle {
if nextDrawidx != i {
continue
}
a.rot.angle += (a.frames[nextDrawidx].Angle - a.frames[a.drawidx].Angle) * progress
break
}
// Blend
for _, i := range a.interpolate_blend {
if nextDrawidx != i {
continue
}
// We only interpolate blend if blending actually exists
if a.curtrans != TransNone {
a.interpolate_blend_srcalpha += (float32(a.frames[nextDrawidx].SrcAlpha) - a.interpolate_blend_srcalpha) * progress
a.interpolate_blend_dstalpha += (float32(a.frames[nextDrawidx].DstAlpha) - a.interpolate_blend_dstalpha) * progress
}
break
}
}
func (a *Animation) Action() {
// Ignore invalid animation instead of crashing engine
if a == nil || a.frames == nil {
return
}
if len(a.frames) == 0 {
a.loopend = true
return
}
a.UpdateSprite()
next := func() {
if a.totaltime != -1 || int(a.curelem) < len(a.frames)-1 {
a.curelemtime = 0
a.newframe = true
for {
a.curelem++
if a.totaltime == -1 && int(a.curelem) == len(a.frames)-1 ||
int(a.curelem) >= len(a.frames) || a.curFrame().Time > 0 {
break
}
}
}
}
if a.curFrame().Time <= 0 {
next()
}
if int(a.curelem) < len(a.frames) {
a.curelemtime++
if a.curelemtime >= a.curFrame().Time {
next()
if int(a.curelem) >= len(a.frames) {
a.curelem = a.loopstart
}
}
} else {
a.curelem = a.loopstart
}
if a.totaltime != -1 && a.curtime >= a.totaltime {
a.curtime = a.totaltime - a.looptime
}
a.curtime++
if a.totaltime != -1 && a.curtime >= a.totaltime {
a.loopend = true
}
}
// Convert animation transparency to RenderParams transparency
func (a *Animation) alphaToBlend() (blendMode TransType, blendAlpha [2]int32) {
var sa, da byte
blendMode = a.transType
if blendMode == TT_default {
blendMode = a.curtrans
sa = byte(a.interpolate_blend_srcalpha)
da = byte(a.interpolate_blend_dstalpha)
} else {
sa = byte(a.srcAlpha)
da = byte(a.dstAlpha)
}
// Fallback: Make sure blend mode is not default
if blendMode == TT_default {
blendMode = TT_none
sa = byte(255)
da = byte(0)
}
// Apply system brightness
sa = byte(float32(sa) * sys.brightness)
blendAlpha = [2]int32{int32(sa), int32(da)}
return
}
func (a *Animation) pal(pfx *PalFX) (p []uint32, plt Texture) {
if a.palettedata != nil {
// Apply temporary palette remap if provided
if pfx != nil && len(pfx.remap) > 0 {
a.palettedata.SwapPalMap(&pfx.remap)
}
// Get palette colors and texture
p = a.spr.GetPal(a.palettedata)
plt = a.spr.GetPalTex(a.palettedata)
// Restore original palette mapping
if pfx != nil && len(pfx.remap) > 0 {
a.palettedata.SwapPalMap(&pfx.remap)
}
} else {
if pfx != nil && len(pfx.remap) > 0 {
a.sff.palList.SwapPalMap(&pfx.remap)
}
p = a.spr.GetPal(&a.sff.palList)
plt = a.spr.GetPalTex(&a.sff.palList)
if pfx != nil && len(pfx.remap) > 0 {
a.sff.palList.SwapPalMap(&pfx.remap)
}
}
return
}
func (a *Animation) drawSub1(angle, facing float32) (h, v, agl float32) {
h, v = 1, 1
if len(a.frames) > 0 {
h, v = float32(a.frames[a.drawidx].Hscale), float32(a.frames[a.drawidx].Vscale)
}
agl = angle
h *= a.scale_x
v *= a.scale_y
agl += a.rot.angle * facing
return
}
func (a *Animation) Draw(window *[4]int32, x, y, xcs, ycs, xs, xbs, ys,
rxadd float32, rot Rotation, rcx float32, pfx *PalFX, facing float32,
airOffsetFix [2]float32, projectionMode int32, fLength float32, color uint32,
isReflection bool, shader string, shaderParams [16]float32) {
// Skip blank animations
if a == nil || a.isBlank() {
return
}
// Determine animation angle. Invert for reflection
h, v, angle := a.drawSub1(rot.angle, facing)
if isReflection && sys.stage.reflection.yscale > 0 {
angle = -angle
}
rot.angle = angle
xs *= xcs * h
ys *= ycs * v
// Compute X and Y AIR animation offsets
var xoff, yoff float32
if !a.isVideo {
xoffBase := float32(a.frames[a.drawidx].Xoffset) + a.interpolate_offset_x
yoffBase := float32(a.frames[a.drawidx].Yoffset) + a.interpolate_offset_y
// Phantom pixel adjustment: when frames are explicitly flipped via
// H or V in the AIR data, substract an extra pixel of offset.
if a.phantomPixel {
if a.frames[a.drawidx].Hscale < 0 {
xoffBase--
}
if a.frames[a.drawidx].Vscale < 0 {
yoffBase--
}
}
xoff = xs * airOffsetFix[0] * xoffBase * a.start_scale[0] * (1 / a.scale_x)
yoff = ys * airOffsetFix[1] * yoffBase * a.start_scale[1] * (1 / a.scale_y)
}
x = xcs*x + xoff
y = ycs*y + yoff
var rcy float32
if rot.IsZero() {
if xs < 0 {
x *= -1
// This was deliberately replicating a Mugen bug, but we don't need that
// https://github.com/ikemen-engine/Ikemen-GO/issues/1864
//if old {
// x += xs
//}
}
if ys < 0 {
y *= -1
//if old {
// y += ys
//}
}
if a.tile.xflag == 1 {
var space float32
if a.isParallax {
space = xs * float32(a.tile.xspacing)
if a.tile.xspacing <= 0 {
space += xs * float32(a.spr.Size[0])
}
} else {
space = xs / h * float32(a.tile.xspacing)
if a.tile.xspacing <= 0 {
space += xs / h * float32(a.spr.Size[0])
space += float32(a.spr.Size[0])
}
}
if space != 0 {
x -= float32(int(x/space)) * space
}
}
if a.tile.yflag == 1 {
space := ys * float32(a.tile.yspacing)
if a.tile.yspacing <= 0 {
space += ys * float32(a.spr.Size[1])
}
if space != 0 {
y -= float32(int(y/space)) * space
}
}
rcx, rcy = rcx*sys.widthScale, 0
x = -x + Abs(xs)*float32(a.spr.Offset[0])
y = -y + Abs(ys)*float32(a.spr.Offset[1])
} else {
rcx, rcy = (x+rcx)*sys.widthScale, y*sys.heightScale
x, y = Abs(xs)*float32(a.spr.Offset[0]), Abs(ys)*float32(a.spr.Offset[1])
fLength *= ycs
}
blendMode, blendAlpha := a.alphaToBlend()
var paltex Texture
if !a.isVideo {
var pal []uint32
pal, paltex = a.pal(pfx)
if a.spr.coldepth <= 8 && paltex == nil {
paltex = a.spr.CachePalTex(pal)
}
}
rp := RenderParams{
tex: a.spr.Tex,
paltex: paltex,
size: a.spr.Size,
x: x * sys.widthScale,
y: y * sys.heightScale,
tile: a.tile,
xts: xs * sys.widthScale,
xbs: xcs * xbs * h * sys.widthScale,
ys: ys * sys.heightScale,
vs: 1,
rxadd: xcs * rxadd * sys.widthScale / sys.heightScale,
xas: h,
yas: v,
rot: rot,
tint: color,
blendMode: blendMode,
blendAlpha: blendAlpha,
mask: int32(a.mask),
pfx: pfx,
window: window,
rcx: rcx,
rcy: rcy,
projectionMode: projectionMode,
fLength: fLength * sys.heightScale,
xOffset: xoff * sys.widthScale,
yOffset: yoff * sys.heightScale,
shader: shader,
shaderParams: shaderParams,
}
RenderSprite(rp)
}
func (a *Animation) ShadowDraw(window *[4]int32, x, y, xscl, yscl, vscl, rxadd float32, rot Rotation,
pfx *PalFX, color uint32, intensity int32, facing float32, airOffsetFix [2]float32, projectionMode int32, fLength float32, shader string, shaderParams [16]float32) {
// Skip blank shadows
if a == nil || a.isBlank() {
return
}
// Determine animation angle. Invert for shadows
h, v, angle := a.drawSub1(rot.angle, facing)
rot.angle = -angle
// Compute X and Y AIR animation offsets
xoff := xscl * airOffsetFix[0] * h * (float32(a.frames[a.drawidx].Xoffset) + a.interpolate_offset_x) * (1 / a.scale_x)
yoff := yscl * airOffsetFix[1] * vscl * v * (float32(a.frames[a.drawidx].Yoffset) + a.interpolate_offset_y) * (1 / a.scale_y)
x += xoff
y += yoff
rp := RenderParams{
tex: a.spr.Tex,
paltex: nil,
size: a.spr.Size,
x: Abs(xscl*h) * float32(a.spr.Offset[0]) * sys.widthScale,
y: Abs(yscl*v) * float32(a.spr.Offset[1]) * sys.heightScale,
tile: a.tile,
xts: xscl * h * sys.widthScale,
xbs: xscl * h * sys.widthScale,
ys: yscl * v * sys.heightScale,
vs: vscl,
rxadd: rxadd * sys.widthScale / sys.heightScale,
xas: h,
yas: v,
rot: rot,
tint: color | 0xff000000,
blendMode: TT_sub,
blendAlpha: [2]int32{0, 0},