-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimExporter.c
1568 lines (1246 loc) · 49.3 KB
/
animExporter.c
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
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include <direct.h>
#define mkdir _mkdir
#else
#include <sys/stat.h>
#define mkdir(a) { mode_t perms = 666; mkdir((a), perms); }
#endif
#include "types.h"
#include "ArenaAlloc.h"
// Needs to be included before animExporter.h
#include "animation_commands.h"
#include "animExporter.h"
#define SizeofArray(array) ((sizeof(array)) / (sizeof(array[0])))
#define OffsetPointer(ptrToOffset) (((u8*)(ptrToOffset)) + *(ptrToOffset))
// TODO(Jace): Allow for custom animation table size
#define SA1_ANIMATION_COUNT 908
#define SA2_ANIMATION_COUNT 1133
#define SA3_ANIMATION_COUNT 1524
#define KATAM_ANIMATION_COUNT 939
const u32 g_TotalAnimationCount[] = {
/* 1 */ [SA1] = SA1_ANIMATION_COUNT,
/* 2 */ [SA2] = SA2_ANIMATION_COUNT,
/* 3 */ [SA3] = SA3_ANIMATION_COUNT,
/* 10 */ [KATAM] = KATAM_ANIMATION_COUNT,
};
// Creating separate files for each animation takes a lot of time (4 seconds for me),
// so print to stdout per default, which is instant if it's directed to a file with ' > file.out'
#define PRINT_TO_STDOUT FALSE
// Identifiers
#define AnimCmd_GetTiles -1
#define AnimCmd_GetPalette -2
#define AnimCmd_JumpBack -3
#define AnimCmd_End -4
#define AnimCmd_PlaySoundEffect -5
#define AnimCmd_AddHitbox -6
#define AnimCmd_TranslateSprite -7
#define AnimCmd_8 -8
#define AnimCmd_SetIdAndVariant -9
#define AnimCmd_10 -10
#define AnimCmd_SetSpritePriority -11
#define AnimCmd_12 -12
#define AnimCmd_DisplayFrame (AnimCmd_12-1)
const char* animCommands[] = {
"AnimCmd_GetTiles",
"AnimCmd_GetPalette",
"AnimCmd_JumpBack",
"AnimCmd_End",
"AnimCmd_PlaySoundEffect",
"AnimCmd_AddHitbox",
"AnimCmd_TranslateSprite",
"AnimCmd_8",
"AnimCmd_SetIdAndVariant",
"AnimCmd_10",
"AnimCmd_SetSpritePriority",
"AnimCmd_12",
// NOTE(Jace): This is NOT a "real" command, but a
// notification for the game that it should
// display a specfic frame, and for how long.
// Thanks to @MainMemory_ for reminding me on how they work!
"AnimCmd_Display",
};
const char* macroNames[SizeofArray(animCommands)] = {
"mGetTiles",
"mGetPalette",
"mJumpBack",
"mEnd",
"mPlaySoundEffect",
"mAddHitbox",
"mTranslateSprite",
"mAnimCmd8",
"mSetIdAndVariant",
"mAnimCmd10",
"mAnimCmdSetSpritePriority",
"mAnimCmd12",
"mDisplayFrame"
};
// %s placeholders:
// 1) Macro name (e.g. 'mGetTiles')
// 2) Cmd identifier (e.g. 'AnimCmd_GetTiles')
// 3) Array-Base (e.g. 'gObjPalettes_4bpp')
const char* macros[SizeofArray(animCommands)] = {
[~(AnimCmd_GetTiles)] =
".macro %s tile_index:req, num_tiles_to_copy:req\n"
".4byte %s\n"
" .4byte \\tile_index\n"
" .4byte \\num_tiles_to_copy\n"
".endm\n",
[~(AnimCmd_GetPalette)] =
".macro %s pal_ptr:req, num_colors_to_copy:req, insert_offset:req\n"
".4byte %s\n"
" .4byte (\\pal_ptr - %s) / 0x20\n"
" .2byte \\num_colors_to_copy\n"
" .2byte \\insert_offset\n"
".endm\n",
[~(AnimCmd_JumpBack)] =
".macro %s jmpTarget:req\n"
".4byte %s\n"
" .4byte ((.-0x4) - \\jmpTarget)\n"
".endm\n",
[~(AnimCmd_End)] =
".macro %s\n"
".4byte %s\n"
".endm\n",
[~(AnimCmd_PlaySoundEffect)] =
".macro %s songId:req\n"
".4byte %s\n"
" .2byte \\songId\n"
" .space 2\n" /* Padding */
".endm\n",
// TODO: Parameters might be wrong
[~(AnimCmd_AddHitbox)] =
".macro %s index:req, left:req, top:req, right:req, bottom:req\n"
".4byte %s\n"
" .4byte \\index\n"
" .byte \\left, \\top, \\right, \\bottom\n"
".endm\n",
[~(AnimCmd_TranslateSprite)] =
".macro %s x:req y:req\n"
".4byte %s\n"
" .2byte \\x\n"
" .2byte \\y\n"
".endm\n",
// TODO: Parameters might be wrong
[~(AnimCmd_8)] =
".macro %s unk4:req, unk8:req\n"
".4byte %s\n"
" .4byte \\unk4\n"
" .4byte \\unk8\n"
".endm\n",
[~(AnimCmd_SetIdAndVariant)] =
".macro %s animId:req, variant:req\n"
".4byte %s\n"
" .2byte \\animId\n"
" .2byte \\variant\n"
".endm\n",
[~(AnimCmd_10)] =
".macro %s unk4:req, unk8:req, unkC:req\n"
".4byte %s\n"
" .4byte \\unk4\n"
" .4byte \\unk8\n"
" .4byte \\unkC\n"
".endm\n",
[~(AnimCmd_SetSpritePriority)] =
".macro %s unk4:req\n"
".4byte %s\n"
" .4byte \\unk4\n"
".endm\n",
[~(AnimCmd_12)] =
".macro %s unk4:req\n"
".4byte %s\n"
" .4byte \\unk4\n"
".endm\n",
[~(AnimCmd_DisplayFrame)] =
".macro %s displayFor:req frameIndex:req\n"
" .4byte \\displayFor, \\frameIndex\n"
".endm\n",
};
static void printAnimationTable(FILE* fileStream, DynTable* dynTable, AnimationTable* animTable, LabelStrings* labels);
static u16 countVariants(u8* rom, AnimationTable* animTable, u32 animId);
static StringId pushLabel(LabelStrings* db, MemArena* stringArena, MemArena* offsetArena, char* label);
static long int
getFileSize(FILE* file) {
// Get file stream's position
long int prevPos = ftell(file);
long int size;
// Get file size
fseek(file, 0, SEEK_END);
size = ftell(file);
// Set file stream offset to its previous offset
fseek(file, prevPos, SEEK_SET);
return size;
}
static void*
romToVirtual(u8* rom, u32 gbaPointer) {
// GBA ROM Pointers can only go from 0x08000000 to 0x09FFFFFF (32MB max.)
u8 pointerMSB = ((gbaPointer & 0xFF000000) >> 24);
if (pointerMSB == 8 || pointerMSB == 9)
return (u32*)(rom + (gbaPointer & 0x00FFFFFF));
else
return NULL;
}
// TODO: Can indenting be done with character codes?
static void
printHeaderLine(FILE* fileStream, const char* name, int value, int rightAlign) {
// Print the amount of table entries
fprintf(fileStream, ".equ %s,", name);
// Print the indent
s16 indentSpaces = rightAlign - strlen(name);
for (; indentSpaces > 0; indentSpaces--)
fprintf(fileStream, " ");
// Print the value
fprintf(fileStream, "%d\n", value);
}
static void
printMacros(FILE* fileStream) {
for (int i = 0; i < SizeofArray(macros); i++) {
if(i == ~AnimCmd_GetPalette) {
fprintf(fileStream, macros[i], macroNames[i], animCommands[i], "gObjPalettes_4bpp");
} else {
fprintf(fileStream, macros[i], macroNames[i], animCommands[i]);
}
fprintf(fileStream, "\n");
}
}
static void
printFileHeader(FILE* fileStream, s32 entryCount) {
// Set the section
fprintf(fileStream, "\t.section .rodata\n");
fprintf(fileStream, "\n");
const char* entryCountName = "NUM_ANIMATION_TABLE_ENTRIES";
// Find the biggest string out of the 'animCommands' array
s16 rightAlign = strlen(entryCountName);
for (int i = 0; i < SizeofArray(animCommands); i++)
rightAlign = Max(rightAlign, strlen(animCommands[i]));
// Space behind the comma
rightAlign += 1;
// Print definition of each Cmd's constant
for(int i = 0; i < SizeofArray(animCommands); i++) {
printHeaderLine(fileStream, animCommands[i], ((-1) - i), rightAlign);
}
fprintf(fileStream, "\n");
// Print the number of entries in the table
printHeaderLine(fileStream, entryCountName, entryCount, rightAlign);
fprintf(fileStream, "\n\n");
// Macros depend on knowing the AnimCmd_xyz values, so they have to be printed together.
printMacros(fileStream);
}
static char*
getStringFromId(LabelStrings* label, StringId id) {
char* result = NULL;
if (id < label->count) {
result = &label->strings[label->offsets[id]];
}
return result;
}
static void
printCommand(FILE* fileStream, DynTableAnimCmd* inAnimCmd, LabelStrings* labels) {
ACmd* inCmd = &inAnimCmd->cmd;
// Print macro name
s32 nottedCmdId = ~(inCmd->id);
if (nottedCmdId >= 0)
fprintf(fileStream, "\t%s ", macroNames[nottedCmdId]);
else
fprintf(fileStream, "\t%s ", macroNames[~(AnimCmd_DisplayFrame)]);
// Print the command paramters
switch (inCmd->id) {
case AnimCmd_GetTiles: {
ACmd_GetTiles* cmd = &inCmd->_tiles;
fprintf(fileStream, "0x%X %d\n", cmd->tileIndex, cmd->numTilesToCopy);
} break;
case AnimCmd_GetPalette: {
ACmd_GetPalette* cmd = &inCmd->_pal;
char palName[64];
#if 0
sprintf(palName, "palObj%03d", cmd->palId);
fprintf(fileStream, "%s %d 0x%X\n", palName, cmd->numColors, cmd->insertOffset);
#else
fprintf(fileStream, "%d %d 0x%X\n", cmd->palId, cmd->numColors, cmd->insertOffset);
#endif
} break;
case AnimCmd_JumpBack: {
ExCmd_JumpBack* cmd = &inCmd->_exJump;
StringId jmpCmdLabel = inAnimCmd->label;
StringId targetLabel = ((DynTableAnimCmd*)cmd->jumpTarget)->label;
char* jmpCmdString = getStringFromId(labels, jmpCmdLabel);
char* targetCmdString = getStringFromId(labels, targetLabel);
fprintf(fileStream, "%s\n\n", targetCmdString);
} break;
case AnimCmd_End: {
ACmd_End* cmd = &inCmd->_end;
fprintf(fileStream, "\n\n");
} break;
case AnimCmd_PlaySoundEffect: {
ACmd_PlaySoundEffect* cmd = &inCmd->_sfx;
fprintf(fileStream, "%u\n", cmd->songId);
} break;
case AnimCmd_AddHitbox: {
ACmd_AddHitbox* cmd = &inCmd->_hitbox;
// TODO: Once the tool outputs data as C files, output these as signed bytes (ARM macros don't like '-xyz')
fprintf(fileStream, "%d 0x%02X 0x%02X 0x%02X 0x%02X\n",
cmd->hitbox.index,
(u8)cmd->hitbox.left,
(u8)cmd->hitbox.top,
(u8)cmd->hitbox.right,
(u8)cmd->hitbox.bottom);
} break;
case AnimCmd_TranslateSprite: {
ACmd_TranslateSprite* cmd = &inCmd->_translate;
fprintf(fileStream, "%d %d\n", cmd->x, cmd->y);
} break;
case AnimCmd_8: {
ACmd_8* cmd = &inCmd->_8;
fprintf(fileStream, "0x%x 0x%x", cmd->unk4, cmd->unk8);
} break;
case AnimCmd_SetIdAndVariant: {
ACmd_SetIdAndVariant* cmd = &inCmd->_animId;
// TODO: Insert ANIM_<whatever> from "include/constants/animations.h"
fprintf(fileStream, "%d %d\n", cmd->animId, cmd->variant);
} break;
case AnimCmd_10: {
ACmd_10* cmd = &inCmd->_10;
fprintf(fileStream, "0x%x 0x%x 0x%x\n", cmd->unk4, cmd->unk8, cmd->unkC);
} break;
case AnimCmd_SetSpritePriority: {
ACmd_SetSpritePriority* cmd = &inCmd->_prio;
fprintf(fileStream, "0x%x\n", cmd->unk4);
} break;
case AnimCmd_12: {
ACmd_12* cmd = &inCmd->_12;
fprintf(fileStream, "0x%x\n", cmd->unk4);
} break;
default: {
ACmd_Display* cmd = &inCmd->_display;
if (cmd->cmdId < 0) {
// This shouldn't be reached.
fprintf(stderr,
"Exporting failed, impossible state reached.\n"
"Animation Command was invalid: %d", cmd->cmdId);
exit(-1);
}
else {
if (cmd->cmdId >= ROM_BASE || cmd->frameIndex >= ROM_BASE) {
// @BUG! If we land here, that means a pointer was mistaken as an "unknown command"
fprintf(fileStream, "\t.4byte 0x%07X, 0x%07X\n\n", cmd->displayForNFrames, cmd->frameIndex);
assert(FALSE);
} else
fprintf(fileStream, "%d %d\n\n", cmd->displayForNFrames, cmd->frameIndex);
}
}
}
}
static void
printAnimationDataFile(FILE* fileStream, DynTable* dynTable,
LabelStrings* labels, MemArena *stringArena, MemArena* stringOffsetArena,
u32 numAnims, OutFiles* outFiles) {
AnimationData anim;
printFileHeader(outFiles->header, numAnims);
char filename[256];
DynTableAnim* table = dynTable->animations;
u16* variantCounts = dynTable->variantCounts;
for (int i = 0; i < numAnims; i++) {
if (table[i].offsetVariants >= 0) {
s32* variantOffsets = (s32*)OffsetPointer(&table[i].offsetVariants);
u16 numVariants = variantCounts[i];
char labelBuffer[256];
char* animName = getStringFromId(labels, table[i].name);
// Print all variants' commands
for (int variantId = 0; variantId < numVariants; variantId++) {
// currCmd -> start of variant
s32* offset = &variantOffsets[variantId];
DynTableAnimCmd* currCmd = (DynTableAnimCmd*)OffsetPointer(offset);
currCmd->flags |= ACMD_FLAG__IS_START_OF_ANIM;
int labelId = 0;
while (TRUE) {
// Maybe print label
if (currCmd->flags & (ACMD_FLAG__IS_START_OF_ANIM | ACMD_FLAG__NEEDS_LABEL)) {
sprintf(labelBuffer, "%s__v%d_l%d", animName, variantId, labelId);
StringId varLabel = pushLabel(labels, stringArena, stringOffsetArena, labelBuffer);
currCmd->label = varLabel;
fprintf(fileStream, "%s: @ %07X\n", labelBuffer, currCmd->address);
labelId++;
}
printCommand(fileStream, currCmd, labels);
// Break loop after printing jump/end command
if((currCmd->cmd.id == AnimCmd_End)
|| (currCmd->cmd.id == AnimCmd_JumpBack)
|| (currCmd->cmd.id == AnimCmd_SetIdAndVariant))
break;
currCmd++;
}
}
if (table[i].name) { // Print variant pointers
char* entryName = getStringFromId(labels, table[i].name);
if (entryName)
fprintf(fileStream, "%s:\n", entryName);
for (int variantId = 0; variantId < numVariants; variantId++) {
fprintf(fileStream, "\t.4byte %s__v%d_l%d\n", animName, variantId, 0);
}
fprintf(fileStream, "\n\n");
}
}
}
}
static bool
wasReferencedBefore(AnimationTable* animTable, int entryIndex, int* prevIndex) {
s32* cursor = animTable->data;
bool wasReferencedBefore = FALSE;
for (int index = 0; index < entryIndex; index++) {
if (cursor[index] == cursor[entryIndex]) {
wasReferencedBefore = TRUE;
if (prevIndex)
*prevIndex = index;
break;
}
}
return wasReferencedBefore;
}
static void
printAnimationTable(FILE* fileStream, DynTable* dynTable, AnimationTable* table, LabelStrings* labels) {
const char* animTableVarName = "gAnimations";
fprintf(fileStream,
"\n"
".align 2, 0\n"
".global %s\n"
"%s:\n", animTableVarName, animTableVarName);
s32 numAnims = table->entryCount;
for(int i = 0; i < numAnims; i++) {
if(table->data[i]) {
int prevReferenceIndex;
int animId = -1;
if (wasReferencedBefore(table, i, &prevReferenceIndex))
animId = prevReferenceIndex;
else
animId = i;
assert(animId >= 0 && animId < numAnims);
char* animName = getStringFromId(labels, dynTable->animations[animId].name);
fprintf(fileStream, "\t.4byte %s\n", animName);
} else {
fprintf(fileStream, "\t.4byte 0\n");
}
}
fprintf(fileStream, ".size %s,.-%s\n\n", animTableVarName, animTableVarName);
}
#define getRomRegion(rom) (rom[0xAF])
// TODO: Check a few more things in the ROM Header
static int
getRomIndex(u8* rom) {
eGame result = UNKNOWN;
if (!strncmp((rom + 0xA0), "SONIC ADVANCASOP", 16ul)) {
result = SA1;
}
if(!strncmp((rom + 0xA0), "SONICADVANC2A2N", 15ul) // NTSC
|| !strncmp((rom + 0xA0), "SONIC ADVANCA2N", 15ul) // PAL
) {
result = SA2;
}
if (!strncmp((rom + 0xA0), "SONIC ADVANCB3SP8P", 18ul)) {
result = SA3;
}
if (!strncmp((rom + 0xA0), "AGB KIRBY AMB8K", 15ul)) {
result = KATAM;
}
return result;
}
void getSpriteTables(u8* rom, int gameIndex, SpriteTables* tables) {
RomPointer* result = NULL;
char region = getRomRegion(rom);
switch (gameIndex) {
case 1: {
//SA1 (PAL)
result = romToVirtual(rom, 0x0801A78C); // SA1(PAL)
result = romToVirtual(rom, *result);
} break;
case 2: {
result = romToVirtual(rom, 0x0801A5DC); // SA2(PAL, NTSC)
result = romToVirtual(rom, *result);
} break;
case 3: {
result = romToVirtual(rom, 0x08000404);// SA3(PAL, NTSC)
result = romToVirtual(rom, *result);
} break;
// Kirby ATAM
case 10: {
result = romToVirtual(rom, 0x080002E0); // (PAL, maybe others?)
result = romToVirtual(rom, *result);
}
default: {
memset(tables, 0, sizeof(*tables));
}
}
SpriteTablesROM* romTable = (SpriteTablesROM*)result;
tables->animations = romToVirtual(rom, romTable->animations);
tables->dimensions = romToVirtual(rom, romTable->dimensions);
tables->oamData = romToVirtual(rom, romTable->oamData);
tables->palettes = romToVirtual(rom, romTable->palettes);
tables->tiles_4bpp = romToVirtual(rom, romTable->tiles_4bpp);
tables->tiles_8bpp = romToVirtual(rom, romTable->tiles_8bpp);
}
// Input:
// NOTE: Indices can go from 0-255 -> count can be 256, so count has to be a u16
static u16
countVariants(u8* rom, AnimationTable* animTable, u32 animId) {
u16 count = 0;
RomPointer anim = animTable->data[animId];
if (anim != 0) {
RomPointer* variants = romToVirtual(rom, anim);
for (;;) {
// Check whether we're at a pointer,
// and not the beginning of 'animTable' (which points at the individual anims)
if((romToVirtual(rom, variants[count]) != NULL)
/* SA1 corner-case */ && ((RomPointer*)romToVirtual(rom, variants[count]) < variants)
&& (&variants[count] < animTable->data)) {
// Found another variant
count++;
}
else {
// No more variants found
break;
}
}
}
return count;
}
DynTableAnimCmd*
fillVariantFromRom(MemArena* arena, u8* rom, const RomPointer* variantInRom) {
ACmd* cmdInRom = romToVirtual(rom, *variantInRom); // A 'real' pointer to the current cmd inside the ROM
RomPointer cmdAddress = *variantInRom; // The ROM pointer the current cmd is at
DynTableAnimCmd* variantStart = memArenaReserve(arena, sizeof(DynTableAnimCmd));
DynTableAnimCmd* currCmd = variantStart;
bool breakLoop = FALSE;
while (!breakLoop && (void*)cmdInRom < (void*)variantInRom) {
currCmd->address = cmdAddress;
u32 structSize = 0;
if (cmdInRom->id < 0) {
currCmd->cmd.id = cmdInRom->id;
switch (cmdInRom->id) {
case AnimCmd_GetTiles:
currCmd->cmd._tiles.tileIndex = cmdInRom->_tiles.tileIndex;
currCmd->cmd._tiles.numTilesToCopy = cmdInRom->_tiles.numTilesToCopy;
structSize = sizeof(ACmd_GetTiles);
break;
case AnimCmd_GetPalette:
currCmd->cmd._pal.palId = cmdInRom->_pal.palId;
currCmd->cmd._pal.numColors = cmdInRom->_pal.numColors;
currCmd->cmd._pal.insertOffset = cmdInRom->_pal.insertOffset;
structSize = sizeof(ACmd_GetPalette);
break;
// This sets the jump-address, to make it easier to
// find the commands needing a headline.
case AnimCmd_JumpBack:
currCmd->cmd._jump.offset = cmdInRom->_jump.offset;
currCmd->jmpTarget = cmdAddress - cmdInRom->_jump.offset*sizeof(s32);
structSize = sizeof(ACmd_JumpBack);
// We will use this label to calculate the offset for the jump
currCmd->flags |= ACMD_FLAG__NEEDS_LABEL;
for (DynTableAnimCmd* check = variantStart; check < currCmd; check++) {
if (check->address == currCmd->jmpTarget) {
check->flags |= ACMD_FLAG__IS_POINTED_TO;
StringId labelId = check->label;
currCmd->cmd._exJump.jumpTarget = check;
break;
}
}
breakLoop = TRUE;
break;
// 'End' command
case AnimCmd_End:
structSize = sizeof(ACmd_End);
breakLoop = TRUE;
break;
case AnimCmd_PlaySoundEffect:
currCmd->cmd._sfx.songId = cmdInRom->_sfx.songId;
structSize = sizeof(ACmd_PlaySoundEffect);
break;
case AnimCmd_AddHitbox:
currCmd->cmd._hitbox.hitbox.index = cmdInRom->_hitbox.hitbox.index;
currCmd->cmd._hitbox.hitbox.left = cmdInRom->_hitbox.hitbox.left;
currCmd->cmd._hitbox.hitbox.top = cmdInRom->_hitbox.hitbox.top;
currCmd->cmd._hitbox.hitbox.right = cmdInRom->_hitbox.hitbox.right;
currCmd->cmd._hitbox.hitbox.bottom = cmdInRom->_hitbox.hitbox.bottom;
structSize = sizeof(ACmd_AddHitbox);
break;
hitboxAnimCmd_TranslateSprite:
currCmd->cmd._translate.x = cmdInRom->_translate.x;
currCmd->cmd._translate.y = cmdInRom->_translate.y;
structSize = sizeof(ACmd_TranslateSprite);
break;
case AnimCmd_8:
currCmd->cmd._8.unk4 = cmdInRom->_8.unk4;
currCmd->cmd._8.unk8 = cmdInRom->_8.unk8;
structSize = sizeof(ACmd_8);
break;
case AnimCmd_SetIdAndVariant:
currCmd->cmd._animId.animId = cmdInRom->_animId.animId;
currCmd->cmd._animId.variant = cmdInRom->_animId.variant;
structSize = sizeof(ACmd_SetIdAndVariant);
breakLoop = TRUE;
break;
case AnimCmd_10:
currCmd->cmd._10.unk4 = cmdInRom->_10.unk4;
currCmd->cmd._10.unk8 = cmdInRom->_10.unk8;
currCmd->cmd._10.unkC = cmdInRom->_10.unkC;
structSize = sizeof(ACmd_10);
break;
case AnimCmd_SetSpritePriority:
currCmd->cmd._prio.unk4 = cmdInRom->_prio.unk4;
structSize = sizeof(ACmd_SetSpritePriority);
break;
case AnimCmd_12:
currCmd->cmd._12.unk4 = cmdInRom->_12.unk4;
structSize = sizeof(ACmd_12);
break;
}
}
else {
// NOTE: If the "word" here is negative, it is actually a COMMAND!
// A corner-case thanks to anim_0777 in SA1...
if (cmdInRom->id > ROM_BASE) {
;
}
else {
currCmd->cmd._display.displayForNFrames = cmdInRom->_display.displayForNFrames;
currCmd->cmd._display.frameIndex = cmdInRom->_display.frameIndex;
structSize = sizeof(ACmd_Display);
}
}
cmdInRom = (ACmd*)(((u8*)cmdInRom) + structSize);
cmdAddress += structSize;
// Prevent an empty cmd getting allocated,
// when the loop is about to end
if(!breakLoop)
currCmd = memArenaReserve(arena, sizeof(DynTableAnimCmd));
}
return variantStart;
}
/* +--------------------------------------+
| --------- Data Layout --------- |
+--------------------------------------+
| DynTableAnim[] |
+--------------------------------------+
| u16[animCount] variantsPerAnim |
+--------------------------------------+
| (s32)offsets -> each anim's variants |
| / / / / / / / / / / / / / / / / / / |
| All commands, for each variant |
+--------------------------------------+
*/
static void
createDynamicAnimTable(MemArena* arena, u8* rom, AnimationTable *animTable, DynTable* dynTable) {
u32 animCount = animTable->entryCount;
DynTableAnim* table = NULL;
u16* variantsPerAnim = NULL;
DynTableAnimCmd* variantStart = NULL;
// Init the table and ensure there's enough space in memory
table = memArenaReserve(arena, animCount * sizeof(DynTableAnim));
// Count the number of variants of each animation
variantsPerAnim = memArenaReserve(arena, animCount * sizeof(u16));
for (u32 animId = 0; animId < animCount; animId++)
variantsPerAnim[animId] = countVariants(rom, animTable, animId);
// Convert all the commands of each animation into a format that lets us easily modify it.
for (int animationId = 0; animationId < animCount; animationId++) {
RomPointer animPointer = animTable->data[animationId];
if (animPointer == 0)
continue;
// Don't print an animation that we already printed.
int prevIndex = -1;
if (wasReferencedBefore(animTable, animationId, &prevIndex)) {
s32 offsetCurrPrev = (&table[prevIndex] - &table[animationId]);
table[animationId].offsetVariants = offsetCurrPrev;
variantsPerAnim[animationId] = variantsPerAnim[prevIndex];
continue;
}
RomPointer *variantsInRom = romToVirtual(rom, animPointer);
// allocate offsets of each variant
s32* variantOffsets = memArenaReserve(arena, variantsPerAnim[animationId] * sizeof(u32));
table[animationId].offsetVariants = (s32)((u8*)variantOffsets - (u8*)&table[animationId]);
// - iterate through all variants of the current animation
// - flag commands that are pointed at by jumps.
// - set offset to each variant
u16 numVariants = variantsPerAnim[animationId];
for (u16 variantId = 0; variantId < numVariants; variantId++) {
variantStart = fillVariantFromRom(arena, rom, &variantsInRom[variantId]);
s32 offset = (s32)(((u8*)variantStart) - (u8*)&variantOffsets[variantId]);
variantOffsets[variantId] = offset;
}
// SA1 cornercase, of a pointer pointing at
// supposedly deleted variant, without a replacement "End" command.
if((variantsInRom[numVariants] >= ROM_BASE)
&& (&variantsInRom[numVariants] < animTable->data)){
}
}
dynTable->animations = table;
dynTable->variantCounts = variantsPerAnim;
}
static StringId
pushLabel(LabelStrings* db, MemArena* stringArena, MemArena* offsetArena, char* label) {
char* string = memArenaAddString(stringArena, label);
s32* offset = memArenaReserve(offsetArena, sizeof(s32));
*offset = (s32)(string - (char*)stringArena->memory);
db->count++;
return db->count - 1;
}
static void
createAnimLabels(DynTable* table, u32 numAnimations, LabelStrings* labels, MemArena* stringArena, MemArena* stringOffsetArena) {
labels->strings = stringArena->memory;
labels->offsets = stringOffsetArena->memory;
labels->count = 0;
// Push empty string as "Dummy" value.
pushLabel(labels, stringArena, stringOffsetArena, "");
// Load animation-name labels
// TODO: Implement loading main animation-labels from file.
char buffer[256];
for (u32 animId = 0; animId < numAnimations; animId++) {
// We need to check 'name' as well, since we assigned
// the offsets for already-referenced anims in 'createDynamicAnimTable'
DynTableAnim* current = &table->animations[animId];
if(current->offsetVariants != 0) {
StringId stringId = 0;
// (offset < 0) -> references previous entry,
if (current->offsetVariants < 0) {
DynTableAnim* previous = (DynTableAnim*)OffsetPointer(¤t->offsetVariants);
stringId = previous->name;
}
else {
sprintf(buffer, "anim_%04d", animId);
stringId = pushLabel(labels, stringArena, stringOffsetArena, buffer);
}
current->name = stringId;
}
}
labels->strings = stringArena->memory;
labels->offsets = stringOffsetArena->memory;
}
typedef void (*CmdIterator)(FILE* fileStream, DynTableAnimCmd* cmd, u16 animId, u16 variantId, u16 labelId, void* itParams);
// Go through every command that was found in the game and pass it to 'iterator' function.
void iterateAllCommands(FILE* fileStream, DynTable *dynTable, u16 startAnimId, u16 endAnimId, CmdIterator iterator, void* iteratorParams) {
for (int animId = startAnimId; animId < endAnimId; animId++) {
DynTableAnim* anim = &dynTable->animations[animId];
// Already gone over this anim
if (anim->offsetVariants < 0)
continue;
s32* variOffsets = (s32*)OffsetPointer(&anim->offsetVariants);
u16 variantCount = dynTable->variantCounts[animId];
for (int variantId = 0; variantId < variantCount; variantId++) {
DynTableAnimCmd* dtCmd = (DynTableAnimCmd*)OffsetPointer(&variOffsets[variantId]);
int labelId = 0;
while (TRUE) {
iterator(fileStream, dtCmd, animId, variantId, labelId, iteratorParams);
dtCmd++;
if((dtCmd->cmd.id == AnimCmd_End)
|| (dtCmd->cmd.id == AnimCmd_JumpBack)
|| (dtCmd->cmd.id == AnimCmd_SetIdAndVariant) // Enable for SA1 (only?)
|| (dtCmd->cmd.id < AnimCmd_12))
break;
}
}
}
}
typedef struct {
s32 firstTileId;
u32 minRange;
s32 paletteId;
u16 animId;
u16 variantId;
} TileRange;
typedef struct {
MemArena* tileRanges;
// Tiles
u32 numGetTileCalls;
u32 numTileIndices;
// Palette
s32 cachedPaletteId;
s16 cachedNumColors;
s16 cachedInsertOffset;
} TileInfo;
void itGetNumTileInformation(FILE* fileStream, DynTableAnimCmd* dtCmd, u16 animId, u16 variantId, u16 labelId, void* itParams) {
TileInfo* tileInfo = (TileInfo*)itParams;
if (dtCmd->cmd.id >= 0) {
ACmd_Display* disp = (ACmd_Display*)&dtCmd->cmd;
TileInfo* tileInfo = (TileInfo*)itParams;
}
if (dtCmd->cmd.id == AnimCmd_GetPalette) {
ACmd_GetPalette* cmd = &dtCmd->cmd._pal;
tileInfo->cachedPaletteId = cmd->palId;
tileInfo->cachedNumColors = cmd->numColors;
tileInfo->cachedInsertOffset = cmd->insertOffset;
}
if (dtCmd->cmd.id == AnimCmd_GetTiles) {
ACmd_GetTiles* cmd = &dtCmd->cmd._tiles;
if (cmd->tileIndex < 0) {
#if 0
fprintf(fileStream,
"--- 8BPP FOUND: %d\n"
" Anim: %d, Vari: %d, Label: %d\n"
" Pal: %d, Num: %d, Insrt: %d\n",
cmd->tileIndex,
animId, variantId, labelId,
tileInfo->cachedPaletteId, tileInfo->cachedNumColors, tileInfo->cachedInsertOffset);
#endif
}
else {
#if 0
fprintf(fileStream,
"Pal: %6d, Num: %4d, Insrt: %2d\n",
tileInfo->cachedPaletteId, tileInfo->cachedNumColors, tileInfo->cachedInsertOffset);
#endif
}
TileInfo* tileInfo = (TileInfo*)itParams;
tileInfo->numTileIndices = Max(tileInfo->numTileIndices, (cmd->tileIndex + cmd->numTilesToCopy));
tileInfo->numGetTileCalls++;
TileRange tr;
tr.firstTileId = cmd->tileIndex;
tr.minRange = cmd->numTilesToCopy;
tr.paletteId = tileInfo->cachedPaletteId;
tr.animId = animId;
tr.variantId = variantId;
memArenaAddMemory(tileInfo->tileRanges, &tr, sizeof(tr));
}
}