diff options
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/cable_car_util.c | 47 | ||||
-rw-r--r-- | src/engine/cable_club.c | 491 | ||||
-rw-r--r-- | src/engine/clear_save_data_menu.c | 16 | ||||
-rw-r--r-- | src/engine/clock.c | 3 | ||||
-rw-r--r-- | src/engine/link.c | 221 | ||||
-rw-r--r-- | src/engine/main.c | 16 | ||||
-rw-r--r-- | src/engine/main_menu.c | 150 | ||||
-rw-r--r-- | src/engine/menu.c | 264 | ||||
-rw-r--r-- | src/engine/menu_cursor.c | 62 | ||||
-rw-r--r-- | src/engine/mystery_event_menu.c | 498 | ||||
-rw-r--r-- | src/engine/name_string_util.c | 4 | ||||
-rw-r--r-- | src/engine/naming_screen.c | 65 | ||||
-rw-r--r-- | src/engine/option_menu.c | 209 | ||||
-rw-r--r-- | src/engine/palette.c | 4 | ||||
-rw-r--r-- | src/engine/record_mixing.c | 12 | ||||
-rw-r--r-- | src/engine/reset_rtc_screen.c | 377 | ||||
-rw-r--r-- | src/engine/rtc.c | 29 | ||||
-rw-r--r-- | src/engine/save.c | 726 | ||||
-rw-r--r-- | src/engine/save_failed_screen.c | 230 | ||||
-rw-r--r-- | src/engine/save_menu_util.c | 24 | ||||
-rw-r--r-- | src/engine/sprite.c | 14 | ||||
-rw-r--r-- | src/engine/task.c | 10 | ||||
-rw-r--r-- | src/engine/text.c | 763 | ||||
-rw-r--r-- | src/engine/text_window.c | 157 | ||||
-rw-r--r-- | src/engine/time_events.c | 2 | ||||
-rw-r--r-- | src/engine/trade.c | 243 | ||||
-rw-r--r-- | src/engine/trainer_card.c | 772 |
27 files changed, 3663 insertions, 1746 deletions
diff --git a/src/engine/cable_car_util.c b/src/engine/cable_car_util.c new file mode 100644 index 000000000..02bfbc195 --- /dev/null +++ b/src/engine/cable_car_util.c @@ -0,0 +1,47 @@ +#include "global.h" +#include "cable_car_util.h" + +// static types + +// static declarations + +// rodata + +// text + +void CableCarUtil_FillWrapped(void *dest, u16 value, u8 left, u8 top, u8 width, u8 height) +{ + u8 i; + u8 j; + u8 x; + u8 y; + + for (i = 0, y = top; i < height; i++) + { + for (x = left, j = 0; j < width; j++) + { + *(u16 *)&((u8 *)dest)[y * 64 + x * 2] = value; + x = (x + 1) % 32; + } + y = (y + 1) % 32; + } +} + +void CableCarUtil_CopyWrapped(void *dest, const u16 *src, u8 left, u8 top, u8 width, u8 height) +{ + u8 i; + u8 j; + u8 x; + u8 y; + const u16 *_src; + + for (i = 0, _src = src, y = top; i < height; i++) + { + for (x = left, j = 0; j < width; j++) + { + *(u16 *)&((u8 *)dest)[y * 64 + x * 2] = *_src++; + x = (x + 1) % 32; + } + y = (y + 1) % 32; + } +} diff --git a/src/engine/cable_club.c b/src/engine/cable_club.c index 3187de7ed..ff73e638e 100644 --- a/src/engine/cable_club.c +++ b/src/engine/cable_club.c @@ -23,6 +23,7 @@ #include "text.h" #include "trainer_card.h" +extern u8 unk_2030220; extern u16 gSpecialVar_Result; extern struct TrainerCard gTrainerCards[4]; extern u8 gUnknown_03004860; @@ -41,7 +42,7 @@ extern struct u8 field1; } gUnknown_020297D8; -static void sub_8082F20(u8 taskId); +/*static*/ void sub_8082F20(u8 taskId); static void sub_8082F68(u8 taskId); static void sub_8082FEC(u8 taskId); static void sub_808303C(u8 taskId); @@ -95,6 +96,382 @@ const u8 *const gTrainerCardColorNames[] = TrainerCardColorName_Gold, }; +#if DEBUG + +__attribute__((naked)) +int debug_sub_808A4D0() +{ + asm("\ + push {lr}\n\ + add r1, r0, #0\n\ + ldr r0, ._3 @ sub_8082F68\n\ + cmp r1, r0\n\ + bne ._1 @cond_branch\n\ + mov r0, #0x1\n\ + b ._30\n\ +._4:\n\ + .align 2, 0\n\ +._3:\n\ + .word sub_8082F68+1\n\ +._1:\n\ + ldr r0, ._7 @ sub_8082FEC\n\ + cmp r1, r0\n\ + bne ._5 @cond_branch\n\ + mov r0, #0x11\n\ + b ._30\n\ +._8:\n\ + .align 2, 0\n\ +._7:\n\ + .word sub_8082FEC+1\n\ +._5:\n\ + ldr r0, ._11 @ sub_808303C\n\ + cmp r1, r0\n\ + bne ._9 @cond_branch\n\ + mov r0, #0x12\n\ + b ._30\n\ +._12:\n\ + .align 2, 0\n\ +._11:\n\ + .word sub_808303C+1\n\ +._9:\n\ + ldr r0, ._15 @ sub_8083188\n\ + cmp r1, r0\n\ + bne ._13 @cond_branch\n\ + mov r0, #0x13\n\ + b ._30\n\ +._16:\n\ + .align 2, 0\n\ +._15:\n\ + .word sub_8083188+1\n\ +._13:\n\ + ldr r0, ._19 @ sub_80830E4\n\ + cmp r1, r0\n\ + bne ._17 @cond_branch\n\ + mov r0, #0x14\n\ + b ._30\n\ +._20:\n\ + .align 2, 0\n\ +._19:\n\ + .word sub_80830E4+1\n\ +._17:\n\ + ldr r0, ._23 @ sub_80831F8\n\ + cmp r1, r0\n\ + bne ._21 @cond_branch\n\ + mov r0, #0x21\n\ + b ._30\n\ +._24:\n\ + .align 2, 0\n\ +._23:\n\ + .word sub_80831F8+1\n\ +._21:\n\ + ldr r0, ._27 @ sub_8083314\n\ + cmp r1, r0\n\ + bne ._25 @cond_branch\n\ + mov r0, #0x2\n\ + b ._30\n\ +._28:\n\ + .align 2, 0\n\ +._27:\n\ + .word sub_8083314+1\n\ +._25:\n\ + ldr r0, ._31 @ sub_80833C4\n\ + cmp r1, r0\n\ + beq ._29 @cond_branch\n\ + mov r0, #0x0\n\ + b ._30\n\ +._32:\n\ + .align 2, 0\n\ +._31:\n\ + .word sub_80833C4+1\n\ +._29:\n\ + mov r0, #0x3\n\ +._30:\n\ + pop {r1}\n\ + bx r1"); +} + +__attribute__((naked)) +void debug_sub_808A55C() +{ + asm("\ + push {r4, r5, r6, r7, lr}\n\ + mov r7, r8\n\ + push {r7}\n\ + lsl r0, r0, #0x18\n\ + lsr r7, r0, #0x18\n\ + ldr r2, ._37 @ gTasks\n\ + lsl r0, r7, #0x2\n\ + add r0, r0, r7\n\ + lsl r0, r0, #0x3\n\ + add r4, r0, r2\n\ + mov r0, #0x8\n\ + ldsh r1, [r4, r0]\n\ + lsl r0, r1, #0x2\n\ + add r0, r0, r1\n\ + lsl r0, r0, #0x3\n\ + add r0, r0, r2\n\ + ldrb r0, [r0, #0x4]\n\ + cmp r0, #0\n\ + bne ._33 @cond_branch\n\ + mov r1, #0xa\n\ + ldsh r0, [r4, r1]\n\ + cmp r0, #0x5\n\ + bne ._34 @cond_branch\n\ + add r0, r7, #0\n\ + bl DestroyTask\n\ +._34:\n\ + ldrh r0, [r4, #0xa]\n\ + add r0, r0, #0x1\n\ + strh r0, [r4, #0xa]\n\ +._33:\n\ + ldr r0, ._37 + 4 @ gShouldAdvanceLinkState\n\ + ldrb r0, [r0]\n\ + mov r1, #0x2\n\ + mov r2, #0x0\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + ldr r0, ._37 + 8 @ gBlockSendBuffer\n\ + ldrb r0, [r0]\n\ + mov r1, #0x16\n\ + mov r2, #0x5\n\ + mov r3, #0x4\n\ + bl PrintHex\n\ + mov r4, #0x0\n\ + lsl r3, r7, #0x2\n\ + mov r8, r3\n\ + mov r6, #0xc0\n\ + lsl r6, r6, #0x13\n\ + mov r5, #0xa0\n\ + lsl r5, r5, #0x13\n\ +._35:\n\ + ldr r0, ._37 + 12 @ gLinkPlayerPending\n\ + add r0, r4, r0\n\ + ldrb r0, [r0]\n\ + lsr r1, r5, #0x18\n\ + mov r2, #0x0\n\ + mov r3, #0x1\n\ + bl PrintHex\n\ + ldr r1, ._37 + 16 @ gBlockRecvBuffer\n\ + lsl r0, r4, #0x8\n\ + add r0, r0, r1\n\ + ldrh r0, [r0]\n\ + lsr r2, r6, #0x18\n\ + mov r1, #0x16\n\ + mov r3, #0x4\n\ + bl PrintHex\n\ + mov r0, #0x80\n\ + lsl r0, r0, #0x11\n\ + add r6, r6, r0\n\ + mov r1, #0x80\n\ + lsl r1, r1, #0x12\n\ + add r5, r5, r1\n\ + add r4, r4, #0x1\n\ + cmp r4, #0x3\n\ + ble ._35 @cond_branch\n\ + ldr r4, ._37 + 20 @ gLinkStatus\n\ + ldr r0, [r4]\n\ + mov r1, #0xf\n\ + mov r2, #0x0\n\ + mov r3, #0x8\n\ + bl PrintHex\n\ + ldr r0, ._37 + 24 @ gLink\n\ + ldrb r0, [r0, #0x1]\n\ + mov r1, #0x2\n\ + mov r2, #0xa\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + bl GetMultiplayerId\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0x7\n\ + mov r2, #0xc\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + bl GetBlockReceivedStatus\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0x7\n\ + mov r2, #0xa\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + ldr r0, ._37 + 28 @ gReceivedRemoteLinkPlayers\n\ + ldrb r0, [r0]\n\ + mov r1, #0x2\n\ + mov r2, #0xc\n\ + mov r3, #0x1\n\ + bl PrintHex\n\ + ldr r0, ._37 + 32 @ gSpecialVar_Result\n\ + ldrh r0, [r0]\n\ + mov r1, #0xb\n\ + mov r2, #0x8\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + ldr r0, [r4]\n\ + mov r1, #0x1c\n\ + and r0, r0, r1\n\ + lsr r0, r0, #0x2\n\ + mov r1, #0xb\n\ + mov r2, #0xa\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + bl IsLinkConnectionEstablished\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0xb\n\ + mov r2, #0xc\n\ + mov r3, #0x1\n\ + bl PrintHex\n\ + bl IsLinkTaskFinished\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0xf\n\ + mov r2, #0xa\n\ + mov r3, #0x1\n\ + bl PrintHex\n\ + ldr r2, ._37 @ gTasks\n\ + mov r3, r8\n\ + add r0, r3, r7\n\ + lsl r0, r0, #0x3\n\ + add r0, r0, r2\n\ + mov r3, #0x8\n\ + ldsh r1, [r0, r3]\n\ + lsl r0, r1, #0x2\n\ + add r0, r0, r1\n\ + lsl r0, r0, #0x3\n\ + add r0, r0, r2\n\ + ldr r0, [r0]\n\ + bl debug_sub_808A4D0\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0xf\n\ + mov r2, #0xc\n\ + mov r3, #0x2\n\ + bl PrintHex\n\ + ldr r0, ._37 + 36 @ gLinkCallback\n\ + ldr r0, [r0]\n\ + mov r1, #0x2\n\ + mov r2, #0xd\n\ + mov r3, #0x8\n\ + bl PrintHex\n\ + bl HasLinkErrorOccurred\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0x2\n\ + mov r2, #0x2\n\ + mov r3, #0x1\n\ + bl PrintHex\n\ + ldr r0, ._37 + 40 @ gLinkPlayers\n\ + mov r6, #0x80\n\ + lsl r6, r6, #0x12\n\ + add r5, r0, #0\n\ + add r5, r5, #0x14\n\ + mov r4, #0x3\n\ +._36:\n\ + ldr r0, [r5]\n\ + lsr r1, r6, #0x18\n\ + mov r2, #0x3\n\ + mov r3, #0x4\n\ + bl PrintHex\n\ + mov r0, #0xc0\n\ + lsl r0, r0, #0x13\n\ + add r6, r6, r0\n\ + add r5, r5, #0x1c\n\ + sub r4, r4, #0x1\n\ + cmp r4, #0\n\ + bge ._36 @cond_branch\n\ + ldr r0, ._37 + 44 @ 0x4000128\n\ + ldrh r0, [r0]\n\ + mov r1, #0x2\n\ + mov r2, #0x6\n\ + mov r3, #0x4\n\ + bl PrintHex\n\ + bl debug_sub_808B850\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + mov r1, #0x19\n\ + mov r2, #0x3\n\ + mov r3, #0x1\n\ + bl PrintHex\n\ + pop {r3}\n\ + mov r8, r3\n\ + pop {r4, r5, r6, r7}\n\ + pop {r0}\n\ + bx r0\n\ +._38:\n\ + .align 2, 0\n\ +._37:\n\ + .word gTasks\n\ + .word gShouldAdvanceLinkState\n\ + .word gBlockSendBuffer\n\ + .word gLinkPlayerPending\n\ + .word gBlockRecvBuffer\n\ + .word gLinkStatus\n\ + .word gLink\n\ + .word gReceivedRemoteLinkPlayers\n\ + .word gSpecialVar_Result\n\ + .word gLinkCallback\n\ + .word gLinkPlayers\n\ + .word 0x4000128"); +} +#endif + +#if DEBUG +__attribute__((naked)) +static void sub_8082CD4(u8 arg0, u8 arg1) +{ + asm("\ + push {r4, r5, r6, r7, lr}\n\ + lsl r0, r0, #0x18\n\ + lsr r7, r0, #0x18\n\ + lsl r1, r1, #0x18\n\ + lsr r6, r1, #0x18\n\ + mov r0, #0xc\n\ + mov r1, #0x0\n\ + mov r2, #0x1f\n\ + mov r3, #0x2\n\ + bl InitLinkTestBG_Unused\n\ + ldr r4, ._40 @ sub_8082F20\n\ + add r0, r4, #0\n\ + bl FindTaskIdByFunc\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + cmp r0, #0xff\n\ + bne ._39 @cond_branch\n\ + add r0, r4, #0\n\ + mov r1, #0x50\n\ + bl CreateTask\n\ + add r4, r0, #0\n\ + lsl r4, r4, #0x18\n\ + lsr r4, r4, #0x18\n\ + ldr r5, ._40 + 4 @ gTasks\n\ + lsl r0, r4, #0x2\n\ + add r0, r0, r4\n\ + lsl r0, r0, #0x3\n\ + add r0, r0, r5\n\ + strh r7, [r0, #0xa]\n\ + strh r6, [r0, #0xc]\n\ + ldr r0, ._40 + 8 @ debug_sub_808A55C\n\ + mov r1, #0x50\n\ + bl CreateTask\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + lsl r1, r0, #0x2\n\ + add r1, r1, r0\n\ + lsl r1, r1, #0x3\n\ + add r1, r1, r5\n\ + strh r4, [r1, #0x8]\n\ +._39:\n\ + pop {r4, r5, r6, r7}\n\ + pop {r0}\n\ + bx r0\n\ +._41:\n\ + .align 2, 0\n\ +._40:\n\ + .word sub_8082F20+1\n\ + .word gTasks\n\ + .word debug_sub_808A55C+1"); +} +#else static void sub_8082CD4(u8 arg0, u8 arg1) { if (FindTaskIdByFunc(sub_8082F20) == 0xFF) @@ -105,17 +482,18 @@ static void sub_8082CD4(u8 arg0, u8 arg1) gTasks[taskId].data[2] = arg1; } } +#endif static void sub_8082D18(u32 value) { ConvertIntToDecimalStringN(gStringVar1, value, STR_CONV_MODE_LEFT_ALIGN, 1); - MenuDrawTextWindow(18, 10, 28, 13); + Menu_DrawStdWindowFrame(18, 10, 28, 13); sub_8072BD8(gOtherText_PLink, 19, 11, 72); } static void sub_8082D4C() { - MenuZeroFillWindowRect(18, 10, 28, 13); + Menu_EraseWindowRect(18, 10, 28, 13); } static void sub_8082D60(u8 taskId, u8 arg1) @@ -207,7 +585,7 @@ void unref_sub_8082EEC(u8 taskId) } } -static void sub_8082F20(u8 taskId) +/*static*/ void sub_8082F20(u8 taskId) { s16 *data = gTasks[taskId].data; @@ -360,6 +738,10 @@ void sub_80831F8(u8 taskId) || sub_8082DF4(taskId) == TRUE) return; +#if DEBUG + sub_8082D60(taskId, GetLinkPlayerCount_2()); +#endif + gSpecialVar_Result = sub_8082D9C(local1, local2); if (gSpecialVar_Result == 0) return; @@ -374,7 +756,7 @@ void sub_80831F8(u8 taskId) gFieldLinkPlayerCount = GetLinkPlayerCount_2(); gUnknown_03004860 = GetMultiplayerId(); sub_80081C8(gFieldLinkPlayerCount); - sub_8093390((struct TrainerCard *)gBlockSendBuffer); + TrainerCard_GenerateCardForPlayer((struct TrainerCard *)gBlockSendBuffer); gTasks[taskId].func = sub_8083314; } } @@ -395,7 +777,7 @@ static void sub_8083288(u8 taskId) gFieldLinkPlayerCount = GetLinkPlayerCount_2(); gUnknown_03004860 = GetMultiplayerId(); sub_80081C8(gFieldLinkPlayerCount); - sub_8093390((struct TrainerCard *)gBlockSendBuffer); + TrainerCard_GenerateCardForPlayer((struct TrainerCard *)gBlockSendBuffer); gTasks[taskId].func = sub_8083314; sub_8007E9C(2); } @@ -676,7 +1058,7 @@ static void sub_808382C(u8 taskId) switch (task->data[0]) { case 0: - fade_screen(1, 0); + FadeScreen(1, 0); gLinkType = 0x2211; ClearLinkCallback_2(); task->data[0]++; @@ -790,7 +1172,7 @@ static void sub_80839DC(u8 taskId) case 3: sub_8055588(); HideFieldMessageBox(); - MenuZeroFillScreen(); + Menu_EraseScreen(); DestroyTask(taskId); EnableBothScriptContexts(); break; @@ -812,7 +1194,7 @@ static void sub_8083AAC(u8 taskId) { case 0: ScriptContext2_Enable(); - fade_screen(1, 0); + FadeScreen(1, 0); ClearLinkCallback_2(); task->data[0]++; break; @@ -874,7 +1256,7 @@ void unref_sub_8083BB0(void) void sub_8083BDC(void) { - sub_8093130(gSpecialVar_0x8006, c2_exit_to_overworld_1_continue_scripts_restart_music); + TrainerCard_ShowLinkCard(gSpecialVar_0x8006, c2_exit_to_overworld_1_continue_scripts_restart_music); } bool32 sub_8083BF4(u8 linkPlayerIndex) @@ -908,6 +1290,74 @@ void sub_8083C50(u8 taskId) DestroyTask(taskId); } +#if DEBUG + +__attribute__((naked)) +void debug_sub_808B778() +{ + asm("\ + push {r4, lr}\n\ + ldr r4, ._390 @ debug_sub_808B7A8\n\ + add r0, r4, #0\n\ + bl FuncIsActiveTask\n\ + lsl r0, r0, #0x18\n\ + cmp r0, #0\n\ + bne ._389 @cond_branch\n\ + add r0, r4, #0\n\ + mov r1, #0x50\n\ + bl CreateTask\n\ +._389:\n\ + ldr r1, ._390 + 4 @ unk_3004E98\n\ + ldr r0, [r1]\n\ + add r0, r0, #0x1\n\ + str r0, [r1]\n\ + pop {r4}\n\ + pop {r0}\n\ + bx r0\n\ +._391:\n\ + .align 2, 0\n\ +._390:\n\ + .word debug_sub_808B7A8+1\n\ + .word unk_3004E98"); +} + +__attribute__((naked)) +void debug_sub_808B7A8() +{ + asm("\ + push {lr}\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + ldr r2, ._393 @ gTasks\n\ + lsl r1, r0, #0x2\n\ + add r1, r1, r0\n\ + lsl r1, r1, #0x3\n\ + add r1, r1, r2\n\ + ldrh r0, [r1, #0x8]\n\ + add r0, r0, #0x1\n\ + strh r0, [r1, #0x8]\n\ + lsl r0, r0, #0x10\n\ + asr r0, r0, #0x10\n\ + cmp r0, #0x1e\n\ + bne ._392 @cond_branch\n\ + mov r0, #0x0\n\ + strh r0, [r1, #0x8]\n\ + ldr r2, ._393 + 4 @ unk_3004E94\n\ + ldrh r0, [r2]\n\ + mov r1, #0x1\n\ + orr r0, r0, r1\n\ + strh r0, [r2]\n\ +._392:\n\ + pop {r0}\n\ + bx r0\n\ +._394:\n\ + .align 2, 0\n\ +._393:\n\ + .word gTasks\n\ + .word unk_3004E94"); +} +#endif + static void sub_8083CA4(u8 taskId) { if (!gReceivedRemoteLinkPlayers) @@ -921,4 +1371,23 @@ void unref_sub_8083CC8(u8 taskId) { sub_800832C(); gTasks[taskId].func = sub_8083CA4; -}
\ No newline at end of file +} + +#if DEBUG + +void debug_sub_808B82C(void) +{ + unk_2030220 = 0; +} + +void debug_sub_808B838(u8 a) +{ + unk_2030220 |= 1 << a; +} + +int debug_sub_808B850(void) +{ + return unk_2030220; +} + +#endif
\ No newline at end of file diff --git a/src/engine/clear_save_data_menu.c b/src/engine/clear_save_data_menu.c index 12cde22a7..b6053715a 100644 --- a/src/engine/clear_save_data_menu.c +++ b/src/engine/clear_save_data_menu.c @@ -41,11 +41,11 @@ static void Task_InitMenu(u8 taskId) REG_DISPCNT = DISPCNT_MODE_0 | DISPCNT_OBJ_1D_MAP | DISPCNT_BG0_ON | DISPCNT_BG3_ON | DISPCNT_OBJ_ON; SetVBlankCallback(VBlankCB_ClearSaveDataScreen); - MenuDrawTextWindow(2, 14, 27, 19); - MenuPrint(gSystemText_ClearAllSaveDataPrompt, 3, 15); + Menu_DrawStdWindowFrame(2, 14, 27, 19); + Menu_PrintText(gSystemText_ClearAllSaveDataPrompt, 3, 15); - MenuDrawTextWindow(2, 1, 8, 6); - PrintMenuItems(3, 2, 2, gMenuYesNoItems); + Menu_DrawStdWindowFrame(2, 1, 8, 6); + Menu_PrintItems(3, 2, 2, gMenuYesNoItems); InitMenu(0, 3, 2, 2, 1, 5); gTasks[taskId].func = Task_ProcessMenuInput; @@ -53,7 +53,7 @@ static void Task_InitMenu(u8 taskId) static void Task_ProcessMenuInput(u8 taskId) { - switch (ProcessMenuInputNoWrap_()) + switch (Menu_ProcessInputNoWrap_()) { case 0: PlaySE(SE_SELECT); @@ -73,7 +73,7 @@ static void Task_ProcessMenuInput(u8 taskId) static void Task_ClearSaveData(u8 taskId) { - ClearSaveData(); + Save_EraseAllData(); DestroyTask(taskId); SetMainCallback2(CB2_SoftReset); } @@ -133,8 +133,8 @@ static u8 InitClearSaveDataScreen(void) ResetTasks(); ResetSpriteData(); - SetUpWindowConfig(&gWindowConfig_81E6C3C); - InitMenuWindow(&gWindowConfig_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6C3C); + InitMenuWindow(&gWindowTemplate_81E6CE4); BeginNormalPaletteFade(-1, 0, 0x10, 0, 0xffff); ime = REG_IME; diff --git a/src/engine/clock.c b/src/engine/clock.c index 416aaa6bb..fceccdfcb 100644 --- a/src/engine/clock.c +++ b/src/engine/clock.c @@ -13,12 +13,11 @@ #include "tv.h" #include "wallclock.h" -static void InitTimeBasedEvents(void); static void UpdatePerDay(struct Time *time); static void UpdatePerMinute(struct Time *time); static void ReturnFromStartWallClock(void); -static void InitTimeBasedEvents(void) +void InitTimeBasedEvents(void) { FlagSet(FLAG_SYS_CLOCK_SET); RtcCalcLocalTime(); diff --git a/src/engine/link.c b/src/engine/link.c index 28323207c..00c084069 100644 --- a/src/engine/link.c +++ b/src/engine/link.c @@ -1,4 +1,5 @@ #include "global.h" +#include "cable_club.h" #include "link.h" #include "battle.h" #include "berry.h" @@ -12,6 +13,7 @@ #include "constants/songs.h" #include "sound.h" #include "sprite.h" +#include "string_util.h" #include "strings2.h" #include "task.h" #include "text.h" @@ -66,7 +68,7 @@ u32 sub_8007E40(void); static void SetBlockReceivedFlag(u8); static u16 LinkTestCalcBlockChecksum(void *, u16); static void PrintHexDigit(u8, u8, u8); -static void PrintHex(u32, u8, u8, u8); +void PrintHex(u32, u8, u8, u8); static void LinkCB_RequestPlayerDataExchange(void); static void Task_PrintTestData(u8); bool8 sub_8008224(void); @@ -79,7 +81,7 @@ static void sub_80084C8(void); static void sub_80084F4(void); static void CheckErrorStatus(void); -static void CB2_PrintErrorMessage(void); +void CB2_PrintErrorMessage(void); static u8 IsSioMultiMaster(void); static void DisableSerial(void); static void EnableSerial(void); @@ -145,6 +147,9 @@ void (*gLinkCallback)(void); struct LinkPlayer gSavedLinkPlayers[MAX_LINK_PLAYERS]; u8 gShouldAdvanceLinkState; u16 gLinkTestBlockChecksums[MAX_LINK_PLAYERS]; +#if DEBUG +u8 gUnknown_Debug_30030E0; +#endif u8 gBlockRequestType; u8 gLastSendQueueCount; struct Link gLink; @@ -156,11 +161,11 @@ u8 deUnkValue1; u8 deUnkValue2; #endif -EWRAM_DATA bool8 gLinkTestDebugValuesEnabled = {0}; -EWRAM_DATA bool8 gLinkTestDummyBool = {0}; -EWRAM_DATA u32 gFiller_20238B8 = {0}; -EWRAM_DATA u32 dword_20238BC = {0}; -EWRAM_DATA bool8 gLinkOpen = {0}; +EWRAM_DATA bool8 gLinkTestDebugValuesEnabled = 0; +EWRAM_DATA bool8 gLinkTestDummyBool = 0; +EWRAM_DATA u32 gFiller_20238B8 = 0; +EWRAM_DATA u32 dword_20238BC = 0; +EWRAM_DATA bool8 gLinkOpen = 0; static const u16 sLinkTestDigitPalette[] = INCBIN_U16("graphics/interface/link_test_digits.gbapal"); static const u32 sLinkTestDigitTiles[] = INCBIN_U32("graphics/interface/link_test_digits.4bpp"); @@ -189,9 +194,19 @@ const struct BlockRequest sBlockRequestLookupTable[5] = static const u8 sTestString[] = _("テストな"); -ALIGNED(4) static const u8 sMagic[] = "GameFreak inc."; +// TODO: fix the alignment here + +ALIGNED(4) const u8 sMagic[] = "GameFreak inc."; -ALIGNED(4) static const u8 sEmptyString[] = _(""); +#if DEBUG +const u8 sEmptyString[] = _(" "); +#else +ALIGNED(4) const u8 sEmptyString[] = _(""); +#endif + +#if DEBUG +const u8 linkDebugFillerPleaseRemove[2] = {0}; +#endif void Task_DestroySelf(u8 taskId) { @@ -238,8 +253,8 @@ void LinkTestScreen(void) FreeAllSpritePalettes(); ResetTasks(); SetVBlankCallback(VBlankCB_LinkTest); - SetUpWindowConfig(&gWindowConfig_81E6CE4); - InitMenuWindow((struct WindowConfig *)&gWindowConfig_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6CE4); + InitMenuWindow((struct WindowTemplate *)&gWindowTemplate_81E6CE4); ResetBlockSend(); gLinkType = 0x1111; OpenLink(); @@ -409,7 +424,7 @@ static void LinkTestProcessKeyInput(void) if (gMain.newKeys & START_BUTTON) SetSuppressLinkErrorMessage(TRUE); if (gMain.newKeys & R_BUTTON) - TrySavingData(LINK_SAVE); + Save_WriteData(SAVE_LINK); if (gMain.newKeys & SELECT_BUTTON) sub_800832C(); if (gLinkTestDebugValuesEnabled) @@ -543,6 +558,9 @@ static void ProcessRecvCmds(u8 unusedParam) else { SetBlockReceivedFlag(i); +#if DEBUG + debug_sub_808B838(i); +#endif } } break; @@ -898,14 +916,9 @@ bool8 sub_8007E9C(u8 a1) } } -bool8 sub_8007ECC(void) +bool8 IsLinkTaskFinished(void) { - u8 retVal = FALSE; - - if (!gLinkCallback) - retVal = TRUE; - - return retVal; + return gLinkCallback == NULL; } u8 GetBlockReceivedStatus(void) @@ -957,7 +970,7 @@ static void PrintHexDigit(u8 tileNum, u8 x, u8 y) tilemap[(32 * y) + x] = (gLinkTestBGInfo.paletteNum << 12) | (tileNum + 1); } -static void PrintHex(u32 num, u8 x, u8 y, u8 maxDigits) +void PrintHex(u32 num, u8 x, u8 y, u8 maxDigits) { u8 buffer[16]; s32 i; @@ -975,6 +988,143 @@ static void PrintHex(u32 num, u8 x, u8 y, u8 maxDigits) } } +#if DEBUG + +EWRAM_DATA u16 *debugCharacterBase = NULL; +EWRAM_DATA void *unk_20238C8 = NULL; +EWRAM_DATA u16 (*debugTileMap)[] = NULL; +EWRAM_DATA u32 unk_20238D0 = 0; + +void debug_sub_8008218(u16 *buffer, void *arg1, u16 (*arg2)[], u32 arg3) +{ + CpuSet(sLinkTestDigitTiles, buffer, 272); + debugCharacterBase = buffer; + unk_20238C8 = arg1; + debugTileMap = arg2; + unk_20238D0 = arg3; +} + +#ifdef NONMATCHING +void debug_sub_8008264(u32 value, int left, int top, int r3, int sp0) +{ + u32 buffer[8]; + u32 *ptr; + + u16 *dest; + + int i; + + if (unk_20238D0 != sp0) + return; + + r3 = max(r3, 8); + + ptr = &buffer[0]; + dest = &(*debugTileMap)[left + top * 32]; + + for (i = r3; i != 0; i--) + { + *(ptr++) = value & 0xF; + value = value >> 4; + } + + ptr = &buffer[8 - r3]; + for (i = r3; i != 0; i--) + { + int charOffset = (((uintptr_t) debugCharacterBase) - ((uintptr_t) unk_20238C8)) / 32; + *dest = *ptr + charOffset + 1; + ptr--; + dest++; + } +} +#else +__attribute__((naked)) +void debug_sub_8008264(u32 value, int left, int top, int r3, int sp0) +{ + asm( + " push {r4, r5, r6, r7, lr}\n" + " mov r7, r8\n" + " push {r7}\n" + " add sp, sp, #0xffffffe0\n" + " add r5, r0, #0\n" + " add r6, r1, #0\n" + " add r4, r3, #0\n" + " ldr r0, [sp, #0x38]\n" + " ldr r1, ._347 @ unk_20238D0\n" + " ldr r1, [r1]\n" + " cmp r1, r0\n" + " bne ._345 @cond_branch\n" + " cmp r4, #0x8\n" + " ble ._342 @cond_branch\n" + " mov r4, #0x8\n" + "._342:\n" + " mov r3, sp\n" + " ldr r0, ._347 + 4 @ debugTileMap\n" + " mov r8, r0\n" + " lsl r2, r2, #0x6\n" + " mov ip, r2\n" + " lsl r6, r6, #0x1\n" + " lsl r7, r4, #0x2\n" + " cmp r4, #0\n" + " ble ._343 @cond_branch\n" + " mov r1, #0xf\n" + " add r2, r4, #0\n" + "._344:\n" + " add r0, r5, #0\n" + " and r0, r0, r1\n" + " stmia r3!, {r0}\n" + " lsr r5, r5, #0x4\n" + " sub r2, r2, #0x1\n" + " cmp r2, #0\n" + " bne ._344 @cond_branch\n" + "._343:\n" + " mov r1, r8\n" + " ldr r0, [r1]\n" + " add r0, r0, ip\n" + " add r5, r0, r6\n" + " mov r1, sp\n" + " add r0, r1, r7\n" + " sub r3, r0, #4\n" + " cmp r4, #0\n" + " ble ._345 @cond_branch\n" + " ldr r7, ._347 + 8 @ debugCharacterBase\n" + " ldr r6, ._347 + 12 @ unk_20238C8\n" + " add r2, r4, #0\n" + "._346:\n" + " ldr r1, [r7]\n" + " ldr r0, [r6]\n" + " sub r1, r1, r0\n" + " lsr r1, r1, #0x5\n" + " ldr r0, [r3]\n" + " add r0, r0, r1\n" + " add r0, r0, #0x1\n" + " strh r0, [r5]\n" + " sub r3, r3, #0x4\n" + " add r5, r5, #0x2\n" + " sub r2, r2, #0x1\n" + " cmp r2, #0\n" + " bne ._346 @cond_branch\n" + "._345:\n" + " add sp, sp, #0x20\n" + " pop {r3}\n" + " mov r8, r3\n" + " pop {r4, r5, r6, r7}\n" + " pop {r0}\n" + " bx r0\n" + "._348:\n" + " .align 2, 0\n" + "._347:\n" + " .word unk_20238D0\n" + " .word debugTileMap\n" + " .word debugCharacterBase\n" + " .word unk_20238C8\n" + "\n" + ); +} +#endif + +#endif + static void LinkCB_RequestPlayerDataExchange(void) { // Only one request needs to be sent, so only the master sends it. @@ -1236,9 +1386,9 @@ void CB2_LinkError(void) FillPalette(0, 0, 2); ResetTasks(); SetVBlankCallback(VBlankCB_LinkTest); - SetUpWindowConfig(&gWindowConfig_81E7198); - InitMenuWindow((struct WindowConfig *)&gWindowConfig_81E7198); - MenuZeroFillScreen(); + Text_LoadWindowTemplate(&gWindowTemplate_81E7198); + InitMenuWindow((struct WindowTemplate *)&gWindowTemplate_81E7198); + Menu_EraseScreen(); REG_BLDALPHA = 0; REG_BG0VOFS = 0; REG_BG0HOFS = 0; @@ -1253,14 +1403,33 @@ void CB2_LinkError(void) SetMainCallback2(CB2_PrintErrorMessage); } -static void CB2_PrintErrorMessage(void) +void CB2_PrintErrorMessage(void) { - u8 array[64] __attribute__((unused)); // unused + u8 array[32] __attribute__((unused)); // unused + u8 array2[32] __attribute__((unused)); // unused switch (gMain.state) { case 0: - MenuPrint_PixelCoords(gMultiText_LinkError, 20, 56, 1); + Menu_PrintTextPixelCoords(gMultiText_LinkError, 20, 56, 1); +#if DEBUG + StringCopy(array, sColorCodes); + + ConvertIntToHexStringN(array2, sErrorLinkStatus, STR_CONV_MODE_LEADING_ZEROS, 8); + StringAppend(array, array2); + + StringAppend(array, sEmptyString); + + ConvertIntToHexStringN(array2, sErrorLastSendQueueCount, STR_CONV_MODE_LEADING_ZEROS, 2); + StringAppend(array, array2); + + StringAppend(array, sEmptyString); + + ConvertIntToHexStringN(array2, sErrorLastRecvQueueCount, STR_CONV_MODE_LEADING_ZEROS, 2); + StringAppend(array, array2); + + Menu_PrintText(array, 2, 15); +#endif break; case 30: case 60: diff --git a/src/engine/main.c b/src/engine/main.c index 82a5fffb4..54a443e80 100644 --- a/src/engine/main.c +++ b/src/engine/main.c @@ -13,7 +13,7 @@ #include "rtc.h" #include "siirtc.h" #include "sound.h" -#include "unknown_task.h" +#include "scanline_effect.h" extern struct SoundInfo gSoundInfo; extern u32 IntrMain[]; @@ -83,6 +83,10 @@ static void WaitForVBlank(void); #define B_START_SELECT (B_BUTTON | START_BUTTON | SELECT_BUTTON) +#ifndef NDEBUG + #include <stdlib.h> // don't include if not needed. +#endif + void AgbMain() { RegisterRamReset(RESET_ALL); @@ -98,6 +102,14 @@ void AgbMain() gSoftResetDisabled = FALSE; +// In Fire Red, AGBPrintInit is called at this spot. For user convenience, I +// opt to initialize the print area here. It is up to the user where they choose +// to print stuff from, as anything else declared is NOT authoritative. +#ifndef NDEBUG + AGBPrintInit(); + __mb_cur_max = 1; // fix for AGBPrintf +#endif + if (gFlashMemoryPresent != TRUE) SetMainCallback2(NULL); @@ -350,7 +362,7 @@ void DoSoftReset(void) { REG_IME = 0; m4aSoundVSyncOff(); - remove_some_task(); + ScanlineEffect_Stop(); DmaStop(1); DmaStop(2); DmaStop(3); diff --git a/src/engine/main_menu.c b/src/engine/main_menu.c index 909b5676b..5a3f55b29 100644 --- a/src/engine/main_menu.c +++ b/src/engine/main_menu.c @@ -1,4 +1,6 @@ #include "global.h" +#include "constants/songs.h" +#include "constants/species.h" #include "main_menu.h" #include "data2.h" #include "decompress.h" @@ -13,23 +15,20 @@ #include "overworld.h" #include "rtc.h" #include "save_menu_util.h" -#include "constants/songs.h" +#include "save.h" #include "sound.h" -#include "constants/species.h" #include "string_util.h" #include "strings.h" #include "task.h" #include "text.h" #include "title_screen.h" -#include "unknown_task.h" +#include "scanline_effect.h" #include "ewram.h" -#define BirchSpeechUpdateWindowText() ((u8)MenuUpdateWindowText_OverrideLineLength(24)) +#define BirchSpeechUpdateWindowText() ((u8)Menu_UpdateWindowTextOverrideLineLength(24)) extern struct PaletteFadeControl gPaletteFade; -extern u16 gSaveFileStatus; - extern const u8 gBirchSpeech_Welcome[]; extern const u8 gBirchSpeech_ThisIsPokemon[]; extern const u8 gBirchSpeech_WorldInhabitedByPokemon[]; @@ -229,12 +228,12 @@ u32 InitMainMenu(u8 a1) ResetPaletteFade(); LoadPalette(gMainMenuPalette, 0, 32); - remove_some_task(); + ScanlineEffect_Stop(); ResetTasks(); ResetSpriteData(); FreeAllSpritePalettes(); - SetUpWindowConfig(&gWindowConfig_81E6C3C); - InitMenuWindow((struct WindowConfig *)&gWindowConfig_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6C3C); + InitMenuWindow((struct WindowTemplate *)&gWindowTemplate_81E6CE4); if (a1) BeginNormalPaletteFade(-1, 0, 0x10, 0, 0x0000); // fade to black @@ -284,24 +283,23 @@ void Task_MainMenuCheckSave(u8 taskId) switch (gSaveFileStatus) { - case 1: + case SAVE_STATUS_OK: if (IsMysteryGiftEnabled() == TRUE) gTasks[taskId].tMenuLayout = HAS_MYSTERY_GIFT; else gTasks[taskId].tMenuLayout = HAS_SAVED_GAME; - gTasks[taskId].func = Task_MainMenuCheckRtc; break; case 2: - MenuDrawTextWindow(2, 14, 27, 19); + Menu_DrawStdWindowFrame(2, 14, 27, 19); MenuPrintMessage(gSaveFileDeletedMessage, 3, 15); REG_WIN0H = WIN_RANGE(17, 223); REG_WIN0V = WIN_RANGE(113, 159); gTasks[taskId].tMenuLayout = HAS_NO_SAVED_GAME; gTasks[taskId].func = Task_MainMenuWaitForSaveErrorAck; break; - case 255: - MenuDrawTextWindow(2, 14, 27, 19); + case SAVE_STATUS_ERROR: + Menu_DrawStdWindowFrame(2, 14, 27, 19); MenuPrintMessage(gSaveFileCorruptMessage, 3, 15); REG_WIN0H = WIN_RANGE(17, 223); REG_WIN0V = WIN_RANGE(113, 159); @@ -313,13 +311,13 @@ void Task_MainMenuCheckSave(u8 taskId) else gTasks[taskId].tMenuLayout = HAS_SAVED_GAME; break; - case 0: + case SAVE_STATUS_EMPTY: default: gTasks[taskId].tMenuLayout = HAS_NO_SAVED_GAME; gTasks[taskId].func = Task_MainMenuCheckRtc; break; - case 4: - MenuDrawTextWindow(2, 14, 27, 19); + case SAVE_STATUS_NO_FLASH: + Menu_DrawStdWindowFrame(2, 14, 27, 19); MenuPrintMessage(gBoardNotInstalledMessage, 3, 15); REG_WIN0H = WIN_RANGE(17, 223); REG_WIN0V = WIN_RANGE(113, 159); @@ -331,11 +329,11 @@ void Task_MainMenuCheckSave(u8 taskId) void Task_MainMenuWaitForSaveErrorAck(u8 taskId) { - if (MenuUpdateWindowText()) + if (Menu_UpdateWindowText()) { if (gMain.newKeys & A_BUTTON) { - MenuZeroFillWindowRect(2, 14, 27, 19); + Menu_EraseWindowRect(2, 14, 27, 19); gTasks[taskId].func = Task_MainMenuCheckRtc; } } @@ -359,7 +357,7 @@ void Task_MainMenuCheckRtc(u8 taskId) } else { - MenuDrawTextWindow(2, 14, 27, 19); + Menu_DrawStdWindowFrame(2, 14, 27, 19); MenuPrintMessage(gBatteryDryMessage, 3, 15); REG_WIN0H = WIN_RANGE(17, 223); REG_WIN0V = WIN_RANGE(113, 159); @@ -370,11 +368,11 @@ void Task_MainMenuCheckRtc(u8 taskId) void Task_MainMenuWaitForRtcErrorAck(u8 taskId) { - if (MenuUpdateWindowText()) + if (Menu_UpdateWindowText()) { if ( gMain.newKeys & 1 ) { - MenuZeroFillWindowRect(2, 14, 27, 19); + Menu_EraseWindowRect(2, 14, 27, 19); gTasks[taskId].func = Task_MainMenuDraw; } } @@ -412,28 +410,28 @@ void Task_MainMenuDraw(u8 taskId) { case HAS_NO_SAVED_GAME: default: - MenuDrawTextWindow(1, 0, 28, 3); + Menu_DrawStdWindowFrame(1, 0, 28, 3); PrintMainMenuItem(gMainMenuString_NewGame, 2, 1); - MenuDrawTextWindow(1, 4, 28, 7); + Menu_DrawStdWindowFrame(1, 4, 28, 7); PrintMainMenuItem(gMainMenuString_Option, 2, 5); break; case HAS_SAVED_GAME: - MenuDrawTextWindow(1, 0, 28, 7); + Menu_DrawStdWindowFrame(1, 0, 28, 7); PrintMainMenuItem(gMainMenuString_Continue, 2, 1); - MenuDrawTextWindow(1, 8, 28, 11); + Menu_DrawStdWindowFrame(1, 8, 28, 11); PrintMainMenuItem(gMainMenuString_NewGame, 2, 9); - MenuDrawTextWindow(1, 12, 28, 15); + Menu_DrawStdWindowFrame(1, 12, 28, 15); PrintMainMenuItem(gMainMenuString_Option, 2, 13); PrintSaveFileInfo(); break; case HAS_MYSTERY_GIFT: - MenuDrawTextWindow(1, 0, 28, 7); + Menu_DrawStdWindowFrame(1, 0, 28, 7); PrintMainMenuItem(gMainMenuString_Continue, 2, 1); - MenuDrawTextWindow(1, 8, 28, 11); + Menu_DrawStdWindowFrame(1, 8, 28, 11); PrintMainMenuItem(gMainMenuString_NewGame, 2, 9); - MenuDrawTextWindow(1, 12, 28, 15); + Menu_DrawStdWindowFrame(1, 12, 28, 15); PrintMainMenuItem(gMainMenuString_MysteryEvents, 2, 13); - MenuDrawTextWindow(1, 16, 28, 19); + Menu_DrawStdWindowFrame(1, 16, 28, 19); PrintMainMenuItem(gMainMenuString_Option, 2, 0x11); PrintSaveFileInfo(); break; @@ -681,7 +679,7 @@ void PrintMainMenuItem(const u8 *text, u8 left, u8 top) buffer[29] = EOS; - MenuPrint(buffer, left, top); + Menu_PrintText(buffer, left, top); } void PrintSaveFileInfo(void) @@ -694,8 +692,8 @@ void PrintSaveFileInfo(void) void PrintPlayerName(void) { - MenuPrint(gMainMenuString_Player, 2, 3); - MenuPrint(gSaveBlock2.playerName, 9, 3); + Menu_PrintText(gMainMenuString_Player, 2, 3); + Menu_PrintText(gSaveBlock2.playerName, 9, 3); } void PrintPlayTime(void) @@ -704,15 +702,15 @@ void PrintPlayTime(void) u8 alignedPlayTime[32]; #if defined(ENGLISH) - MenuPrint(gMainMenuString_Time, 16, 3); + Menu_PrintText(gMainMenuString_Time, 16, 3); FormatPlayTime(playTime, gSaveBlock2.playTimeHours, gSaveBlock2.playTimeMinutes, 1); sub_8072C74(alignedPlayTime, playTime, 48, 1); - MenuPrint(alignedPlayTime, 22, 3); + Menu_PrintText(alignedPlayTime, 22, 3); #elif defined(GERMAN) - MenuPrint_PixelCoords(gMainMenuString_Time, 124, 24, TRUE); + Menu_PrintTextPixelCoords(gMainMenuString_Time, 124, 24, TRUE); FormatPlayTime(playTime, gSaveBlock2.playTimeHours, gSaveBlock2.playTimeMinutes, 1); sub_8072C74(alignedPlayTime, playTime, 40, 1); - MenuPrint(alignedPlayTime, 23, 3); + Menu_PrintText(alignedPlayTime, 23, 3); #endif } @@ -720,9 +718,9 @@ void PrintPokedexCount(void) { u8 buffer[16]; - MenuPrint(gMainMenuString_Pokedex, 2, 5); + Menu_PrintText(gMainMenuString_Pokedex, 2, 5); sub_8072C14(buffer, GetPokedexSeenCount(), 18, 0); - MenuPrint(buffer, 9, 5); + Menu_PrintText(buffer, 9, 5); } void PrintBadgeCount(void) @@ -730,12 +728,12 @@ void PrintBadgeCount(void) u8 buffer[16]; #if defined(ENGLISH) - MenuPrint(gMainMenuString_Badges, 16, 5); + Menu_PrintText(gMainMenuString_Badges, 16, 5); #elif defined(GERMAN) - MenuPrint_PixelCoords(gMainMenuString_Badges, 124, 40, TRUE); + Menu_PrintTextPixelCoords(gMainMenuString_Badges, 124, 40, TRUE); #endif ConvertIntToDecimalString(buffer, GetBadgeCount()); - MenuPrint_PixelCoords(buffer, 205, 40, 1); + Menu_PrintTextPixelCoords(buffer, 205, 40, 1); } #define tTrainerSpriteId data[2] @@ -750,8 +748,8 @@ void PrintBadgeCount(void) static void Task_NewGameSpeech1(u8 taskId) { - SetUpWindowConfig(&gWindowConfig_81E6C3C); - InitMenuWindow((struct WindowConfig *)&gWindowConfig_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6C3C); + InitMenuWindow((struct WindowTemplate *)&gWindowTemplate_81E6CE4); REG_WIN0H = 0; REG_WIN0V = 0; REG_WININ = 0; @@ -763,7 +761,7 @@ static void Task_NewGameSpeech1(u8 taskId) LZ77UnCompVram(gUnknown_081E7834, (void *)(BG_VRAM + 0x3800)); LoadPalette(gUnknown_081E764C, 0, 0x40); LoadPalette(gUnknown_081E796C, 1, 0x10); - remove_some_task(); + ScanlineEffect_Stop(); ResetSpriteData(); FreeAllSpritePalettes(); AddBirchSpeechObjects(taskId); @@ -812,7 +810,7 @@ static void Task_NewGameSpeech3(u8 taskId) } else { - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); //"Hi! Sorry to keep you waiting... //...But everyone calls me the POKEMON PROFESSOR." MenuPrintMessage(gBirchSpeech_Welcome, 3, 14); @@ -857,7 +855,7 @@ static void Task_NewGameSpeech7(u8 taskId) //Go on to next sentence after frame 95 if (gTasks[taskId].tFrameCounter > 95) { - MenuSetText(gSystemText_NewPara); + Menu_SetText(gSystemText_NewPara); gTasks[taskId].func = Task_NewGameSpeech8; } } @@ -886,7 +884,7 @@ static void Task_NewGameSpeech9(u8 taskId) { if (BirchSpeechUpdateWindowText()) { - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); //"And you are?" MenuPrintMessage(gBirchSpeech_AndYouAre, 3, 14); gTasks[taskId].func = Task_NewGameSpeech10; @@ -962,7 +960,7 @@ static void Task_NewGameSpeech13(u8 taskId) static void Task_NewGameSpeech14(u8 taskId) { - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); //"Are you a boy? Or are you a girl?" MenuPrintMessage(gBirchSpeech_AreYouBoyOrGirl, 3, 14); gTasks[taskId].func = Task_NewGameSpeech15; @@ -985,22 +983,22 @@ static void Task_NewGameSpeech16(u8 taskId) switch (GenderMenuProcessInput()) { case MALE: - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); PlaySE(SE_SELECT); gSaveBlock2.playerGender = MALE; - MenuZeroFillWindowRect(2, 4, 8, 9); + Menu_EraseWindowRect(2, 4, 8, 9); gTasks[taskId].func = Task_NewGameSpeech19; break; case FEMALE: - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); PlaySE(SE_SELECT); gSaveBlock2.playerGender = FEMALE; - MenuZeroFillWindowRect(2, 4, 8, 9); + Menu_EraseWindowRect(2, 4, 8, 9); gTasks[taskId].func = Task_NewGameSpeech19; break; } - cursorPos = GetMenuCursorPos(); + cursorPos = Menu_GetCursorPos(); if (cursorPos != gTasks[taskId].tGenderSelection) { @@ -1062,7 +1060,7 @@ static void Task_NewGameSpeech18(u8 taskId) static void Task_NewGameSpeech19(u8 taskId) { - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); //"All right. What's your name?" MenuPrintMessage(gBirchSpeech_WhatsYourName, 3, 14); gTasks[taskId].func = Task_NewGameSpeech20; @@ -1088,9 +1086,9 @@ static void Task_NewGameSpeech21(u8 taskId) case 2: case 3: case 4: - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); PlaySE(SE_SELECT); - MenuZeroFillWindowRect(2, 1, 22, 12); + Menu_EraseWindowRect(2, 1, 22, 12); SetPresetPlayerName(selection); gTasks[taskId].func = Task_NewGameSpeech23; break; @@ -1100,9 +1098,9 @@ static void Task_NewGameSpeech21(u8 taskId) gTasks[taskId].func = Task_NewGameSpeech22; break; case -1: //B button - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); PlaySE(SE_SELECT); - MenuZeroFillWindowRect(2, 1, 22, 12); + Menu_EraseWindowRect(2, 1, 22, 12); gTasks[taskId].func = Task_NewGameSpeech14; //Go back to gender menu break; } @@ -1120,7 +1118,7 @@ static void Task_NewGameSpeech22(u8 taskId) static void Task_NewGameSpeech23(u8 taskId) { - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); //"So it's (PLAYER)?" StringExpandPlaceholders(gStringVar4, gBirchSpeech_SoItsPlayer); MenuPrintMessage(gStringVar4, 3, 14); @@ -1139,11 +1137,11 @@ static void Task_NewGameSpeech24(u8 taskId) //Handle yes/no menu selection static void Task_NewGameSpeech25(u8 taskId) { - switch (ProcessMenuInputNoWrap_()) + switch (Menu_ProcessInputNoWrap_()) { case 0: //YES PlaySE(SE_SELECT); - MenuZeroFillWindowRect(2, 1, 8, 7); + Menu_EraseWindowRect(2, 1, 8, 7); gSprites[gTasks[taskId].tTrainerSpriteId].oam.objMode = ST_OAM_OBJ_BLEND; StartSpriteFadeOut(taskId, 2); StartBackgroundFadeOut(taskId, 1); @@ -1152,7 +1150,7 @@ static void Task_NewGameSpeech25(u8 taskId) case -1: //B button case 1: //NO PlaySE(SE_SELECT); - MenuZeroFillWindowRect(2, 1, 8, 7); + Menu_EraseWindowRect(2, 1, 8, 7); gTasks[taskId].func = Task_NewGameSpeech14; //Go back to gender menu break; } @@ -1197,7 +1195,7 @@ static void Task_NewGameSpeech27(u8 taskId) StartSpriteFadeIn(taskId, 2); StartBackgroundFadeIn(taskId, 1); - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); StringExpandPlaceholders(gStringVar4, gBirchSpeech_AhOkayYouArePlayer); //"Ah, okay! You're (PLAYER) who's moving... //...I get it now!" @@ -1267,7 +1265,7 @@ static void Task_NewGameSpeech29(u8 taskId) StartSpriteFadeIn(taskId, 2); StartBackgroundFadeIn(taskId, 1); - MenuDrawTextWindow(2, 13, 27, 18); + Menu_DrawStdWindowFrame(2, 13, 27, 18); MenuPrintMessage(gBirchSpeech_AreYouReady, 3, 14); gTasks[taskId].func = Task_NewGameSpeech30; } @@ -1368,13 +1366,13 @@ void CB_ContinueNewGameSpeechPart2() gTasks[taskId].tBGhofs = -60; - remove_some_task(); + ScanlineEffect_Stop(); ResetSpriteData(); FreeAllSpritePalettes(); AddBirchSpeechObjects(taskId); - SetUpWindowConfig(&gWindowConfig_81E6C3C); - InitMenuWindow((struct WindowConfig *)&gWindowConfig_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6C3C); + InitMenuWindow((struct WindowTemplate *)&gWindowTemplate_81E6CE4); if (gSaveBlock2.playerGender != MALE) { @@ -1661,33 +1659,33 @@ static void StartBackgroundFadeIn(u8 taskId, u8 interval) static void CreateGenderMenu(u8 left, u8 top) { u8 menuLeft, menuTop; - MenuDrawTextWindow(left, top, left + 6, top + 5); + Menu_DrawStdWindowFrame(left, top, left + 6, top + 5); menuLeft = left + 1; menuTop = top + 1; - PrintMenuItems(menuLeft, menuTop, 2, gUnknown_081E79B0); + Menu_PrintItems(menuLeft, menuTop, 2, gUnknown_081E79B0); InitMenu(0, menuLeft, menuTop, 2, 0, 5); } static s8 GenderMenuProcessInput(void) { - return ProcessMenuInputNoWrap(); + return Menu_ProcessInputNoWrap(); } static void CreateNameMenu(u8 left, u8 top) { - MenuDrawTextWindow(left, top, left + 10, top + 11); + Menu_DrawStdWindowFrame(left, top, left + 10, top + 11); if (gSaveBlock2.playerGender == MALE) - PrintMenuItems(left + 1, top + 1, 5, gMalePresetNames); + Menu_PrintItems(left + 1, top + 1, 5, gMalePresetNames); else - PrintMenuItems(left + 1, top + 1, 5, gFemalePresetNames); + Menu_PrintItems(left + 1, top + 1, 5, gFemalePresetNames); InitMenu(0, left + 1, top + 1, 5, 0, 9); } static s8 NameMenuProcessInput(void) { - return ProcessMenuInput(); + return Menu_ProcessInput(); } static void SetPresetPlayerName(u8 index) diff --git a/src/engine/menu.c b/src/engine/menu.c index 10988c1d7..86e20d0eb 100644 --- a/src/engine/menu.c +++ b/src/engine/menu.c @@ -24,13 +24,14 @@ struct Menu u8 columnXCoords[8]; }; -static void MultistepInitMenuWindowInternal(const struct WindowConfig *, u16); -static void InitMenuWindowInternal(const struct WindowConfig *, u16); -static bool8 sub_80723D4(void); +static void MultistepInitMenuWindowInternal(const struct WindowTemplate *, u16); +static void InitMenuWindowInternal(const struct WindowTemplate *, u16); +static bool8 IsGridCursorMovementClamped(void); static u8 sub_8072484(u8, u8, u8, u8, u8, u8, u32); static u8 sub_80724F4(u8, u8, u8, const struct MenuAction[], u8); static void sub_8072620(u8, u8, u8, const struct MenuAction[], u8); static void sub_8072D18(u8, u8); +static void RedrawMenuCursor(u8, u8); static struct Menu gMenu; @@ -52,10 +53,10 @@ const struct MenuAction gMenuYesNoItems[] = void CloseMenu(void) { PlaySE(SE_SELECT); - MenuZeroFillScreen(); + Menu_EraseScreen(); sub_8064E2C(); ScriptContext2_Disable(); - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); } void AppendToList(u8 *list, u8 *pindex, u32 value) @@ -64,22 +65,22 @@ void AppendToList(u8 *list, u8 *pindex, u32 value) (*pindex)++; } -void InitMenuWindow(const struct WindowConfig *winConfig) +void InitMenuWindow(const struct WindowTemplate *winTemplate) { - InitMenuWindowInternal(winConfig, 1); + InitMenuWindowInternal(winTemplate, 1); } -void MultistepInitMenuWindowBegin(const struct WindowConfig *winConfig) +void MultistepInitMenuWindowBegin(const struct WindowTemplate *winTemplate) { - MultistepInitMenuWindowInternal(winConfig, 1); + MultistepInitMenuWindowInternal(winTemplate, 1); } -static void MultistepInitMenuWindowInternal(const struct WindowConfig *winConfig, u16 tileOffset) +static void MultistepInitMenuWindowInternal(const struct WindowTemplate *winTemplate, u16 tileOffset) { gMenuMultistepInitState = 0; gMenuTextTileOffset = tileOffset; gMenuWindowPtr = &gMenuWindow; - InitWindowFromConfig(&gMenuWindow, winConfig); + Text_InitWindowWithTemplate(&gMenuWindow, winTemplate); } bool32 MultistepInitMenuWindowContinue(void) @@ -97,13 +98,13 @@ bool32 MultistepInitMenuWindowContinue(void) goto fail; goto next; case 3: - gMenuTextWindowContentTileOffset = SetTextWindowBaseTileNum(gMenuTextWindowTileOffset); + gMenuTextWindowContentTileOffset = TextWindow_SetBaseTileNum(gMenuTextWindowTileOffset); next: gMenuMultistepInitState++; return 0; case 4: - LoadTextWindowGraphics(gMenuWindowPtr); - gMenuMessageBoxContentTileOffset = SetMessageBoxBaseTileNum(gMenuTextWindowContentTileOffset); + TextWindow_LoadStdFrameGraphics(gMenuWindowPtr); + gMenuMessageBoxContentTileOffset = TextWindow_SetDlgFrameBaseTileNum(gMenuTextWindowContentTileOffset); return 1; default: fail: @@ -111,73 +112,74 @@ bool32 MultistepInitMenuWindowContinue(void) } } -static void InitMenuWindowInternal(const struct WindowConfig *winConfig, u16 tileOffset) +static void InitMenuWindowInternal(const struct WindowTemplate *winTemplate, u16 tileOffset) { gMenuWindowPtr = &gMenuWindow; - InitWindowFromConfig(&gMenuWindow, winConfig); + Text_InitWindowWithTemplate(&gMenuWindow, winTemplate); gMenuTextTileOffset = tileOffset; gMenuTextWindowTileOffset = InitWindowTileData(gMenuWindowPtr, gMenuTextTileOffset); - gMenuTextWindowContentTileOffset = SetTextWindowBaseTileNum(gMenuTextWindowTileOffset); - LoadTextWindowGraphics(gMenuWindowPtr); - gMenuMessageBoxContentTileOffset = SetMessageBoxBaseTileNum(gMenuTextWindowContentTileOffset); + gMenuTextWindowContentTileOffset = TextWindow_SetBaseTileNum(gMenuTextWindowTileOffset); + TextWindow_LoadStdFrameGraphics(gMenuWindowPtr); + gMenuMessageBoxContentTileOffset = TextWindow_SetDlgFrameBaseTileNum(gMenuTextWindowContentTileOffset); } -void unref_sub_8071DA4(struct WindowConfig *winConfig, u16 tileOffset) +void unref_sub_8071DA4(struct WindowTemplate *winTemplate, u16 tileOffset) { gMenuWindowPtr = &gMenuWindow; - InitWindowFromConfig(&gMenuWindow, winConfig); + Text_InitWindowWithTemplate(&gMenuWindow, winTemplate); gMenuTextWindowTileOffset = tileOffset; - gMenuTextWindowContentTileOffset = SetTextWindowBaseTileNum(gMenuTextWindowTileOffset); - LoadTextWindowGraphics(gMenuWindowPtr); - gMenuTextTileOffset = SetMessageBoxBaseTileNum(gMenuTextWindowContentTileOffset); + gMenuTextWindowContentTileOffset = TextWindow_SetBaseTileNum(gMenuTextWindowTileOffset); + TextWindow_LoadStdFrameGraphics(gMenuWindowPtr); + gMenuTextTileOffset = TextWindow_SetDlgFrameBaseTileNum(gMenuTextWindowContentTileOffset); gMenuMessageBoxContentTileOffset = InitWindowTileData(gMenuWindowPtr, gMenuTextTileOffset); } -void MenuLoadTextWindowGraphics_OverrideFrameType(u8 frameType) +void Menu_LoadStdFrameGraphicsOverrideStyle(u8 frameType) { - LoadTextWindowGraphics_OverrideFrameType(gMenuWindowPtr, frameType); + TextWindow_LoadStdFrameGraphicsOverrideStyle(gMenuWindowPtr, frameType); } -void MenuLoadTextWindowGraphics(void) +void Menu_LoadStdFrameGraphics(void) { - LoadTextWindowGraphics(gMenuWindowPtr); + TextWindow_LoadStdFrameGraphics(gMenuWindowPtr); } -void BasicInitMenuWindow(const struct WindowConfig *winConfig) +void BasicInitMenuWindow(const struct WindowTemplate *winTemplate) { - InitWindowFromConfig(gMenuWindowPtr, winConfig); + Text_InitWindowWithTemplate(gMenuWindowPtr, winTemplate); gMenuWindowPtr->tileDataStartOffset = gMenuTextTileOffset; } -void MenuPrint(const u8 *str, u8 left, u8 top) +// Prints +void Menu_PrintText(const u8 *str, u8 left, u8 top) { - sub_8003460(gMenuWindowPtr, str, gMenuTextTileOffset, left, top); + Text_InitWindowAndPrintText(gMenuWindowPtr, str, gMenuTextTileOffset, left, top); } -void MenuZeroFillWindowRect(u8 left, u8 top, u8 right, u8 bottom) +void Menu_EraseWindowRect(u8 left, u8 top, u8 right, u8 bottom) { - ZeroFillWindowRect(gMenuWindowPtr, left, top, right, bottom); + Text_EraseWindowRect(gMenuWindowPtr, left, top, right, bottom); } -void MenuFillWindowRectWithBlankTile(u8 left, u8 top, u8 right, u8 bottom) +void Menu_BlankWindowRect(u8 left, u8 top, u8 right, u8 bottom) { - FillWindowRectWithBlankTile(gMenuWindowPtr, left, top, right, bottom); + Text_BlankWindowRect(gMenuWindowPtr, left, top, right, bottom); } -void MenuZeroFillScreen(void) +void Menu_EraseScreen(void) { - MenuZeroFillWindowRect(0, 0, 29, 19); + Menu_EraseWindowRect(0, 0, 29, 19); } -void MenuDrawTextWindow(u8 left, u8 top, u8 right, u8 bottom) +void Menu_DrawStdWindowFrame(u8 left, u8 top, u8 right, u8 bottom) { - DrawTextWindow(gMenuWindowPtr, left, top, right, bottom); + TextWindow_DrawStdFrame(gMenuWindowPtr, left, top, right, bottom); } void sub_8071F40(const u8 *str) { - MenuDrawTextWindow(2, 14, 28, 19); - MenuPrint(str, 3, 15); + Menu_DrawStdWindowFrame(2, 14, 28, 19); + Menu_PrintText(str, 3, 15); } void sub_8071F60(u8 a1, u8 a2, u8 a3) @@ -187,50 +189,50 @@ void sub_8071F60(u8 a1, u8 a2, u8 a3) u16 unref_sub_8071F98(u8 x, u8 y) { - return GetWindowTilemapEntry(gMenuWindowPtr, x, y); + return Text_GetWindowTilemapEntry(gMenuWindowPtr, x, y); } void unref_sub_8071FBC(u16 a1, u8 a2, u8 a3, u8 a4, u8 a5) { - DrawWindowRect(gMenuWindowPtr, a1, a2, a3, a4, a5); + Text_FillWindowBorder(gMenuWindowPtr, a1, a2, a3, a4, a5); } -void MenuDisplayMessageBox(void) +void Menu_DisplayDialogueFrame(void) { - DisplayMessageBox(gMenuWindowPtr); + TextWindow_DisplayDialogueFrame(gMenuWindowPtr); } void MenuPrintMessage(const u8 *str, u8 left, u8 top) { - sub_8002EB0(gMenuWindowPtr, str, gMenuTextTileOffset, left, top); + Text_InitWindow8002EB0(gMenuWindowPtr, str, gMenuTextTileOffset, left, top); } void MenuPrintMessageDefaultCoords(const u8 *str) { - sub_8002EB0(gMenuWindowPtr, str, gMenuTextTileOffset, 2, 15); + Text_InitWindow8002EB0(gMenuWindowPtr, str, gMenuTextTileOffset, 2, 15); } -void MenuSetText(const u8 *str) +void Menu_SetText(const u8 *str) { - sub_8002E90(gMenuWindowPtr, str); + Text_SetWindowText(gMenuWindowPtr, str); } -u8 MenuUpdateWindowText(void) +u8 Menu_UpdateWindowText(void) { - return sub_80035AC(gMenuWindowPtr); + return Text_UpdateWindow(gMenuWindowPtr); } u8 unref_sub_8072098(void) { - return sub_8003418(gMenuWindowPtr); + return Text_PrintWindowSimple(gMenuWindowPtr); } -void sub_80720B0(void) +void Menu_ClearWindowText(void) { - ClearWindowTextLines(gMenuWindowPtr); + Text_ClearWindow(gMenuWindowPtr); } -u8 MoveMenuCursor(s8 delta) +u8 Menu_MoveCursor(s8 delta) { s32 newPos = gMenu.cursorPos + delta; @@ -245,7 +247,7 @@ u8 MoveMenuCursor(s8 delta) return gMenu.cursorPos; } -u8 MoveMenuCursorNoWrap(s8 delta) +u8 Menu_MoveCursorNoWrap(s8 delta) { s32 newPos = gMenu.cursorPos + delta; @@ -260,45 +262,45 @@ u8 MoveMenuCursorNoWrap(s8 delta) return gMenu.cursorPos; } -u8 GetMenuCursorPos(void) +u8 Menu_GetCursorPos(void) { return gMenu.cursorPos; } -s8 ProcessMenuInput(void) +s8 Menu_ProcessInput(void) { if (gMain.newKeys & A_BUTTON) { PlaySE(SE_SELECT); if (gMenu.menu_field_7) - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); return gMenu.cursorPos; } if (gMain.newKeys & B_BUTTON) { if (gMenu.menu_field_7) - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); return -1; } if (gMain.newKeys & DPAD_UP) { PlaySE(SE_SELECT); - MoveMenuCursor(-1); + Menu_MoveCursor(-1); return -2; } else if (gMain.newKeys & DPAD_DOWN) { PlaySE(SE_SELECT); - MoveMenuCursor(1); + Menu_MoveCursor(1); return -2; } return -2; } -s8 ProcessMenuInputNoWrap(void) +s8 Menu_ProcessInputNoWrap(void) { u8 cursorPos = gMenu.cursorPos; @@ -306,31 +308,31 @@ s8 ProcessMenuInputNoWrap(void) { PlaySE(SE_SELECT); if (gMenu.menu_field_7) - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); return gMenu.cursorPos; } if (gMain.newKeys & B_BUTTON) { if (gMenu.menu_field_7) - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); return -1; } if (gMain.newKeys & DPAD_UP) { - if (cursorPos != MoveMenuCursorNoWrap(-1)) + if (cursorPos != Menu_MoveCursorNoWrap(-1)) PlaySE(SE_SELECT); return -2; } else if (gMain.newKeys & DPAD_DOWN) { - if (cursorPos != MoveMenuCursorNoWrap(1)) + if (cursorPos != Menu_MoveCursorNoWrap(1)) PlaySE(SE_SELECT); return -2; } - MoveMenuCursorNoWrap(0); + Menu_MoveCursorNoWrap(0); return -2; } @@ -353,11 +355,12 @@ u8 MoveMenuCursor3(s8 delta) return gMenu.cursorPos; } -u8 MoveMenuCursor4(s8 delta) +static u8 MoveMenuCursorGridLayout(s8 delta) { + // Don't move cursor if it is clamped if (gMenu.cursorPos + delta <= gMenu.maxCursorPos) { - if (sub_80723D4() == TRUE) + if (IsGridCursorMovementClamped() == TRUE) return gMenu.cursorPos; } else @@ -379,22 +382,35 @@ u8 MoveMenuCursor4(s8 delta) return gMenu.cursorPos; } -static bool8 sub_80723D4(void) +// Returns TRUE if the cursor is on an edge and cannot move in the pressed direction +static bool8 IsGridCursorMovementClamped(void) { - if ((gMain.newKeys & DPAD_UP) && gMenu.cursorPos < gMenu.width) - return TRUE; - - if ((gMain.newKeys & DPAD_DOWN) && gMenu.cursorPos >= (gMenu.maxCursorPos + 1) - gMenu.width) - return TRUE; - - if ((gMain.newKeys & DPAD_LEFT) - && ((gMenu.cursorPos - (gMenu.cursorPos % gMenu.width)) % gMenu.width == 1 // always false - || gMenu.cursorPos == 0 - || gMenu.cursorPos % gMenu.width == 0)) - return TRUE; - - if ((gMain.newKeys & DPAD_RIGHT) && gMenu.cursorPos % gMenu.width == gMenu.width - 1) - return TRUE; + if (gMain.newKeys & DPAD_UP) + { + // is cursor on first row? + if (gMenu.cursorPos < gMenu.width) + return TRUE; + } + if (gMain.newKeys & DPAD_DOWN) + { + // is cursor on last row? + if (gMenu.cursorPos >= gMenu.maxCursorPos + 1 - gMenu.width) + return TRUE; + } + if (gMain.newKeys & DPAD_LEFT) + { + // is cursor on first column? + if ((gMenu.cursorPos - gMenu.cursorPos % gMenu.width) % gMenu.width == 1 // always false + || gMenu.cursorPos == 0 // redundant + || gMenu.cursorPos % gMenu.width == 0) + return TRUE; + } + if (gMain.newKeys & DPAD_RIGHT) + { + // is cursor on last column? + if (gMenu.cursorPos % gMenu.width == gMenu.width - 1) + return TRUE; + } return FALSE; } @@ -465,7 +481,7 @@ static u8 sub_80724F4(u8 left, u8 top, u8 menuItemCount, const struct MenuAction totalWidth = (gMenu.columnXCoords[columnCount] + 1); right = left + totalWidth; - MenuDrawTextWindow(left, top, right, bottom); + Menu_DrawStdWindowFrame(left, top, right, bottom); } return maxWidth; @@ -501,7 +517,7 @@ static void sub_8072620(u8 left, u8 top, u8 menuItemCount, const struct MenuActi u8 row = 0; u8 j; for (j = 0; i + j < menuItemCount; j += columnCount, row++) - MenuPrint(menuItems[i + j].text, left + gMenu.columnXCoords[i % columnCount], top + 2 * row); + Menu_PrintText(menuItems[i + j].text, left + gMenu.columnXCoords[i % columnCount], top + 2 * row); } } @@ -513,93 +529,95 @@ void sub_807274C(u8 left, u8 top, u8 menuItemCount, u8 a4, const struct MenuActi sub_8072620(left + 1, top + 1, menuItemCount, menuItems, columnCount); } -s8 sub_80727CC(void) +s8 Menu_ProcessInputGridLayout(void) { if (gMain.newKeys & A_BUTTON) { if (gMenu.menu_field_7) - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); PlaySE(SE_SELECT); - return GetMenuCursorPos(); + return Menu_GetCursorPos(); } if (gMain.newKeys & B_BUTTON) { if (gMenu.menu_field_7) - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); return -1; } if (gMain.newKeys & DPAD_UP) { PlaySE(SE_SELECT); - MoveMenuCursor4(-gMenu.width); + // Move up one whole row + MoveMenuCursorGridLayout(-gMenu.width); return -2; } else if (gMain.newKeys & DPAD_DOWN) { PlaySE(SE_SELECT); - MoveMenuCursor4(gMenu.width); + // Move down one whole column + MoveMenuCursorGridLayout(gMenu.width); return -2; } else if (gMain.newKeys & DPAD_LEFT) { PlaySE(SE_SELECT); - MoveMenuCursor4(-1); + MoveMenuCursorGridLayout(-1); return -2; } else if (gMain.newKeys & DPAD_RIGHT) { PlaySE(SE_SELECT); - MoveMenuCursor4(1); + MoveMenuCursorGridLayout(1); return -2; } return -2; } -u8 sub_807288C(u8 column) +u8 Menu_GetColumnXCoord(u8 column) { return gMenu.columnXCoords[column]; } -void PrintMenuItems(u8 left, u8 top, u8 menuItemCount, const struct MenuAction menuItems[]) +void Menu_PrintItems(u8 left, u8 top, u8 menuItemCount, const struct MenuAction menuItems[]) { u8 i; for (i = 0; i < menuItemCount; i++) - MenuPrint(menuItems[i].text, left, top + 2 * i); + Menu_PrintText(menuItems[i].text, left, top + 2 * i); } -void PrintMenuItemsReordered(u8 left, u8 top, u8 menuItemCount, const struct MenuAction2 menuItems[], const u8 *order) +void Menu_PrintItemsReordered(u8 left, u8 top, u8 menuItemCount, const struct MenuAction2 menuItems[], const u8 *order) { u8 i; for (i = 0; i < menuItemCount; i++) - MenuPrint(menuItems[order[i]].text, left, top + 2 * i); + Menu_PrintText(menuItems[order[i]].text, left, top + 2 * i); } void InitYesNoMenu(u8 left, u8 top, u8 a3) { - PrintMenuItems(left + 1, top + 1, 2, gMenuYesNoItems); + Menu_PrintItems(left + 1, top + 1, 2, gMenuYesNoItems); InitMenu(0, left + 1, top + 1, 2, 0, a3); } void DisplayYesNoMenu(u8 left, u8 top, u32 a3) { - MenuDrawTextWindow(left, top, left + 6, top + 5); + Menu_DrawStdWindowFrame(left, top, left + 6, top + 5); InitYesNoMenu(left, top, 5); gMenu.menu_field_7 = a3 ? -1 : 0; } -s8 ProcessMenuInputNoWrap_(void) +s8 Menu_ProcessInputNoWrap_(void) { - return ProcessMenuInputNoWrap(); + return Menu_ProcessInputNoWrap(); } -u8 MenuPrint_PixelCoords(const u8 *text, u8 left, u16 top, u8 a4) +u8 Menu_PrintTextPixelCoords(const u8 *text, u8 left, u16 top, u8 a4) { - return sub_8004D04(gMenuWindowPtr, text, gMenuTextTileOffset, left, top, a4); + return Text_InitWindow8004D04(gMenuWindowPtr, text, gMenuTextTileOffset, left, top, a4); } u8 sub_8072A18(const u8 *text, u8 left, u16 top, u8 width, u32 a5) @@ -623,7 +641,7 @@ int sub_8072AB0(const u8 *str, u8 left, u16 top, u8 width, u8 height, u32 a6) height = (height + 7) / 8; if (newlineCount < height) - MenuFillWindowRectWithBlankTile(left, top + 2 * newlineCount, left + width - 1, height + top - 1); + Menu_BlankWindowRect(left, top + 2 * newlineCount, left + width - 1, height + top - 1); } #elif GERMAN __attribute__((naked)) @@ -700,7 +718,7 @@ _08072B0C:\n\ lsls r3, 24\n\ lsrs r3, 24\n\ adds r0, r5, 0\n\ - bl MenuFillWindowRectWithBlankTile\n\ + bl Menu_BlankWindowRect\n\ _08072B3E:\n\ add sp, 0x10\n\ pop {r4-r7}\n\ @@ -712,7 +730,7 @@ _08072B3E:\n\ void MenuPrint_RightAligned(const u8 *str, u8 left, u8 top) { - sub_8004D38(gMenuWindowPtr, str, gMenuTextTileOffset, left, top); + Text_InitWindow8004D38(gMenuWindowPtr, str, gMenuTextTileOffset, left, top); } void sub_8072B80(const u8 *a1, u8 a2, u8 a3, const u8 *a4) @@ -720,12 +738,12 @@ void sub_8072B80(const u8 *a1, u8 a2, u8 a3, const u8 *a4) u8 buffer[64]; u8 width = GetStringWidth(gMenuWindowPtr, a4); AlignString(gMenuWindowPtr, buffer, a1, width, 1); - sub_8003460(gMenuWindowPtr, buffer, gMenuTextTileOffset, a2, a3); + Text_InitWindowAndPrintText(gMenuWindowPtr, buffer, gMenuTextTileOffset, a2, a3); } void sub_8072BD8(const u8 *a1, u8 a2, u8 a3, u16 a4) { - sub_8004DB0(gMenuWindowPtr, a1, gMenuTextTileOffset, a2, a3, a4); + Text_InitWindow8004DB0(gMenuWindowPtr, a1, gMenuTextTileOffset, a2, a3, a4); } u8 *sub_8072C14(u8 *a1, s32 a2, u8 a3, u8 a4) @@ -748,19 +766,19 @@ u8 sub_8072CA4(const u8 *str) return GetStringWidth(gMenuWindowPtr, str); } -u8 sub_8072CBC() +u8 Menu_GetTextWindowPaletteNum(void) { - return sub_8004E24(gMenuWindowPtr); + return Text_GetWindowPaletteNum(gMenuWindowPtr); } -void sub_8072CD4(u8 *a1, u8 *a2, u8 *a3) +void Menu_GetTextColors(u8 *a1, u8 *a2, u8 *a3) { - sub_8004E28(gMenuWindowPtr, a1, a2, a3); + Text_GetTextColors(gMenuWindowPtr, a1, a2, a3); } -u32 MenuUpdateWindowText_OverrideLineLength(u8 lineLength) +u32 Menu_UpdateWindowTextOverrideLineLength(u8 lineLength) { - return sub_80037C8(gMenuWindowPtr, lineLength); + return Text_UpdateWindowOverrideLineLength(gMenuWindowPtr, lineLength); } struct Window *unref_sub_8072D0C(void) @@ -770,7 +788,7 @@ struct Window *unref_sub_8072D0C(void) static void sub_8072D18(u8 a1, u8 a2) { - sub_814A5C0(a1, 0xFFFF, 12, 11679, 8 * a2); + MenuCursor_Create814A5C0(a1, 0xFFFF, 12, 11679, 8 * a2); } u8 InitMenu(u8 cursorSubpriority, u8 left, u8 top, u8 numChoices, u8 cursorPos, u8 cursorWidth) @@ -792,14 +810,14 @@ u8 InitMenu(u8 cursorSubpriority, u8 left, u8 top, u8 numChoices, u8 cursorPos, pos = 0; gMenu.cursorPos = pos; - MoveMenuCursor(0); + Menu_MoveCursor(0); return pos; } -void RedrawMenuCursor(u8 a1, u8 a2) +static void RedrawMenuCursor(u8 x, u8 y) { - sub_814A880((a1 + 1) * 8, 8 * a2); + MenuCursor_SetPos814A880((x + 1) * 8, 8 * y); } void unref_sub_8072DC0() @@ -817,7 +835,7 @@ void sub_8072DDC(u8 a1) sub_8072DCC(8 * a1); } -void HandleDestroyMenuCursors(void) +void Menu_DestroyCursor(void) { DestroyMenuCursor(); } diff --git a/src/engine/menu_cursor.c b/src/engine/menu_cursor.c index 960d3cc87..35116d40b 100644 --- a/src/engine/menu_cursor.c +++ b/src/engine/menu_cursor.c @@ -3,13 +3,13 @@ #include "palette.h" #include "sprite.h" -EWRAM_DATA u16 gUnknown_0203A360[0x10] = {}; -EWRAM_DATA struct Subsprite gMenuCursorSubsprites[10] = {0}; -EWRAM_DATA u8 gUnknown_0203A3D0 = 0; -EWRAM_DATA u8 gUnknown_0203A3D1 = 0; -EWRAM_DATA u8 gUnknown_0203A3D2 = 0; -EWRAM_DATA u8 gUnknown_0203A3D3 = 0; -EWRAM_DATA u8 gUnknown_0203A3D4 = 0; +EWRAM_DATA static u16 gUnknown_0203A360[0x10] = {}; +EWRAM_DATA static struct Subsprite gMenuCursorSubsprites[10] = {0}; +EWRAM_DATA static u8 gUnknown_0203A3D0 = 0; +EWRAM_DATA static u8 gUnknown_0203A3D1 = 0; +EWRAM_DATA static u8 gUnknown_0203A3D2 = 0; +EWRAM_DATA static u8 gUnknown_0203A3D3 = 0; +EWRAM_DATA static u8 gUnknown_0203A3D4 = 0; #if ENGLISH #include "../src/data/menu_cursor_en.h" @@ -26,7 +26,7 @@ void sub_814A590(void) gUnknown_0203A3D4 = 0; } -u8 sub_814A5C0(u8 a1, u16 a2, u8 a3, u16 a4, u8 a5) +u8 MenuCursor_Create814A5C0(u8 subpriority, u16 paletteTag, u8 a3, u16 a4, u8 a5) { int v9; struct Sprite *v10; @@ -35,34 +35,34 @@ u8 sub_814A5C0(u8 a1, u16 a2, u8 a3, u16 a4, u8 a5) DestroyMenuCursor(); v9 = 1; - if (a2 == 0xFFFF) + if (paletteTag == 0xFFFF) { gUnknown_0203A360[a3 & 0xF] = a4; if (LoadSpritePalette(&gUnknown_0842F240) != 0xFF) { - a2 = 0xFFF0; + paletteTag = 0xFFF0; v9 = 0; } } LoadSpriteSheetDeferred(&gUnknown_0842F140[a3 & 0xF]); - gUnknown_0203A3D0 = CreateSprite(&gSpriteTemplate_842F250[v9], 0, 0xA0, a1); - gUnknown_0203A3D1 = CreateSprite(&gSpriteTemplate_842F250[2], 0, 0xA0, a1); + gUnknown_0203A3D0 = CreateSprite(&gSpriteTemplate_842F250[v9], 0, 160, subpriority); + gUnknown_0203A3D1 = CreateSprite(&gSpriteTemplate_842F250[2], 0, 160, subpriority); if (gUnknown_0203A3D0 != 0x40) { v10 = &gSprites[gUnknown_0203A3D0]; - if (a2 == 0xFFFF) + if (paletteTag == 0xFFFF) v10->oam.paletteNum = 0; else - v10->oam.paletteNum = IndexOfSpritePaletteTag(a2); + v10->oam.paletteNum = IndexOfSpritePaletteTag(paletteTag); } if (gUnknown_0203A3D1 != 0x40) { v10 = &gSprites[gUnknown_0203A3D1]; - if (a2 == 0xFFFF) + if (paletteTag == 0xFFFF) v10->oam.paletteNum = 0; else - v10->oam.paletteNum = IndexOfSpritePaletteTag(a2); + v10->oam.paletteNum = IndexOfSpritePaletteTag(paletteTag); if (!(REG_DISPCNT & (DISPCNT_WIN0_ON | DISPCNT_WIN1_ON))) *(u8 *)(REG_ADDR_WINOUT) |= 0x1F; @@ -75,12 +75,13 @@ u8 sub_814A5C0(u8 a1, u16 a2, u8 a3, u16 a4, u8 a5) return gUnknown_0203A3D0; } -u8 sub_814A758(u8 a1, u8 a2, u8 a3, u8 a4) +// unused +u8 sub_814A758(u8 subpriority, u8 a2, u8 a3, u8 a4) { u8 result; struct Sprite *spr; - result = sub_814A5C0(a1, 0, a3, 0, a4); + result = MenuCursor_Create814A5C0(subpriority, 0, a3, 0, a4); if (result != 0x40) { spr = &gSprites[gUnknown_0203A3D0]; @@ -128,7 +129,7 @@ void DestroyMenuCursor(void) return; } -void sub_814A880(u8 a1, u8 a2) +void MenuCursor_SetPos814A880(u8 a1, u8 a2) { struct Sprite *spr; @@ -685,49 +686,48 @@ void unref_sub_814ABE4(int a1) return; } -u8 CreateBlendedOutlineCursor(u8 a1, u16 a2, u8 a3, u16 a4, u8 a5) +u8 CreateBlendedOutlineCursor(u8 subpriority, u16 paletteTag, u8 a3, u16 a4, u8 a5) { int v8; struct Sprite *spr; - u8 var1 = gUnknown_0203A3D2; - if (var1 != 0x40) - sub_814AD44(); + if (gUnknown_0203A3D2 != 0x40) + MenuCursor_Destroy814AD44(); v8 = 1; - if (a2 == 0xFFFF) + if (paletteTag == 0xFFFF) { gUnknown_0203A360[a3 & 0xF] = a4; if (LoadSpritePalette(&gUnknown_0842F248) != 0xFF ) { - a2 = 0xFFF1; + paletteTag = 0xFFF1; v8 = 0; } } LoadSpriteSheetDeferred(&gUnknown_0842F1C0[a3 & 0xF]); #if ENGLISH - gUnknown_0203A3D2 = CreateSprite(&gSpriteTemplate_842F298[v8], 0, 160, a1); + gUnknown_0203A3D2 = CreateSprite(&gSpriteTemplate_842F298[v8], 0, 160, subpriority); #elif GERMAN - gUnknown_0203A3D2 = CreateSprite(&gSpriteTemplate_842F298[v8], 0, 161, a1); + gUnknown_0203A3D2 = CreateSprite(&gSpriteTemplate_842F298[v8], 0, 161, subpriority); #endif if (gUnknown_0203A3D2 != 0x40) { spr = &gSprites[gUnknown_0203A3D2]; - if (a2 == 0xFFFF) + if (paletteTag == 0xFFFF) spr->oam.paletteNum = 0; else - spr->oam.paletteNum = IndexOfSpritePaletteTag(a2); + spr->oam.paletteNum = IndexOfSpritePaletteTag(paletteTag); } sub_814ADF4(a5); return gUnknown_0203A3D2; } -void sub_814AD44(void) +void MenuCursor_Destroy814AD44(void) { if (gUnknown_0203A3D2 != 0x40) { @@ -738,7 +738,7 @@ void sub_814AD44(void) return; } -void sub_814AD7C(u8 a1, u8 a2) +void MenuCursor_SetPos814AD7C(u8 a1, u8 a2) { struct Sprite *spr; if (gUnknown_0203A3D2 != 0x40) diff --git a/src/engine/mystery_event_menu.c b/src/engine/mystery_event_menu.c index b9c3d5ad8..bc14c760d 100644 --- a/src/engine/mystery_event_menu.c +++ b/src/engine/mystery_event_menu.c @@ -45,9 +45,9 @@ void CB2_InitMysteryEventMenu(void) FreeAllSpritePalettes(); ResetTasks(); SetVBlankCallback(VBlankCB); - SetUpWindowConfig(&gWindowConfig_81E6CE4); - InitMenuWindow(&gWindowConfig_81E6CE4); - MenuZeroFillScreen(); + Text_LoadWindowTemplate(&gWindowTemplate_81E6CE4); + InitMenuWindow(&gWindowTemplate_81E6CE4); + Menu_EraseScreen(); REG_DISPCNT = DISPCNT_MODE_0 | DISPCNT_OBJ_1D_MAP | DISPCNT_BG0_ON; REG_BLDCNT = 0; CreateTask(Task_DestroySelf, 0); @@ -86,7 +86,7 @@ static void CB2_MysteryEventMenu(void) switch (gMain.state) { case 0: - MenuDrawTextWindow(0, 14, 29, 19); + Menu_DrawStdWindowFrame(0, 14, 29, 19); BeginNormalPaletteFade(0xFFFFFFFF, 0, 0x10, 0, 0); gMain.state++; break; @@ -97,7 +97,7 @@ static void CB2_MysteryEventMenu(void) gMain.state++; break; case 2: - if (MenuUpdateWindowText()) + if (Menu_UpdateWindowText()) { gMain.state++; gLinkType = 21761; @@ -119,7 +119,7 @@ static void CB2_MysteryEventMenu(void) } break; case 4: - if (MenuUpdateWindowText()) + if (Menu_UpdateWindowText()) gMain.state++; break; #ifdef NONMATCHING @@ -135,8 +135,8 @@ static void CB2_MysteryEventMenu(void) { PlaySE(SE_SELECT); sub_8007F4C(); - MenuDrawTextWindow(6, 5, 23, 8); - MenuPrint(gSystemText_LoadingEvent, 7, 6); + Menu_DrawStdWindowFrame(6, 5, 23, 8); + Menu_PrintText(gSystemText_LoadingEvent, 7, 6); gMain.state++; } else if (gMain.newKeys & B_BUTTON) @@ -155,7 +155,7 @@ static void CB2_MysteryEventMenu(void) if (GetLinkPlayerDataExchangeStatusTimed() == 3) { sub_800832C(); - MenuZeroFillWindowRect(6, 5, 23, 8); + Menu_EraseWindowRect(6, 5, 23, 8); GetEventLoadMessage(gStringVar4, 1); MenuPrintMessageDefaultCoords(gStringVar4); gMain.state = 13; @@ -170,7 +170,7 @@ static void CB2_MysteryEventMenu(void) else { CloseLink(); - MenuZeroFillWindowRect(6, 5, 23, 8); + Menu_EraseWindowRect(6, 5, 23, 8); GetEventLoadMessage(gStringVar4, 1); MenuPrintMessageDefaultCoords(gStringVar4); gMain.state = 13; @@ -195,8 +195,8 @@ static void CB2_MysteryEventMenu(void) { PlaySE(SE_SELECT); sub_8007F4C(); - MenuDrawTextWindow(6, 5, 23, 8); - MenuPrint(gSystemText_LoadingEvent, 7, 6); + Menu_DrawStdWindowFrame(6, 5, 23, 8); + Menu_PrintText(gSystemText_LoadingEvent, 7, 6); gMain.state++; } else if (gMain.newKeys & B_BUTTON) @@ -219,7 +219,7 @@ static void CB2_MysteryEventMenu(void) if (GetLinkPlayerDataExchangeStatusTimed() == 3) { sub_800832C(); - MenuZeroFillWindowRect(6, 5, 23, 8); + Menu_EraseWindowRect(6, 5, 23, 8); GetEventLoadMessage(gStringVar4, 1); MenuPrintMessageDefaultCoords(gStringVar4); ptr = (u8 *)&gMain; @@ -245,7 +245,7 @@ static void CB2_MysteryEventMenu(void) else { CloseLink(); - MenuZeroFillWindowRect(6, 5, 23, 8); + Menu_EraseWindowRect(6, 5, 23, 8); label: GetEventLoadMessage(gStringVar4, 1); MenuPrintMessageDefaultCoords(gStringVar4); @@ -266,7 +266,7 @@ static void CB2_MysteryEventMenu(void) break; #endif case 7: - if (MenuUpdateWindowText()) + if (Menu_UpdateWindowText()) gMain.state++; break; case 8: @@ -289,7 +289,7 @@ static void CB2_MysteryEventMenu(void) unkVal = RunMysteryEventScript(gSharedMem); CpuFill32(0, gSharedMem, 0x7D4); if (!GetEventLoadMessage(gStringVar4, unkVal)) - TrySavingData(NORMAL_SAVE); + Save_WriteData(SAVE_NORMAL); gMain.state++; break; case 12: @@ -297,8 +297,8 @@ static void CB2_MysteryEventMenu(void) gMain.state++; break; case 13: - MenuZeroFillWindowRect(6, 5, 23, 8); - if (MenuUpdateWindowText()) + Menu_EraseWindowRect(6, 5, 23, 8); + if (Menu_UpdateWindowText()) { gMain.state++; gUnknown_02039338 = 0; @@ -326,7 +326,7 @@ static void CB2_MysteryEventMenu(void) if (!IsLinkMaster()) { CloseLink(); - MenuZeroFillWindowRect(6, 5, 23, 8); + Menu_EraseWindowRect(6, 5, 23, 8); GetEventLoadMessage(gStringVar4, 1); MenuPrintMessageDefaultCoords(gStringVar4); gMain.state = 13; @@ -338,3 +338,463 @@ static void CB2_MysteryEventMenu(void) BuildOamBuffer(); UpdatePaletteFade(); } + +#if DEBUG + +void debug_sub_815D1D8(); + +__attribute__((naked)) +void debug_sub_815D04C() +{ + asm( + " push {r4, r5, r6, lr}\n" + " lsl r0, r0, #0x18\n" + " lsr r5, r0, #0x18\n" + " ldr r1, ._125 @ gTasks\n" + " lsl r0, r5, #0x2\n" + " add r0, r0, r5\n" + " lsl r0, r0, #0x3\n" + " add r4, r0, r1\n" + " mov r1, #0x8\n" + " ldsh r0, [r4, r1]\n" + " cmp r0, #0\n" + " bne ._121 @cond_branch\n" + " mov r0, #0x4\n" + " mov r1, #0x4\n" + " mov r2, #0xd\n" + " mov r3, #0x7\n" + " bl Menu_DrawStdWindowFrame\n" + " ldr r1, ._125 + 4 @ gUnknown_Debug_842E2D0\n" + " mov r2, #0xa\n" + " ldsh r0, [r4, r2]\n" + " lsl r0, r0, #0x3\n" + " add r0, r0, r1\n" + " ldr r0, [r0]\n" + " mov r1, #0x5\n" + " mov r2, #0x5\n" + " bl Menu_PrintText\n" + " ldrh r0, [r4, #0x8]\n" + " add r0, r0, #0x1\n" + " strh r0, [r4, #0x8]\n" + "._121:\n" + " ldr r2, ._125 + 8 @ gMain\n" + " ldrh r1, [r2, #0x2e]\n" + " mov r0, #0x20\n" + " and r0, r0, r1\n" + " add r6, r2, #0\n" + " cmp r0, #0\n" + " beq ._122 @cond_branch\n" + " ldrh r1, [r4, #0xa]\n" + " mov r2, #0xa\n" + " ldsh r0, [r4, r2]\n" + " cmp r0, #0\n" + " bne ._123 @cond_branch\n" + " ldr r0, ._125 + 12 @ gUnknown_Debug_842E350\n" + " ldrb r0, [r0]\n" + " sub r0, r0, #0x1\n" + " b ._124\n" + "._126:\n" + " .align 2, 0\n" + "._125:\n" + " .word gTasks\n" + " .word gUnknown_Debug_842E2D0\n" + " .word gMain\n" + " .word gUnknown_Debug_842E350\n" + "._123:\n" + " sub r0, r1, #1\n" + "._124:\n" + " strh r0, [r4, #0xa]\n" + " ldr r0, ._130 @ gTasks\n" + " lsl r1, r5, #0x2\n" + " add r1, r1, r5\n" + " lsl r1, r1, #0x3\n" + " add r1, r1, r0\n" + " mov r0, #0x0\n" + " strh r0, [r1, #0x8]\n" + "._122:\n" + " ldrh r1, [r6, #0x2e]\n" + " mov r0, #0x10\n" + " and r0, r0, r1\n" + " cmp r0, #0\n" + " beq ._127 @cond_branch\n" + " ldr r1, ._130 @ gTasks\n" + " lsl r2, r5, #0x2\n" + " add r0, r2, r5\n" + " lsl r0, r0, #0x3\n" + " add r4, r0, r1\n" + " mov r0, #0xa\n" + " ldsh r3, [r4, r0]\n" + " ldr r0, ._130 + 4 @ gUnknown_Debug_842E350\n" + " ldrb r0, [r0]\n" + " sub r0, r0, #0x1\n" + " cmp r3, r0\n" + " bne ._128 @cond_branch\n" + " mov r0, #0x0\n" + " b ._129\n" + "._131:\n" + " .align 2, 0\n" + "._130:\n" + " .word gTasks\n" + " .word gUnknown_Debug_842E350\n" + "._128:\n" + " ldrh r0, [r4, #0xa]\n" + " add r0, r0, #0x1\n" + "._129:\n" + " strh r0, [r4, #0xa]\n" + " add r0, r2, r5\n" + " lsl r0, r0, #0x3\n" + " add r0, r0, r1\n" + " mov r1, #0x0\n" + " strh r1, [r0, #0x8]\n" + "._127:\n" + " ldrh r1, [r6, #0x2e]\n" + " mov r0, #0x1\n" + " and r0, r0, r1\n" + " cmp r0, #0\n" + " beq ._132 @cond_branch\n" + " ldr r1, ._133 @ gTasks\n" + " lsl r0, r5, #0x2\n" + " add r0, r0, r5\n" + " lsl r0, r0, #0x3\n" + " add r0, r0, r1\n" + " mov r2, #0xa\n" + " ldsh r1, [r0, r2]\n" + " lsl r1, r1, #0x3\n" + " ldr r0, ._133 + 4 @ gUnknown_Debug_842E2D0\n" + " add r0, r0, #0x4\n" + " add r1, r1, r0\n" + " ldr r0, ._133 + 8 @ \n" + " ldr r1, [r1]\n" + " bl _call_via_r1\n" + " ldr r0, ._133 + 12 @ \n" + " add r1, r6, r0\n" + " ldrb r0, [r1]\n" + " add r0, r0, #0x1\n" + " strb r0, [r1]\n" + " add r0, r5, #0\n" + " bl DestroyTask\n" + "._132:\n" + " pop {r4, r5, r6}\n" + " pop {r0}\n" + " bx r0\n" + "._134:\n" + " .align 2, 0\n" + "._133:\n" + " .word gTasks\n" + " .word gUnknown_Debug_842E2D0\n" + " .word +0x2004000\n" + " .word 0x43c\n" + "\n" + ); +} + +void debug_sub_815D15C(void) +{ + ResetSpriteData(); + FreeAllSpritePalettes(); + ResetTasks(); + SetVBlankCallback(VBlankCB); + Text_LoadWindowTemplate(&gWindowTemplate_81E6CE4); + InitMenuWindow(&gWindowTemplate_81E6CE4); + Menu_EraseScreen(); + REG_DISPCNT = DISPCNT_BG0_ON | DISPCNT_OBJ_1D_MAP | DISPCNT_MODE_0; + REG_BLDCNT = 0; + + CreateTask(Task_DestroySelf, 0); + StopMapMusic(); + RunTasks(); + AnimateSprites(); + BuildOamBuffer(); + UpdatePaletteFade(); + FillPalette(0, 0, 2); + SetMainCallback2(debug_sub_815D1D8); +} + +__attribute__((naked)) +void debug_sub_815D1D8() +{ + asm( + " push {r4, lr}\n" + " add sp, sp, #0xfffffffc\n" + " ldr r1, ._139 @ gMain\n" + " ldr r2, ._139 + 4 @ 0x43c\n" + " add r0, r1, r2\n" + " ldrb r0, [r0]\n" + " add r4, r1, #0\n" + " cmp r0, #0xb\n" + " bls ._137 @cond_branch\n" + " b ._196\n" + "._137:\n" + " lsl r0, r0, #0x2\n" + " ldr r1, ._139 + 8 @ \n" + " add r0, r0, r1\n" + " ldr r0, [r0]\n" + " mov pc, r0\n" + "._140:\n" + " .align 2, 0\n" + "._139:\n" + " .word gMain\n" + " .word 0x43c\n" + " .word ._141\n" + "._141:\n" + " .word ._142\n" + " .word ._143\n" + " .word ._144\n" + " .word ._145\n" + " .word ._146\n" + " .word ._196\n" + " .word ._148\n" + " .word ._149\n" + " .word ._150\n" + " .word ._151\n" + " .word ._152\n" + " .word ._153\n" + "._142:\n" + " mov r0, #0x3\n" + " mov r1, #0xe\n" + " mov r2, #0x1b\n" + " mov r3, #0x13\n" + " bl Menu_DrawStdWindowFrame\n" + " mov r0, #0x1\n" + " neg r0, r0\n" + " mov r1, #0x0\n" + " str r1, [sp]\n" + " mov r2, #0x10\n" + " mov r3, #0x0\n" + " bl BeginNormalPaletteFade\n" + " ldr r1, ._155 @ gMain\n" + " ldr r0, ._155 + 4 @ 0x43c\n" + " add r1, r1, r0\n" + " b ._189\n" + "._156:\n" + " .align 2, 0\n" + "._155:\n" + " .word gMain\n" + " .word 0x43c\n" + "._143:\n" + " ldr r0, ._161 @ gPaletteFade\n" + " ldrb r1, [r0, #0x7]\n" + " mov r0, #0x80\n" + " and r0, r0, r1\n" + " cmp r0, #0\n" + " beq ._157 @cond_branch\n" + " b ._196\n" + "._157:\n" + " ldrh r1, [r4, #0x2c]\n" + " mov r0, #0x80\n" + " lsl r0, r0, #0x1\n" + " and r0, r0, r1\n" + " cmp r0, #0\n" + " beq ._159 @cond_branch\n" + " ldr r1, ._161 + 4 @ gUnknown_Debug_30030E0\n" + " ldrb r0, [r1]\n" + " add r0, r0, #0x1\n" + " strb r0, [r1]\n" + "._159:\n" + " ldr r0, ._161 + 8 @ Str_843DA70\n" + " mov r1, #0x4\n" + " mov r2, #0xf\n" + " bl MenuPrintMessage\n" + " ldr r2, ._161 + 12 @ 0x43c\n" + " add r1, r4, r2\n" + " b ._189\n" + "._162:\n" + " .align 2, 0\n" + "._161:\n" + " .word gPaletteFade\n" + " .word gUnknown_Debug_30030E0\n" + " .word Str_843DA70\n" + " .word 0x43c\n" + "._144:\n" + " bl Menu_UpdateWindowText\n" + " lsl r0, r0, #0x18\n" + " cmp r0, #0\n" + " bne ._163 @cond_branch\n" + " b ._196\n" + "._163:\n" + " ldr r1, ._166 @ gMain\n" + " ldr r0, ._166 + 4 @ 0x43c\n" + " add r1, r1, r0\n" + " ldrb r0, [r1]\n" + " add r0, r0, #0x1\n" + " strb r0, [r1]\n" + " ldr r1, ._166 + 8 @ gLinkType\n" + " ldr r2, ._166 + 12 @ 0x5501\n" + " add r0, r2, #0\n" + " strh r0, [r1]\n" + " bl OpenLink\n" + " b ._196\n" + "._167:\n" + " .align 2, 0\n" + "._166:\n" + " .word gMain\n" + " .word 0x43c\n" + " .word gLinkType\n" + " .word 0x5501\n" + "._145:\n" + " ldr r0, ._171 @ gReceivedRemoteLinkPlayers\n" + " ldrb r0, [r0]\n" + " cmp r0, #0\n" + " bne ._168 @cond_branch\n" + " b ._196\n" + "._168:\n" + " ldr r1, ._171 + 4 @ 0x43c\n" + " add r0, r4, r1\n" + " ldrb r1, [r0]\n" + " add r1, r1, #0x1\n" + " strb r1, [r0]\n" + " ldr r0, ._171 + 8 @ Str_843DA84\n" + " mov r1, #0x4\n" + " mov r2, #0xf\n" + " bl MenuPrintMessage\n" + " b ._196\n" + "._172:\n" + " .align 2, 0\n" + "._171:\n" + " .word gReceivedRemoteLinkPlayers\n" + " .word 0x43c\n" + " .word Str_843DA84\n" + "._146:\n" + " bl Menu_UpdateWindowText\n" + " lsl r0, r0, #0x18\n" + " cmp r0, #0\n" + " bne ._173 @cond_branch\n" + " b ._196\n" + "._173:\n" + " ldr r0, ._176 @ debug_sub_815D04C\n" + " mov r1, #0xa\n" + " bl CreateTask\n" + " ldr r1, ._176 + 4 @ gMain\n" + " ldr r2, ._176 + 8 @ 0x43c\n" + " add r1, r1, r2\n" + " b ._189\n" + "._177:\n" + " .align 2, 0\n" + "._176:\n" + " .word debug_sub_815D04C+1\n" + " .word gMain\n" + " .word 0x43c\n" + "._148:\n" + " ldr r0, ._179 @ Str_843DA98\n" + " mov r1, #0x4\n" + " mov r2, #0xf\n" + " bl MenuPrintMessage\n" + " ldr r1, ._179 + 4 @ 0x2004000\n" + " ldr r2, ._179 + 8 @ 0x2004\n" + " mov r0, #0x0\n" + " bl SendBlock\n" + " ldr r1, ._179 + 12 @ gMain\n" + " ldr r0, ._179 + 16 @ 0x43c\n" + " add r1, r1, r0\n" + " b ._189\n" + "._180:\n" + " .align 2, 0\n" + "._179:\n" + " .word Str_843DA98\n" + " .word 0x2004000\n" + " .word 0x2004\n" + " .word gMain\n" + " .word 0x43c\n" + "._149:\n" + " bl Menu_UpdateWindowText\n" + " bl IsLinkTaskFinished\n" + " lsl r0, r0, #0x18\n" + " cmp r0, #0\n" + " beq ._196 @cond_branch\n" + " ldr r1, ._183 @ gMain\n" + " ldr r2, ._183 + 4 @ 0x43c\n" + " add r1, r1, r2\n" + " b ._189\n" + "._184:\n" + " .align 2, 0\n" + "._183:\n" + " .word gMain\n" + " .word 0x43c\n" + "._150:\n" + " bl sub_800832C\n" + " ldr r1, ._186 @ gMain\n" + " ldr r0, ._186 + 4 @ 0x43c\n" + " add r1, r1, r0\n" + " b ._189\n" + "._187:\n" + " .align 2, 0\n" + "._186:\n" + " .word gMain\n" + " .word 0x43c\n" + "._151:\n" + " ldr r0, ._190 @ gReceivedRemoteLinkPlayers\n" + " ldrb r0, [r0]\n" + " cmp r0, #0\n" + " bne ._196 @cond_branch\n" + " mov r0, #0x4\n" + " mov r1, #0xf\n" + " mov r2, #0x1a\n" + " mov r3, #0x12\n" + " bl Menu_BlankWindowRect\n" + " ldr r0, ._190 + 4 @ Str_843DAA3\n" + " mov r1, #0x4\n" + " mov r2, #0xf\n" + " bl Menu_PrintText\n" + " ldr r1, ._190 + 8 @ gMain\n" + " ldr r2, ._190 + 12 @ 0x43c\n" + " add r1, r1, r2\n" + " b ._189\n" + "._191:\n" + " .align 2, 0\n" + "._190:\n" + " .word gReceivedRemoteLinkPlayers\n" + " .word Str_843DAA3\n" + " .word gMain\n" + " .word 0x43c\n" + "._152:\n" + " ldrh r1, [r4, #0x2e]\n" + " mov r0, #0x1\n" + " and r0, r0, r1\n" + " cmp r0, #0\n" + " beq ._196 @cond_branch\n" + " mov r0, #0x1\n" + " neg r0, r0\n" + " mov r1, #0x0\n" + " str r1, [sp]\n" + " mov r2, #0x0\n" + " mov r3, #0x10\n" + " bl BeginNormalPaletteFade\n" + " ldr r0, ._194 @ 0x43c\n" + " add r1, r4, r0\n" + "._189:\n" + " ldrb r0, [r1]\n" + " add r0, r0, #0x1\n" + " strb r0, [r1]\n" + " b ._196\n" + "._195:\n" + " .align 2, 0\n" + "._194:\n" + " .word 0x43c\n" + "._153:\n" + " ldr r0, ._197 @ gPaletteFade\n" + " ldrb r1, [r0, #0x7]\n" + " mov r0, #0x80\n" + " and r0, r0, r1\n" + " cmp r0, #0\n" + " bne ._196 @cond_branch\n" + " ldr r0, ._197 + 4 @ CB2_InitTitleScreen\n" + " bl SetMainCallback2\n" + "._196:\n" + " bl RunTasks\n" + " bl AnimateSprites\n" + " bl BuildOamBuffer\n" + " bl UpdatePaletteFade\n" + " add sp, sp, #0x4\n" + " pop {r4}\n" + " pop {r0}\n" + " bx r0\n" + "._198:\n" + " .align 2, 0\n" + "._197:\n" + " .word gPaletteFade\n" + " .word CB2_InitTitleScreen+1\n" + "\n" + ); +} + +#endif diff --git a/src/engine/name_string_util.c b/src/engine/name_string_util.c index f1a935453..32e9358e1 100644 --- a/src/engine/name_string_util.c +++ b/src/engine/name_string_util.c @@ -7,7 +7,7 @@ void PadNameString(u8 *a1, u8 a2) { u8 i; - StripExtCtrlCodes(a1); + Text_StripExtCtrlCodes(a1); i = StringLength(a1); if (a2 == 0xFC) @@ -36,5 +36,5 @@ void SanitizeNameString(u8 *a1) if (StringLength(a1) < 6) ConvertInternationalString(a1, 1); else - StripExtCtrlCodes(a1); + Text_StripExtCtrlCodes(a1); } diff --git a/src/engine/naming_screen.c b/src/engine/naming_screen.c index 440504d5a..ca77c5483 100644 --- a/src/engine/naming_screen.c +++ b/src/engine/naming_screen.c @@ -26,8 +26,6 @@ #define COLUMN_COUNT 10 #endif -extern u16 gKeyRepeatStartDelay; - extern u8 CreateMonIcon(u16 species, void (*callback)(struct Sprite *), s16 x, s16 y, u8 subpriority, u32 personality); const u8 gSpriteImage_83CE094[] = INCBIN_U8("graphics/naming_screen/pc_icon/0.4bpp"); @@ -258,20 +256,7 @@ static void NamingScreen_InitDisplayMode(void) static void NamingScreen_ClearVram(void) { - u8 *addr = (void *)VRAM; - u32 size = 0x10000; - - while (1) - { - DmaFill16(3, 0, addr, 0x1000); - addr += 0x1000; - size -= 0x1000; - if (size <= 0x1000) - { - DmaFill16(3, 0, addr, size); - break; - } - } + DmaFill16Large(3, 0, (void *)VRAM, 0x10000, 0x1000); } static void NamingScreen_ClearOam(void) @@ -325,8 +310,8 @@ static void NamingScreen_Init(void) static void NamingScreen_SetUpWindow(void) { - SetUpWindowConfig(&gWindowConfig_81E6E88); - InitMenuWindow(&gWindowConfig_81E6E88); + Text_LoadWindowTemplate(&gWindowTemplate_81E6E88); + InitMenuWindow(&gWindowTemplate_81E6E88); } static void NamingScreen_ResetObjects(void) @@ -503,7 +488,7 @@ static bool8 MainState_6(struct Task *task) static bool8 MainState_UpdateSentToPCMessage(struct Task *task) { - if (MenuUpdateWindowText()) + if (Menu_UpdateWindowText()) namingScreenDataPtr->state++; return FALSE; } @@ -1610,8 +1595,8 @@ static void DisplaySentToPCMessage(void) { StringCopy(gStringVar1, namingScreenDataPtr->destBuffer); StringExpandPlaceholders(gStringVar4, gOtherText_SentToPC); - BasicInitMenuWindow(&gWindowConfig_81E6E88); - MenuDisplayMessageBox(); + BasicInitMenuWindow(&gWindowTemplate_81E6E88); + Menu_DisplayDialogueFrame(); MenuPrintMessageDefaultCoords(gStringVar4); } @@ -1629,16 +1614,8 @@ static void sub_80B7558(void) static void sub_80B7568(void) { - const void *src; - void *dst; - - src = gNamingScreenMenu_Gfx; - dst = (void *)(VRAM + gMenuMessageBoxContentTileOffset * 32); - DmaCopy16(3, src, dst, 0x800); - - src = gNamingScreenMenu_Gfx; - dst = (void *)(VRAM + 0x8000 + gMenuMessageBoxContentTileOffset * 32); - DmaCopy16(3, src, dst, 0x800); + DmaCopy16Defvars(3, gNamingScreenMenu_Gfx, (void *)(VRAM + gMenuMessageBoxContentTileOffset * 32), 0x800); + DmaCopy16Defvars(3, gNamingScreenMenu_Gfx, (void *)(VRAM + 0x8000 + gMenuMessageBoxContentTileOffset * 32), 0x800); } static void sub_80B75B0(void) @@ -1757,19 +1734,19 @@ static void (*const gUnknown_083CE310[][2])(void) = sub_80B7844, }; -static const struct WindowConfig *const gUnknown_083CE328[][2][2] = +static const struct WindowTemplate *const gUnknown_083CE328[][2][2] = { { - {&gWindowConfig_81E6EDC, &gWindowConfig_81E6EF8}, - {&gWindowConfig_81E6EA4, &gWindowConfig_81E6EC0}, + {&gWindowTemplate_81E6EDC, &gWindowTemplate_81E6EF8}, + {&gWindowTemplate_81E6EA4, &gWindowTemplate_81E6EC0}, }, { - {&gWindowConfig_81E6EA4, &gWindowConfig_81E6EC0}, - {&gWindowConfig_81E6F14, &gWindowConfig_81E6F30}, + {&gWindowTemplate_81E6EA4, &gWindowTemplate_81E6EC0}, + {&gWindowTemplate_81E6F14, &gWindowTemplate_81E6F30}, }, { - {&gWindowConfig_81E6F14, &gWindowConfig_81E6F30}, - {&gWindowConfig_81E6EDC, &gWindowConfig_81E6EF8}, + {&gWindowTemplate_81E6F14, &gWindowTemplate_81E6F30}, + {&gWindowTemplate_81E6EDC, &gWindowTemplate_81E6EF8}, }, }; @@ -1838,15 +1815,15 @@ static void PrintKeyboardCharacters(u8 page) //print letters on page s16 r5; for (i = 0, r5 = 9; i < 4; i++, r5 += 2) - MenuPrint(sKeyboardCharacters[page][i], 3, r5); + Menu_PrintText(sKeyboardCharacters[page][i], 3, r5); } static void sub_80B78A8(void) { - BasicInitMenuWindow(&gWindowConfig_81E6F4C); + BasicInitMenuWindow(&gWindowTemplate_81E6F4C); gUnknown_083CE358[namingScreenDataPtr->templateNum](); gUnknown_083CE368[namingScreenDataPtr->template->unk3](); - MenuPrint(namingScreenDataPtr->template->title, 9, 2); + Menu_PrintText(namingScreenDataPtr->template->title, 9, 2); } static void nullsub_61(void) @@ -1870,7 +1847,7 @@ static void sub_80B7924(void) { if ((s16)namingScreenDataPtr->unk40 == MON_FEMALE) genderSymbol[0] = 0xB6; //female symbol - MenuPrint(genderSymbol, 0x14, 4); + Menu_PrintText(genderSymbol, 0x14, 4); } } @@ -1886,8 +1863,8 @@ static void sub_80B7960(void) string[5] = 1; string += 6; StringCopy(string, namingScreenDataPtr->textBuffer); - BasicInitMenuWindow(&gWindowConfig_81E6F4C); - MenuPrint(gStringVar1, namingScreenDataPtr->unk2, 4); + BasicInitMenuWindow(&gWindowTemplate_81E6F4C); + Menu_PrintText(gStringVar1, namingScreenDataPtr->unk2, 4); } //-------------------------------------------------- diff --git a/src/engine/option_menu.c b/src/engine/option_menu.c index dfc49b035..a2d32ffb6 100644 --- a/src/engine/option_menu.c +++ b/src/engine/option_menu.c @@ -3,26 +3,16 @@ #include "main.h" #include "menu.h" #include "palette.h" +#include "scanline_effect.h" #include "sprite.h" #include "strings2.h" #include "task.h" extern void SetPokemonCryStereo(u32 val); -extern void remove_some_task(void); - -//Task data -enum { - TD_MENUSELECTION, - TD_TEXTSPEED, - TD_BATTLESCENE, - TD_BATTLESTYLE, - TD_SOUND, - TD_BUTTONMODE, - TD_FRAMETYPE, -}; -//Menu items -enum { +// Menu items +enum +{ MENUITEM_TEXTSPEED, MENUITEM_BATTLESCENE, MENUITEM_BATTLESTYLE, @@ -32,6 +22,15 @@ enum { MENUITEM_CANCEL, }; +// Task data +#define tMenuSelection data[0] +#define tOptTextSpeed data[1] +#define tOptBattleScene data[2] +#define tOptBattleStyle data[3] +#define tOptSound data[4] +#define tOptButtonMode data[5] +#define tOptFrameType data[6] + const u16 gUnknown_0839F5FC[] = INCBIN_U16("graphics/misc/option_menu_text.gbapal"); // note: this is only used in the Japanese release const u8 gUnknown_0839F63C[] = INCBIN_U8("graphics/misc/option_menu_equals_sign.4bpp"); @@ -75,10 +74,6 @@ void CB2_InitOptionMenu(void) { default: case 0: - { - u8 *addr; - u32 size; - SetVBlankCallback(NULL); REG_DISPCNT = 0; REG_BG2CNT = 0; @@ -90,37 +85,24 @@ void CB2_InitOptionMenu(void) REG_BG1VOFS = 0; REG_BG0HOFS = 0; REG_BG0VOFS = 0; - addr = (u8 *)VRAM; - size = 0x18000; - while (1) - { - DmaFill16(3, 0, addr, 0x1000); - addr += 0x1000; - size -= 0x1000; - if (size <= 0x1000) - { - DmaFill16(3, 0, addr, size); - break; - } - } + DmaFill16Large(3, 0, (u8 *)VRAM, 0x18000, 0x1000); DmaClear32(3, OAM, OAM_SIZE); DmaClear16(3, PLTT, PLTT_SIZE); gMain.state++; break; - } case 1: ResetPaletteFade(); - remove_some_task(); + ScanlineEffect_Stop(); ResetTasks(); ResetSpriteData(); gMain.state++; break; case 2: - SetUpWindowConfig(&gWindowConfig_81E71B4); + Text_LoadWindowTemplate(&gWindowTemplate_81E71B4); gMain.state++; break; case 3: - MultistepInitMenuWindowBegin(&gWindowConfig_81E71B4); + MultistepInitMenuWindowBegin(&gWindowTemplate_81E71B4); gMain.state++; break; case 4: @@ -165,37 +147,38 @@ void CB2_InitOptionMenu(void) { u8 taskId = CreateTask(Task_OptionMenuFadeIn, 0); - gTasks[taskId].data[TD_MENUSELECTION] = 0; - gTasks[taskId].data[TD_TEXTSPEED] = gSaveBlock2.optionsTextSpeed; - gTasks[taskId].data[TD_BATTLESCENE] = gSaveBlock2.optionsBattleSceneOff; - gTasks[taskId].data[TD_BATTLESTYLE] = gSaveBlock2.optionsBattleStyle; - gTasks[taskId].data[TD_SOUND] = gSaveBlock2.optionsSound; - gTasks[taskId].data[TD_BUTTONMODE] = gSaveBlock2.optionsButtonMode; - gTasks[taskId].data[TD_FRAMETYPE] = gSaveBlock2.optionsWindowFrameType; - - MenuDrawTextWindow(2, 0, 27, 3); - MenuDrawTextWindow(2, 4, 27, 19); - - MenuPrint(gSystemText_OptionMenu, 4, 1); - MenuPrint(gSystemText_TextSpeed, 4, 5); - MenuPrint(gSystemText_BattleScene, 4, 7); - MenuPrint(gSystemText_BattleStyle, 4, 9); - MenuPrint(gSystemText_Sound, 4, 11); - MenuPrint(gSystemText_ButtonMode, 4, 13); - MenuPrint(gSystemText_Frame, 4, 15); - MenuPrint(gSystemText_Cancel, 4, 17); - - TextSpeed_DrawChoices(gTasks[taskId].data[TD_TEXTSPEED]); - BattleScene_DrawChoices(gTasks[taskId].data[TD_BATTLESCENE]); - BattleStyle_DrawChoices(gTasks[taskId].data[TD_BATTLESTYLE]); - Sound_DrawChoices(gTasks[taskId].data[TD_SOUND]); - ButtonMode_DrawChoices(gTasks[taskId].data[TD_BUTTONMODE]); - FrameType_DrawChoices(gTasks[taskId].data[TD_FRAMETYPE]); + gTasks[taskId].tMenuSelection = 0; + gTasks[taskId].tOptTextSpeed = gSaveBlock2.optionsTextSpeed; + gTasks[taskId].tOptBattleScene = gSaveBlock2.optionsBattleSceneOff; + gTasks[taskId].tOptBattleStyle = gSaveBlock2.optionsBattleStyle; + gTasks[taskId].tOptSound = gSaveBlock2.optionsSound; + gTasks[taskId].tOptButtonMode = gSaveBlock2.optionsButtonMode; + gTasks[taskId].tOptFrameType = gSaveBlock2.optionsWindowFrameType; + + Menu_DrawStdWindowFrame(2, 0, 27, 3); // title box + Menu_DrawStdWindowFrame(2, 4, 27, 19); // options list box + + Menu_PrintText(gSystemText_OptionMenu, 4, 1); + + Menu_PrintText(gSystemText_TextSpeed, 4, 5); + Menu_PrintText(gSystemText_BattleScene, 4, 7); + Menu_PrintText(gSystemText_BattleStyle, 4, 9); + Menu_PrintText(gSystemText_Sound, 4, 11); + Menu_PrintText(gSystemText_ButtonMode, 4, 13); + Menu_PrintText(gSystemText_Frame, 4, 15); + Menu_PrintText(gSystemText_Cancel, 4, 17); + + TextSpeed_DrawChoices(gTasks[taskId].tOptTextSpeed); + BattleScene_DrawChoices(gTasks[taskId].tOptBattleScene); + BattleStyle_DrawChoices(gTasks[taskId].tOptBattleStyle); + Sound_DrawChoices(gTasks[taskId].tOptSound); + ButtonMode_DrawChoices(gTasks[taskId].tOptButtonMode); + FrameType_DrawChoices(gTasks[taskId].tOptFrameType); REG_WIN0H = WIN_RANGE(17, 223); REG_WIN0V = WIN_RANGE(1, 31); - HighlightOptionMenuItem(gTasks[taskId].data[TD_MENUSELECTION]); + HighlightOptionMenuItem(gTasks[taskId].tMenuSelection); gMain.state++; break; } @@ -208,16 +191,14 @@ void CB2_InitOptionMenu(void) static void Task_OptionMenuFadeIn(u8 taskId) { if (!gPaletteFade.active) - { gTasks[taskId].func = Task_OptionMenuProcessInput; - } } static void Task_OptionMenuProcessInput(u8 taskId) { if (gMain.newKeys & A_BUTTON) { - if (gTasks[taskId].data[TD_MENUSELECTION] == MENUITEM_CANCEL) + if (gTasks[taskId].tMenuSelection == MENUITEM_CANCEL) gTasks[taskId].func = Task_OptionMenuSave; } else if (gMain.newKeys & B_BUTTON) @@ -226,47 +207,47 @@ static void Task_OptionMenuProcessInput(u8 taskId) } else if (gMain.newKeys & DPAD_UP) { - if (gTasks[taskId].data[TD_MENUSELECTION] > 0) - gTasks[taskId].data[TD_MENUSELECTION]--; + if (gTasks[taskId].tMenuSelection > 0) + gTasks[taskId].tMenuSelection--; else - gTasks[taskId].data[TD_MENUSELECTION] = 6; - HighlightOptionMenuItem(gTasks[taskId].data[TD_MENUSELECTION]); + gTasks[taskId].tMenuSelection = 6; + HighlightOptionMenuItem(gTasks[taskId].tMenuSelection); } else if (gMain.newKeys & DPAD_DOWN) { - if (gTasks[taskId].data[TD_MENUSELECTION] <= 5) - gTasks[taskId].data[TD_MENUSELECTION]++; + if (gTasks[taskId].tMenuSelection < 6) + gTasks[taskId].tMenuSelection++; else - gTasks[taskId].data[TD_MENUSELECTION] = 0; - HighlightOptionMenuItem(gTasks[taskId].data[TD_MENUSELECTION]); + gTasks[taskId].tMenuSelection = 0; + HighlightOptionMenuItem(gTasks[taskId].tMenuSelection); } else { - switch (gTasks[taskId].data[TD_MENUSELECTION]) + switch (gTasks[taskId].tMenuSelection) { case MENUITEM_TEXTSPEED: - gTasks[taskId].data[TD_TEXTSPEED] = TextSpeed_ProcessInput(gTasks[taskId].data[TD_TEXTSPEED]); - TextSpeed_DrawChoices(gTasks[taskId].data[TD_TEXTSPEED]); + gTasks[taskId].tOptTextSpeed = TextSpeed_ProcessInput(gTasks[taskId].tOptTextSpeed); + TextSpeed_DrawChoices(gTasks[taskId].tOptTextSpeed); break; case MENUITEM_BATTLESCENE: - gTasks[taskId].data[TD_BATTLESCENE] = BattleScene_ProcessInput(gTasks[taskId].data[TD_BATTLESCENE]); - BattleScene_DrawChoices(gTasks[taskId].data[TD_BATTLESCENE]); + gTasks[taskId].tOptBattleScene = BattleScene_ProcessInput(gTasks[taskId].tOptBattleScene); + BattleScene_DrawChoices(gTasks[taskId].tOptBattleScene); break; case MENUITEM_BATTLESTYLE: - gTasks[taskId].data[TD_BATTLESTYLE] = BattleStyle_ProcessInput(gTasks[taskId].data[TD_BATTLESTYLE]); - BattleStyle_DrawChoices(gTasks[taskId].data[TD_BATTLESTYLE]); + gTasks[taskId].tOptBattleStyle = BattleStyle_ProcessInput(gTasks[taskId].tOptBattleStyle); + BattleStyle_DrawChoices(gTasks[taskId].tOptBattleStyle); break; case MENUITEM_SOUND: - gTasks[taskId].data[TD_SOUND] = Sound_ProcessInput(gTasks[taskId].data[TD_SOUND]); - Sound_DrawChoices(gTasks[taskId].data[TD_SOUND]); + gTasks[taskId].tOptSound = Sound_ProcessInput(gTasks[taskId].tOptSound); + Sound_DrawChoices(gTasks[taskId].tOptSound); break; case MENUITEM_BUTTONMODE: - gTasks[taskId].data[TD_BUTTONMODE] = ButtonMode_ProcessInput(gTasks[taskId].data[TD_BUTTONMODE]); - ButtonMode_DrawChoices(gTasks[taskId].data[TD_BUTTONMODE]); + gTasks[taskId].tOptButtonMode = ButtonMode_ProcessInput(gTasks[taskId].tOptButtonMode); + ButtonMode_DrawChoices(gTasks[taskId].tOptButtonMode); break; case MENUITEM_FRAMETYPE: - gTasks[taskId].data[TD_FRAMETYPE] = FrameType_ProcessInput(gTasks[taskId].data[TD_FRAMETYPE]); - FrameType_DrawChoices(gTasks[taskId].data[TD_FRAMETYPE]); + gTasks[taskId].tOptFrameType = FrameType_ProcessInput(gTasks[taskId].tOptFrameType); + FrameType_DrawChoices(gTasks[taskId].tOptFrameType); break; } } @@ -274,12 +255,12 @@ static void Task_OptionMenuProcessInput(u8 taskId) static void Task_OptionMenuSave(u8 taskId) { - gSaveBlock2.optionsTextSpeed = gTasks[taskId].data[TD_TEXTSPEED]; - gSaveBlock2.optionsBattleSceneOff = gTasks[taskId].data[TD_BATTLESCENE]; - gSaveBlock2.optionsBattleStyle = gTasks[taskId].data[TD_BATTLESTYLE]; - gSaveBlock2.optionsSound = gTasks[taskId].data[TD_SOUND]; - gSaveBlock2.optionsButtonMode = gTasks[taskId].data[TD_BUTTONMODE]; - gSaveBlock2.optionsWindowFrameType = gTasks[taskId].data[TD_FRAMETYPE]; + gSaveBlock2.optionsTextSpeed = gTasks[taskId].tOptTextSpeed; + gSaveBlock2.optionsBattleSceneOff = gTasks[taskId].tOptBattleScene; + gSaveBlock2.optionsBattleStyle = gTasks[taskId].tOptBattleStyle; + gSaveBlock2.optionsSound = gTasks[taskId].tOptSound; + gSaveBlock2.optionsButtonMode = gTasks[taskId].tOptButtonMode; + gSaveBlock2.optionsWindowFrameType = gTasks[taskId].tOptFrameType; BeginNormalPaletteFade(-1, 0, 0, 0x10, 0); gTasks[taskId].func = Task_OptionMenuFadeOut; @@ -305,29 +286,29 @@ static void HighlightOptionMenuItem(u8 index) static void DrawOptionMenuChoice(const u8 *text, u8 x, u8 y, u8 style) { - u8 dst[16]; + u8 dst[15]; u16 i; - for (i = 0; *text != EOS && i <= 14; i++) + for (i = 0; *text != EOS && i < 15; i++) dst[i] = *(text++); dst[2] = style; dst[i] = EOS; - MenuPrint_PixelCoords(dst, x, y, 1); + Menu_PrintTextPixelCoords(dst, x, y, 1); } static u8 TextSpeed_ProcessInput(u8 selection) { if (gMain.newKeys & DPAD_RIGHT) { - if (selection <= 1) + if (selection < 2) selection++; else selection = 0; } if (gMain.newKeys & DPAD_LEFT) { - if (selection != 0) + if (selection > 0) selection--; else selection = 2; @@ -356,7 +337,7 @@ static void TextSpeed_DrawChoices(u8 selection) styles[selection] = 0x8; DrawOptionMenuChoice(gSystemText_Slow, TEXTSPEED_SLOW_LEFT, 40, styles[0]); - DrawOptionMenuChoice(gSystemText_Mid, TEXTSPEED_MIX_LEFT, 40, styles[1]); + DrawOptionMenuChoice(gSystemText_Mid, TEXTSPEED_MIX_LEFT, 40, styles[1]); DrawOptionMenuChoice(gSystemText_Fast, TEXTSPEED_FAST_LEFT, 40, styles[2]); } @@ -375,7 +356,7 @@ static void BattleScene_DrawChoices(u8 selection) styles[1] = 0xF; styles[selection] = 0x8; - DrawOptionMenuChoice(gSystemText_On, 120, 56, styles[0]); + DrawOptionMenuChoice(gSystemText_On, 120, 56, styles[0]); DrawOptionMenuChoice(gSystemText_Off, 190, 56, styles[1]); } @@ -403,7 +384,7 @@ static void BattleStyle_DrawChoices(u8 selection) styles[selection] = 0x8; DrawOptionMenuChoice(gSystemText_Shift, BATTLESTYLE_SHIFT, 72, styles[0]); - DrawOptionMenuChoice(gSystemText_Set, BATTLESTYLE_SET, 72, styles[1]); + DrawOptionMenuChoice(gSystemText_Set, BATTLESTYLE_SET, 72, styles[1]); } static u8 Sound_ProcessInput(u8 selection) @@ -424,7 +405,7 @@ static void Sound_DrawChoices(u8 selection) styles[1] = 0xF; styles[selection] = 0x8; - DrawOptionMenuChoice(gSystemText_Mono, 120, 88, styles[0]); + DrawOptionMenuChoice(gSystemText_Mono, 120, 88, styles[0]); DrawOptionMenuChoice(gSystemText_Stereo, 172, 88, styles[1]); } @@ -432,19 +413,19 @@ static u8 FrameType_ProcessInput(u8 selection) { if (gMain.newKeys & DPAD_RIGHT) { - if (selection <= 18) + if (selection < 19) selection++; else selection = 0; - MenuLoadTextWindowGraphics_OverrideFrameType(selection); + Menu_LoadStdFrameGraphicsOverrideStyle(selection); } if (gMain.newKeys & DPAD_LEFT) { - if (selection != 0) + if (selection > 0) selection--; else selection = 19; - MenuLoadTextWindowGraphics_OverrideFrameType(selection); + Menu_LoadStdFrameGraphicsOverrideStyle(selection); } return selection; } @@ -454,11 +435,11 @@ static u8 FrameType_ProcessInput(u8 selection) #if ENGLISH static void FrameType_DrawChoices(u8 selection) { - u8 text[8]; + u8 text[6]; u8 n = selection + 1; u16 i; - for (i = 0; gSystemText_Terminator[i] != EOS && i <= 5; i++) + for (i = 0; gSystemText_Terminator[i] != EOS && i < 6; i++) text[i] = gSystemText_Terminator[i]; //Convert number to decimal string @@ -478,8 +459,8 @@ static void FrameType_DrawChoices(u8 selection) } text[i] = EOS; - MenuPrint(gSystemText_Type, 15, 15); - MenuPrint(text, 18, 15); + Menu_PrintText(gSystemText_Type, 15, 15); + Menu_PrintText(text, 18, 15); } #elif GERMAN __attribute__((naked)) @@ -536,7 +517,7 @@ _0808C380:\n\ mov r0, sp\n\ movs r1, 0xF\n\ movs r2, 0xF\n\ - bl MenuPrint\n\ + bl Menu_PrintText\n\ add sp, 0x10\n\ pop {r4-r6}\n\ pop {r0}\n\ @@ -549,14 +530,14 @@ static u8 ButtonMode_ProcessInput(u8 selection) { if (gMain.newKeys & DPAD_RIGHT) { - if (selection <= 1) + if (selection < 2) selection++; else selection = 0; } if (gMain.newKeys & DPAD_LEFT) { - if (selection != 0) + if (selection > 0) selection--; else selection = 2; @@ -574,6 +555,6 @@ static void ButtonMode_DrawChoices(u8 selection) styles[selection] = 0x8; DrawOptionMenuChoice(gSystemText_Normal, 120, 104, styles[0]); - DrawOptionMenuChoice(gSystemText_LR, 166, 104, styles[1]); - DrawOptionMenuChoice(gSystemText_LA, 188, 104, styles[2]); + DrawOptionMenuChoice(gSystemText_LR, 166, 104, styles[1]); + DrawOptionMenuChoice(gSystemText_LA, 188, 104, styles[2]); } diff --git a/src/engine/palette.c b/src/engine/palette.c index 17e9ca178..2a6602503 100644 --- a/src/engine/palette.c +++ b/src/engine/palette.c @@ -827,8 +827,6 @@ void BlendPalettes(u32 selectedPalettes, u8 coeff, u16 color) void BlendPalettesUnfaded(u32 selectedPalettes, u8 coeff, u16 color) { - void *src = gPlttBufferUnfaded; - void *dest = gPlttBufferFaded; - DmaCopy32(3, src, dest, PLTT_SIZE); + DmaCopy32Defvars(3, gPlttBufferUnfaded, gPlttBufferFaded, PLTT_SIZE); BlendPalettes(selectedPalettes, coeff, color); } diff --git a/src/engine/record_mixing.c b/src/engine/record_mixing.c index c8e2e34d5..a2660bf82 100644 --- a/src/engine/record_mixing.c +++ b/src/engine/record_mixing.c @@ -26,7 +26,7 @@ #include "tv.h" #include "ewram.h" -extern struct RecordMixingDayCareMail gUnknown_02038738; +EWRAM_DATA struct RecordMixingDayCareMail gUnknown_02038738 = {0}; extern u16 gSpecialVar_0x8005; u32 gUnknown_03005D2C; @@ -146,8 +146,8 @@ void Task_RecordMixing_Main(u8 taskId) { tState = 4; data[10] = sub_8083664(); - sub_80720B0(); - MenuPrint(gOtherText_MixingComplete, 2, 15); + Menu_ClearWindowText(); + Menu_PrintText(gOtherText_MixingComplete, 2, 15); data[8] = 0; } break; @@ -160,7 +160,7 @@ void Task_RecordMixing_Main(u8 taskId) if (!gTasks[data[10]].isActive) { sub_8055588(); - MenuZeroFillScreen(); + Menu_EraseScreen(); DestroyTask(taskId); EnableBothScriptContexts(); } @@ -176,8 +176,8 @@ void sub_80B95F0(u8 taskId) { case 0: sub_80B9A78(); - MenuDisplayMessageBox(); - MenuPrint(gOtherText_MixingRecordsWithFriend, 2, 15); + Menu_DisplayDialogueFrame(); + Menu_PrintText(gOtherText_MixingRecordsWithFriend, 2, 15); task->data[8] = 0x708; task->tState = 400; ClearLinkCallback_2(); diff --git a/src/engine/reset_rtc_screen.c b/src/engine/reset_rtc_screen.c index 67e146899..8b67f39a5 100644 --- a/src/engine/reset_rtc_screen.c +++ b/src/engine/reset_rtc_screen.c @@ -13,7 +13,7 @@ #include "strings2.h" #include "task.h" #include "text.h" -#include "unknown_task.h" +#include "scanline_effect.h" struct ResetRtcStruct { @@ -147,6 +147,13 @@ const struct SpriteTemplate gSpriteTemplate_83764E8 = const u8 gUnknown_08376500[] = _(" : "); +#if DEBUG +const u8 gUnknown_Debug_0839AE94[] = _("にっすう"); +const u8 gUnknown_Debug_0839AE99[] = _("じかん"); +const u8 gUnknown_Debug_0839AE9D[] = _("ゲームない じかん"); +const u8 gUnknown_Debug_0839AEA7[] = _("RTC じかん"); +#endif + void SpriteCB_ResetRtcCusor0(struct Sprite *sprite) { int state = gTasks[sprite->data[0]].data[2]; @@ -267,7 +274,7 @@ void ResetRtcScreen_FreeCursorPalette(void) void ResetRtcScreen_HideChooseTimeWindow(void) { - MenuZeroFillWindowRect(3, 8, 25, 11); + Menu_EraseWindowRect(3, 8, 25, 11); } void ResetRtcScreen_PrintTime(u8 x, u8 y, u16 days, u8 hours, u8 minutes, u8 seconds) @@ -286,13 +293,13 @@ void ResetRtcScreen_PrintTime(u8 x, u8 y, u16 days, u8 hours, u8 minutes, u8 sec dest = ConvertIntToDecimalStringN(dest, minutes, STR_CONV_MODE_LEADING_ZEROS, 2); dest = StringCopy(dest, gUnknown_08376500); ConvertIntToDecimalStringN(dest, seconds, STR_CONV_MODE_LEADING_ZEROS, 2); - MenuPrint(gStringVar4, x, y); + Menu_PrintText(gStringVar4, x, y); } void ResetRtcScreen_ShowChooseTimeWindow(u16 days, u8 hours, u8 minutes, u8 seconds) { - MenuDrawTextWindow(3, 8, 25, 11); - MenuPrint(gOtherText_OK, 20, 9); + Menu_DrawStdWindowFrame(3, 8, 25, 11); + Menu_PrintText(gOtherText_OK, 20, 9); ResetRtcScreen_PrintTime(4, 9, days, hours, minutes, seconds); } @@ -414,37 +421,19 @@ void Task_ResetRtc_0(u8 taskId) void CB2_InitResetRtcScreen(void) { - u8 *addr; - u32 size; - REG_DISPCNT = 0; SetVBlankCallback(NULL); - DmaClear16(3, PLTT, PLTT_SIZE); - - addr = (u8 *)VRAM; - size = 0x18000; - while (1) - { - DmaFill16(3, 0, addr, 0x1000); - addr += 0x1000; - size -= 0x1000; - if (size <= 0x1000) - { - DmaFill16(3, 0, addr, size); - break; - } - } - + DmaFill16Large(3, 0, (u8 *)VRAM, 0x18000, 0x1000); ResetOamRange(0, 128); LoadOam(); - remove_some_task(); - dp12_8087EA4(); + ScanlineEffect_Stop(); + ScanlineEffect_Clear(); ResetSpriteData(); ResetTasks(); ResetPaletteFade(); - SetUpWindowConfig(&gWindowConfig_81E6CE4); - InitMenuWindow(&gWindowConfig_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6CE4); + InitMenuWindow(&gWindowTemplate_81E6CE4); REG_DISPCNT = 4352; SetVBlankCallback(VBlankCB_ResetRtcScreen); SetMainCallback2(CB2_ResetRtcScreen); @@ -468,8 +457,8 @@ void VBlankCB_ResetRtcScreen(void) void ResetRtcScreen_ShowMessage(const u8 *str) { - MenuDisplayMessageBox(); - MenuPrint(str, 2, 15); + Menu_DisplayDialogueFrame(); + Menu_PrintText(str, 2, 15); } void Task_ShowResetRtcPrompt(u8 taskId) @@ -479,9 +468,9 @@ void Task_ShowResetRtcPrompt(u8 taskId) switch (data[0]) { case 0: - MenuZeroFillScreen(); - MenuDrawTextWindow(0, 0, 20, 10); - MenuPrint(gSystemText_PresentTime, 1, 1); + Menu_EraseScreen(); + Menu_DrawStdWindowFrame(0, 0, 20, 10); + Menu_PrintText(gSystemText_PresentTime, 1, 1); ResetRtcScreen_PrintTime( 1, 3, @@ -489,7 +478,7 @@ void Task_ShowResetRtcPrompt(u8 taskId) gLocalTime.hours, gLocalTime.minutes, gLocalTime.seconds); - MenuPrint(gSystemText_PreviousTime, 1, 5); + Menu_PrintText(gSystemText_PreviousTime, 1, 5); ResetRtcScreen_PrintTime( 1, 7, @@ -543,7 +532,7 @@ void Task_ResetRtcScreen(u8 taskId) case 2: if (gTasks[data[1]].isActive != TRUE) { - MenuZeroFillScreen(); + Menu_EraseScreen(); ResetRtcScreen_ShowMessage(gSystemText_PleaseResetTime); gLocalTime = gSaveBlock2.lastBerryTreeUpdate; data[1] = CreateTask(Task_ResetRtc_0, 80); @@ -576,7 +565,7 @@ void Task_ResetRtcScreen(u8 taskId) } break; case 4: - if (TrySavingData(0) == TRUE) + if (Save_WriteData(0) == SAVE_STATUS_OK) { ResetRtcScreen_ShowMessage(gSystemText_SaveCompleted); PlaySE(SE_PINPON); @@ -605,3 +594,319 @@ void Task_ResetRtcScreen(u8 taskId) } } } + +#if DEBUG + +__attribute__((naked)) +void debug_sub_806F8F8(void) +{ + asm("\ + push {lr}\n\ + ldr r0, ._131 @ CB2_InitResetRtcScreen\n\ + bl SetMainCallback2\n\ + pop {r0}\n\ + bx r0\n\ +._132:\n\ + .align 2, 0\n\ +._131:\n\ + .word CB2_InitResetRtcScreen+1"); +} + +__attribute__((naked)) +void debug_sub_806F908(u8 a) +{ + asm("\ + push {r4, r5, lr}\n\ + lsl r0, r0, #0x18\n\ + lsr r5, r0, #0x18\n\ + lsl r0, r5, #0x2\n\ + add r0, r0, r5\n\ + lsl r0, r0, #0x3\n\ + ldr r2, ._136 @ gTasks\n\ + add r4, r0, r2\n\ + mov r1, #0x0\n\ + ldsh r0, [r4, r1]\n\ + cmp r0, #0\n\ + beq ._133 @cond_branch\n\ + cmp r0, #0x1\n\ + beq ._134 @cond_branch\n\ + b ._141\n\ +._137:\n\ + .align 2, 0\n\ +._136:\n\ + .word gTasks+0x8\n\ +._133:\n\ + ldr r0, ._139 @ Task_ResetRtc_0\n\ + mov r1, #0x50\n\ + bl CreateTask\n\ + lsl r0, r0, #0x18\n\ + lsr r0, r0, #0x18\n\ + strh r0, [r4, #0x2]\n\ + ldrh r0, [r4]\n\ + add r0, r0, #0x1\n\ + strh r0, [r4]\n\ + b ._141\n\ +._140:\n\ + .align 2, 0\n\ +._139:\n\ + .word Task_ResetRtc_0+1\n\ +._134:\n\ + sub r2, r2, #0x8\n\ + mov r0, #0x2\n\ + ldsh r1, [r4, r0]\n\ + lsl r0, r1, #0x2\n\ + add r0, r0, r1\n\ + lsl r0, r0, #0x3\n\ + add r2, r0, r2\n\ + mov r1, #0x8\n\ + ldsh r0, [r2, r1]\n\ + cmp r0, #0\n\ + beq ._141 @cond_branch\n\ + mov r1, #0xa\n\ + ldsh r0, [r2, r1]\n\ + cmp r0, #0x1\n\ + bne ._142 @cond_branch\n\ + ldr r3, ._143 @ gLocalTime\n\ + mov r1, #0x0\n\ + ldsh r0, [r3, r1]\n\ + mov r1, #0x2\n\ + ldsb r1, [r3, r1]\n\ + mov r2, #0x3\n\ + ldsb r2, [r3, r2]\n\ + ldrb r3, [r3, #0x4]\n\ + lsl r3, r3, #0x18\n\ + asr r3, r3, #0x18\n\ + bl RtcCalcLocalTimeOffset\n\ +._142:\n\ + ldrb r0, [r4, #0x2]\n\ + bl DestroyTask\n\ + bl Menu_EraseScreen\n\ + bl ScriptContext2_Disable\n\ + add r0, r5, #0\n\ + bl DestroyTask\n\ +._141:\n\ + pop {r4, r5}\n\ + pop {r0}\n\ + bx r0\n\ +._144:\n\ + .align 2, 0\n\ +._143:\n\ + .word gLocalTime"); +} + +__attribute__((naked)) +void debug_sub_806F99C() +{ + asm("\ + push {lr}\n\ + bl RtcCalcLocalTime\n\ + ldr r0, ._145 @ debug_sub_806F908\n\ + mov r1, #0x50\n\ + bl CreateTask\n\ + bl ScriptContext2_Enable\n\ + pop {r0}\n\ + bx r0\n\ +._146:\n\ + .align 2, 0\n\ +._145:\n\ + .word debug_sub_806F908+1"); +} + +__attribute__((naked)) +void debug_sub_806F9B8() +{ + asm("\ + push {lr}\n\ + ldr r2, ._147 @ gLocalTime\n\ + ldr r0, ._147 + 4 @ gSaveBlock2\n\ + add r0, r0, #0xa0\n\ + ldr r1, [r0, #0x4]\n\ + ldr r0, [r0]\n\ + str r0, [r2]\n\ + str r1, [r2, #0x4]\n\ + ldr r0, ._147 + 8 @ debug_sub_806F908\n\ + mov r1, #0x50\n\ + bl CreateTask\n\ + bl ScriptContext2_Enable\n\ + pop {r0}\n\ + bx r0\n\ +._148:\n\ + .align 2, 0\n\ +._147:\n\ + .word gLocalTime\n\ + .word gSaveBlock2\n\ + .word debug_sub_806F908+1"); +} + +__attribute__((naked)) +void debug_sub_806F9E4() +{ + asm("\ + push {r4, r5, r6, r7, lr}\n\ + mov r7, sl\n\ + mov r6, r9\n\ + mov r5, r8\n\ + push {r5, r6, r7}\n\ + lsl r0, r0, #0x18\n\ + lsr r7, r0, #0x18\n\ + lsl r0, r7, #0x2\n\ + add r0, r0, r7\n\ + lsl r0, r0, #0x3\n\ + ldr r1, ._153 @ gTasks\n\ + add r5, r0, r1\n\ + ldr r6, ._153 + 4 @ gStringVar4\n\ + mov r1, #0x0\n\ + ldsh r0, [r5, r1]\n\ + cmp r0, #0x1\n\ + beq ._149 @cond_branch\n\ + cmp r0, #0x1\n\ + bgt ._150 @cond_branch\n\ + cmp r0, #0\n\ + beq ._151 @cond_branch\n\ + b ._165\n\ +._154:\n\ + .align 2, 0\n\ +._153:\n\ + .word gTasks+0x8\n\ + .word gStringVar4\n\ +._150:\n\ + cmp r0, #0x2\n\ + beq ._155 @cond_branch\n\ + b ._165\n\ +._149:\n\ + mov r0, #0x0\n\ + mov r1, #0x9\n\ + mov r2, #0x1d\n\ + mov r3, #0x13\n\ + bl Menu_DrawStdWindowFrame\n\ + add r4, r6, #0\n\ + add r4, r4, #0x50\n\ + bl RtcGetErrorStatus\n\ + add r1, r0, #0\n\ + lsl r1, r1, #0x10\n\ + lsr r1, r1, #0x10\n\ + add r0, r4, #0\n\ + mov r2, #0x2\n\ + mov r3, #0x4\n\ + bl ConvertIntToHexStringN\n\ + add r0, r4, #0\n\ + mov r1, #0x2\n\ + mov r2, #0xa\n\ + bl Menu_PrintText\n\ + ldr r0, ._158 @ gUnknown_Debug_0839AE94\n\ + mov r1, #0xc\n\ + mov r2, #0xc\n\ + bl Menu_PrintText\n\ + ldr r0, ._158 + 4 @ gUnknown_Debug_0839AE99\n\ + mov r1, #0x14\n\ + mov r2, #0xc\n\ + bl Menu_PrintText\n\ + ldr r0, ._158 + 8 @ gUnknown_Debug_0839AEA7\n\ + mov r1, #0x1\n\ + mov r2, #0xe\n\ + bl Menu_PrintText\n\ + ldr r0, ._158 + 12 @ gUnknown_Debug_0839AE9D\n\ + mov r1, #0x1\n\ + mov r2, #0x10\n\ + bl Menu_PrintText\n\ +._151:\n\ + ldrh r0, [r5]\n\ + add r0, r0, #0x1\n\ + strh r0, [r5]\n\ + b ._165\n\ +._159:\n\ + .align 2, 0\n\ +._158:\n\ + .word gUnknown_Debug_0839AE94\n\ + .word gUnknown_Debug_0839AE99\n\ + .word gUnknown_Debug_0839AEA7\n\ + .word gUnknown_Debug_0839AE9D\n\ +._155:\n\ + bl RtcCalcLocalTime\n\ + add r0, r6, #0\n\ + bl FormatHexRtcTime\n\ + mov r2, #0x14\n\ + add r2, r2, r6\n\ + mov r9, r2\n\ + mov r0, r9\n\ + bl debug_sub_8009894\n\ + mov r0, #0x28\n\ + add r0, r0, r6\n\ + mov sl, r0\n\ + ldr r4, ._163 @ gLocalTime\n\ + mov r1, #0x2\n\ + ldsb r1, [r4, r1]\n\ + mov r2, #0x3\n\ + ldsb r2, [r4, r2]\n\ + mov r3, #0x4\n\ + ldsb r3, [r4, r3]\n\ + bl FormatDecimalTime\n\ + mov r1, #0x3c\n\ + add r1, r1, r6\n\ + mov r8, r1\n\ + mov r2, #0x0\n\ + ldsh r1, [r4, r2]\n\ + mov r0, r8\n\ + mov r2, #0x1\n\ + mov r3, #0x4\n\ + bl ConvertIntToDecimalStringN\n\ + ldr r0, ._163 + 4 @ gSaveBlock2\n\ + ldrb r0, [r0, #0x12]\n\ + cmp r0, #0\n\ + bne ._160 @cond_branch\n\ + add r0, r6, #0\n\ + mov r1, #0x14\n\ + mov r2, #0xe\n\ + bl Menu_PrintText\n\ + mov r0, r9\n\ + mov r1, #0xc\n\ + mov r2, #0xe\n\ + bl Menu_PrintText\n\ + mov r0, sl\n\ + mov r1, #0x14\n\ + mov r2, #0x10\n\ + bl Menu_PrintText\n\ + mov r0, r8\n\ + mov r1, #0xc\n\ + mov r2, #0x10\n\ + bl Menu_PrintText\n\ +._160:\n\ + ldrh r1, [r5, #0x2]\n\ + mov r2, #0x2\n\ + ldsh r0, [r5, r2]\n\ + cmp r0, #0x13\n\ + ble ._161 @cond_branch\n\ + add r0, r1, #1\n\ + strh r0, [r5, #0x2]\n\ + b ._165\n\ +._164:\n\ + .align 2, 0\n\ +._163:\n\ + .word gLocalTime\n\ + .word gSaveBlock2\n\ +._161:\n\ + ldr r0, ._166 @ gMain\n\ + ldrh r1, [r0, #0x2e]\n\ + mov r0, #0x1\n\ + and r0, r0, r1\n\ + cmp r0, #0\n\ + beq ._165 @cond_branch\n\ + bl Menu_EraseScreen\n\ + add r0, r7, #0\n\ + bl DestroyTask\n\ + bl ScriptContext2_Disable\n\ +._165:\n\ + pop {r3, r4, r5}\n\ + mov r8, r3\n\ + mov r9, r4\n\ + mov sl, r5\n\ + pop {r4, r5, r6, r7}\n\ + pop {r0}\n\ + bx r0\n\ +._167:\n\ + .align 2, 0\n\ +._166:\n\ + .word gMain"); +} + +#endif
\ No newline at end of file diff --git a/src/engine/rtc.c b/src/engine/rtc.c index d73f943d2..5462298ed 100644 --- a/src/engine/rtc.c +++ b/src/engine/rtc.c @@ -268,6 +268,28 @@ void FormatHexDate(u8 *dest, s32 year, s32 month, s32 day) *dest = EOS; } +#if DEBUG + +void debug_sub_800987C(u8 *dest) +{ + FormatHexDate(dest, sRtc.year, sRtc.month, sRtc.day); +} + +void debug_sub_8009894(u8 *dest) +{ + u16 dayCount; + + dayCount = RtcGetDayCount(&sRtc); + ConvertIntToDecimalStringN(dest, dayCount, STR_CONV_MODE_RIGHT_ALIGN, 4); +} + +void debug_sub_80098B8(u8 *dest) +{ + ConvertIntToHexStringN(dest, sRtc.status, STR_CONV_MODE_LEADING_ZEROS, 2); +} + +#endif + void RtcCalcTimeDifference(struct SiiRtcInfo *rtc, struct Time *result, struct Time *t) { u16 days = RtcGetDayCount(rtc); @@ -347,3 +369,10 @@ u32 RtcGetMinuteCount() RtcGetInfo(&sRtc); return (24 * 60) * RtcGetDayCount(&sRtc) + 60 * sRtc.hour + sRtc.minute; } + +#if DEBUG +void debug_sub_8009A60() +{ + RtcGetRawInfo(&sRtc); +} +#endif diff --git a/src/engine/save.c b/src/engine/save.c index d5de2e408..0b7a3dd79 100644 --- a/src/engine/save.c +++ b/src/engine/save.c @@ -4,55 +4,132 @@ #include "save.h" #include "load_save.h" #include "overworld.h" +#include "pokemon.h" #include "save_failed_screen.h" #include "ewram.h" -#define GETVALIDSTATUSBITFIELD ((1 << ARRAY_COUNT(gSaveSectionLocations)) - 1) -#define GETCHUNKSIZE(chunk, n) ((sizeof(chunk) - (0xF80 * (n - 1))) >= 0xF80 ? 0xF80 : (sizeof(chunk) - (0xF80 * (n - 1)))) -#define GETBLOCKOFFSET(n) (0xF80 * (n - 1)) -#define TOTALNUMSECTORS ((ARRAY_COUNT(gSaveSectionLocations) * 2) + (ARRAY_COUNT(gHallOfFameSaveSectionLocations) * 2)) // there are 2 slots, so double each array count and get the sum. +#define FILE_SIGNATURE 0x08012025 // signature value to determine if a sector is in use -u16 gLastWrittenSector; -u32 gLastSaveCounter; +//#define TOTAL_FLASH_SECTORS ((ARRAY_COUNT(sSaveBlockChunks) * 2) + (ARRAY_COUNT(sHallOfFameChunks) * 2)) // there are 2 slots, so double each array count and get the sum. +#define TOTAL_FLASH_SECTORS 32 + +struct SaveBlockChunk +{ + u8 *data; + u16 size; +}; + +struct SaveSector +{ + u8 data[0xFF4]; + u16 id; + u16 checksum; + u32 signature; + u32 counter; +}; // size is 0x1000 + +// headless save section? +struct UnkSaveSection +{ + u8 data[0xFF4]; + u32 signature; +}; // size is 0xFF8 + +static u8 WriteSingleChunk(u16, const struct SaveBlockChunk *); +static u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size); +static u8 TryWriteSector(u8, u8 *); +static u32 RestoreSaveBackupVarsAndIncrement(const struct SaveBlockChunk *location); +static u32 RestoreSaveBackupVars(const struct SaveBlockChunk *location); +static u8 sub_812550C(u16 a1, const struct SaveBlockChunk *location); +static u8 sub_812556C(u16 a1, const struct SaveBlockChunk *location); +static u8 sub_81255B8(u16, const struct SaveBlockChunk *location); +static u8 WriteSomeFlashByteToPrevSector(u16 a1, const struct SaveBlockChunk *location); +static u8 WriteSomeFlashByte0x25ToPrevSector(u16 a1, const struct SaveBlockChunk *location); +static u8 sub_812587C(u16 a1, const struct SaveBlockChunk *location); +static u8 sub_81258BC(u16, const struct SaveBlockChunk *location); +static u8 GetSaveValidStatus(const struct SaveBlockChunk *location); +static u8 ReadSomeUnknownSectorAndVerify(u8 a1, u8 *data, u16 size); +static u8 DoReadFlashWholeSection(u8, struct SaveSector *); +static u16 CalculateChecksum(void *, u16); +bool8 unref_sub_8125F4C(struct UnkSaveSection *a1); +u8 unref_sub_8125FA0(void); +u8 unref_sub_8125FF0(u8 *data, u16 size); +u8 unref_sub_8126068(u8 sector, u8 *data, u32 size); +u8 unref_sub_8126080(u8 sector, u8 *data); + +// Sector num to begin writing save data. Sectors are rotated each time the game is saved. (possibly to avoid wear on flash memory?) +u16 gFirstSaveSector; +u32 gPrevSaveCounter; u16 gLastKnownGoodSector; u32 gDamagedSaveSectors; u32 gSaveCounter; -struct SaveSection *gFastSaveSection; // the pointer is in fast IWRAM but may sometimes point to the slower EWRAM. +struct SaveSector *gFastSaveSection; // the pointer is in fast IWRAM but may sometimes point to the slower EWRAM. u16 gUnknown_03005EB4; u16 gSaveFileStatus; u32 gGameContinueCallback; -extern struct PokemonStorage gPokemonStorage; - static EWRAM_DATA u32 gLastSaveSectorStatus = 0; // used but in an unferenced function, so unused -const struct SaveSectionLocation gSaveSectionLocations[] = -{ - {((u8 *) &gSaveBlock2) + GETBLOCKOFFSET(1), GETCHUNKSIZE(gSaveBlock2, 1)}, - {((u8 *) &gSaveBlock1) + GETBLOCKOFFSET(1), GETCHUNKSIZE(gSaveBlock1, 1)}, - {((u8 *) &gSaveBlock1) + GETBLOCKOFFSET(2), GETCHUNKSIZE(gSaveBlock1, 2)}, - {((u8 *) &gSaveBlock1) + GETBLOCKOFFSET(3), GETCHUNKSIZE(gSaveBlock1, 3)}, - {((u8 *) &gSaveBlock1) + GETBLOCKOFFSET(4), GETCHUNKSIZE(gSaveBlock1, 4)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(1), GETCHUNKSIZE(gPokemonStorage, 1)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(2), GETCHUNKSIZE(gPokemonStorage, 2)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(3), GETCHUNKSIZE(gPokemonStorage, 3)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(4), GETCHUNKSIZE(gPokemonStorage, 4)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(5), GETCHUNKSIZE(gPokemonStorage, 5)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(6), GETCHUNKSIZE(gPokemonStorage, 6)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(7), GETCHUNKSIZE(gPokemonStorage, 7)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(8), GETCHUNKSIZE(gPokemonStorage, 8)}, - {((u8 *) &gPokemonStorage) + GETBLOCKOFFSET(9), GETCHUNKSIZE(gPokemonStorage, 9)} +// Each 4 KiB flash sector contains 3968 bytes of actual data followed by a 128 byte footer +#define SECTOR_DATA_SIZE 3968 +#define SECTOR_FOOTER_SIZE 128 + +/* + * Sector Layout: + * + * Sectors 0 - 13: Save Slot 1 + * Sectors 14 - 27: Save Slot 2 + * Sectors 28 - 29: Hall of Fame + * Sectors 30 - 31: e-Reader battle tower data, maybe? + * + * There are two save slots for saving the player's game data. We alternate between + * them each time the game is saved, so that if the current save slot is corrupt, + * we can load the previous one. We also rotate the sectors in each save slot + * so that the same data is not always being written to the same sector. This + * might be done to reduce wear on the flash memory, but I'm not sure, since all + * 14 sectors get written anyway. + */ + +#define HALL_OF_FAME_SECTOR 28 + +#define NUM_SECTORS_PER_SAVE_SLOT 14 // Number of sectors occupied by a save slot +#define NUM_HALL_OF_FAME_SECTORS 2 + +// Divide save blocks into individual chunks to be written to flash sectors + +#define SAVEBLOCK_CHUNK(structure, chunkNum) \ +{ \ + (u8 *)&structure + chunkNum * SECTOR_DATA_SIZE, \ + min(sizeof(structure) - chunkNum * SECTOR_DATA_SIZE, SECTOR_DATA_SIZE) \ +} \ + +static const struct SaveBlockChunk sSaveBlockChunks[] = +{ + SAVEBLOCK_CHUNK(gSaveBlock2, 0), + + SAVEBLOCK_CHUNK(gSaveBlock1, 0), + SAVEBLOCK_CHUNK(gSaveBlock1, 1), + SAVEBLOCK_CHUNK(gSaveBlock1, 2), + SAVEBLOCK_CHUNK(gSaveBlock1, 3), + + SAVEBLOCK_CHUNK(gPokemonStorage, 0), + SAVEBLOCK_CHUNK(gPokemonStorage, 1), + SAVEBLOCK_CHUNK(gPokemonStorage, 2), + SAVEBLOCK_CHUNK(gPokemonStorage, 3), + SAVEBLOCK_CHUNK(gPokemonStorage, 4), + SAVEBLOCK_CHUNK(gPokemonStorage, 5), + SAVEBLOCK_CHUNK(gPokemonStorage, 6), + SAVEBLOCK_CHUNK(gPokemonStorage, 7), + SAVEBLOCK_CHUNK(gPokemonStorage, 8), }; -const struct SaveSectionLocation gHallOfFameSaveSectionLocations[] = +static const struct SaveBlockChunk sHallOfFameChunks[] = { - {((u8 *) eHallOfFame) + GETBLOCKOFFSET(1), GETCHUNKSIZE(struct HallOfFame, 1)}, // eHallOfFame is not a proper sym, so the struct must be used. - {((u8 *) eHallOfFame) + GETBLOCKOFFSET(2), GETCHUNKSIZE(struct HallOfFame, 2)} + SAVEBLOCK_CHUNK(*eHallOfFame, 0), + SAVEBLOCK_CHUNK(*eHallOfFame, 1), }; -const u8 gFlashSectors[] = { 0x1E, 0x1F }; - -void ClearSaveData(void) +void Save_EraseAllData(void) { u16 i; @@ -60,27 +137,34 @@ void ClearSaveData(void) EraseFlashSector(i); } -void ResetSaveCounters(void) +void Save_ResetSaveCounters(void) { gSaveCounter = 0; - gLastWrittenSector = 0; + gFirstSaveSector = 0; gDamagedSaveSectors = 0; } -bool32 SetDamagedSectorBits(u8 op, u8 bit) +enum +{ + SECTOR_DAMAGED, + SECTOR_OK, + SECTOR_CHECK, // unused +}; + +static bool32 SetSectorDamagedStatus(u8 op, u8 sectorNum) { bool32 retVal = FALSE; switch (op) { - case ENABLE: - gDamagedSaveSectors |= (1 << bit); + case SECTOR_DAMAGED: + gDamagedSaveSectors |= (1 << sectorNum); break; - case DISABLE: - gDamagedSaveSectors &= ~(1 << bit); + case SECTOR_OK: + gDamagedSaveSectors &= ~(1 << sectorNum); break; - case CHECK: // unused - if (gDamagedSaveSectors & (1 << bit)) + case SECTOR_CHECK: // unused + if (gDamagedSaveSectors & (1 << sectorNum)) retVal = TRUE; break; } @@ -88,163 +172,166 @@ bool32 SetDamagedSectorBits(u8 op, u8 bit) return retVal; } -u8 save_write_to_flash(u16 a1, const struct SaveSectionLocation *location) +// If chunkId is 0xFFFF, this function will write all of the chunks pointed to by 'chunks'. +// Otherwise, it will write a single chunk with the given 'chunkId'. +static u8 WriteSaveBlockChunks(u16 chunkId, const struct SaveBlockChunk *chunks) { u32 retVal; u16 i; gFastSaveSection = eSaveSection; - if (a1 != 0xFFFF) // for link + if (chunkId != 0xFFFF) // write single chunk { - retVal = HandleWriteSector(a1, location); + retVal = WriteSingleChunk(chunkId, chunks); } - else + else // write all chunks { - gLastKnownGoodSector = gLastWrittenSector; // backup the current written sector before attempting to write. - gLastSaveCounter = gSaveCounter; - gLastWrittenSector++; - gLastWrittenSector = gLastWrittenSector % ARRAY_COUNT(gSaveSectionLocations); + gLastKnownGoodSector = gFirstSaveSector; + gPrevSaveCounter = gSaveCounter; + gFirstSaveSector++; + gFirstSaveSector %= NUM_SECTORS_PER_SAVE_SLOT; gSaveCounter++; - retVal = 1; + retVal = SAVE_STATUS_OK; - for (i = 0; i < ARRAY_COUNT(gSaveSectionLocations); i++) - HandleWriteSector(i, location); + for (i = 0; i < NUM_SECTORS_PER_SAVE_SLOT; i++) + WriteSingleChunk(i, chunks); + // Check for any bad sectors if (gDamagedSaveSectors != 0) // skip the damaged sector. { - retVal = 0xFF; - gLastWrittenSector = gLastKnownGoodSector; - gSaveCounter = gLastSaveCounter; + retVal = SAVE_STATUS_ERROR; + gFirstSaveSector = gLastKnownGoodSector; + gSaveCounter = gPrevSaveCounter; } } return retVal; } -u8 HandleWriteSector(u16 a1, const struct SaveSectionLocation *location) +static u8 WriteSingleChunk(u16 chunkId, const struct SaveBlockChunk *chunks) { u16 i; - u16 sector; - u8 *data; - u16 size; + u16 sectorNum; + u8 *chunkData; + u16 chunkSize; - sector = a1 + gLastWrittenSector; - sector %= ARRAY_COUNT(gSaveSectionLocations); - sector += ARRAY_COUNT(gSaveSectionLocations) * (gSaveCounter % 2); + // select sector number + sectorNum = chunkId + gFirstSaveSector; + sectorNum %= NUM_SECTORS_PER_SAVE_SLOT; + // select save slot + sectorNum += NUM_SECTORS_PER_SAVE_SLOT * (gSaveCounter % 2); - data = location[a1].data; - size = location[a1].size; + chunkData = chunks[chunkId].data; + chunkSize = chunks[chunkId].size; // clear save section. - for (i = 0; i < sizeof(struct SaveSection); i++) - ((char *)gFastSaveSection)[i] = 0; + for (i = 0; i < sizeof(struct SaveSector); i++) + ((u8 *)gFastSaveSection)[i] = 0; - gFastSaveSection->id = a1; - gFastSaveSection->security = UNKNOWN_CHECK_VALUE; + gFastSaveSection->id = chunkId; + gFastSaveSection->signature = FILE_SIGNATURE; gFastSaveSection->counter = gSaveCounter; + for (i = 0; i < chunkSize; i++) + gFastSaveSection->data[i] = chunkData[i]; + gFastSaveSection->checksum = CalculateChecksum(chunkData, chunkSize); - for (i = 0; i < size; i++) - gFastSaveSection->data[i] = data[i]; - - gFastSaveSection->checksum = CalculateChecksum(data, size); - return TryWriteSector(sector, gFastSaveSection->data); + return TryWriteSector(sectorNum, gFastSaveSection->data); } -u8 HandleWriteSectorNBytes(u8 sector, u8 *data, u16 size) +static u8 HandleWriteSectorNBytes(u8 sectorNum, u8 *data, u16 size) { u16 i; - struct SaveSection *section = eSaveSection; + struct SaveSector *section = eSaveSection; - for (i = 0; i < sizeof(struct SaveSection); i++) + for (i = 0; i < sizeof(struct SaveSector); i++) ((char *)section)[i] = 0; - section->security = UNKNOWN_CHECK_VALUE; - + section->signature = FILE_SIGNATURE; for (i = 0; i < size; i++) section->data[i] = data[i]; - section->id = CalculateChecksum(data, size); // though this appears to be incorrect, it might be some sector checksum instead of a whole save checksum and only appears to be relevent to HOF data, if used. - return TryWriteSector(sector, section->data); + + return TryWriteSector(sectorNum, section->data); } -u8 TryWriteSector(u8 sector, u8 *data) +static u8 TryWriteSector(u8 sectorNum, u8 *data) { - if (ProgramFlashSectorAndVerify(sector, data) != 0) // is damaged? + if (ProgramFlashSectorAndVerify(sectorNum, data) != 0) // is damaged? { - SetDamagedSectorBits(ENABLE, sector); // set damaged sector bits. - return 0xFF; + SetSectorDamagedStatus(SECTOR_DAMAGED, sectorNum); // set damaged sector bits. + return SAVE_STATUS_ERROR; } else { - SetDamagedSectorBits(DISABLE, sector); // unset damaged sector bits. it's safe now. - return 1; + SetSectorDamagedStatus(SECTOR_OK, sectorNum); // unset damaged sector bits. it's safe now. + return SAVE_STATUS_OK; } } -u32 RestoreSaveBackupVarsAndIncrement(const struct SaveSectionLocation *location) // location is unused +static u32 RestoreSaveBackupVarsAndIncrement(const struct SaveBlockChunk *chunk) // chunk is unused { gFastSaveSection = eSaveSection; - gLastKnownGoodSector = gLastWrittenSector; - gLastSaveCounter = gSaveCounter; - gLastWrittenSector++; - gLastWrittenSector = gLastWrittenSector % ARRAY_COUNT(gSaveSectionLocations); + gLastKnownGoodSector = gFirstSaveSector; + gPrevSaveCounter = gSaveCounter; + gFirstSaveSector++; + gFirstSaveSector %= NUM_SECTORS_PER_SAVE_SLOT; gSaveCounter++; gUnknown_03005EB4 = 0; gDamagedSaveSectors = 0; return 0; } -u32 RestoreSaveBackupVars(const struct SaveSectionLocation *location) // only ever called once, and gSaveBlock2 is passed to this function. location is unused +static u32 RestoreSaveBackupVars(const struct SaveBlockChunk *chunk) // chunk is unused { gFastSaveSection = eSaveSection; - gLastKnownGoodSector = gLastWrittenSector; - gLastSaveCounter = gSaveCounter; + gLastKnownGoodSector = gFirstSaveSector; + gPrevSaveCounter = gSaveCounter; gUnknown_03005EB4 = 0; gDamagedSaveSectors = 0; return 0; } -u8 sub_812550C(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_812550C(u16 a1, const struct SaveBlockChunk *chunk) { u8 retVal; if (gUnknown_03005EB4 < a1 - 1) { - retVal = 1; - HandleWriteSector(gUnknown_03005EB4, location); + retVal = SAVE_STATUS_OK; + WriteSingleChunk(gUnknown_03005EB4, chunk); gUnknown_03005EB4++; if (gDamagedSaveSectors) { - retVal = 0xFF; - gLastWrittenSector = gLastKnownGoodSector; - gSaveCounter = gLastSaveCounter; + retVal = SAVE_STATUS_ERROR; + gFirstSaveSector = gLastKnownGoodSector; + gSaveCounter = gPrevSaveCounter; } } else { - retVal = 0xFF; + retVal = SAVE_STATUS_ERROR; } return retVal; } -u8 sub_812556C(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_812556C(u16 a1, const struct SaveBlockChunk *chunk) { - u8 retVal = 1; + u8 retVal = SAVE_STATUS_OK; - sub_81255B8(a1 - 1, location); + sub_81255B8(a1 - 1, chunk); if (gDamagedSaveSectors) { - retVal = 0xFF; - gLastWrittenSector = gLastKnownGoodSector; - gSaveCounter = gLastSaveCounter; + retVal = SAVE_STATUS_ERROR; + gFirstSaveSector = gLastKnownGoodSector; + gSaveCounter = gPrevSaveCounter; } return retVal; } -u8 sub_81255B8(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_81255B8(u16 chunkId, const struct SaveBlockChunk *chunks) { u16 i; u16 sector; @@ -252,19 +339,21 @@ u8 sub_81255B8(u16 a1, const struct SaveSectionLocation *location) u16 size; u8 status; - sector = a1 + gLastWrittenSector; - sector %= ARRAY_COUNT(gSaveSectionLocations); - sector += ARRAY_COUNT(gSaveSectionLocations) * (gSaveCounter % 2); + // select sector number + sector = chunkId + gFirstSaveSector; + sector %= NUM_SECTORS_PER_SAVE_SLOT; + // select save slot + sector += NUM_SECTORS_PER_SAVE_SLOT * (gSaveCounter % 2); - data = location[a1].data; - size = location[a1].size; + data = chunks[chunkId].data; + size = chunks[chunkId].size; // clear temp save section. - for (i = 0; i < sizeof(struct SaveSection); i++) + for (i = 0; i < sizeof(struct SaveSector); i++) ((char *)gFastSaveSection)[i] = 0; - gFastSaveSection->id = a1; - gFastSaveSection->security = UNKNOWN_CHECK_VALUE; + gFastSaveSection->id = chunkId; + gFastSaveSection->signature = FILE_SIGNATURE; gFastSaveSection->counter = gSaveCounter; // set temp section's data. @@ -276,275 +365,275 @@ u8 sub_81255B8(u16 a1, const struct SaveSectionLocation *location) EraseFlashSector(sector); - status = 1; + status = SAVE_STATUS_OK; for (i = 0; i < sizeof(struct UnkSaveSection); i++) { if (ProgramFlashByte(sector, i, gFastSaveSection->data[i])) { - status = 0xFF; + status = SAVE_STATUS_ERROR; break; } } - if (status == 0xFF) + if (status == SAVE_STATUS_ERROR) { - SetDamagedSectorBits(ENABLE, sector); - return 0xFF; + SetSectorDamagedStatus(SECTOR_DAMAGED, sector); + return SAVE_STATUS_ERROR; } else { - status = 1; + status = SAVE_STATUS_OK; for (i = 0; i < 7; i++) { if (ProgramFlashByte(sector, 0xFF9 + i, ((u8 *)gFastSaveSection)[0xFF9 + i])) { - status = 0xFF; + status = SAVE_STATUS_ERROR; break; } } - if (status == 0xFF) + if (status == SAVE_STATUS_ERROR) { - SetDamagedSectorBits(ENABLE, sector); - return 0xFF; + SetSectorDamagedStatus(SECTOR_DAMAGED, sector); + return SAVE_STATUS_ERROR; } else { - SetDamagedSectorBits(DISABLE, sector); - return 1; + SetSectorDamagedStatus(SECTOR_OK, sector); + return SAVE_STATUS_OK; } } } -u8 sub_8125758(u16 a1, const struct SaveSectionLocation *location) +static u8 WriteSomeFlashByteToPrevSector(u16 a1, const struct SaveBlockChunk *chunk) { u16 sector; - sector = a1 + gLastWrittenSector - 1; - sector %= ARRAY_COUNT(gSaveSectionLocations); - sector += ARRAY_COUNT(gSaveSectionLocations) * (gSaveCounter % 2); + // select sector number + sector = a1 + gFirstSaveSector - 1; + sector %= NUM_SECTORS_PER_SAVE_SLOT; + // select save slot + sector += NUM_SECTORS_PER_SAVE_SLOT * (gSaveCounter % 2); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), ((u8 *)gFastSaveSection)[sizeof(struct UnkSaveSection)])) { // sector is damaged, so enable the bit in gDamagedSaveSectors and restore the last written sector and save counter. - SetDamagedSectorBits(ENABLE, sector); - gLastWrittenSector = gLastKnownGoodSector; - gSaveCounter = gLastSaveCounter; - return 0xFF; + SetSectorDamagedStatus(SECTOR_DAMAGED, sector); + gFirstSaveSector = gLastKnownGoodSector; + gSaveCounter = gPrevSaveCounter; + return SAVE_STATUS_ERROR; } else { - SetDamagedSectorBits(DISABLE, sector); - return 1; + SetSectorDamagedStatus(SECTOR_OK, sector); + return SAVE_STATUS_OK; } } -u8 sub_81257F0(u16 a1, const struct SaveSectionLocation *location) +static u8 WriteSomeFlashByte0x25ToPrevSector(u16 a1, const struct SaveBlockChunk *chunk) { u16 sector; - sector = a1 + gLastWrittenSector - 1; - sector %= ARRAY_COUNT(gSaveSectionLocations); - sector += ARRAY_COUNT(gSaveSectionLocations) * (gSaveCounter % 2); + sector = a1 + gFirstSaveSector - 1; + sector %= NUM_SECTORS_PER_SAVE_SLOT; + sector += NUM_SECTORS_PER_SAVE_SLOT * (gSaveCounter % 2); if (ProgramFlashByte(sector, sizeof(struct UnkSaveSection), 0x25)) { // sector is damaged, so enable the bit in gDamagedSaveSectors and restore the last written sector and save counter. - SetDamagedSectorBits(ENABLE, sector); - gLastWrittenSector = gLastKnownGoodSector; - gSaveCounter = gLastSaveCounter; - return 0xFF; + SetSectorDamagedStatus(SECTOR_DAMAGED, sector); + gFirstSaveSector = gLastKnownGoodSector; + gSaveCounter = gPrevSaveCounter; + return SAVE_STATUS_ERROR; } else { - SetDamagedSectorBits(DISABLE, sector); - return 1; + SetSectorDamagedStatus(SECTOR_OK, sector); + return SAVE_STATUS_OK; } } -u8 sub_812587C(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_812587C(u16 a1, const struct SaveBlockChunk *chunk) { u8 retVal; gFastSaveSection = eSaveSection; if (a1 != 0xFFFF) { - retVal = 0xFF; + retVal = SAVE_STATUS_ERROR; } else { - retVal = GetSaveValidStatus(location); - sub_81258BC(0xFFFF, location); + retVal = GetSaveValidStatus(chunk); + sub_81258BC(0xFFFF, chunk); } return retVal; } -u8 sub_81258BC(u16 a1, const struct SaveSectionLocation *location) +static u8 sub_81258BC(u16 a1, const struct SaveBlockChunk *chunks) { u16 i; u16 checksum; - u16 v3 = ARRAY_COUNT(gSaveSectionLocations) * (gSaveCounter % 2); + u16 sector = NUM_SECTORS_PER_SAVE_SLOT * (gSaveCounter % 2); u16 id; - for (i = 0; i < ARRAY_COUNT(gSaveSectionLocations); i++) + for (i = 0; i < NUM_SECTORS_PER_SAVE_SLOT; i++) { - DoReadFlashWholeSection(i + v3, gFastSaveSection); + DoReadFlashWholeSection(i + sector, gFastSaveSection); id = gFastSaveSection->id; if (id == 0) - gLastWrittenSector = i; - checksum = CalculateChecksum(gFastSaveSection->data, location[id].size); - if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE + gFirstSaveSector = i; + checksum = CalculateChecksum(gFastSaveSection->data, chunks[id].size); + if (gFastSaveSection->signature == FILE_SIGNATURE && gFastSaveSection->checksum == checksum) { u16 j; - for (j = 0; j < location[id].size; j++) - location[id].data[j] = gFastSaveSection->data[j]; + for (j = 0; j < chunks[id].size; j++) + chunks[id].data[j] = gFastSaveSection->data[j]; } } return 1; } -u8 GetSaveValidStatus(const struct SaveSectionLocation *location) +static u8 GetSaveValidStatus(const struct SaveBlockChunk *chunks) { - u16 i; + u16 sector; + bool8 signatureValid; u16 checksum; - u32 saveSlot1Counter = 0; - u32 saveSlot2Counter = 0; - u32 slotCheckField = 0; - bool8 securityPassed = FALSE; - u8 saveSlot1Status; - u8 saveSlot2Status; + u32 slot1saveCounter = 0; + u32 slot2saveCounter = 0; + u8 slot1Status; + u8 slot2Status; + u32 validSectors; + const u32 ALL_SECTORS = (1 << NUM_SECTORS_PER_SAVE_SLOT) - 1; // bitmask of all saveblock sectors // check save slot 1. - for (i = 0; i < ARRAY_COUNT(gSaveSectionLocations); i++) + validSectors = 0; + signatureValid = FALSE; + for (sector = 0; sector < NUM_SECTORS_PER_SAVE_SLOT; sector++) { - DoReadFlashWholeSection(i, gFastSaveSection); - if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) + DoReadFlashWholeSection(sector, gFastSaveSection); + if (gFastSaveSection->signature == FILE_SIGNATURE) { - securityPassed = TRUE; - checksum = CalculateChecksum(gFastSaveSection->data, location[gFastSaveSection->id].size); + signatureValid = TRUE; + checksum = CalculateChecksum(gFastSaveSection->data, chunks[gFastSaveSection->id].size); if (gFastSaveSection->checksum == checksum) { - saveSlot1Counter = gFastSaveSection->counter; - slotCheckField |= 1 << gFastSaveSection->id; + slot1saveCounter = gFastSaveSection->counter; + validSectors |= 1 << gFastSaveSection->id; } } } - if (securityPassed) + if (signatureValid) { - if (slotCheckField == GETVALIDSTATUSBITFIELD) - saveSlot1Status = 1; + if (validSectors == ALL_SECTORS) + slot1Status = SAVE_STATUS_OK; else - saveSlot1Status = 255; + slot1Status = SAVE_STATUS_ERROR; } else { - saveSlot1Status = 0; + slot1Status = SAVE_STATUS_EMPTY; } - slotCheckField = 0; - securityPassed = FALSE; - // check save slot 2. - for (i = 0; i < ARRAY_COUNT(gSaveSectionLocations); i++) + validSectors = 0; + signatureValid = FALSE; + for (sector = 0; sector < NUM_SECTORS_PER_SAVE_SLOT; sector++) { - DoReadFlashWholeSection(i + ARRAY_COUNT(gSaveSectionLocations), gFastSaveSection); - if (gFastSaveSection->security == UNKNOWN_CHECK_VALUE) + DoReadFlashWholeSection(NUM_SECTORS_PER_SAVE_SLOT + sector, gFastSaveSection); + if (gFastSaveSection->signature == FILE_SIGNATURE) { - securityPassed = TRUE; - checksum = CalculateChecksum(gFastSaveSection->data, location[gFastSaveSection->id].size); + signatureValid = TRUE; + checksum = CalculateChecksum(gFastSaveSection->data, chunks[gFastSaveSection->id].size); if (gFastSaveSection->checksum == checksum) { - saveSlot2Counter = gFastSaveSection->counter; - slotCheckField |= 1 << gFastSaveSection->id; + slot2saveCounter = gFastSaveSection->counter; + validSectors |= 1 << gFastSaveSection->id; } } } - if (securityPassed) + if (signatureValid) { - if (slotCheckField == GETVALIDSTATUSBITFIELD) - saveSlot2Status = 1; + if (validSectors == ALL_SECTORS) + slot2Status = SAVE_STATUS_OK; else - saveSlot2Status = 255; + slot2Status = SAVE_STATUS_ERROR; } else { - saveSlot2Status = 0; + slot2Status = SAVE_STATUS_EMPTY; } - if (saveSlot1Status == 1 && saveSlot2Status == 1) + if (slot1Status == SAVE_STATUS_OK && slot2Status == SAVE_STATUS_OK) { - if ((saveSlot1Counter == -1 && saveSlot2Counter == 0) || (saveSlot1Counter == 0 && saveSlot2Counter == -1)) + // Choose counter of the most recent save file + if ((slot1saveCounter == -1 && slot2saveCounter == 0) || (slot1saveCounter == 0 && slot2saveCounter == -1)) { - if ((unsigned)(saveSlot1Counter + 1) < (unsigned)(saveSlot2Counter + 1)) - { - gSaveCounter = saveSlot2Counter; - } + if ((unsigned)(slot1saveCounter + 1) < (unsigned)(slot2saveCounter + 1)) + gSaveCounter = slot2saveCounter; else - { - gSaveCounter = saveSlot1Counter; - } + gSaveCounter = slot1saveCounter; } else { - if (saveSlot1Counter < saveSlot2Counter) - { - gSaveCounter = saveSlot2Counter; - } + if (slot1saveCounter < slot2saveCounter) + gSaveCounter = slot2saveCounter; else - { - gSaveCounter = saveSlot1Counter; - } + gSaveCounter = slot1saveCounter; } - return 1; + return SAVE_STATUS_OK; } - if (saveSlot1Status == 1) + if (slot1Status == SAVE_STATUS_OK) { - gSaveCounter = saveSlot1Counter; - if (saveSlot2Status == 255) - return 255; - return 1; + gSaveCounter = slot1saveCounter; + if (slot2Status == SAVE_STATUS_ERROR) + return SAVE_STATUS_ERROR; + else + return SAVE_STATUS_OK; } - if (saveSlot2Status == 1) + if (slot2Status == SAVE_STATUS_OK) { - gSaveCounter = saveSlot2Counter; - if (saveSlot1Status == 255) - return 255; - return 1; + gSaveCounter = slot2saveCounter; + if (slot1Status == SAVE_STATUS_ERROR) + return SAVE_STATUS_ERROR; + else + return SAVE_STATUS_OK; } - if (saveSlot1Status == 0 && saveSlot2Status == 0) + if (slot1Status == SAVE_STATUS_EMPTY && slot2Status == SAVE_STATUS_EMPTY) { gSaveCounter = 0; - gLastWrittenSector = 0; - return 0; + gFirstSaveSector = 0; + return SAVE_STATUS_EMPTY; } gSaveCounter = 0; - gLastWrittenSector = 0; + gFirstSaveSector = 0; return 2; } -u8 sub_8125B88(u8 a1, u8 *data, u16 size) +static u8 ReadSomeUnknownSectorAndVerify(u8 sector, u8 *data, u16 size) { u16 i; - struct SaveSection *section = eSaveSection; - DoReadFlashWholeSection(a1, section); - if (section->security == UNKNOWN_CHECK_VALUE) + struct SaveSector *section = eSaveSection; + + DoReadFlashWholeSection(sector, section); + if (section->signature == FILE_SIGNATURE) { u16 checksum = CalculateChecksum(section->data, size); if (section->id == checksum) { for (i = 0; i < size; i++) data[i] = section->data[i]; - return 1; + return SAVE_STATUS_OK; } else { @@ -553,17 +642,17 @@ u8 sub_8125B88(u8 a1, u8 *data, u16 size) } else { - return 0; + return SAVE_STATUS_EMPTY; } } -u8 DoReadFlashWholeSection(u8 sector, struct SaveSection *section) +static u8 DoReadFlashWholeSection(u8 sector, struct SaveSector *section) { - ReadFlash(sector, 0, section->data, sizeof(struct SaveSection)); + ReadFlash(sector, 0, section->data, sizeof(struct SaveSector)); return 1; } -u16 CalculateChecksum(void *data, u16 size) +static u16 CalculateChecksum(void *data, u16 size) { u16 i; u32 checksum = 0; @@ -574,55 +663,97 @@ u16 CalculateChecksum(void *data, u16 size) return ((checksum >> 16) + checksum); } -u8 HandleSavingData(u8 saveType) +#if DEBUG +void sub_813B79C(void) +{ + struct SaveSector *sbSector; + struct SaveSector *hofSector; + const struct SaveBlockChunk *sbChunks; + const struct SaveBlockChunk *hofChunks; + u16 i; + + sbSector = eSaveSection; + sbChunks = sSaveBlockChunks; + for (i = 0; i < NUM_SECTORS_PER_SAVE_SLOT * 2; i++) + { + DoReadFlashWholeSection(i, sbSector); + sbSector->checksum = CalculateChecksum(sbSector, sbChunks[sbSector->id].size); + ProgramFlashSectorAndVerify(i, sbSector->data); + } + + hofSector = eSaveSection; + hofChunks = sHallOfFameChunks; + for (i = 0; i < NUM_HALL_OF_FAME_SECTORS; i++) + { + DoReadFlashWholeSection(HALL_OF_FAME_SECTOR + i, hofSector); + hofSector->id = CalculateChecksum(hofSector, hofChunks[i].size); // why id? + ProgramFlashSectorAndVerify(HALL_OF_FAME_SECTOR + i, hofSector->data); + } +} +#endif + +u8 Save_WriteDataInternal(u8 saveType) { u8 i; + switch (saveType) { - case HOF_DELETE_SAVE: // deletes HOF before overwriting HOF completely. unused - for (i = (ARRAY_COUNT(gSaveSectionLocations) * 2 + 0); i < TOTALNUMSECTORS; i++) + case SAVE_HALL_OF_FAME_ERASE_BEFORE: // wipes all hall of fame data, then saves hall of fame. unused + for (i = HALL_OF_FAME_SECTOR; i < TOTAL_FLASH_SECTORS; i++) EraseFlashSector(i); - case HOF_SAVE: // hall of fame. - if (GetGameStat(10) < 999) - IncrementGameStat(10); - for (i = 0; i < ARRAY_COUNT(gHallOfFameSaveSectionLocations); i++) - HandleWriteSectorNBytes((ARRAY_COUNT(gSaveSectionLocations) * 2 + 0) + i, gHallOfFameSaveSectionLocations[i].data, gHallOfFameSaveSectionLocations[i].size); + // fall through + case SAVE_HALL_OF_FAME: // hall of fame. + if (GetGameStat(GAME_STAT_ENTERED_HOF) < 999) + IncrementGameStat(GAME_STAT_ENTERED_HOF); + for (i = 0; i < NUM_HALL_OF_FAME_SECTORS; i++) + HandleWriteSectorNBytes(HALL_OF_FAME_SECTOR + i, sHallOfFameChunks[i].data, sHallOfFameChunks[i].size); SaveSerializedGame(); - save_write_to_flash(0xFFFF, gSaveSectionLocations); + WriteSaveBlockChunks(0xFFFF, sSaveBlockChunks); break; - case NORMAL_SAVE: // normal save. also called by overwriting your own save. + case SAVE_NORMAL: // normal save. also called by overwriting your own save. default: SaveSerializedGame(); - save_write_to_flash(0xFFFF, gSaveSectionLocations); + WriteSaveBlockChunks(0xFFFF, sSaveBlockChunks); break; - case LINK_SAVE: // link save. updates only gSaveBlock1 and gSaveBlock2. + case SAVE_LINK: // link save. updates only gSaveBlock1 and gSaveBlock2. SaveSerializedGame(); for (i = 0; i < 5; i++) - save_write_to_flash(i, gSaveSectionLocations); + WriteSaveBlockChunks(i, sSaveBlockChunks); break; - case EREADER_SAVE: // used in mossdeep "game corner" before/after battling old man e-reader trainer + case SAVE_EREADER: // used in mossdeep "game corner" before/after battling old man e-reader trainer SaveSerializedGame(); - save_write_to_flash(0, gSaveSectionLocations); + WriteSaveBlockChunks(0, sSaveBlockChunks); break; - case DIFFERENT_FILE_SAVE: // there is a different file, so erase the file and overwrite it completely. - for (i = (ARRAY_COUNT(gSaveSectionLocations) * 2 + 0); i < TOTALNUMSECTORS; i++) - EraseFlashSector(i); // erase HOF. + case SAVE_OVERWRITE_DIFFERENT_FILE: // there is a different file, so overwrite it completely. + // Erase Hall of Fame. + for (i = HALL_OF_FAME_SECTOR; i < TOTAL_FLASH_SECTORS; i++) + EraseFlashSector(i); SaveSerializedGame(); - save_write_to_flash(0xFFFF, gSaveSectionLocations); + WriteSaveBlockChunks(0xFFFF, sSaveBlockChunks); break; } return 0; } -u8 TrySavingData(u8 saveType) // TrySave +#if DEBUG +extern u32 gUnknown_Debug_03004BD0; +#endif + +u8 Save_WriteData(u8 saveType) // TrySave { if (gFlashMemoryPresent != TRUE) - return 0xFF; - HandleSavingData(saveType); - if (!gDamagedSaveSectors) - return 1; + return SAVE_STATUS_ERROR; + + Save_WriteDataInternal(saveType); + if (!gDamagedSaveSectors +#if DEBUG + && gUnknown_Debug_03004BD0 == 0 +#endif + ) + return SAVE_STATUS_OK; + DoSaveFailedScreen(saveType); - return 0xFF; + return SAVE_STATUS_ERROR; } u8 sub_8125D80(void) // trade.s save @@ -630,16 +761,16 @@ u8 sub_8125D80(void) // trade.s save if (gFlashMemoryPresent != TRUE) return 1; SaveSerializedGame(); - RestoreSaveBackupVarsAndIncrement(gSaveSectionLocations); + RestoreSaveBackupVarsAndIncrement(sSaveBlockChunks); return 0; } bool8 sub_8125DA8(void) // trade.s save { - u8 retVal = sub_812550C(ARRAY_COUNT(gSaveSectionLocations), gSaveSectionLocations); + u8 retVal = sub_812550C(ARRAY_COUNT(sSaveBlockChunks), sSaveBlockChunks); if (gDamagedSaveSectors) DoSaveFailedScreen(0); - if (retVal == 0xFF) + if (retVal == SAVE_STATUS_ERROR) return 1; else return 0; @@ -647,7 +778,7 @@ bool8 sub_8125DA8(void) // trade.s save u8 sub_8125DDC(void) // trade.s save { - sub_812556C(ARRAY_COUNT(gSaveSectionLocations), gSaveSectionLocations); + sub_812556C(ARRAY_COUNT(sSaveBlockChunks), sSaveBlockChunks); if (gDamagedSaveSectors) DoSaveFailedScreen(0); return 0; @@ -655,7 +786,7 @@ u8 sub_8125DDC(void) // trade.s save u8 sub_8125E04(void) // trade.s save { - sub_8125758(ARRAY_COUNT(gSaveSectionLocations), gSaveSectionLocations); + WriteSomeFlashByteToPrevSector(ARRAY_COUNT(sSaveBlockChunks), sSaveBlockChunks); if (gDamagedSaveSectors) DoSaveFailedScreen(0); return 0; @@ -667,23 +798,24 @@ u8 sub_8125E2C(void) return 1; SaveSerializedGame(); - RestoreSaveBackupVars(gSaveSectionLocations); - sub_812556C(gUnknown_03005EB4 + 1, gSaveSectionLocations); + RestoreSaveBackupVars(sSaveBlockChunks); + sub_812556C(gUnknown_03005EB4 + 1, sSaveBlockChunks); return 0; } +// something to do with multiplayer. Possibly record mizing? bool8 sub_8125E6C(void) { u8 retVal = FALSE; u16 val = ++gUnknown_03005EB4; if (val <= 4) { - sub_812556C(gUnknown_03005EB4 + 1, gSaveSectionLocations); - sub_81257F0(val, gSaveSectionLocations); + sub_812556C(gUnknown_03005EB4 + 1, sSaveBlockChunks); + WriteSomeFlashByte0x25ToPrevSector(val, sSaveBlockChunks); } else { - sub_81257F0(val, gSaveSectionLocations); + WriteSomeFlashByte0x25ToPrevSector(val, sSaveBlockChunks); retVal = TRUE; } if (gDamagedSaveSectors) @@ -691,46 +823,48 @@ bool8 sub_8125E6C(void) return retVal; } -u8 sub_8125EC8(u8 a1) +u8 Save_LoadGameData(u8 saveType) { u8 result; if (gFlashMemoryPresent != TRUE) { - gSaveFileStatus = 4; - return 0xFF; + gSaveFileStatus = SAVE_STATUS_NO_FLASH; + return SAVE_STATUS_ERROR; } - switch (a1) + switch (saveType) { - case 0: + case SAVE_NORMAL: default: - result = sub_812587C(0xFFFF, gSaveSectionLocations); + result = sub_812587C(0xFFFF, sSaveBlockChunks); LoadSerializedGame(); gSaveFileStatus = result; gGameContinueCallback = 0; break; - case 3: - result = sub_8125B88((ARRAY_COUNT(gSaveSectionLocations) * 2 + 0), gHallOfFameSaveSectionLocations[0].data, gHallOfFameSaveSectionLocations[0].size); - if (result == 1) - result = sub_8125B88((ARRAY_COUNT(gSaveSectionLocations) * 2 + 1), gHallOfFameSaveSectionLocations[1].data, gHallOfFameSaveSectionLocations[1].size); + case SAVE_HALL_OF_FAME: + result = ReadSomeUnknownSectorAndVerify(HALL_OF_FAME_SECTOR, sHallOfFameChunks[0].data, sHallOfFameChunks[0].size); + if (result == SAVE_STATUS_OK) + result = ReadSomeUnknownSectorAndVerify(HALL_OF_FAME_SECTOR + 1, sHallOfFameChunks[1].data, sHallOfFameChunks[1].size); break; } return result; } +const u8 sUnusedFlashSectors[] = { 30, 31 }; + bool8 unref_sub_8125F4C(struct UnkSaveSection *a1) { u16 i; char *raw = (char *)a1; - for (i = 0; i < sizeof(struct SaveSection); i++) + for (i = 0; i < sizeof(struct SaveSector); i++) raw[i] = 0; - ReadFlash(gFlashSectors[0], 0, a1->data, 4096); + ReadFlash(sUnusedFlashSectors[0], 0, a1->data, 4096); - if (a1->security != UNKNOWN_CHECK_VALUE) + if (a1->signature != FILE_SIGNATURE) return FALSE; return TRUE; @@ -739,22 +873,22 @@ bool8 unref_sub_8125F4C(struct UnkSaveSection *a1) u8 unref_sub_8125FA0(void) { u16 i; - u8 v0 = TrySavingData(0); + u8 status = Save_WriteData(SAVE_NORMAL); for (i = 0; i < 2; i++) - EraseFlashSector(gFlashSectors[i]); + EraseFlashSector(sUnusedFlashSectors[i]); - if (v0 == 255) + if (status == SAVE_STATUS_ERROR) { return 3; } - else if (v0 == 3) + else if (status == 3) { return 2; } else { - sub_8125EC8(0); + Save_LoadGameData(SAVE_NORMAL); return 1; } } @@ -764,32 +898,32 @@ u8 unref_sub_8125FF0(u8 *data, u16 size) u16 i; struct UnkSaveSection *section = (struct UnkSaveSection *)eSaveSection; - for (i = 0; i < sizeof(struct SaveSection); i++) + for (i = 0; i < sizeof(struct SaveSector); i++) ((char *)section)[i] = 0; - section->security = UNKNOWN_CHECK_VALUE; + section->signature = FILE_SIGNATURE; for (i = 0; i < size; i++) section->data[i] = data[i]; - gLastSaveSectorStatus = ProgramFlashSectorAndVerifyNBytes(gFlashSectors[0], section, sizeof(struct SaveSection)); + gLastSaveSectorStatus = ProgramFlashSectorAndVerifyNBytes(sUnusedFlashSectors[0], section, sizeof(struct SaveSector)); if (gLastSaveSectorStatus) - return 0xFF; + return SAVE_STATUS_ERROR; else - return 1; + return SAVE_STATUS_OK; } u8 unref_sub_8126068(u8 sector, u8 *data, u32 size) { if (ProgramFlashSectorAndVerify(sector, data)) - return 255; + return SAVE_STATUS_ERROR; else - return 1; + return SAVE_STATUS_OK; } u8 unref_sub_8126080(u8 sector, u8 *data) { - ReadFlash(sector, 0, data, sizeof(struct SaveSection)); + ReadFlash(sector, 0, data, sizeof(struct SaveSector)); return 1; } diff --git a/src/engine/save_failed_screen.c b/src/engine/save_failed_screen.c index ba2d46546..49cf166d4 100644 --- a/src/engine/save_failed_screen.c +++ b/src/engine/save_failed_screen.c @@ -61,16 +61,18 @@ static const u8 sClockFrames[8][3] = static const u8 gSaveFailedClockPal[] = INCBIN_U8("graphics/misc/clock_small.gbapal"); static const u8 gSaveFailedClockGfx[] = INCBIN_U8("graphics/misc/clock_small.4bpp.lz"); +#define static + static void VBlankCB(void); static void CB2_SaveFailedScreen(void); static void CB2_WipeSave(void); -static void CB2_GameplayCannotBeContinued(void); +/*static*/ void CB2_GameplayCannotBeContinued(void); static void CB2_FadeAndReturnToTitleScreen(void); static void CB2_ReturnToTitleScreen(void); static void VBlankCB_UpdateClockGraphics(void); static bool8 VerifySectorWipe(u16 sector); static bool8 WipeSector(u16 sector); -static bool8 WipeSectors(u32 sectorBits); +/*static*/ bool8 WipeSectors(u32 sectorBits); void DoSaveFailedScreen(u8 saveType) { @@ -120,11 +122,11 @@ static void CB2_SaveFailedScreen(void) ResetPaletteFade(); LoadPalette(&gBirchBagGrassPal, 0, sizeof(gBirchBagGrassPal)); LoadPalette(&gSaveFailedClockPal, 0x100, sizeof(gSaveFailedClockPal)); - SetUpWindowConfig(&gWindowConfig_81E6C3C); - InitMenuWindow(&gWindowConfig_81E6CE4); - MenuDrawTextWindow(13, CLOCK_WIN_TOP, 16, CLOCK_WIN_TOP + 3); // clock window - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); // message window - MenuPrint(gSystemText_SaveFailedBackupCheck, 2, MSG_WIN_TOP + 1); + Text_LoadWindowTemplate(&gWindowTemplate_81E6C3C); + InitMenuWindow(&gWindowTemplate_81E6CE4); + Menu_DrawStdWindowFrame(13, CLOCK_WIN_TOP, 16, CLOCK_WIN_TOP + 3); // clock window + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); // message window + Menu_PrintText(gSystemText_SaveFailedBackupCheck, 2, MSG_WIN_TOP + 1); BeginNormalPaletteFade(0xFFFFFFFF, 0, 16, 0, 0); ime = REG_IME; REG_IME = 0; @@ -148,6 +150,151 @@ static void CB2_SaveFailedScreen(void) } } +#if DEBUG +__attribute__((naked)) +void CB2_WipeSave() +{ + asm( + " push {r4, r5, lr}\n" + " mov r4, #0x0\n" + " ldr r0, ._20 @ gSaveFailedClockInfo\n" + " mov r2, #0x1\n" + " strh r2, [r0]\n" + " ldr r0, ._20 + 4 @ gUnknown_Debug_03004BD0\n" + " ldr r0, [r0]\n" + " ldr r1, ._20 + 8 @ gDamagedSaveSectors\n" + " cmp r0, #0\n" + " beq ._12 @cond_branch\n" + " str r2, [r1]\n" + "._12:\n" + " ldr r0, [r1]\n" + " cmp r0, #0\n" + " beq ._16 @cond_branch\n" + " add r5, r1, #0\n" + "._17:\n" + " ldr r0, [r5]\n" + " bl WipeSectors\n" + " lsl r0, r0, #0x18\n" + " cmp r0, #0\n" + " bne ._14 @cond_branch\n" + " mov r0, #0x1\n" + " mov r1, #0xa\n" + " mov r2, #0x1c\n" + " mov r3, #0x13\n" + " bl Menu_DrawStdWindowFrame\n" + " ldr r0, ._20 + 12 @ gSystemText_CheckCompleteSaveAttempt\n" + " mov r1, #0x2\n" + " mov r2, #0xb\n" + " bl Menu_PrintText\n" + " ldr r0, ._20 + 16 @ gSaveFailedType\n" + " ldrb r0, [r0]\n" + " bl Save_WriteDataInternal\n" + " ldr r0, [r5]\n" + " cmp r0, #0\n" + " beq ._15 @cond_branch\n" + " mov r0, #0x1\n" + " mov r1, #0xa\n" + " mov r2, #0x1c\n" + " mov r3, #0x13\n" + " bl Menu_DrawStdWindowFrame\n" + " ldr r0, ._20 + 20 @ gSystemText_SaveFailedBackupCheck\n" + " mov r1, #0x2\n" + " mov r2, #0xb\n" + " bl Menu_PrintText\n" + "._15:\n" + " add r0, r4, #1\n" + " lsl r0, r0, #0x18\n" + " lsr r4, r0, #0x18\n" + " ldr r0, [r5]\n" + " cmp r0, #0\n" + " beq ._16 @cond_branch\n" + " cmp r4, #0x2\n" + " bls ._17 @cond_branch\n" + "._16:\n" + " cmp r4, #0x3\n" + " bne ._18 @cond_branch\n" + " mov r0, #0x1\n" + " mov r1, #0xa\n" + " mov r2, #0x1c\n" + " mov r3, #0x13\n" + " bl Menu_DrawStdWindowFrame\n" + " ldr r0, ._20 + 24 @ gSystemText_BackupDamagedGameContinue\n" + " mov r1, #0x2\n" + " mov r2, #0xb\n" + " bl Menu_PrintText\n" + " ldr r0, ._20 + 28 @ CB2_FadeAndReturnToTitleScreen\n" + " bl SetMainCallback2\n" + " b ._23\n" + "._21:\n" + " .align 2, 0\n" + "._20:\n" + " .word gSaveFailedClockInfo\n" + " .word gUnknown_Debug_03004BD0\n" + " .word gDamagedSaveSectors\n" + " .word gSystemText_CheckCompleteSaveAttempt\n" + " .word gSaveFailedType\n" + " .word gSystemText_SaveFailedBackupCheck\n" + " .word gSystemText_BackupDamagedGameContinue\n" + " .word CB2_FadeAndReturnToTitleScreen+1\n" + "._18:\n" + " mov r0, #0x1\n" + " mov r1, #0xa\n" + " mov r2, #0x1c\n" + " mov r3, #0x13\n" + " bl Menu_DrawStdWindowFrame\n" + " ldr r0, ._24 @ gGameContinueCallback\n" + " ldr r0, [r0]\n" + " cmp r0, #0\n" + " bne ._22 @cond_branch\n" + " ldr r0, ._24 + 4 @ gSystemText_SaveCompletedGameEnd\n" + " mov r1, #0x2\n" + " mov r2, #0xb\n" + " bl Menu_PrintText\n" + " b ._23\n" + "._25:\n" + " .align 2, 0\n" + "._24:\n" + " .word gGameContinueCallback\n" + " .word gSystemText_SaveCompletedGameEnd\n" + "._14:\n" + " mov r0, #0x1\n" + " mov r1, #0xa\n" + " mov r2, #0x1c\n" + " mov r3, #0x13\n" + " bl Menu_DrawStdWindowFrame\n" + " ldr r0, ._27 @ gSystemText_BackupDamagedGameContinue\n" + " mov r1, #0x2\n" + " mov r2, #0xb\n" + " bl Menu_PrintText\n" + " ldr r0, ._27 + 4 @ CB2_GameplayCannotBeContinued\n" + " bl SetMainCallback2\n" + " b ._26\n" + "._28:\n" + " .align 2, 0\n" + "._27:\n" + " .word gSystemText_BackupDamagedGameContinue\n" + " .word CB2_GameplayCannotBeContinued+1\n" + "._22:\n" + " ldr r0, ._29 @ gSystemText_SaveCompletedPressA\n" + " mov r1, #0x2\n" + " mov r2, #0xb\n" + " bl Menu_PrintText\n" + "._23:\n" + " ldr r0, ._29 + 4 @ CB2_FadeAndReturnToTitleScreen\n" + " bl SetMainCallback2\n" + "._26:\n" + " pop {r4, r5}\n" + " pop {r0}\n" + " bx r0\n" + "._30:\n" + " .align 2, 0\n" + "._29:\n" + " .word gSystemText_SaveCompletedPressA\n" + " .word CB2_FadeAndReturnToTitleScreen+1\n" + "\n" + ); +} +#else static void CB2_WipeSave(void) { u8 wipeTries = 0; @@ -158,22 +305,22 @@ static void CB2_WipeSave(void) { if (WipeSectors(gDamagedSaveSectors) != FALSE) { - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); - MenuPrint(gSystemText_BackupDamagedGameContinue, 2, MSG_WIN_TOP + 1); + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); + Menu_PrintText(gSystemText_BackupDamagedGameContinue, 2, MSG_WIN_TOP + 1); SetMainCallback2(CB2_GameplayCannotBeContinued); return; } - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); - MenuPrint(gSystemText_CheckCompleteSaveAttempt, 2, MSG_WIN_TOP + 1); - HandleSavingData(gSaveFailedType); + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); + Menu_PrintText(gSystemText_CheckCompleteSaveAttempt, 2, MSG_WIN_TOP + 1); + Save_WriteDataInternal(gSaveFailedType); if (gDamagedSaveSectors != 0) { #ifdef BUGFIX_SAVEFAILEDSCREEN2 - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); #endif - MenuPrint(gSystemText_SaveFailedBackupCheck, 2, MSG_WIN_TOP + 1); + Menu_PrintText(gSystemText_SaveFailedBackupCheck, 2, MSG_WIN_TOP + 1); } wipeTries++; @@ -181,32 +328,33 @@ static void CB2_WipeSave(void) if (wipeTries == 3) { - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); - MenuPrint(gSystemText_BackupDamagedGameContinue, 2, MSG_WIN_TOP + 1); + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); + Menu_PrintText(gSystemText_BackupDamagedGameContinue, 2, MSG_WIN_TOP + 1); SetMainCallback2(CB2_FadeAndReturnToTitleScreen); // called again below } else { - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); // no callback exists, so the game cannot continue. if (gGameContinueCallback == 0) - MenuPrint(gSystemText_SaveCompletedGameEnd, 2, MSG_WIN_TOP + 1); + Menu_PrintText(gSystemText_SaveCompletedGameEnd, 2, MSG_WIN_TOP + 1); else // callback exists, so continue - MenuPrint(gSystemText_SaveCompletedPressA, 2, MSG_WIN_TOP + 1); + Menu_PrintText(gSystemText_SaveCompletedPressA, 2, MSG_WIN_TOP + 1); } SetMainCallback2(CB2_FadeAndReturnToTitleScreen); } +#endif -static void CB2_GameplayCannotBeContinued(void) +/*static*/ void CB2_GameplayCannotBeContinued(void) { gSaveFailedClockInfo[0] = FALSE; if (gMain.newKeys & A_BUTTON) { - MenuDrawTextWindow(1, MSG_WIN_TOP, 28, 19); - MenuPrint(gSystemText_GameplayEnded, 2, MSG_WIN_TOP + 1); + Menu_DrawStdWindowFrame(1, MSG_WIN_TOP, 28, 19); + Menu_PrintText(gSystemText_GameplayEnded, 2, MSG_WIN_TOP + 1); SetVBlankCallback(VBlankCB); SetMainCallback2(CB2_FadeAndReturnToTitleScreen); } @@ -264,6 +412,41 @@ static void VBlankCB_UpdateClockGraphics(void) gSaveFailedClockInfo[1]--; } +#if DEBUG +__attribute__((naked)) +bool8 VerifySectorWipe(u16 sector) +{ + asm( + " push {lr}\n" + " lsl r0, r0, #0x10\n" + " lsr r0, r0, #0x10\n" + " ldr r2, ._50 @ \n" + " mov r3, #0x80\n" + " lsl r3, r3, #0x5\n" + " mov r1, #0x0\n" + " bl ReadFlash\n" + " mov r0, #0x0\n" + " ldr r1, ._50 + 4 @ \n" + "._49:\n" + " add r0, r0, #0x1\n" + " lsl r0, r0, #0x10\n" + " lsr r0, r0, #0x10\n" + " cmp r0, r1\n" + " bls ._49 @cond_branch\n" + " ldr r0, ._50 + 8 @ \n" + " ldrb r0, [r0]\n" + " pop {r1}\n" + " bx r1\n" + "._51:\n" + " .align 2, 0\n" + "._50:\n" + " .word +0x2000000\n" + " .word 0x3ff\n" + " .word gUnknown_Debug_03004BD0\n" + "\n" + ); +} +#else static bool8 VerifySectorWipe(u16 sector) { u32 *ptr = (u32 *)&gSharedMem; @@ -277,6 +460,7 @@ static bool8 VerifySectorWipe(u16 sector) return FALSE; } +#endif static bool8 WipeSector(u16 sector) { @@ -294,7 +478,7 @@ static bool8 WipeSector(u16 sector) return failed; } -static bool8 WipeSectors(u32 sectorBits) +/*static*/ bool8 WipeSectors(u32 sectorBits) { u16 i; diff --git a/src/engine/save_menu_util.c b/src/engine/save_menu_util.c index 771cb72c7..ccf4d9f6f 100644 --- a/src/engine/save_menu_util.c +++ b/src/engine/save_menu_util.c @@ -18,7 +18,7 @@ void HandleDrawSaveWindowInfo(s16 left, s16 top) if (FlagGet(FLAG_SYS_POKEDEX_GET)) { // print info + dex information. - MenuDrawTextWindow(left, top, left + width, top + 11); + Menu_DrawStdWindowFrame(left, top, left + width, top + 11); PrintSaveMapName(++left, ++top); // MAP NAME PrintSavePlayerName(left, top + 2); // PLAYER PrintSaveBadges(left, top + 4); // BADGES @@ -28,7 +28,7 @@ void HandleDrawSaveWindowInfo(s16 left, s16 top) else { // print everything besides dex. - MenuDrawTextWindow(left, top, left + width, top + 9); + Menu_DrawStdWindowFrame(left, top, left + width, top + 9); PrintSaveMapName(++left, ++top); // MAP NAME PrintSavePlayerName(left, top + 2); // PLAYER PrintSaveBadges(left, top + 4); // BADGES @@ -45,9 +45,9 @@ void HandleCloseSaveWindow(u16 left, u16 top) width = 13; if (FlagGet(FLAG_SYS_POKEDEX_GET)) - MenuZeroFillWindowRect(left, top, left + width, top + 11); + Menu_EraseWindowRect(left, top, left + width, top + 11); else - MenuZeroFillWindowRect(left, top, left + width, top + 9); + Menu_EraseWindowRect(left, top, left + width, top + 9); } /* @@ -62,7 +62,7 @@ u8 IsResizeSaveWindowEnabled(void) // i don't know what else to name it.. void PrintSavePlayerName(s16 x, s16 y) { - MenuPrint(gOtherText_Player, x, y); + Menu_PrintText(gOtherText_Player, x, y); MenuPrint_RightAligned(gSaveBlock2.playerName, x + 12, y); } @@ -71,14 +71,14 @@ void PrintSaveMapName(s16 x, s16 y) char name[32]; CopyMapName(name, gMapHeader.regionMapSectionId); - MenuPrint(name, x, y); + Menu_PrintText(name, x, y); } void PrintSaveBadges(s16 x, s16 y) { char badges[16]; - MenuPrint(gOtherText_Badges, x, y); + Menu_PrintText(gOtherText_Badges, x, y); ConvertIntToDecimalString(badges, GetBadgeCount()); MenuPrint_RightAligned(badges, x + 12, y); } @@ -87,7 +87,7 @@ void PrintSavePokedexCount(s16 x, s16 y) { char pokedex[16]; - MenuPrint(gOtherText_Pokedex, x, y); + Menu_PrintText(gOtherText_Pokedex, x, y); ConvertIntToDecimalStringN(pokedex, GetPokedexSeenCount(), 1, 3); MenuPrint_RightAligned(pokedex, x + 12, y); } @@ -96,7 +96,7 @@ void PrintSavePlayTime(s16 x, s16 y) { char playtime[16]; - MenuPrint(gOtherText_PlayTime, x, y); + Menu_PrintText(gOtherText_PlayTime, x, y); FormatPlayTime(playtime, gSaveBlock2.playTimeHours, gSaveBlock2.playTimeMinutes, 1); MenuPrint_RightAligned(playtime, x + 12, y); } @@ -125,9 +125,9 @@ u16 GetPokedexSeenCount() return pokedexSeenCount; } -void FormatPlayTime(char *playtime, u16 hours, u16 minutes, u16 unk) +void FormatPlayTime(char *playtime, u16 hours, u16 minutes, u16 colon) { - s16 colon = unk; + s16 _colon = colon; playtime = ConvertIntToDecimalString(playtime, hours); // playtime[0] is hours. @@ -136,7 +136,7 @@ void FormatPlayTime(char *playtime, u16 hours, u16 minutes, u16 unk) playtime[0] = 0; - if (colon) + if (_colon) playtime[1] = 0xF0; // set middle character to ":" else playtime[1] = 0; diff --git a/src/engine/sprite.c b/src/engine/sprite.c index cf35e12a3..a9d84e01a 100644 --- a/src/engine/sprite.c +++ b/src/engine/sprite.c @@ -1,8 +1,10 @@ #include "global.h" +#include "debug.h" #include "sprite.h" #include "main.h" #include "menu_cursor.h" #include "palette.h" +#include "sprite.h" #define MAX_SPRITE_COPY_REQUESTS 64 @@ -822,6 +824,12 @@ static void RequestSpriteFrameImageCopy(u16 index, u16 tileNum, const struct Spr gSpriteCopyRequests[gSpriteCopyRequestCount].size = images[index].size; gSpriteCopyRequestCount++; } +#if DEBUG + else + { + Crash(sDmaOverErrorMsg); + } +#endif } void RequestSpriteCopy(const void *src, u8 *dest, u16 size) @@ -833,6 +841,12 @@ void RequestSpriteCopy(const void *src, u8 *dest, u16 size) gSpriteCopyRequests[gSpriteCopyRequestCount].size = size; gSpriteCopyRequestCount++; } +#if DEBUG + else + { + Crash(sDmaOverErrorMsg); + } +#endif } // these two functions are unused. diff --git a/src/engine/task.c b/src/engine/task.c index 7bd2b5937..3e8a5588c 100644 --- a/src/engine/task.c +++ b/src/engine/task.c @@ -1,4 +1,5 @@ #include "global.h" +#include "debug.h" #include "task.h" #define ACTIVE_SENTINEL 0x10 @@ -11,8 +12,9 @@ struct Task gTasks[ACTIVE_SENTINEL]; static void InsertTask(u8 newTaskId); static u8 FindFirstActiveTask(); -// Unused string -const u8 sTaskOverString[] = _("TASK OVER\nタスクがオーバーしました"); +const u8 gError_NoTasksLeft[] = _( + "TASK OVER\n" + "タスクがオーバーしました"); void ResetTasks() { @@ -49,6 +51,10 @@ u8 CreateTask(TaskFunc func, u8 priority) } } +#if DEBUG + Crash(gError_NoTasksLeft); +#endif + return 0; } diff --git a/src/engine/text.c b/src/engine/text.c index 1b158d979..fe18a6d79 100644 --- a/src/engine/text.c +++ b/src/engine/text.c @@ -12,16 +12,31 @@ enum WIN_STATE_END, WIN_STATE_BEGIN, WIN_STATE_NORMAL, - WIN_STATE_INTERRUPTIBLE_PAUSE, + WIN_STATE_CHAR_DELAY, WIN_STATE_PAUSE, WIN_STATE_WAIT_BUTTON, WIN_STATE_NEWLINE, WIN_STATE_PLACEHOLDER, - WIN_STATE_PARAGRAPH, - WIN_STATE_NEWLINE_WAIT, + WIN_STATE_WAIT_CLEAR, + WIN_STATE_WAIT_SCROLL, WIN_STATE_WAIT_SOUND, }; +enum +{ + TEXT_MODE_UNKNOWN0, + TEXT_MODE_MONOSPACE, + TEXT_MODE_UNKNOWN2, // variable width? +}; + +enum +{ + WAIT_TYPE_NORMAL, // allows the player to interrupt the text delay with A or B + WAIT_TYPE_BATTLE, // disables interrupting the text delay if in a link battle + WAIT_TYPE_AUTOSCROLL, // disables interrupting the text delay + WAIT_TYPE_CONTEST, // disables interrupting the text delay if in a link contest +}; + struct Font { u32 type; @@ -66,7 +81,7 @@ static u16 LoadFixedWidthFont_Font4Latin(struct Window *, u16); static u16 LoadFixedWidthFont_Braille(struct Window *, u16); static void MultistepLoadFont_LoadGlyph(struct Window *, u16, u8); static u8 sub_8002FA0(struct Window *, const u8 *); -static u8 InterpretText(struct Window *); +static u8 PrintNextChar(struct Window *); static u8 HandleExtCtrlCode(struct Window *); static u8 UpdateWindowText(struct Window *); static u8 DrawGlyph_TextMode0(struct Window *, u32); @@ -82,27 +97,27 @@ static void AddToCursorX(struct Window *, u8); static void AddToCursorY(struct Window *, u8); static void ClipLeft(struct Window *); static void ClipRight(struct Window *); -static void InitColors(struct Window *); -static void SetBackgroundColor(struct Window *, u8); -static void SetShadowColor(struct Window *, u8); -static void SetForegroundColor(struct Window *, u8); +static void SetWindowDefaultColors(struct Window *); +static void SetWindowBackgroundColor(struct Window *, u8); +static void SetWindowShadowColor(struct Window *, u8); +static void SetWindowForegroundColor(struct Window *, u8); static u8 GetTextDelay(struct Window *); -static bool8 PlayerCanInterruptWait(struct Window *); +static bool8 PlayerCanInterruptDelay(struct Window *); static void ScrollWindowTextLines(struct Window *); static void ScrollWindowTextLines_TextMode0(struct Window *); static void DoScroll_TextMode0(struct Window *, u16); -static void ScrollWindowTextLines_TextMode1(struct Window *); -static void DoScroll_TextMode1(struct Window *, u16); +static void ScrollWindowTextLines_TextModeMonospace(struct Window *); +static void DoScroll_TextModeMonospace(struct Window *, u16); static void ScrollWindowTextLines_TextMode2(struct Window *); static void DoScroll_TextMode2(struct Window *, u8); -void ClearWindowTextLines(struct Window *); +void Text_ClearWindow(struct Window *); static void ClearWindowTextLines_TextMode0_TextMode1(struct Window *, u8); static void ClearWindowTextLines_TextMode2(struct Window *, u8); static void TryEraseDownArrow(struct Window *); static u16 GetBlankTileNum(struct Window *); static u8 WaitWithDownArrow(struct Window *); static void DrawInitialDownArrow(struct Window *); -static void DrawMovingDownArrow(struct Window *); +static void UpdateDownArrowAnimation(struct Window *); static u16 GetCursorTileNum(struct Window *, u32, u32); static s32 DrawGlyphTiles(struct Window *, u32, u32); static void UpdateTilemap(struct Window *, u32); @@ -477,7 +492,7 @@ static const ShiftGlyphTileShadowedFunc sShiftGlyphTileShadowedFuncs[] = ShiftGlyphTile_ShadowedFont_Width8, }; -const struct WindowConfig gWindowConfig_81E6C3C = +const struct WindowTemplate gWindowTemplate_81E6C3C = { 0, // BG number 2, // BG character base block @@ -498,7 +513,7 @@ const struct WindowConfig gWindowConfig_81E6C3C = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6C58 = +const struct WindowTemplate gWindowTemplate_81E6C58 = { 0, // BG number 0, // BG character base block @@ -519,7 +534,7 @@ const struct WindowConfig gWindowConfig_81E6C58 = BG_SCREEN_ADDR(24), // tilemap }; -const struct WindowConfig gWindowConfig_81E6C74 = +const struct WindowTemplate gWindowTemplate_81E6C74 = { 0, // BG number 0, // BG character base block @@ -540,7 +555,7 @@ const struct WindowConfig gWindowConfig_81E6C74 = NULL, // tilemap }; -const struct WindowConfig gWindowConfig_81E6C90 = +const struct WindowTemplate gWindowTemplate_81E6C90 = { 0, // BG number 1, // BG character base block @@ -561,7 +576,7 @@ const struct WindowConfig gWindowConfig_81E6C90 = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E6CAC = +const struct WindowTemplate gWindowTemplate_81E6CAC = { 0, // BG number 0, // BG character base block @@ -582,7 +597,7 @@ const struct WindowConfig gWindowConfig_81E6CAC = NULL, // tilemap }; -const struct WindowConfig gWindowConfig_81E6CC8 = +const struct WindowTemplate gWindowTemplate_81E6CC8 = { 2, // BG number 2, // BG character base block @@ -603,7 +618,7 @@ const struct WindowConfig gWindowConfig_81E6CC8 = BG_SCREEN_ADDR(15), // tilemap }; -const struct WindowConfig gWindowConfig_81E6CE4 = +const struct WindowTemplate gWindowTemplate_81E6CE4 = { 0, // BG number 2, // BG character base block @@ -624,7 +639,7 @@ const struct WindowConfig gWindowConfig_81E6CE4 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6D00 = +const struct WindowTemplate gWindowTemplate_81E6D00 = { 0, // BG number 0, // BG character base block @@ -645,7 +660,7 @@ const struct WindowConfig gWindowConfig_81E6D00 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6D1C = +const struct WindowTemplate gWindowTemplate_81E6D1C = { 1, // BG number 0, // BG character base block @@ -666,7 +681,7 @@ const struct WindowConfig gWindowConfig_81E6D1C = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6D38 = +const struct WindowTemplate gWindowTemplate_81E6D38 = { 0, // BG number 0, // BG character base block @@ -687,7 +702,7 @@ const struct WindowConfig gWindowConfig_81E6D38 = NULL, // tilemap }; -const struct WindowConfig gWindowConfig_81E6D54 = +const struct WindowTemplate gWindowTemplate_81E6D54 = { 3, // BG number 3, // BG character base block @@ -708,7 +723,7 @@ const struct WindowConfig gWindowConfig_81E6D54 = BG_SCREEN_ADDR(15), // tilemap }; -const struct WindowConfig gWindowConfig_81E6D70 = +const struct WindowTemplate gWindowTemplate_81E6D70 = { 3, // BG number 3, // BG character base block @@ -729,7 +744,7 @@ const struct WindowConfig gWindowConfig_81E6D70 = BG_SCREEN_ADDR(15), // tilemap }; -const struct WindowConfig gWindowConfig_81E6D8C = +const struct WindowTemplate gWindowTemplate_81E6D8C = { 1, // BG number 0, // BG character base block @@ -750,7 +765,7 @@ const struct WindowConfig gWindowConfig_81E6D8C = BG_SCREEN_ADDR(14), // tilemap }; -const struct WindowConfig gWindowConfig_81E6DA8 = +const struct WindowTemplate gWindowTemplate_81E6DA8 = { 0, // BG number 0, // BG character base block @@ -771,7 +786,7 @@ const struct WindowConfig gWindowConfig_81E6DA8 = BG_SCREEN_ADDR(11), // tilemap }; -const struct WindowConfig WindowConfig_TrainerCard_Back_Values = +const struct WindowTemplate gWindowTemplate_TrainerCard_Back_Values = { 0, // BG number 2, // BG character base block @@ -792,7 +807,7 @@ const struct WindowConfig WindowConfig_TrainerCard_Back_Values = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig WindowConfig_TrainerCard_Back_Labels = +const struct WindowTemplate gWindowTemplate_TrainerCard_Back_Labels = { 0, // BG number 2, // BG character base block @@ -813,7 +828,7 @@ const struct WindowConfig WindowConfig_TrainerCard_Back_Labels = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E6DFC = +const struct WindowTemplate gWindowTemplate_81E6DFC = { 0, // BG number 2, // BG character base block @@ -834,7 +849,7 @@ const struct WindowConfig gWindowConfig_81E6DFC = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6E18 = +const struct WindowTemplate gWindowTemplate_81E6E18 = { 0, // BG number 2, // BG character base block @@ -855,7 +870,7 @@ const struct WindowConfig gWindowConfig_81E6E18 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6E34 = +const struct WindowTemplate gWindowTemplate_81E6E34 = { 1, // BG number 0, // BG character base block @@ -876,7 +891,7 @@ const struct WindowConfig gWindowConfig_81E6E34 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6E50 = +const struct WindowTemplate gWindowTemplate_81E6E50 = { 0, // BG number 2, // BG character base block @@ -897,7 +912,7 @@ const struct WindowConfig gWindowConfig_81E6E50 = BG_SCREEN_ADDR(28), // tilemap }; -const struct WindowConfig gWindowConfig_81E6E6C = +const struct WindowTemplate gWindowTemplate_81E6E6C = { 0, // BG number 2, // BG character base block @@ -918,7 +933,7 @@ const struct WindowConfig gWindowConfig_81E6E6C = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E6E88 = +const struct WindowTemplate gWindowTemplate_81E6E88 = { 0, // BG number 0, // BG character base block @@ -939,7 +954,7 @@ const struct WindowConfig gWindowConfig_81E6E88 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6EA4 = +const struct WindowTemplate gWindowTemplate_81E6EA4 = { 1, // BG number 0, // BG character base block @@ -960,7 +975,7 @@ const struct WindowConfig gWindowConfig_81E6EA4 = BG_SCREEN_ADDR(28), // tilemap }; -const struct WindowConfig gWindowConfig_81E6EC0 = +const struct WindowTemplate gWindowTemplate_81E6EC0 = { 2, // BG number 2, // BG character base block @@ -981,7 +996,7 @@ const struct WindowConfig gWindowConfig_81E6EC0 = BG_SCREEN_ADDR(29), // tilemap }; -const struct WindowConfig gWindowConfig_81E6EDC = +const struct WindowTemplate gWindowTemplate_81E6EDC = { 1, // BG number 0, // BG character base block @@ -1002,7 +1017,7 @@ const struct WindowConfig gWindowConfig_81E6EDC = BG_SCREEN_ADDR(28), // tilemap }; -const struct WindowConfig gWindowConfig_81E6EF8 = +const struct WindowTemplate gWindowTemplate_81E6EF8 = { 2, // BG number 2, // BG character base block @@ -1023,7 +1038,7 @@ const struct WindowConfig gWindowConfig_81E6EF8 = BG_SCREEN_ADDR(29), // tilemap }; -const struct WindowConfig gWindowConfig_81E6F14 = +const struct WindowTemplate gWindowTemplate_81E6F14 = { 1, // BG number 0, // BG character base block @@ -1044,7 +1059,7 @@ const struct WindowConfig gWindowConfig_81E6F14 = BG_SCREEN_ADDR(28), // tilemap }; -const struct WindowConfig gWindowConfig_81E6F30 = +const struct WindowTemplate gWindowTemplate_81E6F30 = { 2, // BG number 2, // BG character base block @@ -1065,7 +1080,7 @@ const struct WindowConfig gWindowConfig_81E6F30 = BG_SCREEN_ADDR(29), // tilemap }; -const struct WindowConfig gWindowConfig_81E6F4C = +const struct WindowTemplate gWindowTemplate_81E6F4C = { 3, // BG number 0, // BG character base block @@ -1086,7 +1101,7 @@ const struct WindowConfig gWindowConfig_81E6F4C = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E6F68 = +const struct WindowTemplate gWindowTemplate_81E6F68 = { 0, // BG number 2, // BG character base block @@ -1107,7 +1122,7 @@ const struct WindowConfig gWindowConfig_81E6F68 = BG_SCREEN_ADDR(13), // tilemap }; -const struct WindowConfig gWindowConfig_81E6F84 = +const struct WindowTemplate gWindowTemplate_81E6F84 = { 0, // BG number 2, // BG character base block @@ -1128,7 +1143,7 @@ const struct WindowConfig gWindowConfig_81E6F84 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E6FA0 = +const struct WindowTemplate gWindowTemplate_81E6FA0 = { 1, // BG number 0, // BG character base block @@ -1149,7 +1164,7 @@ const struct WindowConfig gWindowConfig_81E6FA0 = BG_SCREEN_ADDR(24), // tilemap }; -const struct WindowConfig gWindowConfig_81E6FBC = +const struct WindowTemplate gWindowTemplate_81E6FBC = { 0, // BG number 0, // BG character base block @@ -1170,7 +1185,7 @@ const struct WindowConfig gWindowConfig_81E6FBC = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E6FD8 = +const struct WindowTemplate gWindowTemplate_81E6FD8 = { 0, // BG number 0, // BG character base block @@ -1191,7 +1206,7 @@ const struct WindowConfig gWindowConfig_81E6FD8 = BG_SCREEN_ADDR(24), // tilemap }; -const struct WindowConfig gWindowConfig_81E6FF4 = +const struct WindowTemplate gWindowTemplate_81E6FF4 = { 0, // BG number 0, // BG character base block @@ -1212,7 +1227,7 @@ const struct WindowConfig gWindowConfig_81E6FF4 = BG_SCREEN_ADDR(24), // tilemap }; -const struct WindowConfig gWindowConfig_81E7010 = +const struct WindowTemplate gWindowTemplate_81E7010 = { 0, // BG number 0, // BG character base block @@ -1233,7 +1248,7 @@ const struct WindowConfig gWindowConfig_81E7010 = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E702C = +const struct WindowTemplate gWindowTemplate_81E702C = { 3, // BG number 2, // BG character base block @@ -1254,7 +1269,7 @@ const struct WindowConfig gWindowConfig_81E702C = BG_SCREEN_ADDR(15), // tilemap }; -const struct WindowConfig gWindowConfig_81E7048 = +const struct WindowTemplate gWindowTemplate_81E7048 = { 2, // BG number 2, // BG character base block @@ -1275,7 +1290,7 @@ const struct WindowConfig gWindowConfig_81E7048 = BG_SCREEN_ADDR(14), // tilemap }; -const struct WindowConfig gWindowConfig_81E7064 = +const struct WindowTemplate gWindowTemplate_81E7064 = { 2, // BG number 2, // BG character base block @@ -1296,7 +1311,7 @@ const struct WindowConfig gWindowConfig_81E7064 = BG_SCREEN_ADDR(14), // tilemap }; -const struct WindowConfig gWindowConfig_81E7080 = +const struct WindowTemplate gWindowTemplate_81E7080 = { 3, // BG number 0, // BG character base block @@ -1317,7 +1332,7 @@ const struct WindowConfig gWindowConfig_81E7080 = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E709C = +const struct WindowTemplate gWindowTemplate_81E709C = { 0, // BG number 0, // BG character base block @@ -1338,7 +1353,7 @@ const struct WindowConfig gWindowConfig_81E709C = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E70B8 = +const struct WindowTemplate gWindowTemplate_81E70B8 = { 2, // BG number 0, // BG character base block @@ -1359,7 +1374,7 @@ const struct WindowConfig gWindowConfig_81E70B8 = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E70D4 = +const struct WindowTemplate gWindowTemplate_81E70D4 = { 3, // BG number 0, // BG character base block @@ -1380,7 +1395,7 @@ const struct WindowConfig gWindowConfig_81E70D4 = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E70F0 = +const struct WindowTemplate gWindowTemplate_81E70F0 = { 0, // BG number 0, // BG character base block @@ -1401,7 +1416,7 @@ const struct WindowConfig gWindowConfig_81E70F0 = NULL, // tilemap }; -const struct WindowConfig gWindowConfig_81E710C = +const struct WindowTemplate gWindowTemplate_81E710C = { 0, // BG number 0, // BG character base block @@ -1422,7 +1437,7 @@ const struct WindowConfig gWindowConfig_81E710C = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E7128 = +const struct WindowTemplate gWindowTemplate_81E7128 = { 0, // BG number 2, // BG character base block @@ -1443,7 +1458,7 @@ const struct WindowConfig gWindowConfig_81E7128 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E7144 = +const struct WindowTemplate gWindowTemplate_81E7144 = { 0, // BG number 2, // BG character base block @@ -1464,7 +1479,7 @@ const struct WindowConfig gWindowConfig_81E7144 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E7160 = +const struct WindowTemplate gWindowTemplate_81E7160 = { 1, // BG number 1, // BG character base block @@ -1485,7 +1500,7 @@ const struct WindowConfig gWindowConfig_81E7160 = BG_SCREEN_ADDR(10), // tilemap }; -const struct WindowConfig gWindowConfig_81E717C = +const struct WindowTemplate gWindowTemplate_81E717C = { 0, // BG number 3, // BG character base block @@ -1506,7 +1521,7 @@ const struct WindowConfig gWindowConfig_81E717C = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E7198 = +const struct WindowTemplate gWindowTemplate_81E7198 = { 0, // BG number 2, // BG character base block @@ -1527,7 +1542,7 @@ const struct WindowConfig gWindowConfig_81E7198 = BG_SCREEN_ADDR(15), // tilemap }; -const struct WindowConfig gWindowConfig_81E71B4 = +const struct WindowTemplate gWindowTemplate_81E71B4 = { 0, // BG number 2, // BG character base block @@ -1548,7 +1563,7 @@ const struct WindowConfig gWindowConfig_81E71B4 = BG_SCREEN_ADDR(15), // tilemap }; -const struct WindowConfig gWindowConfig_81E71D0 = +const struct WindowTemplate gWindowTemplate_81E71D0 = { 1, // BG number 1, // BG character base block @@ -1569,7 +1584,7 @@ const struct WindowConfig gWindowConfig_81E71D0 = BG_SCREEN_ADDR(28), // tilemap }; -const struct WindowConfig gWindowConfig_81E71EC = +const struct WindowTemplate gWindowTemplate_81E71EC = { 2, // BG number 1, // BG character base block @@ -1590,7 +1605,7 @@ const struct WindowConfig gWindowConfig_81E71EC = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E7208 = +const struct WindowTemplate gWindowTemplate_81E7208 = { 0, // BG number 2, // BG character base block @@ -1611,7 +1626,7 @@ const struct WindowConfig gWindowConfig_81E7208 = BG_SCREEN_ADDR(28), // tilemap }; -const struct WindowConfig gWindowConfig_81E7224 = +const struct WindowTemplate gWindowTemplate_81E7224 = { 0, // BG number 0, // BG character base block @@ -1632,7 +1647,7 @@ const struct WindowConfig gWindowConfig_81E7224 = BG_SCREEN_ADDR(31), // tilemap }; -const struct WindowConfig gWindowConfig_81E7240 = +const struct WindowTemplate gWindowTemplate_81E7240 = { 1, // BG number 2, // BG character base block @@ -1653,7 +1668,7 @@ const struct WindowConfig gWindowConfig_81E7240 = BG_SCREEN_ADDR(30), // tilemap }; -const struct WindowConfig gWindowConfig_81E725C = +const struct WindowTemplate gWindowTemplate_81E725C = { 0, // BG number 0, // BG character base block @@ -1674,7 +1689,7 @@ const struct WindowConfig gWindowConfig_81E725C = NULL, // tilemap }; -const struct WindowConfig gWindowConfig_81E7278 = +const struct WindowTemplate gWindowTemplate_81E7278 = { 0, // BG number 0, // BG character base block @@ -1695,7 +1710,7 @@ const struct WindowConfig gWindowConfig_81E7278 = NULL, // tilemap }; -const struct WindowConfig gWindowConfig_81E7294 = +const struct WindowTemplate gWindowTemplate_81E7294 = { 0, // BG number 0, // BG character base block @@ -1716,32 +1731,32 @@ const struct WindowConfig gWindowConfig_81E7294 = NULL, // tilemap }; -static void UpdateBGRegs(const struct WindowConfig *winConfig) +static void UpdateBGRegs(const struct WindowTemplate *winTemplate) { - u8 bgNum = winConfig->bgNum; + u8 bgNum = winTemplate->bgNum; *gBGHOffsetRegs[bgNum] = 0; *gBGVOffsetRegs[bgNum] = 0; - *gBGControlRegs[bgNum] = winConfig->priority | (winConfig->screenBaseBlock << 8) | (winConfig->charBaseBlock << 2); + *gBGControlRegs[bgNum] = winTemplate->priority | (winTemplate->screenBaseBlock << 8) | (winTemplate->charBaseBlock << 2); } -static void ClearBGMem(const struct WindowConfig *winConfig) +static void ClearBGMem(const struct WindowTemplate *winTemplate) { - CpuFastFill(0, winConfig->tileData, 32); + CpuFastFill(0, winTemplate->tileData, 32); - if (winConfig->tilemap) - CpuFastFill(0, winConfig->tilemap, 0x800); + if (winTemplate->tilemap) + CpuFastFill(0, winTemplate->tilemap, 0x800); } -void LoadFontDefaultPalette(const struct WindowConfig *winConfig) +void LoadFontDefaultPalette(const struct WindowTemplate *winTemplate) { - LoadPalette(gFontDefaultPalette, 16 * winConfig->paletteNum, 32); + LoadPalette(gFontDefaultPalette, 16 * winTemplate->paletteNum, 32); } -void SetUpWindowConfig(const struct WindowConfig *winConfig) +void Text_LoadWindowTemplate(const struct WindowTemplate *winTemplate) { - UpdateBGRegs(winConfig); - ClearBGMem(winConfig); - LoadFontDefaultPalette(winConfig); + UpdateBGRegs(winTemplate); + ClearBGMem(winTemplate); + LoadFontDefaultPalette(winTemplate); } u16 InitWindowTileData(struct Window *win, u16 startOffset) @@ -1751,13 +1766,13 @@ u16 InitWindowTileData(struct Window *win, u16 startOffset) win->tileDataStartOffset = startOffset; retVal = 0; - switch (win->config->textMode) + switch (win->template->textMode) { - case 2: + case TEXT_MODE_UNKNOWN2: retVal = InitVariableWidthFontTileData(win, startOffset); break; - case 1: - switch (win->config->fontNum) + case TEXT_MODE_MONOSPACE: + switch (win->template->fontNum) { case 0: case 3: @@ -1789,7 +1804,7 @@ static u16 InitVariableWidthFontTileData(struct Window *win, u16 startOffset) win->tileDataOffset = 2; buffer = win->tileData + 32 * win->tileDataStartOffset; CpuFastFill(0, buffer, 32); - ApplyColors_UnshadowedFont(sBlankTile, (u32 *)(buffer + 32), win->config->foregroundColor, win->config->backgroundColor); + ApplyColors_UnshadowedFont(sBlankTile, (u32 *)(buffer + 32), win->template->foregroundColor, win->template->backgroundColor); return win->tileDataStartOffset + win->tileDataOffset + win->width * win->height; } @@ -1850,15 +1865,15 @@ u32 MultistepInitWindowTileData(struct Window *win, u16 startOffset) win->tileDataStartOffset = startOffset; retVal = 0; - switch (win->config->textMode) + switch (win->template->textMode) { - case 2: + case TEXT_MODE_UNKNOWN2: retVal = InitVariableWidthFontTileData(win, startOffset); break; - case 1: + case TEXT_MODE_MONOSPACE: retVal = 256; - if (win->config->fontNum == 0 - || win->config->fontNum == 3) + if (win->template->fontNum == 0 + || win->template->fontNum == 3) retVal *= 2; break; } @@ -1870,7 +1885,7 @@ bool32 MultistepLoadFont(void) { bool32 retVal = TRUE; - if (sMultistepLoadFont_Window->config->textMode == 1) + if (sMultistepLoadFont_Window->template->textMode == TEXT_MODE_MONOSPACE) { s32 i; @@ -1890,7 +1905,7 @@ static void MultistepLoadFont_LoadGlyph(struct Window *win, u16 startOffset, u8 { u8 *buffer; - switch (win->config->fontNum) + switch (win->template->fontNum) { case 0: case 3: @@ -1923,42 +1938,42 @@ void EmptyFunc(void) { } -void InitWindowFromConfig(struct Window *win, const struct WindowConfig *winConfig) +void Text_InitWindowWithTemplate(struct Window *win, const struct WindowTemplate *winTemplate) { *win = sDefaultWindow; - win->config = (struct WindowConfig *)winConfig; - win->textMode = winConfig->textMode; - win->spacing = winConfig->spacing; - win->fontNum = winConfig->fontNum; - win->paletteNum = winConfig->paletteNum; - win->tilemapLeft = winConfig->tilemapLeft; - win->tilemapTop = winConfig->tilemapTop; - win->width = winConfig->width; - win->height = winConfig->height; - win->tileData = winConfig->tileData; - win->tilemap = winConfig->tilemap; - InitColors(win); - SetBackgroundColor(win, winConfig->backgroundColor); - SetShadowColor(win, winConfig->shadowColor); - SetForegroundColor(win, winConfig->foregroundColor); -} - -void InitWindow(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) -{ - const struct WindowConfig *winConfig = win->config; - win->textMode = winConfig->textMode; - win->fontNum = winConfig->fontNum; + win->template = (struct WindowTemplate *)winTemplate; + win->textMode = winTemplate->textMode; + win->spacing = winTemplate->spacing; + win->fontNum = winTemplate->fontNum; + win->paletteNum = winTemplate->paletteNum; + win->tilemapLeft = winTemplate->tilemapLeft; + win->tilemapTop = winTemplate->tilemapTop; + win->width = winTemplate->width; + win->height = winTemplate->height; + win->tileData = winTemplate->tileData; + win->tilemap = winTemplate->tilemap; + SetWindowDefaultColors(win); + SetWindowBackgroundColor(win, winTemplate->backgroundColor); + SetWindowShadowColor(win, winTemplate->shadowColor); + SetWindowForegroundColor(win, winTemplate->foregroundColor); +} + +void Text_InitWindow(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) +{ + const struct WindowTemplate *winTemplate = win->template; + win->textMode = winTemplate->textMode; + win->fontNum = winTemplate->fontNum; win->language = GAME_LANGUAGE; - win->paletteNum = winConfig->paletteNum; + win->paletteNum = winTemplate->paletteNum; win->win_field_B = 0; win->win_field_C = 0; win->delayCounter = 0; - win->spacing = winConfig->spacing; + win->spacing = winTemplate->spacing; win->win_field_F = 0; - win->tilemapLeft = winConfig->tilemapLeft; - win->tilemapTop = winConfig->tilemapTop; - win->width = winConfig->width; - win->height = winConfig->height; + win->tilemapLeft = winTemplate->tilemapLeft; + win->tilemapTop = winTemplate->tilemapTop; + win->width = winTemplate->width; + win->height = winTemplate->height; win->text = text; win->textIndex = 0; win->tileDataStartOffset = tileDataStartOffset; @@ -1969,19 +1984,19 @@ void InitWindow(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 win->cursorY = 0; win->state = WIN_STATE_BEGIN; win->downArrowCounter = 0; - win->tileData = winConfig->tileData; - win->tilemap = winConfig->tilemap; - InitColors(win); - SetBackgroundColor(win, winConfig->backgroundColor); - SetShadowColor(win, winConfig->shadowColor); - SetForegroundColor(win, winConfig->foregroundColor); + win->tileData = winTemplate->tileData; + win->tilemap = winTemplate->tilemap; + SetWindowDefaultColors(win); + SetWindowBackgroundColor(win, winTemplate->backgroundColor); + SetWindowShadowColor(win, winTemplate->shadowColor); + SetWindowForegroundColor(win, winTemplate->foregroundColor); } -void sub_8002E4C(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u16 top, u32 a6) +void Text_InitWindow8002E4C(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u16 top, u32 a6) { u8 val; - InitWindow(win, text, tileDataStartOffset, 0, 0); + Text_InitWindow(win, text, tileDataStartOffset, 0, 0); win->left = left; win->top = top; val = 0; @@ -1992,7 +2007,7 @@ void sub_8002E4C(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 ClipLeft(win); } -void sub_8002E90(struct Window *win, const u8 *text) +void Text_SetWindowText(struct Window *win, const u8 *text) { win->state = WIN_STATE_NORMAL; win->text = text; @@ -2003,15 +2018,15 @@ void sub_8002E90(struct Window *win, const u8 *text) win->delayCounter = 0; } -void sub_8002EB0(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) +void Text_InitWindow8002EB0(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) { gMain.watchedKeysMask = A_BUTTON | B_BUTTON; gMain.watchedKeysPressed = 0; - sWaitType = 0; + sWaitType = WAIT_TYPE_NORMAL; sLineLength = 26; - InitWindow(win, text, tileDataStartOffset, left, top); + Text_InitWindow(win, text, tileDataStartOffset, left, top); win->win_field_B = -1; - if (win->textMode == 0) + if (win->textMode == TEXT_MODE_UNKNOWN0) { u16 val = GetCursorTileNum(win, 0, 0); u8 *buffer = win->tileData + 32 * val; @@ -2021,9 +2036,9 @@ void sub_8002EB0(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 } } -u8 sub_8002F44(struct Window *win) +u8 Text_PrintWindow8002F44(struct Window *win) { - while (win->state) + while (win->state != WIN_STATE_END) { if (win->state == WIN_STATE_NEWLINE) { @@ -2038,7 +2053,7 @@ u8 sub_8002F44(struct Window *win) sub_8002FA0(win, GetExpandedPlaceholder(win->text[win->textIndex++])); } - InterpretText(win); + PrintNextChar(win); } return 1; } @@ -2048,11 +2063,12 @@ static u8 sub_8002FA0(struct Window *win, const u8 *text) u8 retVal; u8 savedLanguage = win->language; const u8 *savedText = win->text; + u16 savedTextIndex = win->textIndex; win->text = text; win->textIndex = 0; win->state = WIN_STATE_NORMAL; - retVal = sub_8002F44(win); + retVal = Text_PrintWindow8002F44(win); win->text = savedText; win->textIndex = savedTextIndex; win->state = WIN_STATE_NORMAL; @@ -2060,32 +2076,33 @@ static u8 sub_8002FA0(struct Window *win, const u8 *text) return retVal; } -static u8 InterpretText(struct Window *win) +static u8 PrintNextChar(struct Window *win) { u8 c = win->text[win->textIndex++]; + // Handle special control characters switch (c) { - case 0xFF: + case EOS: ClipRight(win); win->state = WIN_STATE_END; return 0; - case 0xFD: + case PLACEHOLDER_BEGIN: win->state = WIN_STATE_PLACEHOLDER; return 2; - case 0xFE: + case CHAR_NEWLINE: ClipRight(win); win->state = WIN_STATE_NEWLINE; return 2; - case 0xFB: + case CHAR_PROMPT_CLEAR: DrawInitialDownArrow(win); - win->state = WIN_STATE_PARAGRAPH; + win->state = WIN_STATE_WAIT_CLEAR; return 2; - case 0xFA: + case CHAR_PROMPT_SCROLL: DrawInitialDownArrow(win); - win->state = WIN_STATE_NEWLINE_WAIT; + win->state = WIN_STATE_WAIT_SCROLL; return 2; - case 0xFC: + case EXT_CTRL_CODE_BEGIN: return HandleExtCtrlCode(win); } @@ -2093,6 +2110,8 @@ static u8 InterpretText(struct Window *win) return 1; } +// Extended 0xFC control functions + static u8 HandleExtCtrlCode(struct Window *win) { return sExtCtrlCodeFuncs[win->text[win->textIndex++]](win); @@ -2105,27 +2124,27 @@ static u8 ExtCtrlCode_Nop(struct Window *win) static u8 ExtCtrlCode_ForegroundColor(struct Window *win) { - SetForegroundColor(win, win->text[win->textIndex++]); + SetWindowForegroundColor(win, win->text[win->textIndex++]); return 2; } static u8 ExtCtrlCode_BackgroundColor(struct Window *win) { - SetBackgroundColor(win, win->text[win->textIndex++]); + SetWindowBackgroundColor(win, win->text[win->textIndex++]); return 2; } static u8 ExtCtrlCode_ShadowColor(struct Window *win) { - SetShadowColor(win, win->text[win->textIndex++]); + SetWindowShadowColor(win, win->text[win->textIndex++]); return 2; } static u8 ExtCtrlCode_AllColors(struct Window *win) { - SetForegroundColor(win, win->text[win->textIndex++]); - SetBackgroundColor(win, win->text[win->textIndex++]); - SetShadowColor(win, win->text[win->textIndex++]); + SetWindowForegroundColor(win, win->text[win->textIndex++]); + SetWindowBackgroundColor(win, win->text[win->textIndex++]); + SetWindowShadowColor(win, win->text[win->textIndex++]); return 2; } @@ -2143,7 +2162,7 @@ static u8 ExtCtrlCode_Font(struct Window *win) static u8 ExtCtrlCode_DefaultFont(struct Window *win) { - win->fontNum = win->config->fontNum; + win->fontNum = win->template->fontNum; return 2; } @@ -2197,7 +2216,7 @@ static u8 ExtCtrlCode_SetCursorY(struct Window *win) static u8 ExtCtrlCode_ClearWindowTextLines(struct Window *win) { - ClearWindowTextLines(win); + Text_ClearWindow(win); return 2; } @@ -2211,7 +2230,7 @@ static u8 ExtCtrlCode_PlaySE(struct Window *win) static void DrawSpace(struct Window *win) { - if (win->textMode == 1 || (win->left + win->cursorX) & 7 || win->spacing <= 7) + if (win->textMode == TEXT_MODE_MONOSPACE || (win->left + win->cursorX) & 7 || win->spacing <= 7) { sPrintGlyphFuncs[win->textMode](win, 0); } @@ -2302,10 +2321,11 @@ static u8 ExtCtrlCode_Latin(struct Window *win) return 2; } -u8 sub_8003418(struct Window *win) +// Prints the window text without expanding any placeholders +u8 Text_PrintWindowSimple(struct Window *win) { u8 retVal = 1; - while (win->state) + while (win->state != WIN_STATE_END) { if (win->state == WIN_STATE_NEWLINE) { @@ -2315,7 +2335,7 @@ u8 sub_8003418(struct Window *win) ClipLeft(win); win->state = WIN_STATE_NORMAL; } - if (InterpretText(win) == 1) + if (PrintNextChar(win) == 1) { retVal = 0; break; @@ -2324,10 +2344,10 @@ u8 sub_8003418(struct Window *win) return retVal; } -u8 sub_8003460(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) +u8 Text_InitWindowAndPrintText(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) { - InitWindow(win, text, tileDataStartOffset, left, top); - return sub_8002F44(win); + Text_InitWindow(win, text, tileDataStartOffset, left, top); + return Text_PrintWindow8002F44(win); } u8 sub_8003490(struct Window *win, u8 c, u16 tileDataStartOffset, u8 left, u8 top) @@ -2336,48 +2356,51 @@ u8 sub_8003490(struct Window *win, u8 c, u16 tileDataStartOffset, u8 left, u8 to u8 text[2]; text[0] = c; text[1] = EOS; - InitWindow(win, text, tileDataStartOffset, left, top); - retVal = InterpretText(win); + Text_InitWindow(win, text, tileDataStartOffset, left, top); + retVal = PrintNextChar(win); ClipRight(win); return retVal; } void sub_80034D4(u8 *tileData, const u8 *text) { - sub_8004E3C(&gWindowConfig_81E6C74, tileData, text); + Text_InitWindow8004E3C(&gWindowTemplate_81E6C74, tileData, text); } u8 sub_80034EC(u8 *str) { - return GetStringWidthGivenWindowConfig((struct WindowConfig *)&gWindowConfig_81E6C74, str); + return Text_GetStringWidthFromWindowTemplate((struct WindowTemplate *)&gWindowTemplate_81E6C74, str); } u8 *sub_8003504(u8 *dest, s32 value, u8 alignAmount, u8 alignType) { - sTempWindow.config = (struct WindowConfig *)&gWindowConfig_81E6C74; - InitWindow(&sTempWindow, 0, 0, 0, 0); + sTempWindow.template = (struct WindowTemplate *)&gWindowTemplate_81E6C74; + Text_InitWindow(&sTempWindow, 0, 0, 0, 0); return AlignInt2(&sTempWindow, dest, value, alignAmount, alignType); } u8 *sub_8003558(u8 *dest, const u8 *src, u8 alignAmount, u8 alignType) { - sTempWindow.config = (struct WindowConfig *)&gWindowConfig_81E6C74; - InitWindow(&sTempWindow, src, 0, 0, 0); + sTempWindow.template = (struct WindowTemplate *)&gWindowTemplate_81E6C74; + Text_InitWindow(&sTempWindow, src, 0, 0, 0); return AlignString(&sTempWindow, dest, src, alignAmount, alignType); } -u8 sub_80035AC(struct Window *win) +// Updates the window text +// The text delay can be accelerated by holding the A button +u8 Text_UpdateWindow(struct Window *win) { - sWaitType = 0; + sWaitType = WAIT_TYPE_NORMAL; return UpdateWindowText(win); } +// Returns TRUE when all of the text has been printed. static u8 UpdateWindowText(struct Window *win) { switch (win->state) { case WIN_STATE_WAIT_BUTTON: - if (PlayerCanInterruptWait(win)) + if (PlayerCanInterruptDelay(win)) { if (gMain.newKeys & (A_BUTTON | B_BUTTON)) { @@ -2385,48 +2408,56 @@ static u8 UpdateWindowText(struct Window *win) } else { - return 0; + return FALSE; } } else { win->delayCounter--; if (win->delayCounter) - return 0; + return FALSE; } win->state = WIN_STATE_NORMAL; - return 0; - case WIN_STATE_INTERRUPTIBLE_PAUSE: - if (PlayerCanInterruptWait(win) && (gMain.heldKeys & (A_BUTTON | B_BUTTON)) && gMain.watchedKeysPressed == TRUE) + return FALSE; + case WIN_STATE_CHAR_DELAY: + // Allow the player to speed up text by holding a button + if (PlayerCanInterruptDelay(win) + && (gMain.heldKeys & (A_BUTTON | B_BUTTON)) + && gMain.watchedKeysPressed == TRUE) { win->delayCounter = 0; win->state = WIN_STATE_NORMAL; break; } + // fall through case WIN_STATE_PAUSE: + // Wait for timer to expire, then continue printing if (win->delayCounter) { win->delayCounter--; if (win->delayCounter) - return 0; + return FALSE; } - win->state = WIN_STATE_NORMAL; break; - case WIN_STATE_PARAGRAPH: - if (!WaitWithDownArrow(win)) - return 0; - ClearWindowTextLines(win); - win->state = WIN_STATE_NORMAL; - BLOCK_CROSS_JUMP - return 0; - case WIN_STATE_NEWLINE_WAIT: - if (!WaitWithDownArrow(win)) - return 0; - ScrollWindowTextLines(win); - win->state = WIN_STATE_NORMAL; - BLOCK_CROSS_JUMP - return 0; + case WIN_STATE_WAIT_CLEAR: + // Erase the text once a button is pressed + if (WaitWithDownArrow(win)) + { + Text_ClearWindow(win); + win->state = WIN_STATE_NORMAL; + asm(""); + } + return FALSE; + case WIN_STATE_WAIT_SCROLL: + // Scroll the text once a button is pressed + if (WaitWithDownArrow(win)) + { + ScrollWindowTextLines(win); + win->state = WIN_STATE_NORMAL; + asm(""); + } + return FALSE; case WIN_STATE_PLACEHOLDER: win->textIndex++; win->state = WIN_STATE_NORMAL; @@ -2434,35 +2465,35 @@ static u8 UpdateWindowText(struct Window *win) case WIN_STATE_NEWLINE: ScrollWindowTextLines(win); win->state = WIN_STATE_NORMAL; - BLOCK_CROSS_JUMP - return 0; + asm(""); + return FALSE; case WIN_STATE_BEGIN: - ClearWindowTextLines(win); + Text_ClearWindow(win); break; case WIN_STATE_WAIT_SOUND: if (IsSEPlaying()) - return 0; + return FALSE; win->state = WIN_STATE_NORMAL; break; case WIN_STATE_END: - return 1; + return TRUE; // done printing text case WIN_STATE_NORMAL: break; default: win->state = WIN_STATE_END; - return 1; + return TRUE; } - InterpretText(win); + PrintNextChar(win); switch (win->state) { case WIN_STATE_END: - return 1; + return TRUE; // done printing text case WIN_STATE_WAIT_BUTTON: - case WIN_STATE_PARAGRAPH: - case WIN_STATE_NEWLINE_WAIT: - if (PlayerCanInterruptWait(win)) + case WIN_STATE_WAIT_CLEAR: + case WIN_STATE_WAIT_SCROLL: + if (PlayerCanInterruptDelay(win)) return 0; win->delayCounter = 60; break; @@ -2471,8 +2502,9 @@ static u8 UpdateWindowText(struct Window *win) case WIN_STATE_WAIT_SOUND: break; default: - win->state = WIN_STATE_INTERRUPTIBLE_PAUSE; + win->state = WIN_STATE_CHAR_DELAY; win->delayCounter = GetTextDelay(win); + break; } return 0; @@ -2484,45 +2516,45 @@ static u8 UpdateWindowText(struct Window *win) #define SUB_800374C_LINE_LENGTH 27 #endif -u8 sub_800374C(struct Window *win) +u8 Text_UpdateWindowInBattle(struct Window *win) { u8 retVal; - sWaitType = 1; + sWaitType = WAIT_TYPE_BATTLE; sLineLength = SUB_800374C_LINE_LENGTH; retVal = UpdateWindowText(win); sLineLength = 26; - sWaitType = 0; + sWaitType = WAIT_TYPE_NORMAL; return retVal; } -u8 sub_8003778(struct Window *win) +u8 Text_UpdateWindowAutoscroll(struct Window *win) { u8 retVal; - sWaitType = 2; + sWaitType = WAIT_TYPE_AUTOSCROLL; sLineLength = 26; retVal = UpdateWindowText(win); - sWaitType = 0; + sWaitType = WAIT_TYPE_NORMAL; return retVal; } -u8 sub_80037A0(struct Window *win) +u8 Text_UpdateWindowInContest(struct Window *win) { u8 retVal; - sWaitType = 3; + sWaitType = WAIT_TYPE_CONTEST; sLineLength = 17; retVal = UpdateWindowText(win); sLineLength = 26; return retVal; } -u32 sub_80037C8(struct Window *win, u8 lineLength) +u32 Text_UpdateWindowOverrideLineLength(struct Window *win, u8 lineLength) { u8 retVal; - sWaitType = 0; + sWaitType = WAIT_TYPE_NORMAL; sLineLength = lineLength; retVal = UpdateWindowText(win); sLineLength = 26; @@ -2757,14 +2789,14 @@ static void ApplyColors_ShadowedFont(const void *src, void *dest, u8 foreground, static void SetCursorX(struct Window *win, u8 x) { - if (win->textMode == 0 && ((win->left + win->cursorX) & 7)) + if (win->textMode == TEXT_MODE_UNKNOWN0 && ((win->left + win->cursorX) & 7)) win->tileDataOffset += 2; win->cursorX = x; } static void AddToCursorX(struct Window *win, u8 deltaX) { - if (win->textMode == 0) + if (win->textMode == TEXT_MODE_UNKNOWN0) { u8 x = win->cursorX; win->cursorX += deltaX; @@ -2779,7 +2811,7 @@ static void AddToCursorX(struct Window *win, u8 deltaX) static void AddToCursorY(struct Window *win, u8 deltaY) { - if (win->textMode == 0 && ((win->left + win->cursorX) & 7)) + if (win->textMode == TEXT_MODE_UNKNOWN0 && ((win->left + win->cursorX) & 7)) win->tileDataOffset += 2; win->cursorY += deltaY; } @@ -2788,11 +2820,11 @@ static void EraseAtCursor(struct Window *win) { switch (win->textMode) { - case 0: - case 2: + case TEXT_MODE_UNKNOWN0: + case TEXT_MODE_UNKNOWN2: DrawGlyphTiles(win, 0, 8); break; - case 1: + case TEXT_MODE_MONOSPACE: sWriteGlyphTilemapFuncs[win->fontNum](win, 0); break; } @@ -2801,7 +2833,8 @@ static void EraseAtCursor(struct Window *win) static void ClipLeft(struct Window *win) { u32 pixel = win->left & 7; - if (win->textMode != 1 && pixel) + + if (win->textMode != TEXT_MODE_MONOSPACE && pixel) { const u32 *masks = sGlyphMasks[8][pixel]; u32 outsideMask = masks[0]; @@ -2834,37 +2867,45 @@ static void ClipRight(struct Window *win) { register u8 cursorX asm("r0") = win->cursorX; u8 left = win->left; - u32 pixel = (cursorX + left) & 7; - if (win->textMode != 1 && pixel) + u32 pixelX = (cursorX + left) & 7; + + if (win->textMode != TEXT_MODE_MONOSPACE && pixelX != 0) { - const u32 *masks = sGlyphMasks[8 - pixel][pixel]; + const u32 *masks = sGlyphMasks[8 - pixelX][pixelX]; u32 insideMask = masks[0]; u32 outside = (sGlyphBuffer.background & ~insideMask); - u16 tileNum = GetCursorTileNum(win, 0, 0); - u32 *buffer = (u32 *)(win->tileData + 32 * tileNum); - buffer[0] = (buffer[0] & insideMask) | outside; - buffer[1] = (buffer[1] & insideMask) | outside; - buffer[2] = (buffer[2] & insideMask) | outside; - buffer[3] = (buffer[3] & insideMask) | outside; - buffer[4] = (buffer[4] & insideMask) | outside; - buffer[5] = (buffer[5] & insideMask) | outside; - buffer[6] = (buffer[6] & insideMask) | outside; - buffer[7] = (buffer[7] & insideMask) | outside; + u16 tileNum; + u32 *tileData; + + tileNum = GetCursorTileNum(win, 0, 0); + tileData = (u32 *)(win->tileData + 32 * tileNum); + + tileData[0] = (tileData[0] & insideMask) | outside; + tileData[1] = (tileData[1] & insideMask) | outside; + tileData[2] = (tileData[2] & insideMask) | outside; + tileData[3] = (tileData[3] & insideMask) | outside; + tileData[4] = (tileData[4] & insideMask) | outside; + tileData[5] = (tileData[5] & insideMask) | outside; + tileData[6] = (tileData[6] & insideMask) | outside; + tileData[7] = (tileData[7] & insideMask) | outside; + tileNum = GetCursorTileNum(win, 0, 1); - buffer = (u32 *)(win->tileData + 32 * tileNum); - buffer[0] = (buffer[0] & insideMask) | outside; - buffer[1] = (buffer[1] & insideMask) | outside; - buffer[2] = (buffer[2] & insideMask) | outside; - buffer[3] = (buffer[3] & insideMask) | outside; - buffer[4] = (buffer[4] & insideMask) | outside; - buffer[5] = (buffer[5] & insideMask) | outside; - buffer[6] = (buffer[6] & insideMask) | outside; - buffer[7] = (buffer[7] & insideMask) | outside; + tileData = (u32 *)(win->tileData + 32 * tileNum); + + tileData[0] = (tileData[0] & insideMask) | outside; + tileData[1] = (tileData[1] & insideMask) | outside; + tileData[2] = (tileData[2] & insideMask) | outside; + tileData[3] = (tileData[3] & insideMask) | outside; + tileData[4] = (tileData[4] & insideMask) | outside; + tileData[5] = (tileData[5] & insideMask) | outside; + tileData[6] = (tileData[6] & insideMask) | outside; + tileData[7] = (tileData[7] & insideMask) | outside; + UpdateTilemap(win, 1); } } -static void InitColors(struct Window *win) +static void SetWindowDefaultColors(struct Window *win) { u32 i; @@ -2876,7 +2917,7 @@ static void InitColors(struct Window *win) sGlyphBuffer.colors[i] = i; } -static void SetBackgroundColor(struct Window *win, u8 color) +static void SetWindowBackgroundColor(struct Window *win, u8 color) { u32 val1; u32 val2; @@ -2889,13 +2930,13 @@ static void SetBackgroundColor(struct Window *win, u8 color) sGlyphBuffer.background = val3; } -static void SetShadowColor(struct Window *win, u8 color) +static void SetWindowShadowColor(struct Window *win, u8 color) { win->shadowColor = color; sGlyphBuffer.colors[14] = color; } -static void SetForegroundColor(struct Window *win, u8 color) +static void SetWindowForegroundColor(struct Window *win, u8 color) { win->foregroundColor = color; sGlyphBuffer.colors[15] = color; @@ -2903,25 +2944,25 @@ static void SetForegroundColor(struct Window *win, u8 color) static u8 GetTextDelay(struct Window *win) { - if (!PlayerCanInterruptWait(win)) + if (!PlayerCanInterruptDelay(win)) return 3; return sTextSpeedDelays[gSaveBlock2.optionsTextSpeed]; } -static bool8 PlayerCanInterruptWait(struct Window *win) +static bool8 PlayerCanInterruptDelay(struct Window *win) { bool8 retVal = TRUE; switch (sWaitType) { - case 2: + case WAIT_TYPE_AUTOSCROLL: retVal = FALSE; break; - case 3: + case WAIT_TYPE_CONTEST: retVal = gIsLinkContest ? FALSE : TRUE; break; - case 1: + case WAIT_TYPE_BATTLE: retVal = (gBattleTypeFlags & BATTLE_TYPE_LINK) ? FALSE : TRUE; break; } @@ -2933,13 +2974,13 @@ static void ScrollWindowTextLines(struct Window *win) { switch (win->textMode) { - case 0: + case TEXT_MODE_UNKNOWN0: ScrollWindowTextLines_TextMode0(win); break; - case 1: - ScrollWindowTextLines_TextMode1(win); + case TEXT_MODE_MONOSPACE: + ScrollWindowTextLines_TextModeMonospace(win); break; - case 2: + case TEXT_MODE_UNKNOWN2: ScrollWindowTextLines_TextMode2(win); break; } @@ -2949,6 +2990,7 @@ static void ScrollWindowTextLines_TextMode0(struct Window *win) { if (win->cursorY == 0) { + // Advance to the next line win->tileDataOffset = 2 * sLineLength + 2; win->cursorX = 0; win->cursorY += 16; @@ -2959,7 +3001,7 @@ static void ScrollWindowTextLines_TextMode0(struct Window *win) win->tileDataOffset = 2 * sLineLength + 2; else win->tileDataOffset = 2; - win->win_field_C = win->win_field_C ^ 2; + win->win_field_C ^= 2; win->cursorX = 0; DoScroll_TextMode0(win, sLineLength); } @@ -2973,16 +3015,21 @@ static void DoScroll_TextMode0(struct Window *win, u16 lineLength) u16 fill; buffer += val1 + val2; fill = (win->paletteNum << 12) | GetBlankTileNum(win); + + // Move the bottom line up CpuCopy16(buffer + 64, buffer, lineLength * 2); CpuCopy16(buffer + 96, buffer + 32, lineLength * 2); + + // Clear the bottom line CpuFill16(fill, buffer + 64, lineLength * 2); CpuFill16(fill, buffer + 96, lineLength * 2); } -static void ScrollWindowTextLines_TextMode1(struct Window *win) +static void ScrollWindowTextLines_TextModeMonospace(struct Window *win) { if (win->cursorY == 0) { + // Advance to the next line win->cursorX = 0; win->cursorY += 16; } @@ -2990,17 +3037,21 @@ static void ScrollWindowTextLines_TextMode1(struct Window *win) { win->win_field_C ^= 2; win->cursorX = 0; - DoScroll_TextMode1(win, sLineLength); + DoScroll_TextModeMonospace(win, sLineLength); } } -static void DoScroll_TextMode1(struct Window *win, u16 lineLength) +static void DoScroll_TextModeMonospace(struct Window *win, u16 lineLength) { u16 *buffer = GetCursorTilemapPointer(win); u16 *dest = buffer - 32; u16 fill = (win->paletteNum << 12) | GetBlankTileNum(win); + + // Move the bottom line up CpuCopy16(buffer + 32, dest, lineLength * 2); CpuCopy16(buffer + 64, buffer, lineLength * 2); + + // Clear the bottom line CpuFill16(fill, buffer + 32, lineLength * 2); CpuFill16(fill, buffer + 64, lineLength * 2); } @@ -3009,6 +3060,7 @@ static void ScrollWindowTextLines_TextMode2(struct Window *win) { if (win->cursorY == 0) { + // Advance to the next line win->cursorX = 0; win->cursorY += 16; } @@ -3055,18 +3107,18 @@ static void DoScroll_TextMode2(struct Window *win, u8 lineLength) } } -void ClearWindowTextLines(struct Window *win) +void Text_ClearWindow(struct Window *win) { switch (win->textMode) { - case 0: + case TEXT_MODE_UNKNOWN0: ClearWindowTextLines_TextMode0_TextMode1(win, sLineLength); win->tileDataOffset = 2; break; - case 1: + case TEXT_MODE_MONOSPACE: ClearWindowTextLines_TextMode0_TextMode1(win, sLineLength); break; - case 2: + case TEXT_MODE_UNKNOWN2: ClearWindowTextLines_TextMode2(win, sLineLength); break; } @@ -3074,7 +3126,7 @@ void ClearWindowTextLines(struct Window *win) static void ClearWindowTextLines_TextMode0_TextMode1(struct Window *win, u8 lineLength) { - u8 i; + u8 x, y; u16 *buffer; u16 fill; @@ -3085,11 +3137,10 @@ static void ClearWindowTextLines_TextMode0_TextMode1(struct Window *win, u8 line buffer = GetCursorTilemapPointer(win); fill = GetBlankTileNum(win) | (win->paletteNum << 12); - for (i = 0; i < 4; i++) + for (y = 0; y < 4; y++) { - u8 j; - for (j = 0; j < lineLength; j++) - buffer[j] = fill; + for (x = 0; x < lineLength; x++) + buffer[x] = fill; buffer += 32; } } @@ -3114,13 +3165,13 @@ static void ClearWindowTextLines_TextMode2(struct Window *win, u8 lineLength) static void DrawDownArrow(struct Window *win) { - if (PlayerCanInterruptWait(win)) + if (PlayerCanInterruptDelay(win)) { const u32 *downArrowTiles = &sDownArrowTiles[((win->downArrowCounter & 0x0F00) >> 8) * 16]; switch (win->textMode) { - case 1: + case TEXT_MODE_MONOSPACE: { u8 *buffer; u16 tileNum = win->tileDataStartOffset + 254; @@ -3132,8 +3183,8 @@ static void DrawDownArrow(struct Window *win) WriteGlyphTilemap(win, tileNum, tileNum + 1); break; } - case 0: - case 2: + case TEXT_MODE_UNKNOWN0: + case TEXT_MODE_UNKNOWN2: { struct GlyphTileInfo glyphTileInfo; glyphTileInfo.textMode = win->textMode; @@ -3172,9 +3223,9 @@ static void DrawDownArrow(struct Window *win) static u8 WaitWithDownArrow(struct Window *win) { - u8 retVal = 1; + u8 retVal = TRUE; - if (!PlayerCanInterruptWait(win)) + if (!PlayerCanInterruptDelay(win)) { win->delayCounter--; if (!win->delayCounter) @@ -3183,8 +3234,8 @@ static u8 WaitWithDownArrow(struct Window *win) } else { - DrawMovingDownArrow(win); - retVal = 0; + UpdateDownArrowAnimation(win); + retVal = FALSE; } } else @@ -3196,8 +3247,8 @@ static u8 WaitWithDownArrow(struct Window *win) } else { - DrawMovingDownArrow(win); - retVal = 0; + UpdateDownArrowAnimation(win); + retVal = FALSE; } } @@ -3210,7 +3261,7 @@ static void DrawInitialDownArrow(struct Window *win) DrawDownArrow(win); } -static void DrawMovingDownArrow(struct Window *win) +static void UpdateDownArrowAnimation(struct Window *win) { u16 downArrowPos = (win->downArrowCounter & 0x0F00) >> 8; u16 wait = win->downArrowCounter & 0x000F; @@ -3237,31 +3288,37 @@ static void DrawMovingDownArrow(struct Window *win) static void TryEraseDownArrow(struct Window *win) { win->downArrowCounter = 0; - if (PlayerCanInterruptWait(win) == TRUE) + if (PlayerCanInterruptDelay(win) == TRUE) EraseAtCursor(win); } -u16 GetWindowTilemapEntry(struct Window *win, u8 x, u8 y) +// unused +u16 Text_GetWindowTilemapEntry(struct Window *win, u8 x, u8 y) { u16 *tilemap = win->tilemap; return tilemap[32 * y + x]; } -void DrawWindowRect(struct Window *win, u16 tilemapEntry, u8 left, u8 top, u8 right, u8 bottom) +// unused +void Text_FillWindowBorder(struct Window *win, u16 tilemapEntry, u8 left, u8 top, u8 right, u8 bottom) { u8 i; u16 *buffer = &win->tilemap[top * 32]; + // Fill top border for (i = left; i <= right; i++) buffer[i] = tilemapEntry; for (i = top + 1; i < bottom - 1; i++) { buffer += 32; + // left border buffer[left] = tilemapEntry; + // right border buffer[right] = tilemapEntry; } + // Fill bottom border if (top != bottom) { buffer += 32; @@ -3270,37 +3327,39 @@ void DrawWindowRect(struct Window *win, u16 tilemapEntry, u8 left, u8 top, u8 ri } } +// unused void DrawWindowRect_DefaultPalette(struct Window *win, u16 tileNum, u8 left, u8 top, u8 right, u8 bottom) { - DrawWindowRect(win, (win->paletteNum << 12) | tileNum, left, top, right, bottom); + Text_FillWindowBorder(win, (win->paletteNum << 12) | tileNum, left, top, right, bottom); } -void FillWindowRect(struct Window *win, u16 tilemapEntry, u8 left, u8 top, u8 right, u8 bottom) +// Fills the whole window area with tilemapEntry +void Text_FillWindowRect(struct Window *win, u16 tilemapEntry, u8 left, u8 top, u8 right, u8 bottom) { u16 *buffer = &win->tilemap[top * 32]; while (top++ <= bottom) { - u8 j; - for (j = left; j <= right; j++) - buffer[j] = tilemapEntry; + u8 x; + for (x = left; x <= right; x++) + buffer[x] = tilemapEntry; buffer += 32; } } -void FillWindowRect_DefaultPalette(struct Window *win, u16 tileNum, u8 left, u8 top, u8 right, u8 bottom) +void Text_FillWindowRectDefPalette(struct Window *win, u16 tileNum, u8 left, u8 top, u8 right, u8 bottom) { - FillWindowRect(win, (win->paletteNum << 12) | tileNum, left, top, right, bottom); + Text_FillWindowRect(win, (win->paletteNum << 12) | tileNum, left, top, right, bottom); } -void ZeroFillWindowRect(struct Window *win, u8 left, u8 top, u8 right, u8 bottom) +void Text_EraseWindowRect(struct Window *win, u8 left, u8 top, u8 right, u8 bottom) { - FillWindowRect_DefaultPalette(win, 0, left, top, right, bottom); + Text_FillWindowRectDefPalette(win, 0, left, top, right, bottom); } -void FillWindowRectWithBlankTile(struct Window *win, u8 left, u8 top, u8 right, u8 bottom) +void Text_BlankWindowRect(struct Window *win, u8 left, u8 top, u8 right, u8 bottom) { u16 tileNum = GetBlankTileNum(win); - FillWindowRect_DefaultPalette(win, tileNum, left, top, right, bottom); + Text_FillWindowRectDefPalette(win, tileNum, left, top, right, bottom); } static u16 GetBlankTileNum(struct Window *win) @@ -3309,12 +3368,12 @@ static u16 GetBlankTileNum(struct Window *win) switch (win->textMode) { - case 0: + case TEXT_MODE_UNKNOWN0: break; - case 2: + case TEXT_MODE_UNKNOWN2: retVal++; break; - case 1: + case TEXT_MODE_MONOSPACE: switch (win->fontNum) { case 1: @@ -3451,7 +3510,7 @@ u8 *AlignInt2(struct Window *win, u8 *dest, s32 value, u8 alignAmount, u8 alignT ConvertIntToDecimalString(temp, value); width = GetStringWidth(win, temp); dest = StringCopy(dest, temp); - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 17; dest[2] = alignAmount - width; dest += 3; @@ -3462,7 +3521,7 @@ u8 *AlignInt2(struct Window *win, u8 *dest, s32 value, u8 alignAmount, u8 alignT width = GetStringWidth(win, temp); if (alignAmount > width) { - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 17; dest[2] = alignAmount - width; dest += 3; @@ -3474,7 +3533,7 @@ u8 *AlignInt2(struct Window *win, u8 *dest, s32 value, u8 alignAmount, u8 alignT width = GetStringWidth(win, temp); if (alignAmount > width) { - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 17; dest[2] = (alignAmount - width) / 2; dest += 3; @@ -3482,7 +3541,7 @@ u8 *AlignInt2(struct Window *win, u8 *dest, s32 value, u8 alignAmount, u8 alignT dest = StringCopy(dest, temp); if (alignAmount > width) { - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 17; dest[2] = (alignAmount - width) / 2; dest += 3; @@ -3500,7 +3559,7 @@ u8 *AlignString(struct Window *win, u8 *dest, const u8 *src, u8 alignAmount, u8 { case 0: dest = StringCopy(dest, src); - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 19; dest[2] = alignAmount; dest += 3; @@ -3510,7 +3569,7 @@ u8 *AlignString(struct Window *win, u8 *dest, const u8 *src, u8 alignAmount, u8 width = GetStringWidth(win, src); if (alignAmount > width) { - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 19; dest[2] = alignAmount - width; dest += 3; @@ -3521,7 +3580,7 @@ u8 *AlignString(struct Window *win, u8 *dest, const u8 *src, u8 alignAmount, u8 width = GetStringWidth(win, src); if (alignAmount > width) { - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 19; dest[2] = (alignAmount - width) / 2; dest += 3; @@ -3529,7 +3588,7 @@ u8 *AlignString(struct Window *win, u8 *dest, const u8 *src, u8 alignAmount, u8 dest = StringCopy(dest, src); if (alignAmount > width) { - dest[0] = 0xFC; + dest[0] = EXT_CTRL_CODE_BEGIN; dest[1] = 19; dest[2] = alignAmount; dest += 3; @@ -3544,16 +3603,16 @@ u8 GetStringWidth(struct Window *win, const u8 *s) { u8 width = 0; u8 savedFontNum = win->fontNum; - u8 savedCharset = win->language; + u8 savedLanguage = win->language; u8 savedSpacing = win->spacing; s32 i = 0; - while (s[i] != 0xFF) + while (s[i] != EOS) { u8 c = s[i]; switch (c) { - case 0xFD: + case PLACEHOLDER_BEGIN: { u8 temp; i++; @@ -3563,7 +3622,7 @@ u8 GetStringWidth(struct Window *win, const u8 *s) i++; break; } - case 0xFC: + case EXT_CTRL_CODE_BEGIN: i++; switch (s[i]) { @@ -3571,7 +3630,7 @@ u8 GetStringWidth(struct Window *win, const u8 *s) win->fontNum = s[i + 1]; break; case 7: - win->fontNum = win->config->fontNum; + win->fontNum = win->template->fontNum; break; case 0x11: width += s[i + 1]; @@ -3601,64 +3660,64 @@ u8 GetStringWidth(struct Window *win, const u8 *s) } win->spacing = savedSpacing; - win->language = savedCharset; + win->language = savedLanguage; win->fontNum = savedFontNum; return width; } -u8 sub_8004D04(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u16 top, u32 a6) +u8 Text_InitWindow8004D04(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u16 top, u32 a6) { - sub_8002E4C(win, text, tileDataStartOffset, left, top, a6); - return sub_8002F44(win); + Text_InitWindow8002E4C(win, text, tileDataStartOffset, left, top, a6); + return Text_PrintWindow8002F44(win); } -u8 sub_8004D38(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) +u8 Text_InitWindow8004D38(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top) { u8 width = GetStringWidth(win, text); - InitWindow(win, text, tileDataStartOffset, left - ((u32)(width + 7) >> 3), top); + Text_InitWindow(win, text, tileDataStartOffset, left - ((u32)(width + 7) >> 3), top); EraseAtCursor(win); width &= 7; if (width) width = 8 - width; sub_80048D8(win, width, 0); - return sub_8002F44(win); + return Text_PrintWindow8002F44(win); } -u8 sub_8004DB0(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top, u16 a6) +u8 Text_InitWindow8004DB0(struct Window *win, const u8 *text, u16 tileDataStartOffset, u8 left, u8 top, u16 a6) { register u32 val asm("r5") = (u8)((a6 >> 1) - (GetStringWidth(win, text) >> 1)); left += (val >> 3); - InitWindow(win, text, tileDataStartOffset, left, top); + Text_InitWindow(win, text, tileDataStartOffset, left, top); EraseAtCursor(win); sub_80048D8(win, val & 7, 0); - return sub_8002F44(win); + return Text_PrintWindow8002F44(win); } -u8 sub_8004E24(struct Window *win) +u8 Text_GetWindowPaletteNum(struct Window *win) { return win->paletteNum; } -void sub_8004E28(struct Window *win, u8 *foreground, u8 *background, u8 *shadow) +void Text_GetTextColors(struct Window *win, u8 *foreground, u8 *background, u8 *shadow) { *foreground = win->foregroundColor; *background = win->backgroundColor; *shadow = win->shadowColor; } -void sub_8004E3C(const struct WindowConfig *winConfig, u8 *tileData, const u8 *text) +void Text_InitWindow8004E3C(const struct WindowTemplate *winTemplate, u8 *tileData, const u8 *text) { - sTempWindow.config = winConfig; - InitWindow(&sTempWindow, text, 0, 0, 0); + sTempWindow.template = winTemplate; + Text_InitWindow(&sTempWindow, text, 0, 0, 0); sTempWindow.tileData = tileData; - sub_8002F44(&sTempWindow); + Text_PrintWindow8002F44(&sTempWindow); } -u8 GetStringWidthGivenWindowConfig(const struct WindowConfig *winConfig, const u8 *s) +u8 Text_GetStringWidthFromWindowTemplate(const struct WindowTemplate *winTemplate, const u8 *s) { - sTempWindow.config = winConfig; - InitWindow(&sTempWindow, s, 0, 0, 0); + sTempWindow.template = winTemplate; + Text_InitWindow(&sTempWindow, s, 0, 0, 0); return GetStringWidth(&sTempWindow, s); } @@ -3668,11 +3727,11 @@ void ConvertInternationalString(u8 *s, u8 language) { u8 i; - StripExtCtrlCodes(s); + Text_StripExtCtrlCodes(s); i = StringLength(s); - s[i++] = 0xFC; + s[i++] = EXT_CTRL_CODE_BEGIN; s[i++] = 22; - s[i++] = 0xFF; + s[i++] = EOS; i--; @@ -3682,18 +3741,18 @@ void ConvertInternationalString(u8 *s, u8 language) i--; } - s[0] = 0xFC; + s[0] = EXT_CTRL_CODE_BEGIN; s[1] = 21; } } -void StripExtCtrlCodes(u8 *str) +void Text_StripExtCtrlCodes(u8 *str) { u16 srcIndex = 0; u16 destIndex = 0; - while (str[srcIndex] != 0xFF) + while (str[srcIndex] != EOS) { - if (str[srcIndex] == 0xFC) + if (str[srcIndex] == EXT_CTRL_CODE_BEGIN) { srcIndex++; srcIndex += GetExtCtrlCodeLength(str[srcIndex]); @@ -3703,12 +3762,12 @@ void StripExtCtrlCodes(u8 *str) str[destIndex++] = str[srcIndex++]; } } - str[destIndex] = 0xFF; + str[destIndex] = EOS; } static const u8 *SkipExtCtrlCode(const u8 *s) { - while (*s == 0xFC) + while (*s == EXT_CTRL_CODE_BEGIN) { s++; s += GetExtCtrlCodeLength(*s); @@ -3801,8 +3860,8 @@ u8 sub_8004FD0(struct Window *win, u8 *dest, const u8 *src, u16 tileDataStartOff if (endsWithoutNewline) newlineCount++; - sub_8002E4C(win, start, tileDataStartOffset, left, top, a8); - sub_8002F44(win); + Text_InitWindow8002E4C(win, start, tileDataStartOffset, left, top, a8); + Text_PrintWindow8002F44(win); return newlineCount; } @@ -3827,7 +3886,7 @@ static s32 DrawGlyphTile_UnshadowedFont(struct GlyphTileInfo *glyphTileInfo) if (glyphTileInfo->startPixel + glyphTileInfo->width > 8) { u32 mask2 = masks[1]; - if (glyphTileInfo->textMode == 2) + if (glyphTileInfo->textMode == TEXT_MODE_UNKNOWN2) { glyphBuffer->pixelRows[8] = buffer[8] & mask2; glyphBuffer->pixelRows[9] = buffer[9] & mask2; @@ -3867,7 +3926,7 @@ static s32 DrawGlyphTile_UnshadowedFont(struct GlyphTileInfo *glyphTileInfo) if (glyphTileInfo->startPixel + glyphTileInfo->width > 8) { - if (glyphTileInfo->textMode != 2) + if (glyphTileInfo->textMode != TEXT_MODE_UNKNOWN2) buffer += 8; buffer[8] = glyphBuffer->pixelRows[8]; buffer[9] = glyphBuffer->pixelRows[9]; @@ -4039,7 +4098,7 @@ static s32 DrawGlyphTile_ShadowedFont(struct GlyphTileInfo *glyphTileInfo) if (glyphTileInfo->startPixel + glyphTileInfo->width > 8) { u32 mask2 = masks[1]; - if (glyphTileInfo->textMode == 2) + if (glyphTileInfo->textMode == TEXT_MODE_UNKNOWN2) { glyphBuffer->pixelRows[8] = buffer[8] & mask2; glyphBuffer->pixelRows[9] = buffer[9] & mask2; @@ -4076,7 +4135,7 @@ static s32 DrawGlyphTile_ShadowedFont(struct GlyphTileInfo *glyphTileInfo) if (glyphTileInfo->startPixel + glyphTileInfo->width > 8) { - if (glyphTileInfo->textMode != 2) + if (glyphTileInfo->textMode != TEXT_MODE_UNKNOWN2) buffer += 8; buffer[8] = glyphBuffer->pixelRows[8]; buffer[9] = glyphBuffer->pixelRows[9]; @@ -4314,7 +4373,7 @@ static u16 GetCursorTileNum(struct Window *win, u32 xOffset, u32 yOffset) { u16 index; - if (win->textMode == 2) + if (win->textMode == TEXT_MODE_UNKNOWN2) index = win->tileDataStartOffset + win->tileDataOffset + (((win->top + win->cursorY) >> 3) + yOffset) * win->width diff --git a/src/engine/text_window.c b/src/engine/text_window.c index c001ddefc..ade349f4e 100644 --- a/src/engine/text_window.c +++ b/src/engine/text_window.c @@ -4,19 +4,22 @@ #include "palette.h" #include "text.h" -#define STD_MSG_BOX_LEFT 0 -#define STD_MSG_BOX_TOP 14 -#define STD_MSG_BOX_WIDTH 26 -#define STD_MSG_BOX_HEIGHT 4 +#define STD_WINDOW_PALETTE_NUM 14 + +// Dimensions (in tiles) of a field dialogue frame +#define STD_DLG_FRAME_LEFT 0 +#define STD_DLG_FRAME_TOP 14 +#define STD_DLG_FRAME_WIDTH 26 +#define STD_DLG_FRAME_HEIGHT 4 static void LoadTextWindowTiles(u8, void *); static void LoadTextWindowPalette(u8, u8); -static void DrawTextWindowInternal(u16 *dest, u16 baseTileNum, u8 left, u8 top, u8 right, u8 bottom); -static u16 GetMessageBoxTilemapEntry(u16 tilemapEntry, u8 x, u8 y, u8 width, u8 height); -static void DrawMessageBox(struct Window *win, u8 left, u8 top, u8 width, u8 height); +static void DrawStandardFrame(u16 *dest, u16 baseTileNum, u8 left, u8 top, u8 right, u8 bottom); +static u16 GetDialogueFrameTilemapEntry(u16 tilemapEntry, u8 x, u8 y, u8 width, u8 height); +static void DrawDialogueFrame(struct Window *win, u8 left, u8 top, u8 width, u8 height); static u16 sTextWindowBaseTileNum; -static u16 sMessageBoxBaseTileNum; +static u16 sDialogueFrameBaseTileNum; extern const u8 gTextWindowFrame1_Gfx[]; extern const u8 gTextWindowFrame2_Gfx[]; @@ -60,9 +63,9 @@ extern const u16 gTextWindowFrame18_Pal[]; extern const u16 gTextWindowFrame19_Pal[]; extern const u16 gTextWindowFrame20_Pal[]; -extern const u8 gMessageBox_Gfx[]; +extern const u8 gDialogueFrame_Gfx[]; -static const struct FrameGraphics gUnknown_083761F0[20] = +static const struct FrameGraphics sTextWindowFrameGraphics[20] = { {gTextWindowFrame1_Gfx, gTextWindowFrame1_Pal}, {gTextWindowFrame2_Gfx, gTextWindowFrame2_Pal}, @@ -86,7 +89,7 @@ static const struct FrameGraphics gUnknown_083761F0[20] = {gTextWindowFrame20_Gfx, gTextWindowFrame20_Pal}, }; -static const u16 gMessageBoxTilemap[5][7] = +static const u16 sDialogueFrameTilemap[5][7] = { {1, 3, 4, 4, 5, 6, 9}, {11, 9, 9, 9, 9, 0x040B, 9}, @@ -95,113 +98,124 @@ static const u16 gMessageBoxTilemap[5][7] = {0x0801, 0x0803, 0x0804, 0x0804, 0x0805, 0x0806, 9}, }; -u16 SetTextWindowBaseTileNum(u16 baseTileNum) +u16 TextWindow_SetBaseTileNum(u16 baseTileNum) { sTextWindowBaseTileNum = baseTileNum; return baseTileNum + 9; } -void LoadTextWindowGraphics(struct Window *win) +// Loads the tiles and palette of the window frame into VRAM using the selected frame type +void TextWindow_LoadStdFrameGraphics(struct Window *win) { - u8 *tileData = win->config->tileData + TILE_SIZE_4BPP * sTextWindowBaseTileNum; + u8 *tileData = win->template->tileData + TILE_SIZE_4BPP * sTextWindowBaseTileNum; LoadTextWindowTiles(gSaveBlock2.optionsWindowFrameType, tileData); - LoadTextWindowPalette(gSaveBlock2.optionsWindowFrameType, 0xE); + LoadTextWindowPalette(gSaveBlock2.optionsWindowFrameType, STD_WINDOW_PALETTE_NUM); } -void LoadTextWindowGraphics_OverridePalSlot(struct Window *win, u8 palSlot) +// Loads the tiles and palette of the window frame into VRAM with an alternate palette +void TextWindow_LoadStdFrameGraphicsOverridePal(struct Window *win, u8 palSlot) { - u8 *tileData = win->config->tileData + TILE_SIZE_4BPP * sTextWindowBaseTileNum; + u8 *tileData = win->template->tileData + TILE_SIZE_4BPP * sTextWindowBaseTileNum; LoadTextWindowTiles(gSaveBlock2.optionsWindowFrameType, tileData); LoadTextWindowPalette(gSaveBlock2.optionsWindowFrameType, palSlot); } -void LoadTextWindowGraphics_OverrideFrameType(struct Window *win, u8 frameType) +// Loads the tiles and palette of the window frame into VRAM with an alternate frame type +void TextWindow_LoadStdFrameGraphicsOverrideStyle(struct Window *win, u8 frameType) { - u8 *tileData = win->config->tileData + TILE_SIZE_4BPP * sTextWindowBaseTileNum; + u8 *tileData = win->template->tileData + TILE_SIZE_4BPP * sTextWindowBaseTileNum; LoadTextWindowTiles(frameType, tileData); - LoadTextWindowPalette(frameType, 0xE); + LoadTextWindowPalette(frameType, STD_WINDOW_PALETTE_NUM); } -void DrawTextWindow(struct Window *win, u8 left, u8 top, u8 right, u8 bottom) +void TextWindow_DrawStdFrame(struct Window *win, u8 left, u8 top, u8 right, u8 bottom) { - DrawTextWindowInternal(win->config->tilemap, sTextWindowBaseTileNum, left, top, right, bottom); + DrawStandardFrame(win->template->tilemap, sTextWindowBaseTileNum, left, top, right, bottom); } -const struct FrameGraphics *GetTextWindowFrameGraphics(u8 frameType) +// Returns the tile data and palette for the specified frame type +const struct FrameGraphics *TextWindow_GetFrameGraphics(u8 frameType) { if (frameType > 19) - return &gUnknown_083761F0[0]; + return &sTextWindowFrameGraphics[0]; else - return &gUnknown_083761F0[frameType]; + return &sTextWindowFrameGraphics[frameType]; } static void LoadTextWindowTiles(u8 frameType, void *dest) { - const struct FrameGraphics *frameGraphics = GetTextWindowFrameGraphics(frameType); + const struct FrameGraphics *frameGraphics = TextWindow_GetFrameGraphics(frameType); CpuFastCopy(frameGraphics->tiles, dest, 9 * TILE_SIZE_4BPP); } static void LoadTextWindowPalette(u8 frameType, u8 palSlot) { - const struct FrameGraphics *frameGraphics = GetTextWindowFrameGraphics(frameType); + const struct FrameGraphics *frameGraphics = TextWindow_GetFrameGraphics(frameType); LoadPalette(frameGraphics->palette, 16 * palSlot, 0x20); } -static void DrawTextWindowInternal(u16 *dest, u16 baseTileNum, u8 left, u8 top, u8 right, u8 bottom) +// Draws a standard window frame +static void DrawStandardFrame(u16 *tilemap, u16 baseTileNum, u8 left, u8 top, u8 right, u8 bottom) { + u8 startX = min(left, right); + u8 endX = max(left, right); + u8 startY = min(top, bottom); + u8 endY = max(top, bottom); u8 x, y; - u8 startX, endX; - u8 startY, endY; - - startX = (left < right) ? left : right; - endX = (right > left) ? right : left; - - startY = (top < bottom) ? top : bottom; - endY = (bottom > top) ? bottom : top; - dest[32 * startY + startX] = baseTileNum | 0xE000; + // top left corner + tilemap[32 * startY + startX] = baseTileNum | (STD_WINDOW_PALETTE_NUM << 12); + // top border for (x = startX + 1; x < endX; x++) - dest[32 * startY + x] = (baseTileNum + 1) | 0xE000; + tilemap[32 * startY + x] = (baseTileNum + 1) | (STD_WINDOW_PALETTE_NUM << 12); - dest[32 * startY + endX] = (baseTileNum + 2) | 0xE000; + // top right corner + tilemap[32 * startY + endX] = (baseTileNum + 2) | (STD_WINDOW_PALETTE_NUM << 12); for (y = startY + 1; y < endY; y++) { - dest[32 * y + startX] = (baseTileNum + 3) | 0xE000; + // left border + tilemap[32 * y + startX] = (baseTileNum + 3) | (STD_WINDOW_PALETTE_NUM << 12); + // middle for (x = startX + 1; x < endX; x++) - dest[32 * y + x] = (baseTileNum + 4) | 0xE000; + tilemap[32 * y + x] = (baseTileNum + 4) | (STD_WINDOW_PALETTE_NUM << 12); - dest[32 * y + endX] = (baseTileNum + 5) | 0xE000; + // right border + tilemap[32 * y + endX] = (baseTileNum + 5) | (STD_WINDOW_PALETTE_NUM << 12); } - dest[32 * endY + startX] = (baseTileNum + 6) | 0xE000; + // bottom left corner + tilemap[32 * endY + startX] = (baseTileNum + 6) | (STD_WINDOW_PALETTE_NUM << 12); + // bottom border for (x = startX + 1; x < endX; x++) - dest[32 * endY + x] = (baseTileNum + 7) | 0xE000; + tilemap[32 * endY + x] = (baseTileNum + 7) | (STD_WINDOW_PALETTE_NUM << 12); - dest[32 * endY + endX] = (baseTileNum + 8) | 0xE000; + // bottom right corner + tilemap[32 * endY + endX] = (baseTileNum + 8) | (STD_WINDOW_PALETTE_NUM << 12); } -u16 SetMessageBoxBaseTileNum(u16 baseTileNum) +u16 TextWindow_SetDlgFrameBaseTileNum(u16 baseTileNum) { - sMessageBoxBaseTileNum = baseTileNum; + sDialogueFrameBaseTileNum = baseTileNum; return baseTileNum + 14; } void unref_sub_80651DC(struct Window *win, u8 *text) { - sub_8002EB0(win, text, sMessageBoxBaseTileNum + 14, 2, 15); + Text_InitWindow8002EB0(win, text, sDialogueFrameBaseTileNum + 14, 2, 15); } -void DisplayMessageBox(struct Window *win) +// Loads and draws a dialogue window frame +void TextWindow_DisplayDialogueFrame(struct Window *win) { - LoadMessageBoxTiles(win); - DrawStandardMessageBox(win); + TextWindow_LoadDialogueFrameTiles(win); + TextWindow_DrawDialogueFrame(win); } -static u16 GetMessageBoxTilemapEntry(u16 baseTilemapEntry, u8 x, u8 y, u8 width, u8 height) +static u16 GetDialogueFrameTilemapEntry(u16 baseTilemapEntry, u8 x, u8 y, u8 width, u8 height) { u16 tilemapEntry = 9; @@ -215,42 +229,47 @@ static u16 GetMessageBoxTilemapEntry(u16 baseTilemapEntry, u8 x, u8 y, u8 width, else if (x > 2) x = 3; - if (x <= 6 && y <= 4) - tilemapEntry = gMessageBoxTilemap[y][x]; + if (x < 7 && y < 5) + tilemapEntry = sDialogueFrameTilemap[y][x]; tilemapEntry += baseTilemapEntry; return tilemapEntry; } -static void DrawMessageBox(struct Window *win, u8 left, u8 top, u8 width, u8 height) +static void DrawDialogueFrame(struct Window *win, u8 left, u8 top, u8 width, u8 height) { - u8 i, j; - u16 tilemapEntry = (win->paletteNum << 12) | sMessageBoxBaseTileNum; - u16 *tilemap = win->config->tilemap; + u8 x, y; + u16 baseTilemapEntry = (win->paletteNum << 12) | sDialogueFrameBaseTileNum; + u16 *tilemap = win->template->tilemap; - for (i = 0; i < height + 2; i++) - for (j = 0; j < width + 6; j++) - tilemap[(left + j) + 32 * (top + i)] = (win->paletteNum << 12) | GetMessageBoxTilemapEntry(tilemapEntry, j, i, width, height); + for (y = 0; y < height + 2; y++) + { + for (x = 0; x < width + 6; x++) + tilemap[(left + x) + 32 * (top + y)] = (win->paletteNum << 12) | GetDialogueFrameTilemapEntry(baseTilemapEntry, x, y, width, height); + } } -void DrawStandardMessageBox(struct Window *win) +// Draws an alternate styled frame used for dialogue windows that appear on the overworld +void TextWindow_DrawDialogueFrame(struct Window *win) { - DrawMessageBox(win, STD_MSG_BOX_LEFT, STD_MSG_BOX_TOP, STD_MSG_BOX_WIDTH, STD_MSG_BOX_HEIGHT); + DrawDialogueFrame(win, STD_DLG_FRAME_LEFT, STD_DLG_FRAME_TOP, STD_DLG_FRAME_WIDTH, STD_DLG_FRAME_HEIGHT); } -void LoadMessageBoxTiles(struct Window *win) +// Loads the dialogue window frame tiles into VRAM +void TextWindow_LoadDialogueFrameTiles(struct Window *win) { - u8 *tileData = win->config->tileData; - CpuFastCopy(gMessageBox_Gfx, tileData + 32 * sMessageBoxBaseTileNum, 14 * TILE_SIZE_4BPP); + u8 *tileData = win->template->tileData; + CpuFastCopy(gDialogueFrame_Gfx, tileData + 32 * sDialogueFrameBaseTileNum, 14 * TILE_SIZE_4BPP); } -void ClearStandardMessageBox(struct Window *win) +// Erases a dialogue window frame +void TextWindow_EraseDialogueFrame(struct Window *win) { u8 i; - u16 *tilemap = win->config->tilemap + (STD_MSG_BOX_TOP * 32); + u16 *tilemap = win->template->tilemap + (STD_DLG_FRAME_TOP * 32); u16 tilemapEntry = win->paletteNum << 12; - for (i = 0; i < ((STD_MSG_BOX_HEIGHT + 2) * 32); i++) + for (i = 0; i < ((STD_DLG_FRAME_HEIGHT + 2) * 32); i++) tilemap[i] = tilemapEntry; } diff --git a/src/engine/time_events.c b/src/engine/time_events.c index 3e3054aa3..4bd732788 100644 --- a/src/engine/time_events.c +++ b/src/engine/time_events.c @@ -93,7 +93,7 @@ void UpdateShoalTideFlag(void) static void Task_WaitWeather(u8 taskId) { - if (sub_807DDFC()) + if (IsWeatherChangeComplete()) { EnableBothScriptContexts(); DestroyTask(taskId); diff --git a/src/engine/trade.c b/src/engine/trade.c index 0bc589ff4..36e6fc757 100644 --- a/src/engine/trade.c +++ b/src/engine/trade.c @@ -141,7 +141,7 @@ struct TradeEwramStruct { }; static void sub_8047EC0(void); -static void sub_804AFB8(const struct WindowConfig *, u8 *, const u8 *, u8); +static void sub_804AFB8(const struct WindowTemplate *, u8 *, const u8 *, u8); static void sub_804ACD8(const u8 *, u8 *, u8); static void nullsub_5(u8, u8); static void sub_804AA88(void); @@ -213,12 +213,14 @@ static #endif void sub_804DB84(void); -extern u8 gUnknown_020297D8[2]; -extern u8 *gUnknown_020296CC[13]; +EWRAM_DATA u8 *gUnknown_020296CC[13] = {0}; +EWRAM_DATA struct MailStruct gUnknown_02029700[6] = {0}; +EWRAM_DATA u8 gUnknown_020297D8[2] = {0}; + extern struct TradeEwramSubstruct *gUnknown_03004824; -extern struct MailStruct gUnknown_02029700[16]; -const u32 unref_data_820ABD4[] = { +const u32 unref_data_820ABD4[] = +{ 0x00000890, 0x00003AC0, 0x0000001C, @@ -1016,10 +1018,10 @@ void sub_8047CD8(void) static void sub_8047CE8(void) { u8 mpId; - sub_804AFB8(&gWindowConfig_81E725C, gUnknown_020296CC[0], gSaveBlock2.playerName, 0xC); + sub_804AFB8(&gWindowTemplate_81E725C, gUnknown_020296CC[0], gSaveBlock2.playerName, 0xC); mpId = GetMultiplayerId(); - sub_804AFB8(&gWindowConfig_81E725C, gUnknown_020296CC[3], gLinkPlayers[mpId ^ 1].name, 0xC); - sub_804AFB8(&gWindowConfig_81E725C, gUnknown_020296CC[6], gUnknown_0820C14C[0], 0x8); + sub_804AFB8(&gWindowTemplate_81E725C, gUnknown_020296CC[3], gLinkPlayers[mpId ^ 1].name, 0xC); + sub_804AFB8(&gWindowTemplate_81E725C, gUnknown_020296CC[6], gUnknown_0820C14C[0], 0x8); sub_804ACD8(gUnknown_0820C14C[1], gUnknown_020296CC[8], 0x14); nullsub_5(3, 0); } @@ -1087,12 +1089,12 @@ static void sub_8047EC0(void) ResetTasks(); sub_804A964(&gUnknown_03004824->unk_00c8, BG_SCREEN_ADDR(5)); SetVBlankCallback(sub_80489F4); - InitMenuWindow(&gWindowConfig_81E6CE4); - SetUpWindowConfig(&gWindowConfig_81E6F84); - InitWindowFromConfig(&gUnknown_03004824->window, &gWindowConfig_81E6F84); - gUnknown_03004824->unk_007a = SetTextWindowBaseTileNum(20); - LoadTextWindowGraphics(&gUnknown_03004824->window); - MenuZeroFillScreen(); + InitMenuWindow(&gWindowTemplate_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6F84); + Text_InitWindowWithTemplate(&gUnknown_03004824->window, &gWindowTemplate_81E6F84); + gUnknown_03004824->unk_007a = TextWindow_SetBaseTileNum(20); + TextWindow_LoadStdFrameGraphics(&gUnknown_03004824->window); + Menu_EraseScreen(); sub_809D51C(); gUnknown_03004824->unk_0075 = 0; gUnknown_03004824->unk_007b = 0; @@ -1157,7 +1159,7 @@ static void sub_8047EC0(void) break; case 6: CalculateEnemyPartyCount(); - FillWindowRect_DefaultPalette(&gUnknown_03004824->window, 0, 0, 0, 29, 19); + Text_FillWindowRectDefPalette(&gUnknown_03004824->window, 0, 0, 0, 29, 19); REG_DISPCNT = 0; gUnknown_03004824->partyCounts[0] = gPlayerPartyCount; gUnknown_03004824->partyCounts[1] = gEnemyPartyCount; @@ -1267,12 +1269,12 @@ static void sub_80484F4(void) ResetTasks(); sub_804A964(&gUnknown_03004824->unk_00c8, BG_SCREEN_ADDR(5)); SetVBlankCallback(sub_80489F4); - InitMenuWindow(&gWindowConfig_81E6CE4); - SetUpWindowConfig(&gWindowConfig_81E6F84); - InitWindowFromConfig(&gUnknown_03004824->window, &gWindowConfig_81E6F84); - gUnknown_03004824->unk_007a = SetTextWindowBaseTileNum(20); - LoadTextWindowGraphics(&gUnknown_03004824->window); - MenuZeroFillScreen(); + InitMenuWindow(&gWindowTemplate_81E6CE4); + Text_LoadWindowTemplate(&gWindowTemplate_81E6F84); + Text_InitWindowWithTemplate(&gUnknown_03004824->window, &gWindowTemplate_81E6F84); + gUnknown_03004824->unk_007a = TextWindow_SetBaseTileNum(20); + TextWindow_LoadStdFrameGraphics(&gUnknown_03004824->window); + Menu_EraseScreen(); sub_809D51C(); gUnknown_03004824->unk_0075 = 0; gUnknown_03004824->unk_007b = 0; @@ -1453,9 +1455,7 @@ static void sub_8048AB4(void) static void sub_8048B0C(u8 a0) { int i; - u16 *dest; - const u16 *src; - u32 size; + switch (a0) { case 0: @@ -1464,30 +1464,13 @@ static void sub_8048B0C(u8 a0) gPlttBufferUnfaded[i] = *(gUnknown_08EA02C8 + i); gPlttBufferFaded[i] = *(gUnknown_08EA02C8 + i); } - src = gUnknown_08EA0348; - dest = (u16 *)BG_VRAM; - size = 0x1280; - while (1) - { - DmaCopy16(3, src, dest, BLOCKSIZE * sizeof(u16)); - src += BLOCKSIZE; - dest += BLOCKSIZE; - size -= BLOCKSIZE * sizeof(u16); - if (size <= BLOCKSIZE * sizeof(u16)) - { - DmaCopy16(3, src, dest, size); - break; - } - } + DmaCopyLarge16(3, gUnknown_08EA0348, (void *)BG_VRAM, 0x1280, 0x1000); for (i = 0; i < 0x400; i ++) gUnknown_03004824->unk_00c8.unk_12[i] = gUnknown_08EA15C8[i]; - dest = BG_SCREEN_ADDR(6); - DmaCopy16(3, gTradeStripesBG2Tilemap, dest, 0x800); + DmaCopy16Defvars(3, gTradeStripesBG2Tilemap, BG_SCREEN_ADDR(6), 0x800); break; case 1: - src = gTradeStripesBG3Tilemap; - dest = BG_SCREEN_ADDR(7); - DmaCopy16(3, src, dest, 0x800); + DmaCopy16Defvars(3, gTradeStripesBG3Tilemap, BG_SCREEN_ADDR(7), 0x800); sub_804A6DC(0); sub_804A6DC(1); sub_804A938(&gUnknown_03004824->unk_00c8); @@ -1728,7 +1711,7 @@ static bool8 sub_8048D44(void) gUnknown_03004824->unk_0075 ++; break; case 1: - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { if (GetBlockReceivedStatus() == 0) { @@ -2096,8 +2079,8 @@ static void sub_8049680(void) PlaySE(SE_SELECT); if (gUnknown_03004824->tradeMenuCursorPosition < PARTY_SIZE) { - DrawTextWindow(&gUnknown_03004824->window, 18, 14, 28, 19); - PrintMenuItems(19, 15, 2, (const struct MenuAction *)gUnknown_0820C320); + TextWindow_DrawStdFrame(&gUnknown_03004824->window, 18, 14, 28, 19); + Menu_PrintItems(19, 15, 2, (const struct MenuAction *)gUnknown_0820C320); InitMenu(0, 19, 15, 2, 0, 9); gUnknown_03004824->unk_007b = 1; } @@ -2108,7 +2091,7 @@ static void sub_8049680(void) } else if (gUnknown_03004824->tradeMenuCursorPosition == 2 * PARTY_SIZE) { - DrawTextWindow(&gUnknown_03004824->window, 24, 14, 29, 19); + TextWindow_DrawStdFrame(&gUnknown_03004824->window, 24, 14, 29, 19); InitYesNoMenu(24, 14, 4); gUnknown_03004824->unk_007b = 4; sub_804ACD8(gUnknown_0820C14C[4], BG_CHAR_ADDR(4) + 32 * gUnknown_03004824->unk_007e, 20); @@ -2126,7 +2109,7 @@ static void sub_8049680(void) static void sub_8049804(void) { - HandleDestroyMenuCursors(); + Menu_DestroyCursor(); sub_804A80C(); gUnknown_03004824->unk_007b = 0; gSprites[gUnknown_03004824->tradeMenuCursorSpriteIdx].invisible = FALSE; @@ -2138,17 +2121,17 @@ static void sub_8049860(void) if (gMain.newAndRepeatedKeys & DPAD_UP) { PlaySE(SE_SELECT); - gUnknown_03004824->unk_007c = MoveMenuCursor(-1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(-1); } else if (gMain.newAndRepeatedKeys & DPAD_DOWN) { PlaySE(SE_SELECT); - gUnknown_03004824->unk_007c = MoveMenuCursor(+1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(+1); } if (gMain.newKeys & A_BUTTON) { PlaySE(SE_SELECT); - if (GetMenuCursorPos() == 0) + if (Menu_GetCursorPos() == 0) { BeginNormalPaletteFade(-1, 0, 0, 16, 0); gUnknown_03004824->unk_007b = 2; @@ -2227,7 +2210,7 @@ static void sub_8049A20(void) { sub_804AADC(3, 1); gUnknown_03004824->linkData[0] = 0xbbbb; - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { Trade_SendData(gUnknown_03004824); } @@ -2239,24 +2222,24 @@ static void sub_8049AC0(void) if (gMain.newAndRepeatedKeys & DPAD_UP) { PlaySE(SE_SELECT); - gUnknown_03004824->unk_007c = MoveMenuCursor(-1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(-1); } else if (gMain.newAndRepeatedKeys & DPAD_DOWN) { PlaySE(SE_SELECT); - gUnknown_03004824->unk_007c = MoveMenuCursor(+1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(+1); } if (gMain.newKeys & A_BUTTON) { PlaySE(SE_SELECT); - if (GetMenuCursorPos() == 0) + if (Menu_GetCursorPos() == 0) { sub_8049A20(); } else { sub_804AADC(3, 1); - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { gUnknown_03004824->linkData[0] = 0xbbcc; Trade_SendData(gUnknown_03004824); @@ -2267,9 +2250,9 @@ static void sub_8049AC0(void) else if (gMain.newKeys & B_BUTTON) { sub_804AADC(3, 1); - if (GetMenuCursorPos() == 0) + if (Menu_GetCursorPos() == 0) { - gUnknown_03004824->unk_007c = MoveMenuCursor(+1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(+1); } gUnknown_03004824->linkData[0] = 0xbbcc; Trade_SendData(gUnknown_03004824); @@ -2282,17 +2265,17 @@ static void sub_8049BC0(void) if (gMain.newAndRepeatedKeys & DPAD_UP) { PlaySE(SE_SELECT); - gUnknown_03004824->unk_007c = MoveMenuCursor(-1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(-1); } else if (gMain.newAndRepeatedKeys & DPAD_DOWN) { PlaySE(SE_SELECT); - gUnknown_03004824->unk_007c = MoveMenuCursor(+1); + gUnknown_03004824->unk_007c = Menu_MoveCursor(+1); } if (gMain.newKeys & A_BUTTON) { PlaySE(SE_SELECT); - if (GetMenuCursorPos() == 0) + if (Menu_GetCursorPos() == 0) { sub_804AA0C(4); gUnknown_03004824->linkData[0] = 0xeeaa; @@ -2337,7 +2320,7 @@ static void DisplayMessageAndContinueTask(void) gUnknown_03004824->unk_00b4++; if (gUnknown_03004824->unk_00b4 > 120) { - DrawTextWindow(&gUnknown_03004824->window, 24, 14, 29, 19); + TextWindow_DrawStdFrame(&gUnknown_03004824->window, 24, 14, 29, 19); InitYesNoMenu(24, 14, 4); gUnknown_03004824->unk_00b4 = 0; gUnknown_03004824->unk_007b = 3; @@ -2464,8 +2447,8 @@ static void sub_8049ED4(u8 a0) StoreSpriteCallbackInData(&gSprites[gUnknown_03004824->partyIcons[whichParty][whichPokemon]], sub_809D62C); gUnknown_03004824->unk_0080[a0] ++; sub_8078A34(&gSprites[gUnknown_03004824->partyIcons[whichParty][whichPokemon]]); - HandleDestroyMenuCursors(); - FillWindowRect_DefaultPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[whichParty][0], 0, gUnknown_0820C330[whichParty][1], 19); + Menu_DestroyCursor(); + Text_FillWindowRectDefPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[whichParty][0], 0, gUnknown_0820C330[whichParty][1], 19); sub_804A96C_alt(&gUnknown_03004824->unk_00c8, 15 * a0, 0, gTradePartyBoxTilemap, 15, 17, 0); if (whichParty == 0) { @@ -2491,10 +2474,10 @@ static void sub_8049ED4(u8 a0) string1[3] = 0xFC; string1[4] = 0x11; string1[5] = (64 - stringLength) / 2; - sub_8003460(&gUnknown_03004824->window, string1, gUnknown_03004824->unk_007a + whichParty * 6 * 32, gUnknown_0820C334[whichParty][0], gUnknown_0820C334[whichParty][1]); + Text_InitWindowAndPrintText(&gUnknown_03004824->window, string1, gUnknown_03004824->unk_007a + whichParty * 6 * 32, gUnknown_0820C334[whichParty][0], gUnknown_0820C334[whichParty][1]); sub_804A33C(string2, whichParty, whichPokemon); - sub_8003460(&gUnknown_03004824->window, gOtherText_Terminator2, gUnknown_03004824->unk_007a + whichParty * 6 * 32 + 32, gUnknown_0820C334[whichParty + 1][0], gUnknown_0820C334[whichParty + 1][1] + 1); - sub_8003460(&gUnknown_03004824->window, string2, gUnknown_03004824->unk_007a + whichParty * 6 * 32 + 38, gUnknown_0820C334[whichParty + 1][0], gUnknown_0820C334[whichParty + 1][1] + 1); + Text_InitWindowAndPrintText(&gUnknown_03004824->window, gOtherText_Terminator2, gUnknown_03004824->unk_007a + whichParty * 6 * 32 + 32, gUnknown_0820C334[whichParty + 1][0], gUnknown_0820C334[whichParty + 1][1] + 1); + Text_InitWindowAndPrintText(&gUnknown_03004824->window, string2, gUnknown_03004824->unk_007a + whichParty * 6 * 32 + 38, gUnknown_0820C334[whichParty + 1][0], gUnknown_0820C334[whichParty + 1][1] + 1); gUnknown_03004824->unk_0080[a0] ++; break; case 4: @@ -2692,7 +2675,7 @@ static void sub_8049ED4(u8 a0) "\tlsls r0, 2\n" "\tadds r0, r7\n" "\tbl sub_8078A34\n" - "\tbl HandleDestroyMenuCursors\n" + "\tbl Menu_DestroyCursor\n" "\tldr r3, _0804A09C @ =gUnknown_03004824\n" "\tldr r0, [r3]\n" "\tadds r0, 0x4\n" @@ -2708,7 +2691,7 @@ static void sub_8049ED4(u8 a0) "\tstr r1, [sp, 0x4]\n" "\tmovs r1, 0\n" "\tmovs r3, 0\n" - "\tbl FillWindowRect_DefaultPalette\n" + "\tbl Text_FillWindowRectDefPalette\n" "\tldr r1, _0804A09C @ =gUnknown_03004824\n" "\tldr r0, [r1]\n" "\tadds r0, 0xC8\n" @@ -2884,7 +2867,7 @@ static void sub_8049ED4(u8 a0) "\tldrb r1, [r1]\n" "\tstr r1, [sp]\n" "\tadd r1, sp, 0xC\n" - "\tbl sub_8003460\n" + "\tbl Text_InitWindowAndPrintText\n" "\tadd r7, sp, 0x34\n" "\tadds r0, r7, 0\n" "\tldr r1, [sp, 0x6C]\n" @@ -2910,7 +2893,7 @@ static void sub_8049ED4(u8 a0) "\tlsrs r4, 24\n" "\tstr r4, [sp]\n" "\tadds r3, r5, 0\n" - "\tbl sub_8003460\n" + "\tbl Text_InitWindowAndPrintText\n" "\tldr r0, _0804A22C @ =gUnknown_03004824\n" "\tldr r1, [r0]\n" "\tadds r0, r1, 0x4\n" @@ -2921,7 +2904,7 @@ static void sub_8049ED4(u8 a0) "\tstr r4, [sp]\n" "\tadds r1, r7, 0\n" "\tadds r3, r5, 0\n" - "\tbl sub_8003460\n" + "\tbl Text_InitWindowAndPrintText\n" "\tldr r2, _0804A22C @ =gUnknown_03004824\n" "\tldr r1, [r2]\n" "\tb _0804A28A\n" @@ -3010,7 +2993,7 @@ u8 sub_804A2B4(u8 *a0, u8 whichParty, u8 whichPokemon) GetMonGender(&gEnemyParty[whichPokemon]); GetMonData(&gEnemyParty[whichPokemon], MON_DATA_LEVEL); } - return GetStringWidthGivenWindowConfig(&gWindowConfig_81E7294, a0); + return Text_GetStringWidthFromWindowTemplate(&gWindowTemplate_81E7294, a0); } #ifdef NONMATCHING @@ -3067,8 +3050,8 @@ static void sub_804A41C(u8 whichParty) GetMonData(pokemon, MON_DATA_NICKNAME, nickname); StringCopy10(string + 6, nickname); GetMonGender(pokemon); - string[5] = (50 - GetStringWidthGivenWindowConfig(&gWindowConfig_81E7294, string + 6)) / 2; - sub_8003460(&gUnknown_03004824->window, string, gUnknown_03004824->unk_007a + 22 * 6 * whichParty + 22 * i, gTradeMonSpriteCoords[i + 6 * whichParty][0], gTradeMonSpriteCoords[i + 6 * whichParty][1]); + string[5] = (50 - Text_GetStringWidthFromWindowTemplate(&gWindowTemplate_81E7294, string + 6)) / 2; + Text_InitWindowAndPrintText(&gUnknown_03004824->window, string, gUnknown_03004824->unk_007a + 22 * 6 * whichParty + 22 * i, gTradeMonSpriteCoords[i + 6 * whichParty][0], gTradeMonSpriteCoords[i + 6 * whichParty][1]); } } #else @@ -3141,8 +3124,8 @@ static void sub_804A41C(u8 whichParty) "\tbl GetMonGender\n" "\tmov r1, sp\n" "\tadds r1, 0x22\n" - "\tldr r0, _0804A518 @ =gWindowConfig_81E7294\n" - "\tbl GetStringWidthGivenWindowConfig\n" + "\tldr r0, _0804A518 @ =gWindowTemplate_81E7294\n" + "\tbl Text_GetStringWidthFromWindowTemplate\n" "\tlsls r0, 24\n" "\tlsrs r0, 24\n" "\tmovs r1, 0x32\n" @@ -3177,7 +3160,7 @@ static void sub_804A41C(u8 whichParty) "\tldrb r1, [r1]\n" "\tstr r1, [sp]\n" "\tmov r1, r8\n" - "\tbl sub_8003460\n" + "\tbl Text_InitWindowAndPrintText\n" "\tadds r0, r6, 0x1\n" "\tlsls r0, 24\n" "\tlsrs r6, r0, 24\n" @@ -3198,7 +3181,7 @@ static void sub_804A41C(u8 whichParty) "\tbx r0\n" "\t.align 2, 0\n" "_0804A514: .4byte gEnemyParty\n" - "_0804A518: .4byte gWindowConfig_81E7294"); + "_0804A518: .4byte gWindowTemplate_81E7294"); } #endif @@ -3337,7 +3320,7 @@ static void sub_804A740(u8 whichParty) static void sub_804A80C(void) { - FillWindowRect_DefaultPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[1][0], 0, gUnknown_0820C330[1][1], 19); + Text_FillWindowRectDefPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[1][0], 0, gUnknown_0820C330[1][1], 19); sub_804A41C(1); } @@ -3345,7 +3328,7 @@ static void sub_804A840(u8 whichParty) { if (whichParty == 0) { - FillWindowRect_DefaultPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[0][0], 0, gUnknown_0820C330[0][1], 19); + Text_FillWindowRectDefPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[0][0], 0, gUnknown_0820C330[0][1], 19); sub_804A96C_alt(&gUnknown_03004824->unk_00c8, 0, 0, gTradePartyBoxTilemap, 15, 17, 0); sub_804A6DC(0); sub_804A41C(0); @@ -3354,8 +3337,8 @@ static void sub_804A840(u8 whichParty) } else { - HandleDestroyMenuCursors(); - FillWindowRect_DefaultPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[1][0], 0, gUnknown_0820C330[1][1], 19); + Menu_DestroyCursor(); + Text_FillWindowRectDefPalette(&gUnknown_03004824->window, 0, gUnknown_0820C330[1][0], 0, gUnknown_0820C330[1][1], 19); sub_804A96C_alt(&gUnknown_03004824->unk_00c8, 15, 0, gTradePartyBoxTilemap, 15, 17, 0); sub_804A6DC(1); sub_804A41C(1); @@ -3413,18 +3396,18 @@ static void sub_804A96C_alt(struct UnkStructD *arg0, u8 left, u8 top, const u16 static void sub_804A9F4(u8 unused) { - MenuZeroFillScreen(); + Menu_EraseScreen(); } static void sub_804AA00(u8 unused) { - MenuZeroFillScreen(); + Menu_EraseScreen(); } static void sub_804AA0C(u8 a0) { - DrawTextWindow(&gUnknown_03004824->window, gTradeMessageWindowRects[a0][0], gTradeMessageWindowRects[a0][1], gTradeMessageWindowRects[a0][2], gTradeMessageWindowRects[a0][3]); - sub_8003460(&gUnknown_03004824->window, gUnknown_0820C2F0[a0], 0x180 + gUnknown_03004824->unk_007a, gTradeMessageWindowRects[a0][0] + 1, gTradeMessageWindowRects[a0][1] + 1); + TextWindow_DrawStdFrame(&gUnknown_03004824->window, gTradeMessageWindowRects[a0][0], gTradeMessageWindowRects[a0][1], gTradeMessageWindowRects[a0][2], gTradeMessageWindowRects[a0][3]); + Text_InitWindowAndPrintText(&gUnknown_03004824->window, gUnknown_0820C2F0[a0], 0x180 + gUnknown_03004824->unk_007a, gTradeMessageWindowRects[a0][0] + 1, gTradeMessageWindowRects[a0][1] + 1); } static void sub_804AA88(void) @@ -3523,7 +3506,7 @@ static bool8 sub_804ABF8(void) static void sub_804ACD8(const u8 *src, u8 *dest, u8 a2) { - sub_804AFB8(&gWindowConfig_81E725C, dest, src, a2); + sub_804AFB8(&gWindowTemplate_81E725C, dest, src, a2); } #ifdef NONMATCHING @@ -3798,7 +3781,7 @@ static void sub_804AF84(void) gSaveBlock1.giftRibbons[i] = gUnknown_03004824->unk_00b5[i]; } -static void sub_804AFB8(const struct WindowConfig *windowConfig, u8 *dest, const u8 *src, u8 size) +static void sub_804AFB8(const struct WindowTemplate *windowConfig, u8 *dest, const u8 *src, u8 size) { u8 i; u8 *tileBuffer; @@ -3806,7 +3789,7 @@ static void sub_804AFB8(const struct WindowConfig *windowConfig, u8 *dest, const tileBuffer = gTileBuffer; CpuFill16(0, tileBuffer, size * 0x80); CpuFill16(0, tileBuffer + windowConfig->width * 0x20, size * 0x80); - sub_8004E3C(windowConfig, tileBuffer, src); + Text_InitWindow8004E3C(windowConfig, tileBuffer, src); for (i = 0; i < size; i ++) { CpuCopy16(&tileBuffer[32 * (i * 4)], &dest[32 * (i * 8)], 0x80); @@ -4028,11 +4011,11 @@ static void sub_804B41C(void) FreeAllSpritePalettes(); SetVBlankCallback(sub_804B210); sub_804B228(); - SetUpWindowConfig(&gWindowConfig_81E6F84); - InitWindowFromConfig(&gUnknown_03004828->window, &gWindowConfig_81E6F84); - gUnknown_03004828->textWindowBaseTileNum = SetTextWindowBaseTileNum(2); - LoadTextWindowGraphics(&gUnknown_03004828->window); - MenuZeroFillScreen(); + Text_LoadWindowTemplate(&gWindowTemplate_81E6F84); + Text_InitWindowWithTemplate(&gUnknown_03004828->window, &gWindowTemplate_81E6F84); + gUnknown_03004828->textWindowBaseTileNum = TextWindow_SetBaseTileNum(2); + TextWindow_LoadStdFrameGraphics(&gUnknown_03004828->window); + Menu_EraseScreen(); gLinkType = 0x1144; gMain.state ++; LZDecompressVram(gUnknown_08D00000, (void *)VRAM); @@ -4182,22 +4165,22 @@ static __attribute__((naked)) void sub_804B41C(void) "\tldr r0, _0804B578 @ =sub_804B210\n" "\tbl SetVBlankCallback\n" "\tbl sub_804B228\n" - "\tldr r4, _0804B57C @ =gWindowConfig_81E6F84\n" + "\tldr r4, _0804B57C @ =gWindowTemplate_81E6F84\n" "\tadds r0, r4, 0\n" - "\tbl SetUpWindowConfig\n" + "\tbl Text_LoadWindowTemplate\n" "\tldr r0, [r6]\n" "\tadds r0, 0x4\n" "\tadds r1, r4, 0\n" - "\tbl InitWindowFromConfig\n" + "\tbl Text_InitWindowWithTemplate\n" "\tmovs r0, 0x2\n" - "\tbl SetTextWindowBaseTileNum\n" + "\tbl TextWindow_SetBaseTileNum\n" "\tldr r1, [r6]\n" "\tadds r1, 0x34\n" "\tstrb r0, [r1]\n" "\tldr r0, [r6]\n" "\tadds r0, 0x4\n" - "\tbl LoadTextWindowGraphics\n" - "\tbl MenuZeroFillScreen\n" + "\tbl TextWindow_LoadStdFrameGraphics\n" + "\tbl Menu_EraseScreen\n" "\tldr r1, _0804B580 @ =gLinkType\n" "\tldr r4, _0804B584 @ =0x00001144\n" "\tadds r0, r4, 0\n" @@ -4279,7 +4262,7 @@ static __attribute__((naked)) void sub_804B41C(void) "_0804B570: .4byte gUnknown_03004828\n" "_0804B574: .4byte gSharedMem + 0x1F000\n" "_0804B578: .4byte sub_804B210\n" - "_0804B57C: .4byte gWindowConfig_81E6F84\n" + "_0804B57C: .4byte gWindowTemplate_81E6F84\n" "_0804B580: .4byte gLinkType\n" "_0804B584: .4byte 0x00001144\n" "_0804B588: .4byte gMain\n" @@ -4512,8 +4495,8 @@ static __attribute__((naked)) void sub_804B41C(void) } #endif -static void sub_804B790(void) // In-game trade init +void sub_804B790(void) { u8 otName[11]; switch (gMain.state) @@ -4531,11 +4514,11 @@ static void sub_804B790(void) FreeAllSpritePalettes(); SetVBlankCallback(sub_804B210); sub_804B228(); - SetUpWindowConfig(&gWindowConfig_81E717C); - InitWindowFromConfig(&gUnknown_03004828->window, &gWindowConfig_81E717C); - gUnknown_03004828->textWindowBaseTileNum = SetTextWindowBaseTileNum(2); - LoadTextWindowGraphics(&gUnknown_03004828->window); - MenuZeroFillScreen(); + Text_LoadWindowTemplate(&gWindowTemplate_81E717C); + Text_InitWindowWithTemplate(&gUnknown_03004828->window, &gWindowTemplate_81E717C); + gUnknown_03004828->textWindowBaseTileNum = TextWindow_SetBaseTileNum(2); + TextWindow_LoadStdFrameGraphics(&gUnknown_03004828->window); + Menu_EraseScreen(); gLinkType = 0x1144; gUnknown_03004828->isLinkTrade = FALSE; gUnknown_03004828->unk_00b6 = 0; @@ -4649,7 +4632,7 @@ static void sub_804BB78(void) switch (gUnknown_03004828->unk_00bd) { case 1: - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { Trade_SendData(gUnknown_03004828); gUnknown_03004828->unk_00bd ++; @@ -4733,7 +4716,7 @@ static void sub_804BBE8(u8 a0) for (i = 0; i < 0x280; i ++) buffer[i] |= 0x7000; DmaCopy16Defvars(3, gSharedMem, BG_SCREEN_ADDR(5), 0x500); - MenuZeroFillWindowRect(2, 15, 27, 18); + Menu_EraseWindowRect(2, 15, 27, 18); break; case 6: REG_DISPCNT = DISPCNT_MODE_1 | DISPCNT_OBJ_1D_MAP | DISPCNT_BG2_ON | DISPCNT_OBJ_ON; @@ -4843,7 +4826,7 @@ static bool8 sub_804C29C(void) case 10: StringExpandPlaceholders(gStringVar4, gTradeText_WillBeSent); - sub_8003460(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); + Text_InitWindowAndPrintText(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); gUnknown_03004828->unk_00c4 = 11; gUnknown_03004828->unk_00c0 = 0; break; @@ -4852,13 +4835,13 @@ static bool8 sub_804C29C(void) { gUnknown_03004828->unk_0102 = sub_8047580(gUnknown_03004828->pokePicSpriteIdxs[0], gSprites[gUnknown_03004828->pokePicSpriteIdxs[0]].oam.paletteNum, 0x78, 0x20, 0x2, 0x1, 0x14, 0xfffff); gUnknown_03004828->unk_00c4 ++; - ZeroFillWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); + Text_EraseWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); StringExpandPlaceholders(gStringVar4, gTradeText_ByeBye); - sub_8003460(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); + Text_InitWindowAndPrintText(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); } break; case 12: - if (gSprites[gUnknown_03004828->unk_0102].callback == SpriteCallbackDummy && sub_80035AC(&gUnknown_03004828->window) == TRUE) + if (gSprites[gUnknown_03004828->unk_0102].callback == SpriteCallbackDummy && Text_UpdateWindow(&gUnknown_03004828->window) == TRUE) { gUnknown_03004828->unk_0103 = CreateSprite(&gSpriteTemplate_821595C, 0x78, 0x20, 0); gSprites[gUnknown_03004828->unk_0103].callback = sub_804D738; @@ -5199,9 +5182,9 @@ static bool8 sub_804C29C(void) break; case 67: REG_DISPCNT = DISPCNT_MODE_0 | DISPCNT_OBJ_1D_MAP | DISPCNT_BG0_ON | DISPCNT_BG1_ON | DISPCNT_BG2_ON | DISPCNT_OBJ_ON; - ZeroFillWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); + Text_EraseWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); StringExpandPlaceholders(gStringVar4, gTradeText_SentOverPoke); - sub_8003460(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); + Text_InitWindowAndPrintText(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); gUnknown_03004828->unk_00c4 ++; gUnknown_03004828->unk_00c0 = 0; break; @@ -5213,9 +5196,9 @@ static bool8 sub_804C29C(void) if (gUnknown_03004828->unk_00c0 == 0xf0) { gUnknown_03004828->unk_00c4 ++; - ZeroFillWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); + Text_EraseWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); StringExpandPlaceholders(gStringVar4, gTradeText_TakeGoodCare); - sub_8003460(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); + Text_InitWindowAndPrintText(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); gUnknown_03004828->unk_00c0 = 0; } break; @@ -5536,9 +5519,9 @@ static void sub_804DC88(void) case 0: gUnknown_03004828 = &ewram_2010000.unk_0f000; gMain.state ++; - ZeroFillWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); + Text_EraseWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); StringExpandPlaceholders(gStringVar4, gOtherText_LinkStandby2); - sub_8003460(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); + Text_InitWindowAndPrintText(&gUnknown_03004828->window, gStringVar4, gUnknown_03004828->textWindowBaseTileNum, 2, 15); break; case 1: sub_80084A4(); @@ -5551,21 +5534,21 @@ static void sub_804DC88(void) gMain.state = 101; gUnknown_03004828->unk_00c0 = 0; } - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { gMain.state = 2; } break; case 101: - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { gMain.state = 2; } break; case 2: gMain.state = 50; - ZeroFillWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); - sub_8003460(&gUnknown_03004828->window, gSystemText_Saving, gUnknown_03004828->textWindowBaseTileNum, 2, 15); + Text_EraseWindowRect(&gUnknown_03004828->window, 0, 0, 29, 19); + Text_InitWindowAndPrintText(&gUnknown_03004828->window, gSystemText_Saving, gUnknown_03004828->textWindowBaseTileNum, 2, 15); break; case 50: SetSecretBase2Field_9_AndHideBG(); @@ -5609,7 +5592,7 @@ static void sub_804DC88(void) gMain.state = 42; break; case 42: - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { sub_8125E04(); gSoftResetDisabled = FALSE; @@ -5624,7 +5607,7 @@ static void sub_804DC88(void) } break; case 6: - if (sub_8007ECC()) + if (IsLinkTaskFinished()) { BeginNormalPaletteFade(-1, 0, 0, 16, 0); gMain.state ++; @@ -5701,13 +5684,9 @@ static void sub_804E1DC(void) void sub_804E22C(void) { - const u16 *src; - u16 *dest; LZDecompressVram(gUnknown_08D00000, (void *)VRAM); CpuCopy16(gUnknown_08D00524, gSharedMem, 0x1000); - src = (const u16 *)gSharedMem; - dest = BG_SCREEN_ADDR(5); - DmaCopy16(3, src, dest, 0x500) + DmaCopy16Defvars(3, gSharedMem, BG_SCREEN_ADDR(5), 0x500); LoadCompressedPalette(gUnknown_08D004E0, 0, 32); REG_BG1CNT = BGCNT_PRIORITY(2) | BGCNT_SCREENBASE(5); } diff --git a/src/engine/trainer_card.c b/src/engine/trainer_card.c index ffa4cbd08..998c4697e 100644 --- a/src/engine/trainer_card.c +++ b/src/engine/trainer_card.c @@ -5,6 +5,7 @@ #include "field_effect.h" #include "graphics.h" #include "link.h" +#include "m4a.h" #include "main.h" #include "menu.h" #include "money.h" @@ -18,7 +19,7 @@ #include "string_util.h" #include "strings2.h" #include "task.h" -#include "unknown_task.h" +#include "scanline_effect.h" #include "util.h" #include "ewram.h" @@ -26,25 +27,25 @@ typedef void (*Callback)(void); struct Struct2000000 { - /*0x00*/ u8 var_0; - /*0x01*/ bool8 var_1; - /*0x02*/ u8 var_2; - /*0x03*/ bool8 var_3; + /*0x00*/ u8 current_state; + /*0x01*/ bool8 isShowingLinkCard; + /*0x02*/ u8 starCount; + /*0x03*/ bool8 backSideShown; /*0x04*/ u8 var_4; - /*0x05*/ u8 var_5; - /*0x06*/ u8 var_6; - /*0x07*/ bool8 var_7; - /*0x08*/ bool8 var_8; - /*0x09*/ bool8 var_9; - /*0x0A*/ bool8 var_a; - /*0x0B*/ bool8 var_b; - /*0x0C*/ bool8 var_c; - /*0x0D*/ bool8 var_d; - /*0x0E*/ u8 var_e[8]; + /*0x05*/ bool8 showColon; + /*0x06*/ u8 frameCounter; /* Used to flash colon */ + /*0x07*/ bool8 showPokedexCount; + /*0x08*/ bool8 showHallOfFame; + /*0x09*/ bool8 showLinkBattleStatus; + /*0x0A*/ bool8 showBattleTowerStatus; + /*0x0B*/ bool8 showContestRecord; + /*0x0C*/ bool8 showMixingRecord; + /*0x0D*/ bool8 showTradingRecord; + /*0x0E*/ bool8 ownedBadges[8]; /*0x16*/ u8 filler_16[10]; - /*0x20*/ u8 var_20[4][0x10]; + /*0x20*/ u8 easyChatPhrase[4][0x10]; /*0x60*/ Callback *var_60; - /*0x64*/ struct TrainerCard var_64; + /*0x64*/ struct TrainerCard displayedCard; /*0x9C*/ u8 language; // 0x9C }; @@ -52,49 +53,76 @@ extern struct LinkPlayerMapObject gLinkPlayerMapObjects[]; EWRAM_DATA struct TrainerCard gTrainerCards[4] = {0}; -extern const u8 gBadgesTiles[]; extern const u16 gUnknown_083B5F0C[]; extern const u16 gBadgesPalette[]; extern const u16 gUnknown_083B5F4C[]; extern const u16 gUnknown_083B5F6C[]; -extern const u16 gUnknown_083B5F8C[][4]; +extern const u16 gTrainerCardBadgesMap[][4]; const u8 gBadgesTiles[] = INCBIN_U8("graphics/trainer_card/badges.4bpp"); // XXX: what is this? u8 *const ewram_ = gSharedMem; -bool8 sub_8093864(struct Task *); -bool8 sub_80938A8(struct Task *); -bool8 sub_80938CC(struct Task *); -bool8 sub_8093918(struct Task *); -bool8 sub_8093938(struct Task *); -bool8 sub_8093954(struct Task *); -bool8 sub_8093980(struct Task *); - -bool8 (*const gUnknown_083B5EBC[])(struct Task *) = -{ - sub_8093864, - sub_80938A8, - sub_80938CC, - sub_8093918, - sub_8093938, - sub_8093954, - sub_8093980, +#if DEBUG +const struct TrainerCard gUnknown_Debug_083E0448 = +{ + .gender = FEMALE, + .stars = 4, + .hasPokedex = TRUE, + .var_3 = TRUE, + .var_4 = TRUE, + .firstHallOfFameA = 999, + .firstHallOfFameB = 99, + .firstHallOfFameC = 99, + .pokedexSeen = 411, + .trainerId = 12345, + .playTimeHours = 99, + .playTimeMinutes = 99, + .linkBattleWins = 9999, + .linkBattleLosses = 9999, + .battleTowerWins = 9999, + .battleTowerLosses = 9999, + .contestsWithFriends = 999, + .pokeblocksWithFriends = 0xFFFF, + .pokemonTrades = 0xFFFF, + .money = 99999, + .var_28 = {1, 2, 3, 4}, + .playerName = _("てすと"), +}; +#endif + +bool8 TrainerCard_Init(struct Task *); +bool8 TrainerCard_WaitForFadeInToFinish(struct Task *); +bool8 TrainerCard_WaitForKeys(struct Task *); +bool8 TrainerCard_StartFlipAntimation(struct Task *); +bool8 TrainerCard_WaitForFlipToFinish(struct Task *); +bool8 TrainerCard_FadeOut(struct Task *); +bool8 TrainerCard_WaitForFadeOutToFinishAndQuit(struct Task *); + +bool8 (*const TrainerCard_StateMachine[])(struct Task *) = +{ + TrainerCard_Init, + TrainerCard_WaitForFadeInToFinish, + TrainerCard_WaitForKeys, + TrainerCard_StartFlipAntimation, + TrainerCard_WaitForFlipToFinish, + TrainerCard_FadeOut, + TrainerCard_WaitForFadeOutToFinishAndQuit, }; -bool8 sub_8093AA0(struct Task *); -bool8 sub_8093AF0(struct Task *); -bool8 sub_8093C0C(struct Task *); -bool8 sub_8093C38(struct Task *); -bool8 sub_8093D50(struct Task *); +bool8 TrainerCard_InitFlipAnimation(struct Task *); +bool8 TrainerCard_ScaleDownFlipAnimation(struct Task *); +bool8 TrainerCard_SwitchToNewSide(struct Task *); +bool8 TrainerCard_ScaleUpFlipAnimation(struct Task *); +bool8 TrainerCard_FinishFlipAnimation(struct Task *); -bool8 (*const gUnknown_083B5ED8[])(struct Task *) = +bool8 (*const TrainerCard_FlipAnimationStateMachine[])(struct Task *) = { - sub_8093AA0, - sub_8093AF0, - sub_8093C0C, - sub_8093C38, - sub_8093D50, + TrainerCard_InitFlipAnimation, + TrainerCard_ScaleDownFlipAnimation, + TrainerCard_SwitchToNewSide, + TrainerCard_ScaleUpFlipAnimation, + TrainerCard_FinishFlipAnimation, }; // FIXME: Other signature than on save_menu_util.h @@ -103,64 +131,64 @@ u16 GetPokedexSeenCount(void); enum { - TD_0, - TD_1, + TD_SHOWING_LINK_CARD, + TD_CARD_INDEX, TD_CALLBACK, }; static void sub_8093174(void); static void sub_809323C(void); static void sub_8093254(void); -static void sub_80932AC(Callback callBack); -static void sub_80932E4(u8 arg1, Callback callBack); -void sub_8093324(void); +static void TrainerCard_InitScreenForPlayer(Callback callBack); +static void TrainerCard_InitScreenForLinkPlayer(u8 arg1, Callback callBack); +void TrainerCard_FillTrainerCardStruct(void); static void nullsub_60(u8); static u32 sav12_xor_get_clamped_above(u8 index, u32 maxVal); -static u8 sub_80934F4(struct TrainerCard *); +static u8 TrainerCard_GetStarCount(struct TrainerCard *); static void sub_8093534(void); static void sub_8093550(void); static void sub_8093598(void); static void sub_80935EC(void); static void sub_8093610(void); static void sub_8093688(void); -void sub_80936D4(void); +static void TrainerCard_FillFlags(void); static void sub_80937A4(void); static void sub_80937BC(void); static void sub_80937D8(void); static void sub_80937F0(void); static void nullsub_15(void); static void sub_8093800(void); -static void sub_809380C(); -static void sub_809382C(u8 taskId); -static void sub_80939A4(void); -static void sub_80939C0(void); -static void sub_80939DC(u8 taskId); -static void sub_8093A28(void); -static u8 sub_8093A48(void); -static void sub_8093A68(u8 taskId); -void sub_8093D7C(void); -static void sub_8093DAC(void); -static void sub_8093DC8(void); -static void sub_8093DEC(void); -static void sub_8093E04(void); -static void sub_8093E28(void); -void sub_8093EA0(void); -static void sub_8093EF8(void); +static void TrainerCard_CreateStateMachine(void); +static void TrainerCard_RunStateMachine(u8 taskId); +static void TrainerCard_CreatePrintPlayTimeTask(void); +static void TrainerCard_DestoryPlayTimeTask(void); +static void TrainerCard_Front_PrintPlayTime(u8 taskId); +static void TrainerCard_CreateFlipAnimationTask(void); +static u8 TrainerCard_HasFlipAnimationFinished(void); +static void TrainerCard_RunFlipAnimationStateMachine(u8 taskId); +static void TrainerCard_FlipAnimationHBlankCallback(void); +static void TrainerCard_DrawCard(void); +static void TrainerCard_DrawCardFront(void); +static void TrainerCard_DrawCardBack(void); +static void TrainerCard_ResetOffsetRegisters(void); +static void TrainerCard_CopyGraphics(void); +static void TrainerCard_LoadPalettes(void); +static void TrainerCard_LoadTrainerGraphics(void); static void sub_8093F14(void); static void sub_8093F48(void); static void sub_8093F64(void); -static void sub_8093F80(void); -static void sub_8093FD0(void); -static void sub_8094038(void); -static void sub_80940E4(void); -static void sub_8094110(void); -static void sub_8094140(void); -static void sub_8094188(void); +static void TrainerCard_LoadTrainerTilemap(void); +static void TrainerCard_DrawStars(void); +static void TrainerCard_DisplayBadges(void); +static void TrainerCard_ClearTrainerGraphics(void); +static void TrainerCard_ClearPokedexLabel(void); +static void TrainerCard_Front_PrintTexts(void); +static void TrainerCard_Back_PrintTexts(void); static void TrainerCard_Front_PrintTrainerID(void); static void TrainerCard_Front_PrintMoney(void); static void TrainerCard_Front_PrintPokedexCount(void); -static void TrainerCard_Front_PrintPlayTime(u8 *arg1, s16 colon); -static void sub_809429C(void); +static void TrainerCard_Front_GetPlayTimeString(u8 *arg1, s16 colon); +static void TrainerCard_PrintEasyChatPhrase(void); static void TrainerCard_Back_PrintName(void); static void TrainerCard_Back_PrintHallOfFameTime_Label(void); static void TrainerCard_Back_PrintHallOfFameTime(void); @@ -176,20 +204,57 @@ static void TrainerCard_Back_PrintPokemonTrades_Label(void); static void TrainerCard_Back_PrintPokemonTrades(void); void unref_sub_8094588(u16 left, u16 top); -void sub_8093110(Callback arg1) +#if DEBUG +static u8 gDebug_03000748; +#endif + +void TrainerCard_ShowPlayerCard(Callback arg1) { - sub_80932AC(arg1); +#if DEBUG + gDebug_03000748 = 0; +#endif + TrainerCard_InitScreenForPlayer(arg1); SetMainCallback2(sub_8093174); ewram0_2.language = GAME_LANGUAGE; } -void sub_8093130(u8 playerIndex, Callback arg2) +void TrainerCard_ShowLinkCard(u8 playerIndex, Callback arg2) { - sub_80932E4(playerIndex, arg2); +#if DEBUG + gDebug_03000748 = 0; +#endif + TrainerCard_InitScreenForLinkPlayer(playerIndex, arg2); SetMainCallback2(sub_8093174); ewram0_2.language = gLinkPlayers[gLinkPlayerMapObjects[playerIndex].linkPlayerId].language; } +#if DEBUG +void debug_sub_80A0710(Callback callback) +{ + gDebug_03000748 = TRUE; + TrainerCard_InitScreenForPlayer(callback); + SetMainCallback2(sub_8093174); + ewram0_2.language = GAME_LANGUAGE; +} + +void debug_sub_80A073C(Callback callback) +{ + memcpy(&gTrainerCards[0], &gUnknown_Debug_083E0448, sizeof(struct TrainerCard)); + gDebug_03000748=TRUE; + TrainerCard_InitScreenForLinkPlayer(0, callback); + SetMainCallback2(sub_8093174); + ewram0_2.language = GAME_LANGUAGE; +} + +void debug_sub_80A0780() +{ + int i; + + for (i = 0; i < 4; i++) + memcpy(&gTrainerCards[i], &gUnknown_Debug_083E0448, sizeof(struct TrainerCard)); +} +#endif + static void sub_8093174(void) { switch (gMain.state) @@ -249,49 +314,49 @@ static void sub_8093254(void) LoadOam(); ProcessSpriteCopyRequests(); TransferPlttBuffer(); - ewram0_2.var_6++; - if (ewram0_2.var_6 >= 60) + ewram0_2.frameCounter++; + if (ewram0_2.frameCounter >= 60) { - ewram0_2.var_6 = 0; - ewram0_2.var_5 ^= 1; + ewram0_2.frameCounter = 0; + ewram0_2.showColon ^= 1; } if (ewram0_2.var_4) - DmaCopy16(3, &gUnknown_03004DE0[0], &gUnknown_03004DE0[1], 0x140); + DmaCopy16(3, &gScanlineEffectRegBuffers[0], &gScanlineEffectRegBuffers[1], 0x140); } -static void sub_80932AC(Callback callBack) +static void TrainerCard_InitScreenForPlayer(Callback callBack) { u8 taskId = CreateTask(nullsub_60, 0xFF); struct Task *task = &gTasks[taskId]; - task->data[TD_0] = FALSE; + task->data[TD_SHOWING_LINK_CARD] = FALSE; StoreWordInTwoHalfwords(&task->data[TD_CALLBACK], (u32)callBack); } -static void sub_80932E4(u8 arg1, Callback callBack) +static void TrainerCard_InitScreenForLinkPlayer(u8 arg1, Callback callBack) { u8 taskId = CreateTask(nullsub_60, 0xFF); struct Task *task = &gTasks[taskId]; - task->data[TD_0] = TRUE; - task->data[TD_1] = arg1; + task->data[TD_SHOWING_LINK_CARD] = TRUE; + task->data[TD_CARD_INDEX] = arg1; StoreWordInTwoHalfwords(&task->data[TD_CALLBACK], (u32)callBack); } -void sub_8093324(void) +void TrainerCard_FillTrainerCardStruct(void) { u8 taskId = FindTaskIdByFunc(nullsub_60); struct Task *task = &gTasks[taskId]; - ewram0_2.var_1 = task->data[TD_0]; + ewram0_2.isShowingLinkCard = task->data[TD_SHOWING_LINK_CARD]; LoadWordFromTwoHalfwords((u16 *)&task->data[TD_CALLBACK], (u32 *)&ewram0_2.var_60); - if (ewram0_2.var_1) + if (ewram0_2.isShowingLinkCard) { - ewram0_2.var_64 = gTrainerCards[task->data[TD_1]]; + ewram0_2.displayedCard = gTrainerCards[task->data[TD_CARD_INDEX]]; } else { - sub_8093390(&ewram0_2.var_64); + TrainerCard_GenerateCardForPlayer(&ewram0_2.displayedCard); } } @@ -299,7 +364,7 @@ static void nullsub_60(u8 taskid) { } -void sub_8093390(struct TrainerCard *trainerCard) +void TrainerCard_GenerateCardForPlayer(struct TrainerCard *trainerCard) { u32 playTime; bool32 enteredHallOfFame; @@ -370,7 +435,7 @@ void sub_8093390(struct TrainerCard *trainerCard) trainerCard->playerName[i] = gSaveBlock2.playerName[i]; } - trainerCard->stars = sub_80934F4(trainerCard); + trainerCard->stars = TrainerCard_GetStarCount(trainerCard); } u8 sub_80934C4(u8 id) @@ -390,7 +455,7 @@ static u32 sav12_xor_get_clamped_above(u8 index, u32 maxVal) return value; } -static u8 sub_80934F4(struct TrainerCard *trainerCard) +static u8 TrainerCard_GetStarCount(struct TrainerCard *trainerCard) { u8 value = 0; @@ -441,27 +506,12 @@ static void sub_8093550(void) void sub_8093598(void) { - u8 *addr = (void *)VRAM; - u32 size = 0x10000; - - while (1) - { - DmaFill16(3, 0, addr, 0x1000); - addr += 0x1000; - size -= 0x1000; - if (size <= 0x1000) - { - DmaFill16(3, 0, addr, size); - break; - } - } + DmaFill16Large(3, 0, (void *)VRAM, 0x10000, 0x1000); } void sub_80935EC(void) { - void *addr = (void *)OAM; - - DmaFill16(3, 0, addr, 0x400); + DmaFill16Defvars(3, 0, (void *)OAM, 0x400); } void sub_8093610(void) @@ -489,53 +539,53 @@ static void sub_8093688(void) { u8 i; - sub_8093324(); - ewram0_2.var_0 = 0; - ewram0_2.var_3 = 0; + TrainerCard_FillTrainerCardStruct(); + ewram0_2.current_state = 0; + ewram0_2.backSideShown = FALSE; ewram0_2.var_4 = FALSE; - ewram0_2.var_2 = ewram0_2.var_64.stars; - ewram0_2.var_5 = 0; - ewram0_2.var_6 = 0; + ewram0_2.starCount = ewram0_2.displayedCard.stars; + ewram0_2.showColon = 0; + ewram0_2.frameCounter = 0; for (i = 0; i < 4; i++) - EasyChat_GetWordText(ewram0_2.var_20[i], ewram0_2.var_64.var_28[i]); - sub_80936D4(); + EasyChat_GetWordText(ewram0_2.easyChatPhrase[i], ewram0_2.displayedCard.var_28[i]); + TrainerCard_FillFlags(); } -void sub_80936D4(void) +static void TrainerCard_FillFlags(void) { - ewram0_2.var_7 = 0; - ewram0_2.var_8 = 0; - ewram0_2.var_9 = 0; - ewram0_2.var_a = 0; - ewram0_2.var_b = 0; - ewram0_2.var_c = 0; - ewram0_2.var_d = 0; - memset(ewram0_2.var_e, 0, sizeof(ewram0_2.var_e)); + ewram0_2.showPokedexCount = 0; + ewram0_2.showHallOfFame = 0; + ewram0_2.showLinkBattleStatus = 0; + ewram0_2.showBattleTowerStatus = 0; + ewram0_2.showContestRecord = 0; + ewram0_2.showMixingRecord = 0; + ewram0_2.showTradingRecord = 0; + memset(ewram0_2.ownedBadges, 0, sizeof(ewram0_2.ownedBadges)); - if (ewram0_2.var_64.hasPokedex) - ewram0_2.var_7++; + if (ewram0_2.displayedCard.hasPokedex) + ewram0_2.showPokedexCount++; - if (ewram0_2.var_64.firstHallOfFameA != 0 - || ewram0_2.var_64.firstHallOfFameB != 0 - || ewram0_2.var_64.firstHallOfFameC != 0) - ewram0_2.var_8++; + if (ewram0_2.displayedCard.firstHallOfFameA != 0 + || ewram0_2.displayedCard.firstHallOfFameB != 0 + || ewram0_2.displayedCard.firstHallOfFameC != 0) + ewram0_2.showHallOfFame++; - if (ewram0_2.var_64.linkBattleWins != 0 || ewram0_2.var_64.linkBattleLosses != 0) - ewram0_2.var_9++; + if (ewram0_2.displayedCard.linkBattleWins != 0 || ewram0_2.displayedCard.linkBattleLosses != 0) + ewram0_2.showLinkBattleStatus++; - if (ewram0_2.var_64.battleTowerWins != 0 || ewram0_2.var_64.battleTowerLosses != 0) - ewram0_2.var_a++; + if (ewram0_2.displayedCard.battleTowerWins != 0 || ewram0_2.displayedCard.battleTowerLosses != 0) + ewram0_2.showBattleTowerStatus++; - if (ewram0_2.var_64.contestsWithFriends != 0) - ewram0_2.var_b++; + if (ewram0_2.displayedCard.contestsWithFriends != 0) + ewram0_2.showContestRecord++; - if (ewram0_2.var_64.pokeblocksWithFriends != 0) - ewram0_2.var_c++; + if (ewram0_2.displayedCard.pokeblocksWithFriends != 0) + ewram0_2.showMixingRecord++; - if (ewram0_2.var_64.pokemonTrades != 0) - ewram0_2.var_d++; + if (ewram0_2.displayedCard.pokemonTrades != 0) + ewram0_2.showTradingRecord++; - if (!ewram0_2.var_1) + if (!ewram0_2.isShowingLinkCard) { u32 badgeFlag; int i = 0; @@ -544,7 +594,7 @@ void sub_80936D4(void) while (1) { if (FlagGet(badgeFlag)) - ewram0_2.var_e[i]++; + ewram0_2.ownedBadges[i]++; badgeFlag++; i++; if (badgeFlag > FLAG_BADGE08_GET) @@ -553,6 +603,19 @@ void sub_80936D4(void) } } } + +#if DEBUG + if (gDebug_03000748 != 0) + { + ewram0_2.showHallOfFame = TRUE; + ewram0_2.showLinkBattleStatus = TRUE; + ewram0_2.showBattleTowerStatus = TRUE; + ewram0_2.showContestRecord = TRUE; + ewram0_2.showMixingRecord = TRUE; + ewram0_2.showTradingRecord = TRUE; + memset(ewram0_2.ownedBadges, TRUE, sizeof(ewram0_2.ownedBadges)); + } +#endif } void sub_80937A4() @@ -565,21 +628,21 @@ void sub_80937A4() void sub_80937BC() { - SetUpWindowConfig(&WindowConfig_TrainerCard_Back_Values); - MultistepInitMenuWindowBegin(&WindowConfig_TrainerCard_Back_Values); + Text_LoadWindowTemplate(&gWindowTemplate_TrainerCard_Back_Values); + MultistepInitMenuWindowBegin(&gWindowTemplate_TrainerCard_Back_Values); } static void sub_80937D8() { - sub_8093E04(); - sub_8093E28(); + TrainerCard_ResetOffsetRegisters(); + TrainerCard_CopyGraphics(); sub_8093F64(); - sub_8093DAC(); + TrainerCard_DrawCard(); } static void sub_80937F0() { - sub_8093EF8(); + TrainerCard_LoadTrainerGraphics(); } static void nullsub_15(void) @@ -588,159 +651,173 @@ static void nullsub_15(void) static void sub_8093800() { - sub_809380C(); + TrainerCard_CreateStateMachine(); } -static void sub_809380C() +static void TrainerCard_CreateStateMachine(void) { u8 taskId; - taskId = CreateTask(sub_809382C, 0); - sub_809382C(taskId); + taskId = CreateTask(TrainerCard_RunStateMachine, 0); + TrainerCard_RunStateMachine(taskId); } -static void sub_809382C(u8 taskId) +static void TrainerCard_RunStateMachine(u8 taskId) { - while (gUnknown_083B5EBC[ewram0_2.var_0](&gTasks[taskId]) != 0) + while (TrainerCard_StateMachine[ewram0_2.current_state](&gTasks[taskId]) != 0) ; } -bool8 sub_8093864(struct Task *task) +bool8 TrainerCard_Init(struct Task *task) { - ewram0_2.var_5 = gSaveBlock2.playTimeSeconds & 1; - ewram0_2.var_6 = gSaveBlock2.playTimeVBlanks; - sub_80939A4(); + ewram0_2.showColon = gSaveBlock2.playTimeSeconds & 1; + ewram0_2.frameCounter = gSaveBlock2.playTimeVBlanks; + TrainerCard_CreatePrintPlayTimeTask(); BeginNormalPaletteFade(0xFFFFFFFF, 0, 16, 0, 0); - ewram0_2.var_0++; + ewram0_2.current_state++; /* Advance state machine */ return FALSE; } -bool8 sub_80938A8(struct Task *task) +bool8 TrainerCard_WaitForFadeInToFinish(struct Task *task) { if (!gPaletteFade.active) - ewram0_2.var_0++; + ewram0_2.current_state++; /* Advance state machine */ return FALSE; } -bool8 sub_80938CC(struct Task *task) +bool8 TrainerCard_WaitForKeys(struct Task *task) { if (gMain.newKeys & B_BUTTON) { - ewram0_2.var_0 = 5; + ewram0_2.current_state = 5; /* Jump to fadeout state */ return TRUE; } else if (gMain.newKeys & A_BUTTON) { - if (ewram0_2.var_3 != 0) + /* It appears that it was previously possible to return the the front side + after viewing the back side. This was probably removed due to being + unintuitive. */ + if (ewram0_2.backSideShown) { - ewram0_2.var_0 = 5; + ewram0_2.current_state = 5; /* Jump to fadeout state */ } else { - ewram0_2.var_3 ^= 1; - ewram0_2.var_0 = 3; + ewram0_2.backSideShown ^= 1; /* Switch to back side */ + ewram0_2.current_state = 3; /* Jump to start flip animation state */ } return TRUE; } +#if DEBUG + else if (gDebug_03000748 && gMain.newKeys & R_BUTTON) + { + ewram0_2.starCount++; + ewram0_2.starCount %= 5; + TrainerCard_LoadPalettes(); + if (ewram0_2.backSideShown == 0) + TrainerCard_DrawStars(); + } +#endif + return FALSE; } -bool8 sub_8093918(struct Task *task) +bool8 TrainerCard_StartFlipAntimation(struct Task *task) { - sub_8093A28(); + TrainerCard_CreateFlipAnimationTask(); PlaySE(SE_CARD); - ewram0_2.var_0++; + ewram0_2.current_state++; /* Advance state machine */ return FALSE; } -bool8 sub_8093938(struct Task *task) +bool8 TrainerCard_WaitForFlipToFinish(struct Task *task) { - if (sub_8093A48()) - ewram0_2.var_0 = 2; + if (TrainerCard_HasFlipAnimationFinished()) + ewram0_2.current_state = 2; /* Return to wait for keys state */ return FALSE; } -bool8 sub_8093954(struct Task *task) +bool8 TrainerCard_FadeOut(struct Task *task) { - sub_80939C0(); + TrainerCard_DestoryPlayTimeTask(); BeginNormalPaletteFade(0xFFFFFFFF, 0, 0, 16, 0); - ewram0_2.var_0++; + ewram0_2.current_state++; /* Advance state machine */ return FALSE; } -bool8 sub_8093980(struct Task *task) +bool8 TrainerCard_WaitForFadeOutToFinishAndQuit(struct Task *task) { if (!gPaletteFade.active) SetMainCallback2((MainCallback)ewram0_2.var_60); return FALSE; } -static void sub_80939A4(void) +static void TrainerCard_CreatePrintPlayTimeTask(void) { - CreateTask(sub_80939DC, 0); - BasicInitMenuWindow(&WindowConfig_TrainerCard_Back_Values); + CreateTask(TrainerCard_Front_PrintPlayTime, 0); + BasicInitMenuWindow(&gWindowTemplate_TrainerCard_Back_Values); } -static void sub_80939C0(void) +static void TrainerCard_DestoryPlayTimeTask(void) { - u8 taskId = FindTaskIdByFunc(sub_80939DC); + u8 taskId = FindTaskIdByFunc(TrainerCard_Front_PrintPlayTime); if (taskId != 0xFF) DestroyTask(taskId); } -static void sub_80939DC(u8 taskId) +static void TrainerCard_Front_PrintPlayTime(u8 taskId) { u8 buffer[32]; struct Task *task = &gTasks[taskId]; - if (ewram0_2.var_5 != task->data[TD_1]) + if (ewram0_2.showColon != task->data[TD_CARD_INDEX]) { - task->data[TD_1] = ewram0_2.var_5; - task->data[TD_0] ^= TRUE; + task->data[TD_CARD_INDEX] = ewram0_2.showColon; + task->data[TD_SHOWING_LINK_CARD] ^= TRUE; } - TrainerCard_Front_PrintPlayTime(buffer, task->data[TD_0]); - MenuPrint(buffer, 10, 12); + TrainerCard_Front_GetPlayTimeString(buffer, task->data[TD_SHOWING_LINK_CARD]); + Menu_PrintText(buffer, 10, 12); } -static void sub_8093A28(void) +static void TrainerCard_CreateFlipAnimationTask(void) { u8 taskId; - taskId = CreateTask(sub_8093A68, 0); - sub_8093A68(taskId); + taskId = CreateTask(TrainerCard_RunFlipAnimationStateMachine, 0); + TrainerCard_RunFlipAnimationStateMachine(taskId); } -static u8 sub_8093A48(void) +static u8 TrainerCard_HasFlipAnimationFinished(void) { - if (FindTaskIdByFunc(sub_8093A68) == 0xFF) + if (FindTaskIdByFunc(TrainerCard_RunFlipAnimationStateMachine) == 0xFF) return TRUE; else return FALSE; } -static void sub_8093A68(u8 taskId) +static void TrainerCard_RunFlipAnimationStateMachine(u8 taskId) { - while (gUnknown_083B5ED8[gTasks[taskId].data[0]](&gTasks[taskId]) != 0) + while (TrainerCard_FlipAnimationStateMachine[gTasks[taskId].data[0]](&gTasks[taskId]) != 0) ; } -bool8 sub_8093AA0(struct Task *task) +bool8 TrainerCard_InitFlipAnimation(struct Task *task) { u32 i; ewram0_2.var_4 = FALSE; - dp12_8087EA4(); + ScanlineEffect_Clear(); for (i = 0; i < 0xA0; i++) - gUnknown_03004DE0[1][i] = -4; - SetHBlankCallback(sub_8093D7C); + gScanlineEffectRegBuffers[1][i] = -4; + SetHBlankCallback(TrainerCard_FlipAnimationHBlankCallback); ewram0_2.var_4 = TRUE; task->data[0]++; return FALSE; } /* -bool8 sub_8093AF0(struct Task *task) +bool8 TrainerCard_ScaleDownFlipAnimation(struct Task *task) { u32 r7; u16 r9; @@ -768,7 +845,7 @@ bool8 sub_8093AF0(struct Task *task) for (i = 0; i < r7; i++) { - gUnknown_03004DE0.filler0[i] = -4 - (u32)i; + gScanlineEffectRegBuffers.filler0[i] = -4 - (u32)i; } //_08093B74 for (; i < r9; i++) @@ -776,10 +853,10 @@ bool8 sub_8093AF0(struct Task *task) u16 var = r6 >> 16; r6 += r5; r5 -= sp0; - gUnknown_03004DE0.filler0[i] = -4 + var; + gScanlineEffectRegBuffers.filler0[i] = -4 + var; } for (; i < 160; i++) - gUnknown_03004DE0.filler0[i] = -4 + (u16)(r10 >> 16); + gScanlineEffectRegBuffers.filler0[i] = -4 + (u16)(r10 >> 16); ewram0_2.var_4 = 1; if (task->data[1] > 0x4A) task->data[0]++; @@ -788,7 +865,7 @@ bool8 sub_8093AF0(struct Task *task) */ __attribute__((naked)) -bool8 sub_8093AF0(struct Task *task) +bool8 TrainerCard_ScaleDownFlipAnimation(struct Task *task) { asm(".syntax unified\n\ push {r4-r7,lr}\n\ @@ -840,7 +917,7 @@ _08093B18:\n\ movs r3, 0\n\ cmp r3, r7\n\ bcs _08093B74\n\ - ldr r2, _08093C04 @ =gUnknown_03004DE0\n\ + ldr r2, _08093C04 @ =gScanlineEffectRegBuffers\n\ mov r12, r2\n\ ldr r0, _08093C08 @ =0x0000fffc\n\ adds r4, r0, 0\n\ @@ -866,7 +943,7 @@ _08093B74:\n\ lsrs r7, r4, 16\n\ cmp r2, r0\n\ bge _08093BAE\n\ - ldr r0, _08093C04 @ =gUnknown_03004DE0\n\ + ldr r0, _08093C04 @ =gScanlineEffectRegBuffers\n\ mov r9, r0\n\ ldr r4, _08093C08 @ =0x0000fffc\n\ mov r12, r4\n\ @@ -894,7 +971,7 @@ _08093BAE:\n\ asrs r0, 16\n\ cmp r0, 0x9F\n\ bgt _08093BD4\n\ - ldr r4, _08093C04 @ =gUnknown_03004DE0\n\ + ldr r4, _08093C04 @ =gScanlineEffectRegBuffers\n\ ldr r0, _08093C08 @ =0x0000fffc\n\ adds r2, r1, r0\n\ _08093BBE:\n\ @@ -934,23 +1011,25 @@ _08093BEA:\n\ .align 2, 0\n\ _08093BFC: .4byte gSharedMem\n\ _08093C00: .4byte 0xffff0000\n\ -_08093C04: .4byte gUnknown_03004DE0\n\ +_08093C04: .4byte gScanlineEffectRegBuffers\n\ _08093C08: .4byte 0x0000fffc\n\ .syntax divided\n"); } -bool8 sub_8093C0C(struct Task *task) +bool8 TrainerCard_SwitchToNewSide(struct Task *task) { - sub_80939C0(); - sub_8093DAC(); - if (!ewram0_2.var_3) - sub_80939A4(); + TrainerCard_DestoryPlayTimeTask(); + TrainerCard_DrawCard(); + if (!ewram0_2.backSideShown) { + /* This code never runs because it is impossible to flip the back side back to the front side */ + TrainerCard_CreatePrintPlayTimeTask(); + } task->data[0]++; return TRUE; } __attribute__((naked)) -bool8 sub_8093C38(struct Task *task) +bool8 TrainerCard_ScaleUpFlipAnimation(struct Task *task) { asm(".syntax unified\n\ push {r4-r7,lr}\n\ @@ -1000,7 +1079,7 @@ _08093C5C:\n\ movs r3, 0\n\ cmp r3, r7\n\ bcs _08093CB8\n\ - ldr r2, _08093D48 @ =gUnknown_03004DE0\n\ + ldr r2, _08093D48 @ =gScanlineEffectRegBuffers\n\ mov r12, r2\n\ ldr r0, _08093D4C @ =0x0000fffc\n\ adds r4, r0, 0\n\ @@ -1026,7 +1105,7 @@ _08093CB8:\n\ lsrs r7, r4, 16\n\ cmp r2, r0\n\ bge _08093CF2\n\ - ldr r0, _08093D48 @ =gUnknown_03004DE0\n\ + ldr r0, _08093D48 @ =gScanlineEffectRegBuffers\n\ mov r9, r0\n\ ldr r3, _08093D4C @ =0x0000fffc\n\ mov r12, r3\n\ @@ -1054,7 +1133,7 @@ _08093CF2:\n\ asrs r0, 16\n\ cmp r0, 0x9F\n\ bgt _08093D18\n\ - ldr r4, _08093D48 @ =gUnknown_03004DE0\n\ + ldr r4, _08093D48 @ =gScanlineEffectRegBuffers\n\ ldr r0, _08093D4C @ =0x0000fffc\n\ adds r2, r1, r0\n\ _08093D02:\n\ @@ -1094,60 +1173,60 @@ _08093D2E:\n\ .align 2, 0\n\ _08093D40: .4byte gSharedMem\n\ _08093D44: .4byte 0xffff0000\n\ -_08093D48: .4byte gUnknown_03004DE0\n\ +_08093D48: .4byte gScanlineEffectRegBuffers\n\ _08093D4C: .4byte 0x0000fffc\n\ .syntax divided\n"); } -bool8 sub_8093D50(struct Task *task) +bool8 TrainerCard_FinishFlipAnimation(struct Task *task) { u8 taskId; ewram0_2.var_4 = FALSE; SetHBlankCallback(NULL); - sub_8093E04(); - taskId = FindTaskIdByFunc(sub_8093A68); + TrainerCard_ResetOffsetRegisters(); + taskId = FindTaskIdByFunc(TrainerCard_RunFlipAnimationStateMachine); DestroyTask(taskId); return FALSE; } -void sub_8093D7C(void) +static void TrainerCard_FlipAnimationHBlankCallback(void) { - u16 bgVOffset = gUnknown_03004DE0[1][REG_VCOUNT & 0xFF]; + u16 bgVOffset = gScanlineEffectRegBuffers[1][REG_VCOUNT & 0xFF]; REG_BG0VOFS = bgVOffset; REG_BG1VOFS = bgVOffset; REG_BG2VOFS = bgVOffset; } -static void sub_8093DAC(void) +static void TrainerCard_DrawCard(void) { - if (ewram0_2.var_3) - sub_8093DEC(); + if (ewram0_2.backSideShown) + TrainerCard_DrawCardBack(); else - sub_8093DC8(); + TrainerCard_DrawCardFront(); } -static void sub_8093DC8(void) +static void TrainerCard_DrawCardFront(void) { - MenuZeroFillScreen(); - sub_80940E4(); + Menu_EraseScreen(); + TrainerCard_ClearTrainerGraphics(); sub_8093F14(); - sub_8093F80(); - sub_8093FD0(); - sub_8094038(); - sub_8094140(); + TrainerCard_LoadTrainerTilemap(); + TrainerCard_DrawStars(); + TrainerCard_DisplayBadges(); + TrainerCard_Front_PrintTexts(); } -static void sub_8093DEC(void) +static void TrainerCard_DrawCardBack(void) { - MenuZeroFillScreen(); - sub_80940E4(); + Menu_EraseScreen(); + TrainerCard_ClearTrainerGraphics(); sub_8093F48(); - sub_8094188(); + TrainerCard_Back_PrintTexts(); } -static void sub_8093E04(void) +static void TrainerCard_ResetOffsetRegisters(void) { REG_BG0VOFS = -4; REG_BG1HOFS = 0; @@ -1156,65 +1235,42 @@ static void sub_8093E04(void) REG_BG2VOFS = -4; } -static void sub_8093E28(void) +static void TrainerCard_CopyGraphics(void) { - const u8 *src; - u8 *dst; - u32 size; - - sub_8093EA0(); + TrainerCard_LoadPalettes(); LoadPalette(gUnknown_083B5F6C, 0xE0, 32); - src = gMenuTrainerCard_Gfx; - dst = (void *)VRAM; - size = 0x1480; - while (1) - { - DmaCopy16(3, src, dst, 0x1000); - src += 0x1000; - dst += 0x1000; - size -= 0x1000; - if (size <= 0x1000) - { - DmaCopy16(3, src, dst, size); - break; - } - } - { - const void *src = gBadgesTiles; - void *dst = (void *)(VRAM + 0x1480); - - DmaCopy16(3, src, dst, 0x400); - } + DmaCopyLarge16(3, gMenuTrainerCard_Gfx, (void *)VRAM, 0x1480, 0x1000); + DmaCopy16Defvars(3, gBadgesTiles, (void *)(VRAM + 0x1480), 0x400); } -extern const u16 *const gUnknown_083B5EF8[]; +extern const u16 *const gTrainerCardPalettes[]; -void sub_8093EA0(void) +static void TrainerCard_LoadPalettes(void) { - LoadPalette(gUnknown_083B5EF8[ewram0_2.var_2], 0, 48 * 2); + LoadPalette(gTrainerCardPalettes[ewram0_2.starCount], 0, 48 * 2); LoadPalette(gBadgesPalette, 48, 16 * 2); LoadPalette(gUnknown_083B5F4C, 64, 16 * 2); - if (ewram0_2.var_64.gender != MALE) + if (ewram0_2.displayedCard.gender != MALE) LoadPalette(gUnknown_083B5F0C, 16, 16 * 2); } -static void sub_8093EF8(void) +static void TrainerCard_LoadTrainerGraphics(void) { - LoadTrainerGfx_TrainerCard(ewram0_2.var_64.gender, 80, (void *)(VRAM + 0x1880)); + LoadTrainerGfx_TrainerCard(ewram0_2.displayedCard.gender, 80, (void *)(VRAM + 0x1880)); } static void sub_8093F14(void) { const void *arr[] = {gUnknown_08E8CAC0, gUnknown_08E8D4C0}; - CpuFastSet(arr[ewram0_2.var_1], (void *)(VRAM + 0x4800), 0x140); + CpuFastSet(arr[ewram0_2.isShowingLinkCard], (void *)(VRAM + 0x4800), 0x140); } // I don't really know where to put the data. It's in such a weird order. const u8 gUnknown_083B5EF4[] = _(" : "); -const u16 *const gUnknown_083B5EF8[] = +const u16 *const gTrainerCardPalettes[] = { gMenuTrainerCard0Star_Pal, gMenuTrainerCard1Star_Pal, @@ -1227,7 +1283,7 @@ const u16 gUnknown_083B5F0C[] = INCBIN_U16("graphics/trainer_card/83B5F0C.gbapal const u16 gBadgesPalette[] = INCBIN_U16("graphics/trainer_card/badges.gbapal"); const u16 gUnknown_083B5F4C[] = INCBIN_U16("graphics/trainer_card/83B5F4C.gbapal"); const u16 gUnknown_083B5F6C[] = INCBIN_U16("graphics/trainer_card/83B5F6C.gbapal"); -const u16 gUnknown_083B5F8C[][4] = INCBIN_U16("graphics/trainer_card/83B5F8C_map.bin"); +const u16 gTrainerCardBadgesMap[][4] = INCBIN_U16("graphics/trainer_card/83B5F8C_map.bin"); static void sub_8093F48(void) { @@ -1239,7 +1295,7 @@ static void sub_8093F64(void) CpuFastSet(gUnknown_08E8D9C0, (void *)(VRAM + 0x5000), 320); } -static void sub_8093F80(void) +static void TrainerCard_LoadTrainerTilemap(void) { u16 r5 = 0xC4; u16 *ptr = (u16 *)(VRAM + 0x4000); @@ -1253,11 +1309,11 @@ static void sub_8093F80(void) } } -static void sub_8093FD0(void) +static void TrainerCard_DrawStars(void) { u16 *ptr = (u16 *)(VRAM + 0x4000); s16 i = 15; - s16 var = 15 + ewram0_2.var_2; + s16 var = 15 + ewram0_2.starCount; while (i < var) { @@ -1271,9 +1327,9 @@ static void sub_8093FD0(void) } } -static void sub_8094038(void) +static void TrainerCard_DisplayBadges(void) { - if (ewram0_2.var_1 == 0) + if (!ewram0_2.isShowingLinkCard) { u16 *ptr = (u16 *)(VRAM + 0x4000); s16 i; @@ -1281,18 +1337,18 @@ static void sub_8094038(void) for (i = 0, r2 = 4; i < 8; i++, r2 += 3) { - if (ewram0_2.var_e[i] != 0) + if (ewram0_2.ownedBadges[i] != 0) { - ptr[15 * 32 + r2 + 0] = gUnknown_083B5F8C[i][0] | 0x3000; - ptr[15 * 32 + r2 + 1] = gUnknown_083B5F8C[i][1] | 0x3000; - ptr[16 * 32 + r2 + 0] = gUnknown_083B5F8C[i][2] | 0x3000; - ptr[16 * 32 + r2 + 1] = gUnknown_083B5F8C[i][3] | 0x3000; + ptr[15 * 32 + r2 + 0] = gTrainerCardBadgesMap[i][0] | 0x3000; + ptr[15 * 32 + r2 + 1] = gTrainerCardBadgesMap[i][1] | 0x3000; + ptr[16 * 32 + r2 + 0] = gTrainerCardBadgesMap[i][2] | 0x3000; + ptr[16 * 32 + r2 + 1] = gTrainerCardBadgesMap[i][3] | 0x3000; } } } } -static void sub_80940E4(void) +static void TrainerCard_ClearTrainerGraphics(void) { s16 i; u16 *ptr; @@ -1301,7 +1357,7 @@ static void sub_80940E4(void) *ptr = 0; } -static void sub_8094110(void) +static void TrainerCard_ClearPokedexLabel(void) { u16 *ptr = (u16 *)(VRAM + 0x4800); u16 i; @@ -1313,26 +1369,26 @@ static void sub_8094110(void) } } -static void sub_8094140(void) +static void TrainerCard_Front_PrintTexts(void) { u8 *buffer; - BasicInitMenuWindow(&WindowConfig_TrainerCard_Back_Values); + BasicInitMenuWindow(&gWindowTemplate_TrainerCard_Back_Values); buffer = gStringVar1; - StringCopy(buffer, ewram0_2.var_64.playerName); + StringCopy(buffer, ewram0_2.displayedCard.playerName); ConvertInternationalString(buffer, ewram0_2.language); - MenuPrint(buffer, 7, 5); + Menu_PrintText(buffer, 7, 5); TrainerCard_Front_PrintTrainerID(); TrainerCard_Front_PrintMoney(); TrainerCard_Front_PrintPokedexCount(); - sub_809429C(); + TrainerCard_PrintEasyChatPhrase(); } -static void sub_8094188(void) +static void TrainerCard_Back_PrintTexts(void) { - BasicInitMenuWindow(&WindowConfig_TrainerCard_Back_Values); + BasicInitMenuWindow(&gWindowTemplate_TrainerCard_Back_Values); TrainerCard_Back_PrintName(); TrainerCard_Back_PrintHallOfFameTime_Label(); TrainerCard_Back_PrintLinkBattlesLabel(); @@ -1341,7 +1397,7 @@ static void sub_8094188(void) TrainerCard_Back_PrintLinkPokeblocks_Label(); TrainerCard_Back_PrintPokemonTrades_Label(); - BasicInitMenuWindow(&WindowConfig_TrainerCard_Back_Labels); + BasicInitMenuWindow(&gWindowTemplate_TrainerCard_Back_Labels); TrainerCard_Back_PrintHallOfFameTime(); TrainerCard_Back_PrintLinkBattles(); TrainerCard_Back_PrintBattleTower(); @@ -1354,31 +1410,35 @@ static void TrainerCard_Front_PrintTrainerID(void) { u8 buffer[8]; - ConvertIntToDecimalStringN(buffer, ewram0_2.var_64.trainerId, STR_CONV_MODE_LEADING_ZEROS, 5); - MenuPrint(buffer, 20, 2); + ConvertIntToDecimalStringN(buffer, ewram0_2.displayedCard.trainerId, STR_CONV_MODE_LEADING_ZEROS, 5); + Menu_PrintText(buffer, 20, 2); } static void TrainerCard_Front_PrintMoney(void) { - sub_80B7AEC(ewram0_2.var_64.money, 16, 8); + sub_80B7AEC(ewram0_2.displayedCard.money, 16, 8); } static void TrainerCard_Front_PrintPokedexCount(void) { u8 buffer[16]; - if (ewram0_2.var_7 == FALSE) + if ( +#if DEBUG + gDebug_03000748 == 0 && +#endif + !ewram0_2.showPokedexCount) { - sub_8094110(); + TrainerCard_ClearPokedexLabel(); } else { - ConvertIntToDecimalStringN(buffer, ewram0_2.var_64.pokedexSeen, STR_CONV_MODE_LEFT_ALIGN, 3); + ConvertIntToDecimalStringN(buffer, ewram0_2.displayedCard.pokedexSeen, STR_CONV_MODE_LEFT_ALIGN, 3); MenuPrint_RightAligned(buffer, 16, 10); } } -static void TrainerCard_Front_PrintPlayTime(u8 *arg1, s16 colon) +static void TrainerCard_Front_GetPlayTimeString(u8 *arg1, s16 colon) { u8 buffer[16]; u16 playTimeHours; @@ -1386,34 +1446,34 @@ static void TrainerCard_Front_PrintPlayTime(u8 *arg1, s16 colon) playTimeHours = gSaveBlock2.playTimeHours; playTimeMinutes = gSaveBlock2.playTimeMinutes; - if (ewram0_2.var_1 != 0) + if (ewram0_2.isShowingLinkCard != 0) { - playTimeHours = ewram0_2.var_64.playTimeHours; - playTimeMinutes = ewram0_2.var_64.playTimeMinutes; + playTimeHours = ewram0_2.displayedCard.playTimeHours; + playTimeMinutes = ewram0_2.displayedCard.playTimeMinutes; } FormatPlayTime(buffer, playTimeHours, playTimeMinutes, colon); sub_8072C74(arg1, buffer, 48, 1); } -static void sub_809429C(void) +static void TrainerCard_PrintEasyChatPhrase(void) { u8 *str; - if (ewram0_2.var_1 != 0) + if (ewram0_2.isShowingLinkCard != 0) { str = gStringVar1; - str = StringCopy(str, ewram0_2.var_20[0]); + str = StringCopy(str, ewram0_2.easyChatPhrase[0]); str[0] = 00; str++; - str = StringCopy(str, ewram0_2.var_20[1]); - MenuPrint(gStringVar1, 2, 14); + str = StringCopy(str, ewram0_2.easyChatPhrase[1]); + Menu_PrintText(gStringVar1, 2, 14); str = gStringVar1; - str = StringCopy(str, ewram0_2.var_20[2]); + str = StringCopy(str, ewram0_2.easyChatPhrase[2]); str[0] = 00; str++; - str = StringCopy(str, ewram0_2.var_20[3]); - MenuPrint(gStringVar1, 2, 16); + str = StringCopy(str, ewram0_2.easyChatPhrase[3]); + Menu_PrintText(gStringVar1, 2, 16); } } @@ -1422,7 +1482,7 @@ static void TrainerCard_Back_PrintName(void) u8 *str; str = gStringVar1; - StringCopy(str, ewram0_2.var_64.playerName); + StringCopy(str, ewram0_2.displayedCard.playerName); ConvertInternationalString(str, ewram0_2.language); #if ENGLISH @@ -1436,113 +1496,113 @@ static void TrainerCard_Back_PrintName(void) static void TrainerCard_Back_PrintHallOfFameTime_Label(void) { - if (ewram0_2.var_8 != 0) - MenuPrint(gOtherText_FirstHOF, 3, 5); + if (ewram0_2.showHallOfFame != 0) + Menu_PrintText(gOtherText_FirstHOF, 3, 5); } static void TrainerCard_Back_PrintHallOfFameTime(void) { u8 *str; - if (ewram0_2.var_8 != 0) + if (ewram0_2.showHallOfFame != 0) { str = gStringVar1; - str = ConvertIntToDecimalStringN(str, ewram0_2.var_64.firstHallOfFameA, STR_CONV_MODE_RIGHT_ALIGN, 3); + str = ConvertIntToDecimalStringN(str, ewram0_2.displayedCard.firstHallOfFameA, STR_CONV_MODE_RIGHT_ALIGN, 3); str = StringCopy(str, gUnknown_083B5EF4); - str = ConvertIntToDecimalStringN(str, ewram0_2.var_64.firstHallOfFameB, STR_CONV_MODE_LEADING_ZEROS, 2); + str = ConvertIntToDecimalStringN(str, ewram0_2.displayedCard.firstHallOfFameB, STR_CONV_MODE_LEADING_ZEROS, 2); str = StringCopy(str, gUnknown_083B5EF4); - str = ConvertIntToDecimalStringN(str, ewram0_2.var_64.firstHallOfFameC, STR_CONV_MODE_LEADING_ZEROS, 2); + str = ConvertIntToDecimalStringN(str, ewram0_2.displayedCard.firstHallOfFameC, STR_CONV_MODE_LEADING_ZEROS, 2); MenuPrint_RightAligned(gStringVar1, 28, 5); } } static void TrainerCard_Back_PrintLinkBattlesLabel(void) { - if (ewram0_2.var_9 != 0) - MenuPrint(gOtherText_LinkCableBattles, 3, 7); + if (ewram0_2.showLinkBattleStatus != 0) + Menu_PrintText(gOtherText_LinkCableBattles, 3, 7); } static void TrainerCard_Back_PrintLinkBattles(void) { u8 buffer[16]; - if (ewram0_2.var_9 != 0) + if (ewram0_2.showLinkBattleStatus != 0) { - ConvertIntToDecimalString(buffer, ewram0_2.var_64.linkBattleWins); + ConvertIntToDecimalString(buffer, ewram0_2.displayedCard.linkBattleWins); MenuPrint_RightAligned(buffer, 22, 7); - ConvertIntToDecimalString(buffer, ewram0_2.var_64.linkBattleLosses); + ConvertIntToDecimalString(buffer, ewram0_2.displayedCard.linkBattleLosses); MenuPrint_RightAligned(buffer, 28, 7); } } static void TrainerCard_Back_PrintBattleTower_Label(void) { - if (ewram0_2.var_a != 0) - MenuPrint(gOtherText_BattleTowerWinRecord, 3, 15); + if (ewram0_2.showBattleTowerStatus != 0) + Menu_PrintText(gOtherText_BattleTowerWinRecord, 3, 15); } static void TrainerCard_Back_PrintBattleTower(void) { u8 buffer[16]; - if (ewram0_2.var_a != 0) + if (ewram0_2.showBattleTowerStatus != 0) { - sub_8072C44(buffer, ewram0_2.var_64.battleTowerWins, 24, 1); - MenuPrint_PixelCoords(buffer, 112, 120, 0); + sub_8072C44(buffer, ewram0_2.displayedCard.battleTowerWins, 24, 1); + Menu_PrintTextPixelCoords(buffer, 112, 120, 0); - sub_8072C44(buffer, ewram0_2.var_64.battleTowerLosses, 24, 1); - MenuPrint_PixelCoords(buffer, 149, 120, 0); + sub_8072C44(buffer, ewram0_2.displayedCard.battleTowerLosses, 24, 1); + Menu_PrintTextPixelCoords(buffer, 149, 120, 0); } } static void TrainerCard_Back_PrintLinkContests_Label(void) { - if (ewram0_2.var_b != 0) - MenuPrint(gOtherText_ContestRecord, 3, 13); + if (ewram0_2.showContestRecord != 0) + Menu_PrintText(gOtherText_ContestRecord, 3, 13); } static void TrainerCard_Back_PrintLinkContests(void) { u8 buffer[8]; - if (ewram0_2.var_b != 0) + if (ewram0_2.showContestRecord != 0) { - ConvertIntToDecimalStringN(buffer, ewram0_2.var_64.contestsWithFriends, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(buffer, ewram0_2.displayedCard.contestsWithFriends, STR_CONV_MODE_RIGHT_ALIGN, 3); MenuPrint_RightAligned(buffer, 28, 13); } } static void TrainerCard_Back_PrintLinkPokeblocks_Label(void) { - if (ewram0_2.var_c != 0) - MenuPrint(gOtherText_MixingRecord, 3, 11); + if (ewram0_2.showMixingRecord != 0) + Menu_PrintText(gOtherText_MixingRecord, 3, 11); } static void TrainerCard_Back_PrintLinkPokeblocks(void) { u8 buffer[8]; - if (ewram0_2.var_c != 0) + if (ewram0_2.showMixingRecord != 0) { - ConvertIntToDecimalStringN(buffer, ewram0_2.var_64.pokeblocksWithFriends, STR_CONV_MODE_RIGHT_ALIGN, 5); + ConvertIntToDecimalStringN(buffer, ewram0_2.displayedCard.pokeblocksWithFriends, STR_CONV_MODE_RIGHT_ALIGN, 5); MenuPrint_RightAligned(buffer, 28, 11); } } static void TrainerCard_Back_PrintPokemonTrades_Label(void) { - if (ewram0_2.var_d != 0) - MenuPrint(gOtherText_TradeRecord, 3, 9); + if (ewram0_2.showTradingRecord != 0) + Menu_PrintText(gOtherText_TradeRecord, 3, 9); } static void TrainerCard_Back_PrintPokemonTrades(void) { u8 buffer[8]; - if (ewram0_2.var_d != 0) + if (ewram0_2.showTradingRecord != 0) { - ConvertIntToDecimalStringN(buffer, ewram0_2.var_64.pokemonTrades, STR_CONV_MODE_RIGHT_ALIGN, 5); + ConvertIntToDecimalStringN(buffer, ewram0_2.displayedCard.pokemonTrades, STR_CONV_MODE_RIGHT_ALIGN, 5); MenuPrint_RightAligned(buffer, 28, 9); } } @@ -1553,5 +1613,5 @@ void unref_sub_8094588(u16 left, u16 top) if (gSaveBlock2.playerGender == FEMALE) text = gOtherText_Girl; - MenuPrint(text, left, top); + Menu_PrintText(text, left, top); } |