-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffects.cpp
More file actions
1848 lines (1678 loc) · 78.1 KB
/
effects.cpp
File metadata and controls
1848 lines (1678 loc) · 78.1 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
/*
* effects.cpp - Color LUTs, explosion sprites, particle rendering
* Addresses: FUN_0045a060=0045A060, FUN_0045b2a0=0045B2A0,
* FUN_00422fc0=00422FC0, FUN_0040d100=0040D100,
* FUN_004076d0=004076D0, FUN_004257e0=004257E0
*/
#include "tou.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
/* ===== Viewport globals (set by FUN_004076d0) ===== */
int DAT_004806dc = 0; /* viewport left */
int DAT_004806d0 = 0; /* viewport right */
int DAT_004806e0 = 0; /* viewport top */
int DAT_004806d4 = 0; /* viewport bottom */
int DAT_004806d8 = 0; /* viewport width */
int DAT_004806e4 = 0; /* viewport height */
int DAT_004806e8 = 0; /* camera/scroll Y offset */
int DAT_004806ec = 0; /* camera/scroll X offset */
/* ===== Entity rendering counts (all zero until entity spawning is implemented) ===== */
int DAT_00489274 = 0; /* static entity count (turrets) */
int DAT_0048924c = 0; /* dynamic entity/trooper count */
int DAT_00489260 = 0; /* projectile count */
int DAT_0048926c = 0; /* explosion count */
int DAT_00489264 = 0; /* misc effect count */
int DAT_00489268 = 0; /* debris/particle count */
int DAT_00487808 = 0; /* active player viewport count */
unsigned short DAT_0048384e = 0; /* laser pixel color A */
unsigned short DAT_00483850 = 0; /* laser pixel color B */
/* ===== FUN_004257e0 - Angle calculation (atan2 to table index) ===== */
/* Returns 11-bit angle index (0-2047) into sin/cos table.
* Original uses x87 FPATAN(ST1=dx, ST0=dy) which is atan2(dx, dy),
* then multiplies by 1024.0 * (1/PI) to convert radians to table index. */
int FUN_004257e0(int cx, int cy, int px, int py)
{
double dx = (double)(px - cx);
double dy = (double)(py - cy);
/* FPATAN(ST1=dx, ST0=dy) = atan2(dx, dy) - angle from Y axis */
double angle = atan2(dx, dy);
int idx = (int)(angle * 1024.0 / 3.14159265358979323846);
return idx & 0x7FF;
}
/* ===== Helper: clamp int to [0, 255] ===== */
static inline int clamp255(int v)
{
if (v < 0) return 0;
if (v > 255) return 255;
return v;
}
/* ===== Helper: pack 8-bit RGB to RGB565 ===== */
static inline unsigned short pack_rgb565(int r, int g, int b)
{
return (unsigned short)(((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3));
}
/* ===== Helper: fill one 4096-entry LUT from a per-pixel transform ===== */
typedef void (*lut_transform_fn)(int src_r, int src_g, int src_b,
int *out_r, int *out_g, int *out_b, int param);
static void fill_lut(unsigned short *lut, lut_transform_fn fn, int param)
{
for (int r4 = 0; r4 < 16; r4++) {
int r8 = (r4 * 255) / 15;
for (int g4 = 0; g4 < 16; g4++) {
int g8 = (g4 * 255) / 15;
for (int b4 = 0; b4 < 16; b4++) {
int b8 = (b4 * 255) / 15;
int or8, og8, ob8;
fn(r8, g8, b8, &or8, &og8, &ob8, param);
int idx = b4 + (g4 + r4 * 16) * 16;
lut[idx] = pack_rgb565(clamp255(or8), clamp255(og8), clamp255(ob8));
}
}
}
}
/* ---- Per-table transform functions ---- */
/* [0..3] Fog-to-white: (ch*4+255)/5 applied N times */
static void xform_fog_white(int r, int g, int b, int *or_, int *og, int *ob, int n)
{
for (int i = 0; i < n; i++) {
r = (r * 4 + 255) / 5;
g = (g * 4 + 255) / 5;
b = (b * 4 + 255) / 5;
}
*or_ = r; *og = g; *ob = b;
}
/* [4..11] Brightness dimming: ch = ch * level / 11 */
static void xform_brightness(int r, int g, int b, int *or_, int *og, int *ob, int level)
{
*or_ = (r * level) / 11;
*og = (g * level) / 11;
*ob = (b * level) / 11;
}
/* [12..13] Cold/green shift: R-=0x30*n, G+=0x30*n, B-=0x30*n */
static void xform_cold(int r, int g, int b, int *or_, int *og, int *ob, int n)
{
*or_ = r - 0x30 * n;
*og = g + 0x30 * n;
*ob = b - 0x30 * n;
}
/* [14..15] Warm/yellow shift: R+=0x30*n, G+=0x30*n, B-=0x30*n */
static void xform_warm(int r, int g, int b, int *or_, int *og, int *ob, int n)
{
*or_ = r + 0x30 * n;
*og = g + 0x30 * n;
*ob = b - 0x30 * n;
}
/* [16..17] Red shift: R+=0x30*n, G-=0x30*n, B-=0x30*n */
static void xform_red(int r, int g, int b, int *or_, int *og, int *ob, int n)
{
*or_ = r + 0x30 * n;
*og = g - 0x30 * n;
*ob = b - 0x30 * n;
}
/* [18..21] Fog-to-gray: ch + ((0x50 - ch) * level) / 7 */
static void xform_fog_gray(int r, int g, int b, int *or_, int *og, int *ob, int level)
{
*or_ = r + ((0x50 - r) * level) / 7;
*og = g + ((0x50 - g) * level) / 7;
*ob = b + ((0x50 - b) * level) / 7;
}
/* [22] Shadow (ice/freeze overlay): additive blue tint.
* Original ASM multiplies 4-bit index by -17.0 then subtracts from constant,
* which is equivalent to: out = constant + scaled_value.
* Produces dark blue-teal at black (25,30,35) ramping to white at full brightness. */
static void xform_shadow(int r, int g, int b, int *or_, int *og, int *ob, int /*unused*/)
{
*or_ = 0x19 + r;
*og = 0x1E + g;
*ob = 0x23 + b;
}
/* [23] Contrast pinch: pull R toward 0x8C, G/B toward 0xDC with +-0x28 */
static void xform_contrast(int r, int g, int b, int *or_, int *og, int *ob, int /*unused*/)
{
/* R: pull toward 0x8C */
if (r > 0x8C) { r -= 0x28; if (r < 0x8C) r = 0x8C; }
else { r += 0x28; if (r > 0x8C) r = 0x8C; }
/* G: pull toward 0xDC */
if (g > 0xDC) { g -= 0x28; if (g < 0xDC) g = 0xDC; }
else { g += 0x28; if (g > 0xDC) g = 0xDC; }
/* B: pull toward 0xDC */
if (b > 0xDC) { b -= 0x28; if (b < 0xDC) b = 0xDC; }
else { b += 0x28; if (b > 0xDC) b = 0xDC; }
*or_ = r; *og = g; *ob = b;
}
/* [24..27] Highlight: R pulled to 0xA0 by +-0xC, G/B reduced by 0x10*n */
static void xform_highlight(int r, int g, int b, int *or_, int *og, int *ob, int n)
{
int og_v = g - 0x10 * n;
int ob_v = b - 0x10 * n;
for (int i = 0; i < n; i++) {
if (r < 0xA0) r += 0x0C;
else r -= 0x0C;
}
*or_ = r; *og = og_v; *ob = ob_v;
}
/* ===== FUN_0045a060 - Color LUT generation (0045A060) ===== */
/*
* Generates the master color remap table (DAT_00489230) and all color
* transformation LUTs (DAT_004876a4[0..47+]). Each LUT is 4096 entries
* of RGB565 pixels, indexed by a 12-bit R4:G4:B4 value.
*
* Table layout in DAT_004876a4[]:
* [0..3] Fog-to-white (4 tables)
* [4..11] Brightness dimming (8 tables, level 10 down to 3)
* [12..13] Cold/green shift (2 tables)
* [14..15] Warm/yellow shift (2 tables)
* [16..17] Red shift (2 tables)
* [18..21] Fog-to-gray (4 tables)
* [22] Shadow (subtractive)
* [23] Contrast pinch
* [24..27] Highlight (4 tables)
* [32..47] Blend-to-background (16 tables, via FUN_0045adc0)
* [48..51] Underwater tint (4 tables, COMPAT - original never fills these)
*/
void FUN_0045a060(void)
{
/* ---- Section 1: RGB565 -> 12-bit remap table (DAT_00489230) ---- */
/* Maps every possible 16-bit pixel to a 12-bit index (R4:G4:B4).
* Original iterates 32x32x32 (5-bit per channel); we fill all 64K. */
unsigned short *remap = (unsigned short *)DAT_00489230;
for (int pixel = 0; pixel < 65536; pixel++) {
int r5 = (pixel >> 11) & 0x1F;
int g6 = (pixel >> 5) & 0x3F;
int b5 = pixel & 0x1F;
int r4 = (r5 * 15) / 31;
int g4 = (g6 * 15) / 63;
int b4 = (b5 * 15) / 31;
remap[pixel] = (unsigned short)(b4 + (g4 + r4 * 16) * 16);
}
/* ---- Section 2: Fog-to-white [0..3] ---- */
for (int i = 0; i < 4; i++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[i];
if (lut) fill_lut(lut, xform_fog_white, i + 1);
}
/* ---- Section 3: Brightness dimming [4..11] ---- */
/* 8 tables: level counts down from 10 to 3 */
{
int level = 10;
for (int i = 4; i < 12; i++, level--) {
unsigned short *lut = (unsigned short *)DAT_004876a4[i];
if (lut) fill_lut(lut, xform_brightness, level);
}
}
/* ---- Section 4: Cold/green shift [12..13] ---- */
for (int i = 0; i < 2; i++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[12 + i];
if (lut) fill_lut(lut, xform_cold, i + 1);
}
/* ---- Section 5: Warm/yellow shift [14..15] ---- */
for (int i = 0; i < 2; i++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[14 + i];
if (lut) fill_lut(lut, xform_warm, i + 1);
}
/* ---- Section 6: Red shift [16..17] ---- */
for (int i = 0; i < 2; i++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[16 + i];
if (lut) fill_lut(lut, xform_red, i + 1);
}
/* ---- Section 7: Fog-to-gray [18..21] ---- */
for (int i = 0; i < 4; i++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[18 + i];
if (lut) fill_lut(lut, xform_fog_gray, i + 1);
}
/* ---- Section 8: Shadow [22] ---- */
{
unsigned short *lut = (unsigned short *)DAT_004876a4[22];
if (lut) fill_lut(lut, xform_shadow, 0);
}
/* ---- Section 9: Contrast pinch [23] ---- */
{
unsigned short *lut = (unsigned short *)DAT_004876a4[23];
if (lut) fill_lut(lut, xform_contrast, 0);
}
/* ---- Section 10: Highlight [24..27] ---- */
for (int i = 0; i < 4; i++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[24 + i];
if (lut) fill_lut(lut, xform_highlight, i + 1);
}
/* ---- Section 11: Blend-to-background [32..47] via FUN_0045adc0 ---- */
FUN_0045adc0();
/* ---- Section 12: Underwater tint [48..51] ---- */
/* COMPAT: Original binary allocates these LUT slots but never fills them,
* causing the ship to render fully black when entering fluid tiles (types 65-71).
* The underwater exposure counter (entity +0x49F) produces blend indices 48-51
* via formula (damage*4/64)+0x30. We generate blue/dark tint tables so the
* ship progressively darkens with a blue cast instead of going instantly black. */
{
/* Underwater target: deep dark blue */
int uw_r = 10, uw_g = 30, uw_b = 90;
for (int step = 0; step < 4; step++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[48 + step];
if (!lut) continue;
/* Blend strength: 15%, 30%, 50%, 70% toward underwater color */
int blend_pct = 15 + step * 18;
for (int r4 = 0; r4 < 16; r4++) {
int r8 = (r4 * 255) / 15;
for (int g4 = 0; g4 < 16; g4++) {
int g8 = (g4 * 255) / 15;
for (int b4 = 0; b4 < 16; b4++) {
int b8 = (b4 * 255) / 15;
int or8 = r8 + ((uw_r - r8) * blend_pct) / 100;
int og8 = g8 + ((uw_g - g8) * blend_pct) / 100;
int ob8 = b8 + ((uw_b - b8) * blend_pct) / 100;
int idx = b4 + (g4 + r4 * 16) * 16;
lut[idx] = pack_rgb565(clamp255(or8), clamp255(og8), clamp255(ob8));
}
}
}
}
}
}
/* ===== FUN_0045adc0 - Blend-to-background LUT generation (0045ADC0) ===== */
/*
* Generates 16 tables at DAT_004876a4[32..47] that linearly blend
* each source pixel toward a target background color (DAT_00483820).
* Step 1/17 through 16/17.
*/
void FUN_0045adc0(void)
{
/* Decode target color from DAT_00483820 (RGB565) */
unsigned short bg = DAT_00483820;
int tgt_r = ((bg >> 11) & 0x1F) << 3;
int tgt_g = ((bg >> 5) & 0x3F) << 2;
int tgt_b = (bg & 0x1F) << 3;
for (int step = 1; step <= 16; step++) {
unsigned short *lut = (unsigned short *)DAT_004876a4[32 + step - 1];
if (!lut) continue;
for (int r4 = 0; r4 < 16; r4++) {
int r8 = (r4 * 255) / 15;
for (int g4 = 0; g4 < 16; g4++) {
int g8 = (g4 * 255) / 15;
for (int b4 = 0; b4 < 16; b4++) {
int b8 = (b4 * 255) / 15;
int or8 = r8 + ((tgt_r - r8) * step) / 17;
int og8 = g8 + ((tgt_g - g8) * step) / 17;
int ob8 = b8 + ((tgt_b - b8) * step) / 17;
int idx = b4 + (g4 + r4 * 16) * 16;
lut[idx] = pack_rgb565(clamp255(or8), clamp255(og8), clamp255(ob8));
}
}
}
}
}
/* ===== FUN_0045b2a0 - Blend LUT generation (0045B2A0) ===== */
/*
* Generates DAT_0048792c blend tables for explosion particle rendering.
* 3 color groups x 16 blend levels = 48 tables.
* Each table: 4096 entries mapping 12-bit source brightness -> RGB565 blended color.
*
* Group 0: Fire (red -> orange -> yellow -> white)
* Group 1: Warm fire variant
* Group 2: Cooler tones
*/
void FUN_0045b2a0(void)
{
/* Color targets per group per level: [R, G, B]
* Extracted from local_bb in Ghidra decompilation */
static const unsigned char fire_colors[3][15][3] = {
/* Group 0: Fire - red to white */
{
{0xFF,0x00,0x00}, {0xFF,0x00,0x00}, {0xFF,0x00,0x00}, {0xFF,0x00,0x00},
{0xFF,0x10,0x00}, {0xFF,0x20,0x00}, {0xFF,0x30,0x00}, {0xFF,0x50,0x00},
{0xFF,0x82,0x00}, {0xFF,0xB4,0x20}, {0xFF,0xE6,0x40}, {0xFF,0xFF,0x60},
{0xFF,0xFF,0x80}, {0xFF,0xFF,0xA0}, {0xFF,0xFF,0xC8},
},
/* Group 1: Warm fire variant */
{
{0xFF,0x00,0x00}, {0xFF,0x00,0x00}, {0xFF,0x00,0x00}, {0xFF,0x00,0x00},
{0xFF,0x1E,0x00}, {0xFF,0x3C,0x00}, {0xFF,0x64,0x00}, {0xFF,0x96,0x00},
{0xFF,0xBE,0x00}, {0xFF,0xDC,0x28}, {0xFF,0xFF,0x32}, {0xFF,0xFF,0x64},
{0xFF,0xFF,0x96}, {0xFF,0xFF,0xC8}, {0xFF,0xFF,0xFF},
},
/* Group 2: Blue/ice (R,G low, B=0xFF dominant) */
{
{0x28,0x28,0xFF}, {0x28,0x28,0xFF}, {0x28,0x28,0xFF}, {0x28,0x28,0xFF},
{0x3C,0x3C,0xFF}, {0x50,0x50,0xFF}, {0x64,0x64,0xFF}, {0x64,0x64,0xFF},
{0x78,0x78,0xFF}, {0x8C,0x8C,0xFF}, {0xA0,0xA0,0xFF}, {0xB4,0xB4,0xFF},
{0xC8,0xC8,0xFF}, {0xDC,0xDC,0xFF}, {0xFF,0xFF,0xFF},
},
};
/* Blend strength per group per level (from local_104 in Ghidra) */
static const unsigned char blend_str[3][15] = {
{ 2, 5, 8,10,11,13,15,18,24,30,35,39,43,47,52},
{ 5, 8,10,12,15,18,24,30,35,42,47,52,57,63,63},
{ 4, 7, 9,11,13,17,22,27,32,37,42,47,52,57,60},
};
for (int group = 0; group < 3; group++) {
for (int level = 0; level < 15; level++) {
int lut_idx = group * 16 + level;
if (lut_idx >= 48) break;
unsigned short *lut = (unsigned short *)DAT_0048792c[lut_idx];
if (!lut) continue;
int tgt_r = fire_colors[group][level][0];
int tgt_g = fire_colors[group][level][1];
int tgt_b = fire_colors[group][level][2];
int alpha = blend_str[group][level];
/* Fill 4096 entries: 12-bit source (R4:G4:B4) -> RGB565 blend */
for (int src_idx = 0; src_idx < 4096; src_idx++) {
int sb = src_idx & 0xF;
int sg = (src_idx >> 4) & 0xF;
int sr = (src_idx >> 8) & 0xF;
/* Scale 4-bit to 8-bit */
int r = (sr * 255) / 15;
int g = (sg * 255) / 15;
int b = (sb * 255) / 15;
/* Alpha blend: result = src + (target - src) * alpha / 63 */
r = r + ((tgt_r - r) * alpha) / 63;
g = g + ((tgt_g - g) * alpha) / 63;
b = b + ((tgt_b - b) * alpha) / 63;
if (r < 0) r = 0; if (r > 255) r = 255;
if (g < 0) g = 0; if (g > 255) g = 255;
if (b < 0) b = 0; if (b > 255) b = 255;
/* Convert to RGB565 */
lut[src_idx] = (unsigned short)(
((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3)
);
}
}
}
}
/* ===== FUN_00422fc0 - Load explode.gfx (00422FC0) ===== */
/*
* Loads explosion sprite atlas from "data\explode.gfx".
* Format: 20 entries, each with 3-byte header [width, height, num_frames]
* followed by width*height*num_frames bytes of pixel data.
* Pixel transform: 0 -> 0xFF (transparent), else value - 1.
*
* Descriptors stored in DAT_00481f20 (8 bytes per entry, 20 entries = 160 bytes):
* +0: int pixel_offset (into DAT_0048787c)
* +4: byte width
* +5: byte height
* +6: byte num_frames
* +7: padding
*
* Pixel data stored in DAT_0048787c (430000 bytes allocated).
*/
int FUN_00422fc0(void)
{
FILE *f;
unsigned char header[3];
int pixel_offset = 0;
f = fopen("data\\explode.gfx", "rb");
if (!f) {
LOG("[FX] Failed to open data\\explode.gfx\n");
return 0;
}
for (int i = 0; i < 20; i++) {
int desc_base = i * 8;
/* Read 3-byte header */
if (fread(header, 1, 3, f) != 3) {
fclose(f);
return 0;
}
/* Store descriptor */
unsigned char *desc = (unsigned char *)DAT_00481f20 + desc_base;
*(int *)desc = pixel_offset; /* +0: pixel_offset */
desc[4] = header[0]; /* +4: width */
desc[5] = header[1]; /* +5: height */
desc[6] = header[2]; /* +6: num_frames */
/* Read pixel data */
int frame_size = (int)header[0] * (int)header[1];
int total_pixels = frame_size * (int)header[2];
unsigned char *pixels = (unsigned char *)DAT_0048787c + pixel_offset;
if (fread(pixels, 1, total_pixels, f) != (size_t)total_pixels) {
fclose(f);
return 0;
}
/* Transform pixels: 0 -> 0xFF (transparent), else value - 1 */
for (int j = 0; j < total_pixels; j++) {
if (pixels[j] == 0) {
pixels[j] = 0xFF;
} else {
pixels[j] = pixels[j] - 1;
}
}
pixel_offset += total_pixels;
}
fclose(f);
LOG("[FX] Loaded explode.gfx: %d bytes of pixel data\n", pixel_offset);
/* Dump sprite descriptors for debugging */
for (int i = 0; i < 20; i++) {
unsigned char *d = (unsigned char *)DAT_00481f20 + i * 8;
LOG("[FX] Sprite %d: w=%d h=%d frames=%d offset=%d\n",
i, d[4], d[5], d[6], *(int *)d);
}
return 1;
}
/* ===== FUN_0040d100 - Particle renderer (0040D100) ===== */
/*
* Renders explosion particles onto the given RGB565 buffer.
* Iterates all active particles (DAT_00489250 count, DAT_00481f34 array).
* Uses table-based alpha blending via DAT_0048792c and DAT_00489230.
*
* Particle structure (32 bytes at DAT_00481f34 + i*0x20):
* +0x00: int x (18-bit fixed point, >> 18 for pixels)
* +0x04: int y (18-bit fixed point)
* +0x08: int dx (velocity x)
* +0x0C: int dy (velocity y)
* +0x10: byte sprite_index (which explode.gfx entry)
* +0x11: byte frame_number (current animation frame)
* +0x12: byte frame_timer
* +0x13: byte unused
* +0x14: byte unused
* +0x15: byte color_index (blend LUT group)
*/
void FUN_0040d100(int buffer, int stride)
{
if (DAT_00489250 <= 0) return;
int i = 0;
int offset = 0;
while (i < DAT_00489250) {
unsigned char *part = (unsigned char *)DAT_00481f34 + offset;
int *pi = (int *)part;
unsigned char sprite_idx = part[0x10];
unsigned char *desc = (unsigned char *)DAT_00481f20 + (int)sprite_idx * 8;
int spr_w = (int)desc[4];
int spr_h = (int)desc[5];
int half_w = spr_w >> 1;
int half_h = spr_h >> 1;
/* Get pixel position from fixed-point */
int px = pi[0] >> 18;
int py = pi[1] >> 18;
/* Viewport culling */
if ((px + half_w) <= DAT_004806dc || (px - half_w) >= DAT_004806d0) {
i++; offset += 0x20; continue;
}
if ((py + half_h) <= DAT_004806e0 || (py - half_h) >= DAT_004806d4) {
i++; offset += 0x20; continue;
}
if (sprite_idx == 100) {
i++; offset += 0x20; continue;
}
/* Position relative to viewport */
int rx = (px - half_w) - DAT_004806dc;
int ry = (py - half_h) - DAT_004806e0;
/* Compute source pixel offset into explode pixel data */
int spr_pixel_base = *(int *)desc; /* pixel_offset from descriptor */
int frame_num = part[0x11];
int src_offset = spr_pixel_base + frame_num * spr_h * spr_w;
/* Destination pointer */
unsigned short *dst = (unsigned short *)
(buffer + ((DAT_004806e8 + ry) * stride + DAT_004806ec + rx) * 2);
int draw_w = spr_w;
int draw_h = spr_h;
int src_skip = 0;
int dst_skip = stride - spr_w;
/* Clip top */
if (ry < 0) {
src_offset += spr_w * (-ry);
dst += (-ry) * stride;
draw_h += ry;
} else if ((int)(spr_h + ry) > DAT_004806e4) {
draw_h = DAT_004806e4 - ry;
}
/* Clip left */
if (rx < 0) {
src_offset += (-rx);
src_skip = -rx;
dst_skip = (stride - spr_w) - rx;
draw_w += rx;
dst += (-rx);
} else if ((int)(spr_w + rx) > DAT_004806d8) {
src_skip = (spr_w - DAT_004806d8) + rx;
dst_skip = (src_skip - spr_w) + stride;
draw_w = spr_w - src_skip;
} else {
dst_skip = stride - spr_w;
src_skip = 0;
}
unsigned char color_idx = part[0x15];
/* Render pixels with alpha blending */
if (draw_h > 0) {
for (int row = 0; row < draw_h; row++) {
if (draw_w > 0) {
for (int col = 0; col < draw_w; col++) {
unsigned char px_val = *((unsigned char *)DAT_0048787c + src_offset);
if (px_val != 0xFF) {
/* Two-stage LUT blend:
* 1. Look up blend color table from [color_idx * 16 + pixel_value]
* 2. Look up source brightness from background pixel
* 3. Use brightness as index into blend color table */
int lut_idx = (int)color_idx * 16 + (int)px_val;
if (lut_idx < 48) {
unsigned short *blend_table =
(unsigned short *)DAT_0048792c[lut_idx];
if (blend_table) {
unsigned short bg_pixel = *dst;
unsigned short remap_idx =
((unsigned short *)DAT_00489230)[bg_pixel];
*dst = blend_table[remap_idx];
}
}
}
dst++;
src_offset++;
}
}
src_offset += src_skip;
dst += dst_skip;
}
}
i++;
offset += 0x20;
}
}
/* ===== FUN_0040c280 - General entity sprite renderer (0040C280) ===== */
/*
* Renders a single sprite frame onto the RGB565 buffer.
* param_1: sprite/frame index into DAT_00489234
* param_2: X position relative to viewport
* param_3: Y position relative to viewport
* param_4: color palette index (0 = no palette)
* param_5: buffer pointer
* param_6: buffer stride (pixels per row)
* param_7: blend mode (0 = no blend)
*
* Pixel data source: DAT_00487ab4 (16-bit sprite pixels, index via DAT_00489234)
* Dimensions: DAT_00489e8c[sprite] = width, DAT_00489e88[sprite] = height
*/
void FUN_0040c280(int param_1, int param_2, int param_3, unsigned char param_4,
int param_5, int param_6, unsigned char param_7)
{
int pixel_base = ((int *)DAT_00489234)[param_1];
int spr_w = (int)((unsigned char *)DAT_00489e8c)[param_1];
int spr_h = (int)((unsigned char *)DAT_00489e88)[param_1];
if (spr_w <= 0 || spr_h <= 0) return;
short *dst = (short *)(param_5
+ ((DAT_004806e8 + param_3) * param_6 + DAT_004806ec + param_2) * 2);
int src_idx = pixel_base;
int draw_w = spr_w;
int draw_h = spr_h;
int src_skip = 0;
int dst_skip;
/* Clip top */
if ((int)param_3 < 0) {
src_idx += spr_w * (-(int)param_3);
dst += (-(int)param_3) * param_6;
draw_h = spr_h + (int)param_3;
} else if ((int)(param_3 + spr_h) > DAT_004806e4) {
draw_h = DAT_004806e4 - (int)param_3;
}
/* Clip left */
if (param_2 < 0) {
src_idx += (-param_2);
dst += (-param_2);
src_skip = -param_2;
draw_w = spr_w + param_2;
dst_skip = param_6 - spr_w - param_2;
} else if ((int)(param_2 + spr_w) > DAT_004806d8) {
src_skip = (spr_w + param_2) - DAT_004806d8;
draw_w = spr_w - src_skip;
dst_skip = param_6 - draw_w;
} else {
dst_skip = param_6 - spr_w;
src_skip = 0;
}
if (draw_w <= 0 || draw_h <= 0) return;
/* Simple non-blended, non-palette mode (param_4==0, param_7==0) - used by intro entities */
if (param_7 == 0 && param_4 == 0) {
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
short pixel = ((short *)DAT_00487ab4)[src_idx];
if (pixel != 0) {
*dst = pixel;
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
}
else if (param_7 == 0 && param_4 != 0) {
/* Palette-remapped mode */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
unsigned short pixel = ((unsigned short *)DAT_00487ab4)[src_idx];
if (pixel != 0) {
unsigned short remap = ((unsigned short *)DAT_00489230)[(int)pixel];
*dst = ((short *)DAT_004876a4[param_4])[(int)remap];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
}
else {
/* Blended mode (param_7 != 0) */
int blend_idx = (param_7 < 0x1C) ? 0x1C : (int)param_7;
if (param_4 == 0) {
/* Path 3: blend only (no palette) */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
unsigned short pixel = ((unsigned short *)DAT_00487ab4)[src_idx];
if (pixel != 0) {
unsigned short remap = ((unsigned short *)DAT_00489230)[(int)pixel];
*dst = ((short *)DAT_004876a4[blend_idx])[(int)remap];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
} else {
/* Path 4: blend + palette (double LUT lookup) — matches original 0040C280.
* First applies blend/darkness, then palette color remap. Used for
* troopers/entities in dark tile areas (both palette and darkness non-zero). */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
unsigned short pixel = ((unsigned short *)DAT_00487ab4)[src_idx];
if (pixel != 0) {
unsigned short remap1 = ((unsigned short *)DAT_00489230)[(int)pixel];
unsigned short dark = ((unsigned short *)DAT_004876a4[blend_idx])[(int)remap1];
unsigned short remap2 = ((unsigned short *)DAT_00489230)[(int)dark];
*dst = ((short *)DAT_004876a4[param_4])[(int)remap2];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
}
}
}
/* ===== FUN_0040c590 - Explosion/Ship sprite blitter (0040C590) ===== */
/*
* Similar to FUN_0040c280 but reads from DAT_00487aac (pre-rendered rotated frames).
* param_1: frame index (angle-based)
* param_2: player index (selects explosion set)
* param_3: X position relative to viewport
* param_4: Y position relative to viewport
* param_5: palette index (0 = no palette remap)
* param_6: buffer pointer
* param_7: buffer stride
* param_8: blend mode (0 = no blend)
*/
void FUN_0040c590(int param_1, int param_2, int param_3, int param_4,
unsigned char param_5, int param_6, int param_7, unsigned char param_8)
{
int base = (int)DAT_00487aac;
int spr_h = *(int *)(base + 0x186a4 + param_2 * 0x186a8);
int spr_w = *(int *)(base + param_2 * 0x186a8 + 100000);
int src_idx = spr_w * spr_h * param_1;
short *dst = (short *)(param_6 + ((DAT_004806e8 + param_4) * param_7 + DAT_004806ec + param_3) * 2);
int draw_h, draw_w, src_skip, dst_skip;
/* Clip top/bottom */
if (param_4 < 0) {
src_idx += spr_w * (-param_4);
dst += (-param_4) * param_7;
draw_h = spr_h + param_4;
} else {
draw_h = spr_h;
if (DAT_004806e4 < spr_h + param_4)
draw_h = DAT_004806e4 - param_4;
}
/* Clip left/right */
if (param_3 < 0) {
src_idx -= param_3;
dst_skip = (param_7 - spr_w) - param_3;
dst += -param_3;
src_skip = -param_3;
draw_w = param_3 + spr_w;
} else if (DAT_004806d8 < spr_w + param_3) {
src_skip = (spr_w - DAT_004806d8) + param_3;
dst_skip = (src_skip - spr_w) + param_7;
draw_w = spr_w - src_skip;
} else {
dst_skip = param_7 - spr_w;
src_skip = 0;
draw_w = spr_w;
}
if (draw_w <= 0 || draw_h <= 0) return;
unsigned char blend = param_8;
if (blend == 0) {
if (param_5 == 0) {
/* Direct copy mode */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
int pixel_offset = (src_idx + param_2 * 0xc354) * 2;
short pixel = *(short *)(base + pixel_offset);
if (pixel != 0) *dst = pixel;
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
} else {
/* Palette-remap mode */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
int pixel_offset = (src_idx + param_2 * 0xc354) * 2;
unsigned short pixel = *(unsigned short *)(base + pixel_offset);
if (pixel != 0) {
unsigned short remap = ((unsigned short *)DAT_00489230)[pixel];
*dst = ((short *)DAT_004876a4[param_5])[remap];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
}
} else {
/* Blended mode */
int blend_idx = (blend < 0x1C) ? 0x1C : (int)blend;
if (param_5 == 0) {
/* Blend only (no palette) */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
int pixel_offset = (src_idx + param_2 * 0xc354) * 2;
unsigned short pixel = *(unsigned short *)(base + pixel_offset);
if (pixel != 0) {
unsigned short remap = ((unsigned short *)DAT_00489230)[pixel];
*dst = ((short *)DAT_004876a4[blend_idx])[remap];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
} else {
/* Palette + blend (double LUT lookup) */
for (int row = 0; row < draw_h; row++) {
for (int col = 0; col < draw_w; col++) {
int pixel_offset = (src_idx + param_2 * 0xc354) * 2;
unsigned short pixel = *(unsigned short *)(base + pixel_offset);
if (pixel != 0) {
unsigned short remap1 = ((unsigned short *)DAT_00489230)[pixel];
unsigned short dark = ((unsigned short *)DAT_004876a4[blend_idx])[remap1];
unsigned short remap2 = ((unsigned short *)DAT_00489230)[dark];
*dst = ((short *)DAT_004876a4[param_5])[remap2];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_skip;
}
}
}
}
/* ===== FUN_0040c940 - Shield/Bubble renderer (0040C940) ===== */
/*
* Renders a translucent shield bubble around an entity.
* Uses sprite 0x1A3 as a grayscale alpha mask.
* param_1: X position (18-bit fixed point)
* param_2: Y position (18-bit fixed point)
* param_3: buffer pointer
* param_4: buffer stride
* param_5: intensity (subtracted from alpha for fade-in effect)
*/
void FUN_0040c940(unsigned int param_1, unsigned int param_2, unsigned int param_3,
int param_4, int param_5)
{
unsigned int spr_w = (unsigned int)((unsigned char *)DAT_00489e8c)[0x1a3];
int screen_x = ((int)param_1 >> 0x12) - (int)(spr_w >> 1) - DAT_004806dc;
unsigned int spr_h = (unsigned int)((unsigned char *)DAT_00489e88)[0x1a3];
int screen_y = ((int)param_2 >> 0x12) - (int)(spr_h >> 1) - DAT_004806e0;
int src_idx = ((int *)DAT_00489234)[0x1a3];
unsigned short *dst = (unsigned short *)(param_3 + ((DAT_004806e8 + screen_y) * param_4 + DAT_004806ec + screen_x) * 2);
unsigned int draw_h, draw_w;
int src_skip, dst_stride;
/* Clip top/bottom */
if (screen_y < 0) {
src_idx += spr_w * (-screen_y);
dst += (-screen_y) * param_4;
draw_h = spr_h + screen_y;
} else {
draw_h = spr_h;
if (DAT_004806e4 < (int)(spr_h + screen_y))
draw_h = DAT_004806e4 - screen_y;
}
/* Clip left/right */
if (screen_x < 0) {
dst_stride = (param_4 - spr_w) - screen_x;
src_idx -= screen_x;
dst += -screen_x;
src_skip = -screen_x;
draw_w = spr_w + screen_x;
} else if (DAT_004806d8 < (int)(spr_w + screen_x)) {
src_skip = (spr_w - DAT_004806d8) + screen_x;
dst_stride = (src_skip - spr_w) + param_4;
draw_w = spr_w - src_skip;
} else {
src_skip = 0;
dst_stride = param_4 - spr_w;
draw_w = spr_w;
}
if ((int)draw_h <= 0 || (int)draw_w <= 0) return;
for (unsigned int row = 0; row < draw_h; row++) {
for (unsigned int col = 0; col < draw_w; col++) {
int alpha = (int)(((unsigned char *)DAT_00489e94)[src_idx] >> 4) - param_5;
if (alpha >= 0x11) alpha = 0x10;
if (alpha > 0) {
unsigned short remap = ((unsigned short *)DAT_00489230)[(unsigned int)*dst];
*dst = ((unsigned short *)DAT_0048792c[31 + alpha])[remap];
}
dst++;
src_idx++;
}
src_idx += src_skip;
dst += dst_stride;
}
}
/* ===== FUN_0040dbd0 - Static entity renderer (0040DBD0) ===== */
/*
* NOTE: This is DEAD CODE in the original binary.
* DAT_00489e98 (stride 0x10, count DAT_00489274) is allocated but never populated
* — the spawning loop in FUN_0041bfe0 has condition "i < 0" (always false).
* DAT_00489274 is always 0, so this renderer never executes.
* Real turret/effect rendering uses FUN_0040d930 (DAT_00487780/DAT_00489264).
*/
void FUN_0040dbd0(int param_1, unsigned int param_2)
{
int offset = 0;
int base = (int)DAT_00489e98;
for (int i = 0; i < DAT_00489274; i++) {
int px = *(int *)(offset + base) >> 0x12;
if (DAT_004806dc < px + 0xe && px - 0xe < DAT_004806d0) {
int py = *(int *)(offset + 4 + base) >> 0x12;
if (DAT_004806e0 < py + 0xe && py - 0xe < DAT_004806d4) {
int angle = *(int *)(offset + 8 + base);
int sprite = ((int)(angle + (angle >> 0x1f & 0x7f)) >> 7) + 0xf3c;
int sw = (int)((unsigned char *)DAT_00489e8c)[sprite];
int sh = (int)((unsigned char *)DAT_00489e88)[sprite];
/* Darkness from tilemap tile type */
unsigned char tile = ((unsigned char *)DAT_0048782c)[(py << ((unsigned char)DAT_00487a18 & 0x1f)) + px];
unsigned char darkness = ((unsigned char *)DAT_00487928)[(unsigned int)tile * 0x20 + 4];
FUN_0040c280(sprite, (px - (sw >> 1)) - DAT_004806dc,
(py - (sh >> 1)) - DAT_004806e0, 0, param_1, param_2, darkness);
base = (int)DAT_00489e98;
}
}
offset += 0x10;
}
}
/* ===== FUN_0040dce0 - Dynamic entity/trooper renderer (0040DCE0) ===== */
/*
* Renders troopers (foot soldiers). Two layers: optional helmet + body.
* Array: DAT_00487884, stride 0x40, count DAT_0048924c.