From 306ce048ad07d62bed2028bfc8f30c03f5bc8db7 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 8 Sep 2019 21:07:54 -0400 Subject: Move gflib srcs and headers to gflib subdir --- Makefile | 23 +- gflib/bg.c | 1305 +++++++++++++++++ gflib/bg.h | 85 ++ gflib/blit.c | 209 +++ gflib/blit.h | 17 + gflib/dma3.h | 55 + gflib/dma3_manager.c | 181 +++ gflib/gpu_regs.c | 195 +++ gflib/gpu_regs.h | 19 + gflib/malloc.c | 210 +++ gflib/malloc.h | 22 + gflib/sprite.c | 1758 +++++++++++++++++++++++ gflib/sprite.h | 319 +++++ gflib/string_util.c | 784 +++++++++++ gflib/string_util.h | 46 + gflib/text.c | 2540 ++++++++++++++++++++++++++++++++++ gflib/text.h | 297 ++++ gflib/window.c | 721 ++++++++++ gflib/window.h | 78 ++ include/alloc.h | 22 - include/bg.h | 85 -- include/blit.h | 17 - include/dma3.h | 55 - include/gpu_regs.h | 19 - include/sprite.h | 319 ----- include/string_util.h | 46 - include/text.h | 297 ---- include/window.h | 78 -- ld_script.txt | 28 +- src/alloc.c | 210 --- src/apprentice.c | 2 +- src/battle_anim_effects_1.c | 2 +- src/battle_anim_effects_2.c | 2 +- src/battle_anim_effects_3.c | 2 +- src/battle_anim_mons.c | 2 +- src/battle_anim_utility_funcs.c | 2 +- src/battle_dome.c | 2 +- src/battle_factory_screen.c | 2 +- src/battle_gfx_sfx_util.c | 2 +- src/battle_main.c | 2 +- src/battle_pike.c | 2 +- src/battle_pyramid.c | 2 +- src/battle_pyramid_bag.c | 2 +- src/battle_records.c | 2 +- src/battle_transition.c | 2 +- src/battle_util2.c | 2 +- src/berry_blender.c | 2 +- src/berry_crush.c | 2 +- src/berry_fix_program.c | 2 +- src/berry_tag_screen.c | 2 +- src/bg.c | 1305 ----------------- src/blit.c | 209 --- src/cable_car.c | 2 +- src/contest.c | 2 +- src/contest_link_80F57C4.c | 2 +- src/contest_painting.c | 2 +- src/credits.c | 2 +- src/data.c | 2 +- src/decompress.c | 2 +- src/decoration.c | 2 +- src/dewford_trend.c | 2 +- src/diploma.c | 2 +- src/dma3_manager.c | 181 --- src/dodrio_berry_picking.c | 2 +- src/easy_chat.c | 2 +- src/egg_hatch.c | 2 +- src/ereader_helpers.c | 2 +- src/ereader_screen.c | 2 +- src/event_object_movement.c | 2 +- src/evolution_scene.c | 2 +- src/field_region_map.c | 2 +- src/field_specials.c | 2 +- src/fldeff_cut.c | 2 +- src/frontier_pass.c | 2 +- src/frontier_util.c | 2 +- src/gpu_regs.c | 195 --- src/hall_of_fame.c | 2 +- src/intro.c | 2 +- src/item.c | 2 +- src/item_icon.c | 2 +- src/item_menu.c | 2 +- src/link.c | 2 +- src/link_rfu.c | 2 +- src/list_menu.c | 2 +- src/load_save.c | 2 +- src/mail.c | 2 +- src/main.c | 2 +- src/match_call.c | 2 +- src/menu.c | 2 +- src/menu_specialized.c | 2 +- src/mevent_801BAAC.c | 2 +- src/mevent_client.c | 2 +- src/mevent_server.c | 2 +- src/mevent_server_helpers.c | 2 +- src/mirage_tower.c | 2 +- src/mossdeep_gym.c | 2 +- src/move_relearner.c | 2 +- src/mystery_gift.c | 2 +- src/naming_screen.c | 2 +- src/overworld.c | 2 +- src/party_menu.c | 2 +- src/player_pc.c | 2 +- src/pokeblock.c | 2 +- src/pokeblock_feed.c | 2 +- src/pokedex.c | 2 +- src/pokedex_area_screen.c | 2 +- src/pokedex_cry_screen.c | 2 +- src/pokemon.c | 2 +- src/pokemon_jump.c | 2 +- src/pokemon_storage_system.c | 2 +- src/pokemon_summary_screen.c | 2 +- src/pokenav.c | 2 +- src/pokenav_unk_2.c | 2 +- src/rayquaza_scene.c | 2 +- src/record_mixing.c | 2 +- src/recorded_battle.c | 2 +- src/region_map.c | 2 +- src/reset_save_heap.c | 2 +- src/rom_8034C54.c | 2 +- src/rom_81520A8.c | 2 +- src/roulette.c | 2 +- src/secret_base.c | 2 +- src/shop.c | 2 +- src/slot_machine.c | 2 +- src/sprite.c | 1758 ----------------------- src/string_util.c | 784 ----------- src/text.c | 2540 ---------------------------------- src/trade.c | 2 +- src/trainer_card.c | 2 +- src/trainer_hill.c | 2 +- src/trainer_pokemon_sprites.c | 2 +- src/tv.c | 2 +- src/union_room.c | 2 +- src/union_room_battle.c | 2 +- src/union_room_chat.c | 2 +- src/unk_pokedex_area_screen_helper.c | 2 +- src/use_pokeblock.c | 2 +- src/window.c | 721 ---------- sym_bss.txt | 12 +- sym_common.txt | 36 +- sym_ewram.txt | 8 +- 141 files changed, 9017 insertions(+), 8972 deletions(-) create mode 100644 gflib/bg.c create mode 100644 gflib/bg.h create mode 100644 gflib/blit.c create mode 100644 gflib/blit.h create mode 100644 gflib/dma3.h create mode 100644 gflib/dma3_manager.c create mode 100644 gflib/gpu_regs.c create mode 100644 gflib/gpu_regs.h create mode 100644 gflib/malloc.c create mode 100644 gflib/malloc.h create mode 100644 gflib/sprite.c create mode 100644 gflib/sprite.h create mode 100644 gflib/string_util.c create mode 100644 gflib/string_util.h create mode 100644 gflib/text.c create mode 100644 gflib/text.h create mode 100644 gflib/window.c create mode 100644 gflib/window.h delete mode 100644 include/alloc.h delete mode 100644 include/bg.h delete mode 100644 include/blit.h delete mode 100644 include/dma3.h delete mode 100644 include/gpu_regs.h delete mode 100644 include/sprite.h delete mode 100644 include/string_util.h delete mode 100644 include/text.h delete mode 100644 include/window.h delete mode 100644 src/alloc.c delete mode 100644 src/bg.c delete mode 100644 src/blit.c delete mode 100644 src/dma3_manager.c delete mode 100644 src/gpu_regs.c delete mode 100644 src/sprite.c delete mode 100644 src/string_util.c delete mode 100644 src/text.c delete mode 100644 src/window.c diff --git a/Makefile b/Makefile index 85d3f6a51..451a28740 100644 --- a/Makefile +++ b/Makefile @@ -43,6 +43,7 @@ ELF = $(ROM:.gba=.elf) MAP = $(ROM:.gba=.map) C_SUBDIR = src +GFLIB_SUBDIR = gflib ASM_SUBDIR = asm DATA_SRC_SUBDIR = src/data DATA_ASM_SUBDIR = data @@ -50,6 +51,7 @@ SONG_SUBDIR = sound/songs MID_SUBDIR = sound/songs/midi C_BUILDDIR = $(OBJ_DIR)/$(C_SUBDIR) +GFLIB_BUILDDIR = $(OBJ_DIR)/$(GFLIB_SUBDIR) ASM_BUILDDIR = $(OBJ_DIR)/$(ASM_SUBDIR) DATA_ASM_BUILDDIR = $(OBJ_DIR)/$(DATA_ASM_SUBDIR) SONG_BUILDDIR = $(OBJ_DIR)/$(SONG_SUBDIR) @@ -73,7 +75,7 @@ OBJ_DIR := build/modern LIBPATH := -L $(TOOLCHAIN)/lib/gcc/arm-none-eabi/$(GCC_VER)/thumb -L $(TOOLCHAIN)/arm-none-eabi/lib/thumb endif -CPPFLAGS := -iquote include -Wno-trigraphs -DMODERN=$(MODERN) +CPPFLAGS := -iquote include -iquote $(GFLIB_SUBDIR) -Wno-trigraphs -DMODERN=$(MODERN) ifeq ($(MODERN),0) CPPFLAGS += -I tools/agbcc/include -I tools/agbcc endif @@ -125,6 +127,9 @@ C_SRCS_IN := $(wildcard $(C_SUBDIR)/*.c $(C_SUBDIR)/*/*.c $(C_SUBDIR)/*/*/*.c) C_SRCS := $(foreach src,$(C_SRCS_IN),$(if $(findstring .inc.c,$(src)),,$(src))) C_OBJS := $(patsubst $(C_SUBDIR)/%.c,$(C_BUILDDIR)/%.o,$(C_SRCS)) +GFLIB_SRCS := $(wildcard $(GFLIB_SUBDIR)/*.c) +GFLIB_OBJS := $(patsubst $(GFLIB_SUBDIR)/%.c,$(GFLIB_BUILDDIR)/%.o,$(GFLIB_SRCS)) + C_ASM_SRCS += $(wildcard $(C_SUBDIR)/*.s $(C_SUBDIR)/*/*.s $(C_SUBDIR)/*/*/*.s) C_ASM_OBJS := $(patsubst $(C_SUBDIR)/%.s,$(C_BUILDDIR)/%.o,$(C_ASM_SRCS)) @@ -140,7 +145,7 @@ SONG_OBJS := $(patsubst $(SONG_SUBDIR)/%.s,$(SONG_BUILDDIR)/%.o,$(SONG_SRCS)) MID_SRCS := $(wildcard $(MID_SUBDIR)/*.mid) MID_OBJS := $(patsubst $(MID_SUBDIR)/%.mid,$(MID_BUILDDIR)/%.o,$(MID_SRCS)) -OBJS := $(C_OBJS) $(C_ASM_OBJS) $(ASM_OBJS) $(DATA_ASM_OBJS) $(SONG_OBJS) $(MID_OBJS) +OBJS := $(C_OBJS) $(GFLIB_OBJS) $(C_ASM_OBJS) $(ASM_OBJS) $(DATA_ASM_OBJS) $(SONG_OBJS) $(MID_OBJS) OBJS_REL := $(patsubst $(OBJ_DIR)/%,%,$(OBJS)) SUBDIRS := $(sort $(dir $(OBJS))) @@ -230,7 +235,7 @@ endif ifeq ($(NODEP),1) $(C_BUILDDIR)/%.o: c_dep := else -$(C_BUILDDIR)/%.o: c_dep = $(shell [[ -f $(C_SUBDIR)/$*.c ]] && $(SCANINC) -I include -I tools/agbcc/include $(C_SUBDIR)/$*.c) +$(C_BUILDDIR)/%.o: c_dep = $(shell [[ -f $(C_SUBDIR)/$*.c ]] && $(SCANINC) -I include -I tools/agbcc/include -I gflib $(C_SUBDIR)/$*.c) endif ifeq ($(DINFO),1) @@ -243,6 +248,18 @@ $(C_BUILDDIR)/%.o : $(C_SUBDIR)/%.c $$(c_dep) @echo -e ".text\n\t.align\t2, 0\n" >> $(C_BUILDDIR)/$*.s $(AS) $(ASFLAGS) -o $@ $(C_BUILDDIR)/$*.s +ifeq ($(NODEP),1) +$(GFLIB_BUILDDIR)/%.o: c_dep := +else +$(GFLIB_BUILDDIR)/%.o: c_dep = $(shell [[ -f $(GFLIB_SUBDIR)/$*.c ]] && $(SCANINC) -I include -I tools/agbcc/include -I gflib $(GFLIB_SUBDIR)/$*.c) +endif + +$(GFLIB_BUILDDIR)/%.o : $(GFLIB_SUBDIR)/%.c $$(c_dep) + @$(CPP) $(CPPFLAGS) $< -o $(GFLIB_BUILDDIR)/$*.i + @$(PREPROC) $(GFLIB_BUILDDIR)/$*.i charmap.txt | $(CC1) $(CFLAGS) -o $(GFLIB_BUILDDIR)/$*.s + @echo -e ".text\n\t.align\t2, 0\n" >> $(GFLIB_BUILDDIR)/$*.s + $(AS) $(ASFLAGS) -o $@ $(GFLIB_BUILDDIR)/$*.s + ifeq ($(NODEP),1) $(C_BUILDDIR)/%.o: c_asm_dep := else diff --git a/gflib/bg.c b/gflib/bg.c new file mode 100644 index 000000000..1678f4023 --- /dev/null +++ b/gflib/bg.c @@ -0,0 +1,1305 @@ +#include "global.h" +#include "bg.h" +#include "dma3.h" +#include "gpu_regs.h" + +#define DISPCNT_ALL_BG_AND_MODE_BITS (DISPCNT_BG_ALL_ON | 0x7) + +struct BgControl +{ + struct BgConfig { + u16 visible:1; + u16 unknown_1:1; + u16 screenSize:2; + u16 priority:2; + u16 mosaic:1; + u16 wraparound:1; + + u16 charBaseIndex:2; + u16 mapBaseIndex:5; + u16 paletteMode:1; + + u8 unknown_2; + u8 unknown_3; + } configs[4]; + + u16 bgVisibilityAndMode; +}; + +struct BgConfig2 +{ + u32 baseTile:10; + u32 basePalette:4; + u32 unk_3:18; + + void* tilemap; + s32 bg_x; + s32 bg_y; +}; + +static struct BgControl sGpuBgConfigs; +static struct BgConfig2 sGpuBgConfigs2[4]; +static u32 sDmaBusyBitfield[4]; + +u32 gUnneededFireRedVariable; + +static const struct BgConfig sZeroedBgControlStruct = { 0 }; + +void ResetBgs(void) +{ + ResetBgControlStructs(); + sGpuBgConfigs.bgVisibilityAndMode = 0; + SetTextModeAndHideBgs(); +} + +static void SetBgModeInternal(u8 bgMode) +{ + sGpuBgConfigs.bgVisibilityAndMode &= 0xFFF8; + sGpuBgConfigs.bgVisibilityAndMode |= bgMode; +} + +u8 GetBgMode(void) +{ + return sGpuBgConfigs.bgVisibilityAndMode & 0x7; +} + +void ResetBgControlStructs(void) +{ + struct BgConfig* bgConfigs = &sGpuBgConfigs.configs[0]; + struct BgConfig zeroedConfig = sZeroedBgControlStruct; + int i; + + for (i = 0; i < 4; i++) + { + bgConfigs[i] = zeroedConfig; + } +} + +void Unused_ResetBgControlStruct(u8 bg) +{ + if (!IsInvalidBg(bg)) + { + sGpuBgConfigs.configs[bg] = sZeroedBgControlStruct; + } +} + +enum +{ + BG_CTRL_ATTR_VISIBLE = 1, + BG_CTRL_ATTR_CHARBASEINDEX = 2, + BG_CTRL_ATTR_MAPBASEINDEX = 3, + BG_CTRL_ATTR_SCREENSIZE = 4, + BG_CTRL_ATTR_PALETTEMODE = 5, + BG_CTRL_ATTR_PRIORITY = 6, + BG_CTRL_ATTR_MOSAIC = 7, + BG_CTRL_ATTR_WRAPAROUND = 8, +}; + +static void SetBgControlAttributes(u8 bg, u8 charBaseIndex, u8 mapBaseIndex, u8 screenSize, u8 paletteMode, u8 priority, u8 mosaic, u8 wraparound) +{ + if (!IsInvalidBg(bg)) + { + if (charBaseIndex != 0xFF) + { + sGpuBgConfigs.configs[bg].charBaseIndex = charBaseIndex & 0x3; + } + + if (mapBaseIndex != 0xFF) + { + sGpuBgConfigs.configs[bg].mapBaseIndex = mapBaseIndex & 0x1F; + } + + if (screenSize != 0xFF) + { + sGpuBgConfigs.configs[bg].screenSize = screenSize & 0x3; + } + + if (paletteMode != 0xFF) + { + sGpuBgConfigs.configs[bg].paletteMode = paletteMode; + } + + if (priority != 0xFF) + { + sGpuBgConfigs.configs[bg].priority = priority & 0x3; + } + + if (mosaic != 0xFF) + { + sGpuBgConfigs.configs[bg].mosaic = mosaic & 0x1; + } + + if (wraparound != 0xFF) + { + sGpuBgConfigs.configs[bg].wraparound = wraparound; + } + + sGpuBgConfigs.configs[bg].unknown_2 = 0; + sGpuBgConfigs.configs[bg].unknown_3 = 0; + + sGpuBgConfigs.configs[bg].visible = 1; + } +} + +static u16 GetBgControlAttribute(u8 bg, u8 attributeId) +{ + if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) + { + switch (attributeId) + { + case BG_CTRL_ATTR_VISIBLE: + return sGpuBgConfigs.configs[bg].visible; + case BG_CTRL_ATTR_CHARBASEINDEX: + return sGpuBgConfigs.configs[bg].charBaseIndex; + case BG_CTRL_ATTR_MAPBASEINDEX: + return sGpuBgConfigs.configs[bg].mapBaseIndex; + case BG_CTRL_ATTR_SCREENSIZE: + return sGpuBgConfigs.configs[bg].screenSize; + case BG_CTRL_ATTR_PALETTEMODE: + return sGpuBgConfigs.configs[bg].paletteMode; + case BG_CTRL_ATTR_PRIORITY: + return sGpuBgConfigs.configs[bg].priority; + case BG_CTRL_ATTR_MOSAIC: + return sGpuBgConfigs.configs[bg].mosaic; + case BG_CTRL_ATTR_WRAPAROUND: + return sGpuBgConfigs.configs[bg].wraparound; + } + } + + return 0xFF; +} + +u8 LoadBgVram(u8 bg, const void *src, u16 size, u16 destOffset, u8 mode) +{ + u16 offset; + s8 cursor; + + if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) + { + switch (mode) + { + case 0x1: + offset = sGpuBgConfigs.configs[bg].charBaseIndex * BG_CHAR_SIZE; + break; + case 0x2: + offset = sGpuBgConfigs.configs[bg].mapBaseIndex * BG_SCREEN_SIZE; + break; + default: + cursor = -1; + goto end; + } + + offset = destOffset + offset; + + cursor = RequestDma3Copy(src, (void*)(offset + BG_VRAM), size, 0); + + if (cursor == -1) + { + return -1; + } + } + else + { + return -1; + } + +end: + return cursor; +} + +static void ShowBgInternal(u8 bg) +{ + u16 value; + if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) + { + value = sGpuBgConfigs.configs[bg].priority | + (sGpuBgConfigs.configs[bg].charBaseIndex << 2) | + (sGpuBgConfigs.configs[bg].mosaic << 6) | + (sGpuBgConfigs.configs[bg].paletteMode << 7) | + (sGpuBgConfigs.configs[bg].mapBaseIndex << 8) | + (sGpuBgConfigs.configs[bg].wraparound << 13) | + (sGpuBgConfigs.configs[bg].screenSize << 14); + + SetGpuReg((bg << 1) + REG_OFFSET_BG0CNT, value); + + sGpuBgConfigs.bgVisibilityAndMode |= 1 << (bg + 8); + sGpuBgConfigs.bgVisibilityAndMode &= DISPCNT_ALL_BG_AND_MODE_BITS; + } +} + +static void HideBgInternal(u8 bg) +{ + if (!IsInvalidBg(bg)) + { + sGpuBgConfigs.bgVisibilityAndMode &= ~(1 << (bg + 8)); + sGpuBgConfigs.bgVisibilityAndMode &= DISPCNT_ALL_BG_AND_MODE_BITS; + } +} + +static void SyncBgVisibilityAndMode(void) +{ + SetGpuReg(REG_OFFSET_DISPCNT, (GetGpuReg(REG_OFFSET_DISPCNT) & ~DISPCNT_ALL_BG_AND_MODE_BITS) | sGpuBgConfigs.bgVisibilityAndMode); +} + +void SetTextModeAndHideBgs(void) +{ + SetGpuReg(REG_OFFSET_DISPCNT, GetGpuReg(REG_OFFSET_DISPCNT) & ~DISPCNT_ALL_BG_AND_MODE_BITS); +} + +static void SetBgAffineInternal(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle) +{ + struct BgAffineSrcData src; + struct BgAffineDstData dest; + + switch (sGpuBgConfigs.bgVisibilityAndMode & 0x7) + { + case 1: + if (bg != 2) + return; + break; + case 2: + if (bg < 2 || bg > 3) + return; + break; + case 0: + default: + return; + } + + src.texX = srcCenterX; + src.texY = srcCenterY; + src.scrX = dispCenterX; + src.scrY = dispCenterY; + src.sx = scaleX; + src.sy = scaleY; + src.alpha = rotationAngle; + + BgAffineSet(&src, &dest, 1); + + SetGpuReg(REG_OFFSET_BG2PA, dest.pa); + SetGpuReg(REG_OFFSET_BG2PB, dest.pb); + SetGpuReg(REG_OFFSET_BG2PC, dest.pc); + SetGpuReg(REG_OFFSET_BG2PD, dest.pd); + SetGpuReg(REG_OFFSET_BG2PA, dest.pa); + SetGpuReg(REG_OFFSET_BG2X_L, (s16)(dest.dx)); + SetGpuReg(REG_OFFSET_BG2X_H, (s16)(dest.dx >> 16)); + SetGpuReg(REG_OFFSET_BG2Y_L, (s16)(dest.dy)); + SetGpuReg(REG_OFFSET_BG2Y_H, (s16)(dest.dy >> 16)); +} + +bool8 IsInvalidBg(u8 bg) +{ + if (bg > 3) + return TRUE; + else + return FALSE; +} + +int DummiedOutFireRedLeafGreenTileAllocFunc(int a1, int a2, int a3, int a4) +{ + return 0; +} + +void ResetBgsAndClearDma3BusyFlags(u32 leftoverFireRedLeafGreenVariable) +{ + int i; + ResetBgs(); + + for (i = 0; i < 4; i++) + { + sDmaBusyBitfield[i] = 0; + } + + gUnneededFireRedVariable = leftoverFireRedLeafGreenVariable; +} + +void InitBgsFromTemplates(u8 bgMode, const struct BgTemplate *templates, u8 numTemplates) +{ + int i; + u8 bg; + + SetBgModeInternal(bgMode); + ResetBgControlStructs(); + + for (i = 0; i < numTemplates; i++) + { + bg = templates[i].bg; + if (bg < 4) + { + SetBgControlAttributes(bg, + templates[i].charBaseIndex, + templates[i].mapBaseIndex, + templates[i].screenSize, + templates[i].paletteMode, + templates[i].priority, + 0, + 0); + + sGpuBgConfigs2[bg].baseTile = templates[i].baseTile; + sGpuBgConfigs2[bg].basePalette = 0; + sGpuBgConfigs2[bg].unk_3 = 0; + + sGpuBgConfigs2[bg].tilemap = NULL; + sGpuBgConfigs2[bg].bg_x = 0; + sGpuBgConfigs2[bg].bg_y = 0; + } + } +} + +void InitBgFromTemplate(const struct BgTemplate *template) +{ + u8 bg = template->bg; + + if (bg < 4) + { + SetBgControlAttributes(bg, + template->charBaseIndex, + template->mapBaseIndex, + template->screenSize, + template->paletteMode, + template->priority, + 0, + 0); + + sGpuBgConfigs2[bg].baseTile = template->baseTile; + sGpuBgConfigs2[bg].basePalette = 0; + sGpuBgConfigs2[bg].unk_3 = 0; + + sGpuBgConfigs2[bg].tilemap = NULL; + sGpuBgConfigs2[bg].bg_x = 0; + sGpuBgConfigs2[bg].bg_y = 0; + } +} + +void SetBgMode(u8 bgMode) +{ + SetBgModeInternal(bgMode); +} + +u16 LoadBgTiles(u8 bg, const void* src, u16 size, u16 destOffset) +{ + u16 tileOffset; + u8 cursor; + + if (GetBgControlAttribute(bg, BG_CTRL_ATTR_PALETTEMODE) == 0) + { + tileOffset = (sGpuBgConfigs2[bg].baseTile + destOffset) * 0x20; + } + else + { + tileOffset = (sGpuBgConfigs2[bg].baseTile + destOffset) * 0x40; + } + + cursor = LoadBgVram(bg, src, size, tileOffset, DISPCNT_MODE_1); + + if (cursor == 0xFF) + { + return -1; + } + + sDmaBusyBitfield[cursor / 0x20] |= (1 << (cursor % 0x20)); + + if (gUnneededFireRedVariable == 1) + { + DummiedOutFireRedLeafGreenTileAllocFunc(bg, tileOffset / 0x20, size / 0x20, 1); + } + + return cursor; +} + +u16 LoadBgTilemap(u8 bg, const void *src, u16 size, u16 destOffset) +{ + u8 cursor = LoadBgVram(bg, src, size, destOffset * 2, DISPCNT_MODE_2); + + if (cursor == 0xFF) + { + return -1; + } + + sDmaBusyBitfield[cursor / 0x20] |= (1 << (cursor % 0x20)); + + return cursor; +} + +u16 Unused_LoadBgPalette(u8 bg, const void *src, u16 size, u16 destOffset) +{ + s8 cursor; + + if (!IsInvalidBg32(bg)) + { + u16 paletteOffset = (sGpuBgConfigs2[bg].basePalette * 0x20) + (destOffset * 2); + cursor = RequestDma3Copy(src, (void*)(paletteOffset + BG_PLTT), size, 0); + + if (cursor == -1) + { + return -1; + } + } + else + { + return -1; + } + + sDmaBusyBitfield[cursor / 0x20] |= (1 << (cursor % 0x20)); + + return (u8)cursor; +} + +bool8 IsDma3ManagerBusyWithBgCopy(void) +{ + int i; + + for (i = 0; i < 0x80; i++) + { + u8 div = i / 0x20; + u8 mod = i % 0x20; + + if ((sDmaBusyBitfield[div] & (1 << mod))) + { + s8 reqSpace = CheckForSpaceForDma3Request(i); + if (reqSpace == -1) + { + return TRUE; + } + + sDmaBusyBitfield[div] &= ~(1 << mod); + } + } + + return FALSE; +} + +void ShowBg(u8 bg) +{ + ShowBgInternal(bg); + SyncBgVisibilityAndMode(); +} + +void HideBg(u8 bg) +{ + HideBgInternal(bg); + SyncBgVisibilityAndMode(); +} + +void SetBgAttribute(u8 bg, u8 attributeId, u8 value) +{ + switch (attributeId) + { + case BG_ATTR_CHARBASEINDEX: + SetBgControlAttributes(bg, value, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); + break; + case BG_ATTR_MAPBASEINDEX: + SetBgControlAttributes(bg, 0xFF, value, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); + break; + case BG_ATTR_SCREENSIZE: + SetBgControlAttributes(bg, 0xFF, 0xFF, value, 0xFF, 0xFF, 0xFF, 0xFF); + break; + case BG_ATTR_PALETTEMODE: + SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, value, 0xFF, 0xFF, 0xFF); + break; + case BG_ATTR_PRIORITY: + SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, 0xFF, value, 0xFF, 0xFF); + break; + case BG_ATTR_MOSAIC: + SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, value, 0xFF); + break; + case BG_ATTR_WRAPAROUND: + SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, value); + break; + } +} + +u16 GetBgAttribute(u8 bg, u8 attributeId) +{ + switch (attributeId) + { + case BG_ATTR_CHARBASEINDEX: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_CHARBASEINDEX); + case BG_ATTR_MAPBASEINDEX: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_MAPBASEINDEX); + case BG_ATTR_SCREENSIZE: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); + case BG_ATTR_PALETTEMODE: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_PALETTEMODE); + case BG_ATTR_PRIORITY: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_PRIORITY); + case BG_ATTR_MOSAIC: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_MOSAIC); + case BG_ATTR_WRAPAROUND: + return GetBgControlAttribute(bg, BG_CTRL_ATTR_WRAPAROUND); + case BG_ATTR_METRIC: + switch (GetBgType(bg)) + { + case 0: + return GetBgMetricTextMode(bg, 0) * 0x800; + case 1: + return GetBgMetricAffineMode(bg, 0) * 0x100; + default: + return 0; + } + case BG_ATTR_TYPE: + return GetBgType(bg); + case BG_ATTR_BASETILE: + return sGpuBgConfigs2[bg].baseTile; + default: + return -1; + } +} + +s32 ChangeBgX(u8 bg, s32 value, u8 op) +{ + u8 mode; + u16 temp1; + u16 temp2; + + if (IsInvalidBg32(bg) || !GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + { + return -1; + } + + switch (op) + { + case 0: + default: + sGpuBgConfigs2[bg].bg_x = value; + break; + case 1: + sGpuBgConfigs2[bg].bg_x += value; + break; + case 2: + sGpuBgConfigs2[bg].bg_x -= value; + break; + } + + mode = GetBgMode(); + + switch (bg) + { + case 0: + temp1 = sGpuBgConfigs2[0].bg_x >> 0x8; + SetGpuReg(REG_OFFSET_BG0HOFS, temp1); + break; + case 1: + temp1 = sGpuBgConfigs2[1].bg_x >> 0x8; + SetGpuReg(REG_OFFSET_BG1HOFS, temp1); + break; + case 2: + if (mode == 0) + { + temp1 = sGpuBgConfigs2[2].bg_x >> 0x8; + SetGpuReg(REG_OFFSET_BG2HOFS, temp1); + } + else + { + temp1 = sGpuBgConfigs2[2].bg_x >> 0x10; + temp2 = sGpuBgConfigs2[2].bg_x & 0xFFFF; + SetGpuReg(REG_OFFSET_BG2X_H, temp1); + SetGpuReg(REG_OFFSET_BG2X_L, temp2); + } + break; + case 3: + if (mode == 0) + { + temp1 = sGpuBgConfigs2[3].bg_x >> 0x8; + SetGpuReg(REG_OFFSET_BG3HOFS, temp1); + } + else if (mode == 2) + { + temp1 = sGpuBgConfigs2[3].bg_x >> 0x10; + temp2 = sGpuBgConfigs2[3].bg_x & 0xFFFF; + SetGpuReg(REG_OFFSET_BG3X_H, temp1); + SetGpuReg(REG_OFFSET_BG3X_L, temp2); + } + break; + } + + return sGpuBgConfigs2[bg].bg_x; +} + +s32 GetBgX(u8 bg) +{ + if (IsInvalidBg32(bg)) + return -1; + else if (!GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + return -1; + else + return sGpuBgConfigs2[bg].bg_x; +} + +s32 ChangeBgY(u8 bg, s32 value, u8 op) +{ + u8 mode; + u16 temp1; + u16 temp2; + + if (IsInvalidBg32(bg) || !GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + { + return -1; + } + + switch (op) + { + case 0: + default: + sGpuBgConfigs2[bg].bg_y = value; + break; + case 1: + sGpuBgConfigs2[bg].bg_y += value; + break; + case 2: + sGpuBgConfigs2[bg].bg_y -= value; + break; + } + + mode = GetBgMode(); + + switch (bg) + { + case 0: + temp1 = sGpuBgConfigs2[0].bg_y >> 0x8; + SetGpuReg(REG_OFFSET_BG0VOFS, temp1); + break; + case 1: + temp1 = sGpuBgConfigs2[1].bg_y >> 0x8; + SetGpuReg(REG_OFFSET_BG1VOFS, temp1); + break; + case 2: + if (mode == 0) + { + temp1 = sGpuBgConfigs2[2].bg_y >> 0x8; + SetGpuReg(REG_OFFSET_BG2VOFS, temp1); + } + else + { + temp1 = sGpuBgConfigs2[2].bg_y >> 0x10; + temp2 = sGpuBgConfigs2[2].bg_y & 0xFFFF; + SetGpuReg(REG_OFFSET_BG2Y_H, temp1); + SetGpuReg(REG_OFFSET_BG2Y_L, temp2); + } + break; + case 3: + if (mode == 0) + { + temp1 = sGpuBgConfigs2[3].bg_y >> 0x8; + SetGpuReg(REG_OFFSET_BG3VOFS, temp1); + } + else if (mode == 2) + { + temp1 = sGpuBgConfigs2[3].bg_y >> 0x10; + temp2 = sGpuBgConfigs2[3].bg_y & 0xFFFF; + SetGpuReg(REG_OFFSET_BG3Y_H, temp1); + SetGpuReg(REG_OFFSET_BG3Y_L, temp2); + } + break; + } + + return sGpuBgConfigs2[bg].bg_y; +} + +s32 ChangeBgY_ScreenOff(u8 bg, u32 value, u8 op) +{ + u8 mode; + u16 temp1; + u16 temp2; + + if (IsInvalidBg32(bg) || !GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + { + return -1; + } + + switch (op) + { + case 0: + default: + sGpuBgConfigs2[bg].bg_y = value; + break; + case 1: + sGpuBgConfigs2[bg].bg_y += value; + break; + case 2: + sGpuBgConfigs2[bg].bg_y -= value; + break; + } + + mode = GetBgMode(); + + switch (bg) + { + case 0: + temp1 = sGpuBgConfigs2[0].bg_y >> 0x8; + SetGpuReg_ForcedBlank(REG_OFFSET_BG0VOFS, temp1); + break; + case 1: + temp1 = sGpuBgConfigs2[1].bg_y >> 0x8; + SetGpuReg_ForcedBlank(REG_OFFSET_BG1VOFS, temp1); + break; + case 2: + if (mode == 0) + { + temp1 = sGpuBgConfigs2[2].bg_y >> 0x8; + SetGpuReg_ForcedBlank(REG_OFFSET_BG2VOFS, temp1); + + } + else + { + temp1 = sGpuBgConfigs2[2].bg_y >> 0x10; + temp2 = sGpuBgConfigs2[2].bg_y & 0xFFFF; + SetGpuReg_ForcedBlank(REG_OFFSET_BG2Y_H, temp1); + SetGpuReg_ForcedBlank(REG_OFFSET_BG2Y_L, temp2); + } + break; + case 3: + if (mode == 0) + { + temp1 = sGpuBgConfigs2[3].bg_y >> 0x8; + SetGpuReg_ForcedBlank(REG_OFFSET_BG3VOFS, temp1); + } + else if (mode == 2) + { + temp1 = sGpuBgConfigs2[3].bg_y >> 0x10; + temp2 = sGpuBgConfigs2[3].bg_y & 0xFFFF; + SetGpuReg_ForcedBlank(REG_OFFSET_BG3Y_H, temp1); + SetGpuReg_ForcedBlank(REG_OFFSET_BG3Y_L, temp2); + } + break; + } + + return sGpuBgConfigs2[bg].bg_y; +} + +s32 GetBgY(u8 bg) +{ + if (IsInvalidBg32(bg)) + return -1; + else if (!GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + return -1; + else + return sGpuBgConfigs2[bg].bg_y; +} + +void SetBgAffine(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle) +{ + SetBgAffineInternal(bg, srcCenterX, srcCenterY, dispCenterX, dispCenterY, scaleX, scaleY, rotationAngle); +} + +u8 Unused_AdjustBgMosaic(u8 a1, u8 a2) +{ + u16 result = GetGpuReg(REG_OFFSET_MOSAIC); + s16 test1 = result & 0xF; + s16 test2 = (result >> 4) & 0xF; + + result &= 0xFF00; + + switch (a2) + { + case 0: + default: + test1 = a1 & 0xF; + test2 = a1 >> 0x4; + break; + case 1: + test1 = a1 & 0xF; + break; + case 2: + if ((test1 + a1) > 0xF) + { + test1 = 0xF; + } + else + { + test1 += a1; + } + break; + case 3: + if ((test1 - a1) < 0) + { + test1 = 0x0; + } + else + { + test1 -= a1; + } + break; + case 4: + test2 = a1 & 0xF; + break; + case 5: + if ((test2 + a1) > 0xF) + { + test2 = 0xF; + } + else + { + test2 += a1; + } + break; + case 6: + if ((test2 - a1) < 0) + { + test2 = 0x0; + } + else + { + test2 -= a1; + } + break; + } + + result |= ((test2 << 0x4) & 0xF0); + result |= (test1 & 0xF); + + SetGpuReg(REG_OFFSET_MOSAIC, result); + + return result; +} + +void SetBgTilemapBuffer(u8 bg, void *tilemap) +{ + if (!IsInvalidBg32(bg) && GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + { + sGpuBgConfigs2[bg].tilemap = tilemap; + } +} + +void UnsetBgTilemapBuffer(u8 bg) +{ + if (!IsInvalidBg32(bg) && GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + { + sGpuBgConfigs2[bg].tilemap = NULL; + } +} + +void* GetBgTilemapBuffer(u8 bg) +{ + if (IsInvalidBg32(bg)) + return NULL; + else if (!GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) + return NULL; + else + return sGpuBgConfigs2[bg].tilemap; +} + +void CopyToBgTilemapBuffer(u8 bg, const void *src, u16 mode, u16 destOffset) +{ + if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) + { + if (mode != 0) + CpuCopy16(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2)), mode); + else + LZ77UnCompWram(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2))); + } +} + +void CopyBgTilemapBufferToVram(u8 bg) +{ + u16 sizeToLoad; + + if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) + { + switch (GetBgType(bg)) + { + case 0: + sizeToLoad = GetBgMetricTextMode(bg, 0) * 0x800; + break; + case 1: + sizeToLoad = GetBgMetricAffineMode(bg, 0) * 0x100; + break; + default: + sizeToLoad = 0; + break; + } + LoadBgVram(bg, sGpuBgConfigs2[bg].tilemap, sizeToLoad, 0, 2); + } +} + +void CopyToBgTilemapBufferRect(u8 bg, const void* src, u8 destX, u8 destY, u8 width, u8 height) +{ + u16 destX16; + u16 destY16; + u16 mode; + + if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) + { + switch (GetBgType(bg)) + { + case 0: + { + const u16 * srcCopy = src; + for (destY16 = destY; destY16 < (destY + height); destY16++) + { + for (destX16 = destX; destX16 < (destX + width); destX16++) + { + ((u16*)sGpuBgConfigs2[bg].tilemap)[((destY16 * 0x20) + destX16)] = *srcCopy++; + } + } + break; + } + case 1: + { + const u8 * srcCopy = src; + mode = GetBgMetricAffineMode(bg, 0x1); + for (destY16 = destY; destY16 < (destY + height); destY16++) + { + for (destX16 = destX; destX16 < (destX + width); destX16++) + { + ((u8*)sGpuBgConfigs2[bg].tilemap)[((destY16 * mode) + destX16)] = *srcCopy++; + } + } + break; + } + } + } +} + +void CopyToBgTilemapBufferRect_ChangePalette(u8 bg, const void *src, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, u8 palette) +{ + CopyRectToBgTilemapBufferRect(bg, src, 0, 0, rectWidth, rectHeight, destX, destY, rectWidth, rectHeight, palette, 0, 0); +} + +void CopyRectToBgTilemapBufferRect(u8 bg, const void *src, u8 srcX, u8 srcY, u8 srcWidth, u8 unused, u8 srcHeight, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, s16 palette1, s16 tileOffset) +{ + u16 screenWidth, screenHeight, screenSize; + u16 var; + const void *srcPtr; + u16 i, j; + + if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) + { + screenSize = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); + screenWidth = GetBgMetricTextMode(bg, 0x1) * 0x20; + screenHeight = GetBgMetricTextMode(bg, 0x2) * 0x20; + switch (GetBgType(bg)) + { + case 0: + srcPtr = src + ((srcY * srcWidth) + srcX) * 2; + for (i = destX; i < (destX + rectWidth); i++) + { + for (j = srcHeight; j < (srcHeight + destY); j++) + { + u16 index = GetTileMapIndexFromCoords(j, i, screenSize, screenWidth, screenHeight); + CopyTileMapEntry(srcPtr, sGpuBgConfigs2[bg].tilemap + (index * 2), rectHeight, palette1, tileOffset); + srcPtr += 2; + } + srcPtr += (srcWidth - destY) * 2; + } + break; + case 1: + srcPtr = src + ((srcY * srcWidth) + srcX); + var = GetBgMetricAffineMode(bg, 0x1); + for (i = destX; i < (destX + rectWidth); i++) + { + for (j = srcHeight; j < (srcHeight + destY); j++) + { + *(u8*)(sGpuBgConfigs2[bg].tilemap + ((var * i) + j)) = *(u8*)(srcPtr) + palette1; + srcPtr++; + } + srcPtr += (srcWidth - destY); + } + break; + } + } +} + +void FillBgTilemapBufferRect_Palette0(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height) +{ + u16 x16; + u16 y16; + u16 mode; + + if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) + { + switch (GetBgType(bg)) + { + case 0: + for (y16 = y; y16 < (y + height); y16++) + { + for (x16 = x; x16 < (x + width); x16++) + { + ((u16*)sGpuBgConfigs2[bg].tilemap)[((y16 * 0x20) + x16)] = tileNum; + } + } + break; + case 1: + mode = GetBgMetricAffineMode(bg, 0x1); + for (y16 = y; y16 < (y + height); y16++) + { + for (x16 = x; x16 < (x + width); x16++) + { + ((u8*)sGpuBgConfigs2[bg].tilemap)[((y16 * mode) + x16)] = tileNum; + } + } + break; + } + } +} + +void FillBgTilemapBufferRect(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height, u8 palette) +{ + WriteSequenceToBgTilemapBuffer(bg, tileNum, x, y, width, height, palette, 0); +} + +void WriteSequenceToBgTilemapBuffer(u8 bg, u16 firstTileNum, u8 x, u8 y, u8 width, u8 height, u8 paletteSlot, s16 tileNumDelta) +{ + u16 mode; + u16 mode2; + u16 attribute; + u16 mode3; + u16 x16, y16; + + if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) + { + attribute = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); + mode = GetBgMetricTextMode(bg, 0x1) * 0x20; + mode2 = GetBgMetricTextMode(bg, 0x2) * 0x20; + switch (GetBgType(bg)) + { + case 0: + for (y16 = y; y16 < (y + height); y16++) + { + for (x16 = x; x16 < (x + width); x16++) + { + CopyTileMapEntry(&firstTileNum, &((u16*)sGpuBgConfigs2[bg].tilemap)[(u16)GetTileMapIndexFromCoords(x16, y16, attribute, mode, mode2)], paletteSlot, 0, 0); + firstTileNum = (firstTileNum & (METATILE_COLLISION_MASK | METATILE_ELEVATION_MASK)) + ((firstTileNum + tileNumDelta) & METATILE_ID_MASK); + } + } + break; + case 1: + mode3 = GetBgMetricAffineMode(bg, 0x1); + for (y16 = y; y16 < (y + height); y16++) + { + for (x16 = x; x16 < (x + width); x16++) + { + ((u8*)sGpuBgConfigs2[bg].tilemap)[(y16 * mode3) + x16] = firstTileNum; + firstTileNum = (firstTileNum & (METATILE_COLLISION_MASK | METATILE_ELEVATION_MASK)) + ((firstTileNum + tileNumDelta) & METATILE_ID_MASK); + } + } + break; + } + } +} + +u16 GetBgMetricTextMode(u8 bg, u8 whichMetric) +{ + u8 screenSize = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); + + switch (whichMetric) + { + case 0: + switch (screenSize) + { + case 0: + return 1; + case 1: + case 2: + return 2; + case 3: + return 4; + } + break; + case 1: + switch (screenSize) + { + case 0: + return 1; + case 1: + return 2; + case 2: + return 1; + case 3: + return 2; + } + break; + case 2: + switch (screenSize) + { + case 0: + case 1: + return 1; + case 2: + case 3: + return 2; + } + break; + } + return 0; +} + +u32 GetBgMetricAffineMode(u8 bg, u8 whichMetric) +{ + u8 screenSize = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); + + switch (whichMetric) + { + case 0: + switch (screenSize) + { + case 0: + return 0x1; + case 1: + return 0x4; + case 2: + return 0x10; + case 3: + return 0x40; + } + break; + case 1: + case 2: + return 0x10 << screenSize; + } + return 0; +} + +u32 GetTileMapIndexFromCoords(s32 x, s32 y, s32 screenSize, u32 screenWidth, u32 screenHeight) +{ + x = x & (screenWidth - 1); + y = y & (screenHeight - 1); + + switch (screenSize) + { + case 0: + case 2: + break; + case 3: + if (y >= 0x20) + y += 0x20; + case 1: + if (x >= 0x20) + { + x -= 0x20; + y += 0x20; + } + break; + } + return (y * 0x20) + x; +} + +#ifdef NONMATCHING // This one has some weird switch statement cases that refuse to cooperate +void CopyTileMapEntry(const u16 *src, u16 *dest, s32 palette1, s32 tileOffset, s32 palette2) +{ + u16 var; + switch (palette1) + { + case 0 ... 16: + if (palette1 != 16) + var = ((*src + tileOffset) & 0xFFF) + ((palette1 + palette2) << 12); + else + var = ((*dest & 0xFC00) + (palette2 << 12)) | ((*src + tileOffset) & 0x3FF); + break; + default: + var = *src + tileOffset + (palette2 << 12); + break; + } + + *dest = var; +} +#else +NAKED +void CopyTileMapEntry(const u16 *src, u16 *dest, s32 palette1, s32 tileOffset, s32 palette2) +{ + asm("push {r4-r6,lr}\n\ + add r4, r0, #0\n\ + add r6, r1, #0\n\ + ldr r5, [sp, #0x10]\n\ + cmp r2, #0x10\n\ + beq _08002B14\n\ + cmp r2, #0x10\n\ + bgt _08002B34\n\ + cmp r2, #0\n\ + blt _08002B34\n\ + ldrh r0, [r4]\n\ + add r0, r3\n\ + ldr r3, =0x00000fff\n\ + add r1, r3, #0\n\ + and r0, r1\n\ + add r1, r2, r5\n\ + lsl r1, #12\n\ + b _08002B3A\n\ + .pool\n\ +_08002B14:\n\ + ldrh r1, [r6]\n\ + mov r0, #0xFC\n\ + lsl r0, #8\n\ + and r1, r0\n\ + lsl r2, r5, #12\n\ + add r2, r1, r2\n\ + ldrh r0, [r4]\n\ + add r0, r3\n\ + ldr r3, =0x000003ff\n\ + add r1, r3, #0\n\ + and r0, r1\n\ + orr r0, r2\n\ + b _08002B3C\n\ + .pool\n\ +_08002B34:\n\ + ldrh r0, [r4]\n\ + add r0, r3\n\ + lsl r1, r5, #12\n\ +_08002B3A:\n\ + add r0, r1\n\ +_08002B3C:\n\ + lsl r0, #16\n\ + lsr r1, r0, #16\n\ + strh r1, [r6]\n\ + pop {r4-r6}\n\ + pop {r0}\n\ + bx r0\n"); +} +#endif // NONMATCHING + +u32 GetBgType(u8 bg) +{ + u8 mode = GetBgMode(); + + switch (bg) + { + case 0: + case 1: + switch (mode) + { + case 0: + case 1: + return 0; + } + break; + case 2: + switch (mode) + { + case 0: + return 0; + case 1: + case 2: + return 1; + } + break; + case 3: + switch (mode) + { + case 0: + return 0; + case 2: + return 1; + } + break; + } + + return 0xFFFF; +} + +bool32 IsInvalidBg32(u8 bg) +{ + if (bg > 3) + return TRUE; + else + return FALSE; +} + +bool32 IsTileMapOutsideWram(u8 bg) +{ + if (sGpuBgConfigs2[bg].tilemap > (void*)IWRAM_END) + return TRUE; + else if (sGpuBgConfigs2[bg].tilemap == NULL) + return TRUE; + else + return FALSE; +} diff --git a/gflib/bg.h b/gflib/bg.h new file mode 100644 index 000000000..3c7eee292 --- /dev/null +++ b/gflib/bg.h @@ -0,0 +1,85 @@ +#ifndef GUARD_BG_H +#define GUARD_BG_H + +struct BGCntrlBitfield // for the I/O registers +{ + volatile u16 priority:2; + volatile u16 charBaseBlock:2; + volatile u16 field_0_2:4; + volatile u16 field_1_0:5; + volatile u16 areaOverflowMode:1; + volatile u16 screenSize:2; +}; + +enum +{ + BG_ATTR_CHARBASEINDEX = 1, + BG_ATTR_MAPBASEINDEX, + BG_ATTR_SCREENSIZE, + BG_ATTR_PALETTEMODE, + BG_ATTR_MOSAIC, + BG_ATTR_WRAPAROUND, + BG_ATTR_PRIORITY, + BG_ATTR_METRIC, + BG_ATTR_TYPE, + BG_ATTR_BASETILE, +}; + +struct BgTemplate +{ + u16 bg:2; // 0x1, 0x2 -> 0x3 + u16 charBaseIndex:2; // 0x4, 0x8 -> 0xC + u16 mapBaseIndex:5; // 0x10, 0x20, 0x40, 0x80, 0x100 -> 0x1F0 + u16 screenSize:2; // 0x200, 0x400 -> 0x600 + u16 paletteMode:1; // 0x800 + u16 priority:2; // 0x1000, 0x2000 > 0x3000 + u16 baseTile:10; +}; + +void ResetBgs(void); +u8 GetBgMode(void); +void ResetBgControlStructs(void); +void Unused_ResetBgControlStruct(u8 bg); +u8 LoadBgVram(u8 bg, const void *src, u16 size, u16 destOffset, u8 mode); +void SetTextModeAndHideBgs(void); +bool8 IsInvalidBg(u8 bg); +int DummiedOutFireRedLeafGreenTileAllocFunc(int a1, int a2, int a3, int a4); +void ResetBgsAndClearDma3BusyFlags(u32 leftoverFireRedLeafGreenVariable); +void InitBgsFromTemplates(u8 bgMode, const struct BgTemplate *templates, u8 numTemplates); +void InitBgFromTemplate(const struct BgTemplate *template); +void SetBgMode(u8 bgMode); +u16 LoadBgTiles(u8 bg, const void* src, u16 size, u16 destOffset); +u16 LoadBgTilemap(u8 bg, const void *src, u16 size, u16 destOffset); +u16 Unused_LoadBgPalette(u8 bg, const void *src, u16 size, u16 destOffset); +bool8 IsDma3ManagerBusyWithBgCopy(void); +void ShowBg(u8 bg); +void HideBg(u8 bg); +void SetBgAttribute(u8 bg, u8 attributeId, u8 value); +u16 GetBgAttribute(u8 bg, u8 attributeId); +s32 ChangeBgX(u8 bg, s32 value, u8 op); +s32 GetBgX(u8 bg); +s32 ChangeBgY(u8 bg, s32 value, u8 op); +s32 ChangeBgY_ScreenOff(u8 bg, u32 value, u8 op); +s32 GetBgY(u8 bg); +void SetBgAffine(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle); +u8 Unused_AdjustBgMosaic(u8 a1, u8 a2); +void SetBgTilemapBuffer(u8 bg, void *tilemap); +void UnsetBgTilemapBuffer(u8 bg); +void* GetBgTilemapBuffer(u8 bg); +void CopyToBgTilemapBuffer(u8 bg, const void *src, u16 mode, u16 destOffset); +void CopyBgTilemapBufferToVram(u8 bg); +void CopyToBgTilemapBufferRect(u8 bg, const void* src, u8 destX, u8 destY, u8 width, u8 height); +void CopyToBgTilemapBufferRect_ChangePalette(u8 bg, const void *src, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, u8 palette); +void CopyRectToBgTilemapBufferRect(u8 bg, const void *src, u8 srcX, u8 srcY, u8 srcWidth, u8 unused, u8 srcHeight, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, s16 palette1, s16 tileOffset); +void FillBgTilemapBufferRect_Palette0(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height); +void FillBgTilemapBufferRect(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height, u8 palette); +void WriteSequenceToBgTilemapBuffer(u8 bg, u16 firstTileNum, u8 x, u8 y, u8 width, u8 height, u8 paletteSlot, s16 tileNumDelta); +u16 GetBgMetricTextMode(u8 bg, u8 whichMetric); +u32 GetBgMetricAffineMode(u8 bg, u8 whichMetric); +u32 GetTileMapIndexFromCoords(s32 x, s32 y, s32 screenSize, u32 screenWidth, u32 screenHeight); +void CopyTileMapEntry(const u16 *src, u16 *dest, s32 palette1, s32 tileOffset, s32 palette2); +u32 GetBgType(u8 bg); +bool32 IsInvalidBg32(u8 bg); +bool32 IsTileMapOutsideWram(u8 bg); + +#endif // GUARD_BG_H diff --git a/gflib/blit.c b/gflib/blit.c new file mode 100644 index 000000000..26a63fe9b --- /dev/null +++ b/gflib/blit.c @@ -0,0 +1,209 @@ +#include "global.h" +#include "blit.h" + +void BlitBitmapRect4BitWithoutColorKey(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height) +{ + BlitBitmapRect4Bit(src, dst, srcX, srcY, dstX, dstY, width, height, 0xFF); +} + +void BlitBitmapRect4Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey) +{ + s32 xEnd; + s32 yEnd; + s32 multiplierSrcY; + s32 multiplierDstY; + s32 loopSrcY, loopDstY; + s32 loopSrcX, loopDstX; + const u8 *pixelsSrc; + u8 *pixelsDst; + s32 toOrr; + s32 toAnd; + s32 toShift; + + if (dst->width - dstX < width) + xEnd = (dst->width - dstX) + srcX; + else + xEnd = srcX + width; + + if (dst->height - dstY < height) + yEnd = (dst->height - dstY) + srcY; + else + yEnd = height + srcY; + + multiplierSrcY = (src->width + (src->width & 7)) >> 3; + multiplierDstY = (dst->width + (dst->width & 7)) >> 3; + + if (colorKey == 0xFF) + { + for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) + { + for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) + { + pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B); + pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B); + toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF); + toShift = ((loopDstX & 1) << 2); + toOrr <<= toShift; + toAnd = 0xF0 >> (toShift); + *pixelsDst = toOrr | (*pixelsDst & toAnd); + } + } + } + else + { + for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) + { + for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) + { + pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B); + pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B); + toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF); + if (toOrr != colorKey) + { + toShift = ((loopDstX & 1) << 2); + toOrr <<= toShift; + toAnd = 0xF0 >> (toShift); + *pixelsDst = toOrr | (*pixelsDst & toAnd); + } + } + } + } +} + +void FillBitmapRect4Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue) +{ + s32 xEnd; + s32 yEnd; + s32 multiplierY; + s32 loopX, loopY; + s32 toOrr1, toOrr2; + + xEnd = x + width; + if (xEnd > surface->width) + xEnd = surface->width; + + yEnd = y + height; + if (yEnd > surface->height) + yEnd = surface->height; + + multiplierY = (surface->width + (surface->width & 7)) >> 3; + toOrr1 = (u32)(fillValue << 0x1C) >> 0x18; + toOrr2 = (fillValue & 0xF); + + for (loopY = y; loopY < yEnd; loopY++) + { + for (loopX = x; loopX < xEnd; loopX++) + { + u8 *pixels = surface->pixels + ((loopX >> 1) & 3) + ((loopX >> 3) << 5) + (((loopY >> 3) * multiplierY) << 5) + ((u32)(loopY << 0x1d) >> 0x1B); + if ((loopX << 0x1F) != 0) + *pixels = toOrr1 | (*pixels & 0xF); + else + *pixels = toOrr2 | (*pixels & 0xF0); + } + } +} + +void BlitBitmapRect4BitTo8Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey, u8 paletteOffset) +{ + s32 palOffsetBits; + s32 xEnd; + s32 yEnd; + s32 multiplierSrcY; + s32 multiplierDstY; + s32 loopSrcY, loopDstY; + s32 loopSrcX, loopDstX; + const u8 *pixelsSrc; + u8 *pixelsDst; + s32 colorKeyBits; + + palOffsetBits = (u32)(paletteOffset << 0x1C) >> 0x18; + colorKeyBits = (u32)(colorKey << 0x1C) >> 0x18; + + if (dst->width - dstX < width) + xEnd = (dst->width - dstX) + srcX; + else + xEnd = width + srcX; + + if (dst->height - dstY < height) + yEnd = (srcY + dst->height) - dstY; + else + yEnd = srcY + height; + + multiplierSrcY = (src->width + (src->width & 7)) >> 3; + multiplierDstY = (dst->width + (dst->width & 7)) >> 3; + + if (colorKey == 0xFF) + { + for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) + { + pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); + for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) + { + pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a); + if (loopSrcX & 1) + { + *pixelsDst = palOffsetBits + (*pixelsSrc >> 4); + } + else + { + pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); + *pixelsDst = palOffsetBits + (*pixelsSrc & 0xF); + } + } + } + } + else + { + for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) + { + pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); + for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) + { + if (loopSrcX & 1) + { + if ((*pixelsSrc & 0xF0) != colorKeyBits) + { + pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a); + *pixelsDst = palOffsetBits + (*pixelsSrc >> 4); + } + } + else + { + pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); + if ((*pixelsSrc & 0xF) != colorKey) + { + pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a); + *pixelsDst = palOffsetBits + (*pixelsSrc & 0xF); + } + } + } + } + } +} + +void FillBitmapRect8Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue) +{ + s32 xEnd; + s32 yEnd; + s32 multiplierY; + s32 loopX, loopY; + + xEnd = x + width; + if (xEnd > surface->width) + xEnd = surface->width; + + yEnd = y + height; + if (yEnd > surface->height) + yEnd = surface->height; + + multiplierY = (surface->width + (surface->width & 7)) >> 3; + + for (loopY = y; loopY < yEnd; loopY++) + { + for (loopX = x; loopX < xEnd; loopX++) + { + u8 *pixels = surface->pixels + (loopX & 7) + ((loopX >> 3) << 6) + (((loopY >> 3) * multiplierY) << 6) + ((u32)(loopY << 0x1d) >> 0x1a); + *pixels = fillValue; + } + } +} diff --git a/gflib/blit.h b/gflib/blit.h new file mode 100644 index 000000000..78f67766e --- /dev/null +++ b/gflib/blit.h @@ -0,0 +1,17 @@ +#ifndef GUARD_BLIT_H +#define GUARD_BLIT_H + +struct Bitmap +{ + u8 *pixels; + u32 width:16; + u32 height:16; +}; + +void BlitBitmapRect4BitWithoutColorKey(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height); +void BlitBitmapRect4Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey); +void FillBitmapRect4Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue); +void BlitBitmapRect4BitTo8Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey, u8 paletteOffset); +void FillBitmapRect8Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue); + +#endif // GUARD_BLIT_H diff --git a/gflib/dma3.h b/gflib/dma3.h new file mode 100644 index 000000000..8eff34f55 --- /dev/null +++ b/gflib/dma3.h @@ -0,0 +1,55 @@ +#ifndef GUARD_DMA3_H +#define GUARD_DMA3_H + +// Maximum amount of data we will transfer in one operation +#define MAX_DMA_BLOCK_SIZE 0x1000 + +#define Dma3CopyLarge_(src, dest, size, bit) \ +{ \ + const void *_src = src; \ + void *_dest = dest; \ + u32 _size = size; \ + while (1) \ + { \ + if (_size <= MAX_DMA_BLOCK_SIZE) \ + { \ + DmaCopy##bit(3, _src, _dest, _size); \ + break; \ + } \ + DmaCopy##bit(3, _src, _dest, MAX_DMA_BLOCK_SIZE); \ + _src += MAX_DMA_BLOCK_SIZE; \ + _dest += MAX_DMA_BLOCK_SIZE; \ + _size -= MAX_DMA_BLOCK_SIZE; \ + } \ +} + +#define Dma3CopyLarge16_(src, dest, size) Dma3CopyLarge_(src, dest, size, 16) +#define Dma3CopyLarge32_(src, dest, size) Dma3CopyLarge_(src, dest, size, 32) + +#define Dma3FillLarge_(value, dest, size, bit) \ +{ \ + void *_dest = dest; \ + u32 _size = size; \ + while (1) \ + { \ + if (_size <= MAX_DMA_BLOCK_SIZE) \ + { \ + DmaFill##bit(3, value, _dest, _size); \ + break; \ + } \ + DmaFill##bit(3, value, _dest, MAX_DMA_BLOCK_SIZE); \ + _dest += MAX_DMA_BLOCK_SIZE; \ + _size -= MAX_DMA_BLOCK_SIZE; \ + } \ +} + +#define Dma3FillLarge16_(value, dest, size) Dma3FillLarge_(value, dest, size, 16) +#define Dma3FillLarge32_(value, dest, size) Dma3FillLarge_(value, dest, size, 32) + +void ClearDma3Requests(void); +void ProcessDma3Requests(void); +s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode); +s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode); +s16 CheckForSpaceForDma3Request(s16 index); + +#endif // GUARD_DMA3_H diff --git a/gflib/dma3_manager.c b/gflib/dma3_manager.c new file mode 100644 index 000000000..43744883f --- /dev/null +++ b/gflib/dma3_manager.c @@ -0,0 +1,181 @@ +#include "global.h" +#include "dma3.h" + +#define MAX_DMA_REQUESTS 128 + +#define DMA_REQUEST_COPY32 1 +#define DMA_REQUEST_FILL32 2 +#define DMA_REQUEST_COPY16 3 +#define DMA_REQUEST_FILL16 4 + +BSS_DATA struct +{ + const u8 *src; + u8 *dest; + u16 size; + u16 mode; + u32 value; +} gDma3Requests[MAX_DMA_REQUESTS]; + +static volatile bool8 gDma3ManagerLocked; +static u8 gDma3RequestCursor; + +void ClearDma3Requests(void) +{ + int i; + + gDma3ManagerLocked = TRUE; + gDma3RequestCursor = 0; + + for (i = 0; i < MAX_DMA_REQUESTS; i++) + { + gDma3Requests[i].size = 0; + gDma3Requests[i].src = NULL; + gDma3Requests[i].dest = NULL; + } + + gDma3ManagerLocked = FALSE; +} + +void ProcessDma3Requests(void) +{ + u16 bytesTransferred; + + if (gDma3ManagerLocked) + return; + + bytesTransferred = 0; + + // as long as there are DMA requests to process (unless size or vblank is an issue), do not exit + while (gDma3Requests[gDma3RequestCursor].size != 0) + { + bytesTransferred += gDma3Requests[gDma3RequestCursor].size; + + if (bytesTransferred > 40 * 1024) + return; // don't transfer more than 40 KiB + if (*(u8 *)REG_ADDR_VCOUNT > 224) + return; // we're about to leave vblank, stop + + switch (gDma3Requests[gDma3RequestCursor].mode) + { + case DMA_REQUEST_COPY32: // regular 32-bit copy + Dma3CopyLarge32_(gDma3Requests[gDma3RequestCursor].src, + gDma3Requests[gDma3RequestCursor].dest, + gDma3Requests[gDma3RequestCursor].size); + break; + case DMA_REQUEST_FILL32: // repeat a single 32-bit value across RAM + Dma3FillLarge32_(gDma3Requests[gDma3RequestCursor].value, + gDma3Requests[gDma3RequestCursor].dest, + gDma3Requests[gDma3RequestCursor].size); + break; + case DMA_REQUEST_COPY16: // regular 16-bit copy + Dma3CopyLarge16_(gDma3Requests[gDma3RequestCursor].src, + gDma3Requests[gDma3RequestCursor].dest, + gDma3Requests[gDma3RequestCursor].size); + break; + case DMA_REQUEST_FILL16: // repeat a single 16-bit value across RAM + Dma3FillLarge16_(gDma3Requests[gDma3RequestCursor].value, + gDma3Requests[gDma3RequestCursor].dest, + gDma3Requests[gDma3RequestCursor].size); + break; + } + + // Free the request + gDma3Requests[gDma3RequestCursor].src = NULL; + gDma3Requests[gDma3RequestCursor].dest = NULL; + gDma3Requests[gDma3RequestCursor].size = 0; + gDma3Requests[gDma3RequestCursor].mode = 0; + gDma3Requests[gDma3RequestCursor].value = 0; + gDma3RequestCursor++; + + if (gDma3RequestCursor >= MAX_DMA_REQUESTS) // loop back to the first DMA request + gDma3RequestCursor = 0; + } +} + +s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode) +{ + int cursor; + int i = 0; + + gDma3ManagerLocked = TRUE; + cursor = gDma3RequestCursor; + + while (i < MAX_DMA_REQUESTS) + { + if (gDma3Requests[cursor].size == 0) // an empty request was found. + { + gDma3Requests[cursor].src = src; + gDma3Requests[cursor].dest = dest; + gDma3Requests[cursor].size = size; + + if (mode == 1) + gDma3Requests[cursor].mode = DMA_REQUEST_COPY32; + else + gDma3Requests[cursor].mode = DMA_REQUEST_COPY16; + + gDma3ManagerLocked = FALSE; + return cursor; + } + if (++cursor >= MAX_DMA_REQUESTS) // loop back to start. + cursor = 0; + i++; + } + gDma3ManagerLocked = FALSE; + return -1; // no free DMA request was found +} + +s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode) +{ + int cursor; + int i = 0; + + cursor = gDma3RequestCursor; + gDma3ManagerLocked = TRUE; + + while (i < MAX_DMA_REQUESTS) + { + if (gDma3Requests[cursor].size == 0) // an empty request was found. + { + gDma3Requests[cursor].dest = dest; + gDma3Requests[cursor].size = size; + gDma3Requests[cursor].mode = mode; + gDma3Requests[cursor].value = value; + + if(mode == 1) + gDma3Requests[cursor].mode = DMA_REQUEST_FILL32; + else + gDma3Requests[cursor].mode = DMA_REQUEST_FILL16; + + gDma3ManagerLocked = FALSE; + return cursor; + } + if (++cursor >= MAX_DMA_REQUESTS) // loop back to start. + cursor = 0; + i++; + } + gDma3ManagerLocked = FALSE; + return -1; // no free DMA request was found +} + +s16 CheckForSpaceForDma3Request(s16 index) +{ + int i = 0; + + if (index == -1) // check if all requests are free + { + while (i < MAX_DMA_REQUESTS) + { + if (gDma3Requests[i].size != 0) + return -1; + i++; + } + return 0; + } + else // check the specified request + { + if (gDma3Requests[index].size != 0) + return -1; + return 0; + } +} diff --git a/gflib/gpu_regs.c b/gflib/gpu_regs.c new file mode 100644 index 000000000..3bcc4fd93 --- /dev/null +++ b/gflib/gpu_regs.c @@ -0,0 +1,195 @@ +#include "global.h" +#include "gpu_regs.h" + +#define GPU_REG_BUF_SIZE 0x60 + +#define GPU_REG_BUF(offset) (*(u16 *)(&sGpuRegBuffer[offset])) +#define GPU_REG(offset) (*(vu16 *)(REG_BASE + offset)) + +#define EMPTY_SLOT 0xFF + +static u8 sGpuRegBuffer[GPU_REG_BUF_SIZE]; +static u8 sGpuRegWaitingList[GPU_REG_BUF_SIZE]; +static volatile bool8 sGpuRegBufferLocked; +static volatile bool8 sShouldSyncRegIE; +static vu16 sRegIE; + +static void CopyBufferedValueToGpuReg(u8 regOffset); +static void SyncRegIE(void); +static void UpdateRegDispstatIntrBits(u16 regIE); + +void InitGpuRegManager(void) +{ + s32 i; + + for (i = 0; i < GPU_REG_BUF_SIZE; i++) + { + sGpuRegBuffer[i] = 0; + sGpuRegWaitingList[i] = EMPTY_SLOT; + } + + sGpuRegBufferLocked = FALSE; + sShouldSyncRegIE = FALSE; + sRegIE = 0; +} + +static void CopyBufferedValueToGpuReg(u8 regOffset) +{ + if (regOffset == REG_OFFSET_DISPSTAT) + { + REG_DISPSTAT &= ~(DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR); + REG_DISPSTAT |= GPU_REG_BUF(REG_OFFSET_DISPSTAT); + } + else + { + GPU_REG(regOffset) = GPU_REG_BUF(regOffset); + } +} + +void CopyBufferedValuesToGpuRegs(void) +{ + if (!sGpuRegBufferLocked) + { + s32 i; + + for (i = 0; i < GPU_REG_BUF_SIZE; i++) + { + u8 regOffset = sGpuRegWaitingList[i]; + if (regOffset == EMPTY_SLOT) + return; + CopyBufferedValueToGpuReg(regOffset); + sGpuRegWaitingList[i] = EMPTY_SLOT; + } + } +} + +void SetGpuReg(u8 regOffset, u16 value) +{ + if (regOffset < GPU_REG_BUF_SIZE) + { + u16 vcount; + + GPU_REG_BUF(regOffset) = value; + vcount = REG_VCOUNT & 0xFF; + + if ((vcount >= 161 && vcount <= 225) || (REG_DISPCNT & DISPCNT_FORCED_BLANK)) + { + CopyBufferedValueToGpuReg(regOffset); + } + else + { + s32 i; + + sGpuRegBufferLocked = TRUE; + + for (i = 0; i < GPU_REG_BUF_SIZE && sGpuRegWaitingList[i] != EMPTY_SLOT; i++) + { + if (sGpuRegWaitingList[i] == regOffset) + { + sGpuRegBufferLocked = FALSE; + return; + } + } + + sGpuRegWaitingList[i] = regOffset; + sGpuRegBufferLocked = FALSE; + } + } +} + +void SetGpuReg_ForcedBlank(u8 regOffset, u16 value) +{ + if (regOffset < GPU_REG_BUF_SIZE) + { + GPU_REG_BUF(regOffset) = value; + + if (REG_DISPCNT & DISPCNT_FORCED_BLANK) + { + CopyBufferedValueToGpuReg(regOffset); + } + else + { + s32 i; + + sGpuRegBufferLocked = TRUE; + + for (i = 0; i < GPU_REG_BUF_SIZE && sGpuRegWaitingList[i] != EMPTY_SLOT; i++) + { + if (sGpuRegWaitingList[i] == regOffset) + { + sGpuRegBufferLocked = FALSE; + return; + } + } + + sGpuRegWaitingList[i] = regOffset; + sGpuRegBufferLocked = FALSE; + } + } +} + +u16 GetGpuReg(u8 regOffset) +{ + if (regOffset == REG_OFFSET_DISPSTAT) + return REG_DISPSTAT; + + if (regOffset == REG_OFFSET_VCOUNT) + return REG_VCOUNT; + + return GPU_REG_BUF(regOffset); +} + +void SetGpuRegBits(u8 regOffset, u16 mask) +{ + u16 regValue = GPU_REG_BUF(regOffset); + SetGpuReg(regOffset, regValue | mask); +} + +void ClearGpuRegBits(u8 regOffset, u16 mask) +{ + u16 regValue = GPU_REG_BUF(regOffset); + SetGpuReg(regOffset, regValue & ~mask); +} + +static void SyncRegIE(void) +{ + if (sShouldSyncRegIE) + { + u16 temp = REG_IME; + REG_IME = 0; + REG_IE = sRegIE; + REG_IME = temp; + sShouldSyncRegIE = FALSE; + } +} + +void EnableInterrupts(u16 mask) +{ + sRegIE |= mask; + sShouldSyncRegIE = TRUE; + SyncRegIE(); + UpdateRegDispstatIntrBits(sRegIE); +} + +void DisableInterrupts(u16 mask) +{ + sRegIE &= ~mask; + sShouldSyncRegIE = TRUE; + SyncRegIE(); + UpdateRegDispstatIntrBits(sRegIE); +} + +static void UpdateRegDispstatIntrBits(u16 regIE) +{ + u16 oldValue = GetGpuReg(REG_OFFSET_DISPSTAT) & (DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR); + u16 newValue = 0; + + if (regIE & INTR_FLAG_VBLANK) + newValue |= DISPSTAT_VBLANK_INTR; + + if (regIE & INTR_FLAG_HBLANK) + newValue |= DISPSTAT_HBLANK_INTR; + + if (oldValue != newValue) + SetGpuReg(REG_OFFSET_DISPSTAT, newValue); +} diff --git a/gflib/gpu_regs.h b/gflib/gpu_regs.h new file mode 100644 index 000000000..89e0cb64b --- /dev/null +++ b/gflib/gpu_regs.h @@ -0,0 +1,19 @@ +#ifndef GUARD_GPU_REGS_H +#define GUARD_GPU_REGS_H + +// Exported type declarations + +// Exported RAM declarations + +// Exported ROM declarations +void InitGpuRegManager(void); +void CopyBufferedValuesToGpuRegs(void); +void SetGpuReg(u8 regOffset, u16 value); +void SetGpuReg_ForcedBlank(u8 regOffset, u16 value); +u16 GetGpuReg(u8 regOffset); +void SetGpuRegBits(u8 regOffset, u16 mask); +void ClearGpuRegBits(u8 regOffset, u16 mask); +void EnableInterrupts(u16 mask); +void DisableInterrupts(u16 mask); + +#endif //GUARD_GPU_REGS_H diff --git a/gflib/malloc.c b/gflib/malloc.c new file mode 100644 index 000000000..4d1a9fe5c --- /dev/null +++ b/gflib/malloc.c @@ -0,0 +1,210 @@ +#include "global.h" + +static void *sHeapStart; +static u32 sHeapSize; +static u32 malloc_c_unused_0300000c; // needed to align dma3_manager.o(.bss) + +#define MALLOC_SYSTEM_ID 0xA3A3 + +struct MemBlock { + // Whether this block is currently allocated. + bool16 flag; + + // Magic number used for error checking. Should equal MALLOC_SYSTEM_ID. + u16 magic; + + // Size of the block (not including this header struct). + u32 size; + + // Previous block pointer. Equals sHeapStart if this is the first block. + struct MemBlock *prev; + + // Next block pointer. Equals sHeapStart if this is the last block. + struct MemBlock *next; + + // Data in the memory block. (Arrays of length 0 are a GNU extension.) + u8 data[0]; +}; + +void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size) +{ + struct MemBlock *header = (struct MemBlock *)block; + + header->flag = FALSE; + header->magic = MALLOC_SYSTEM_ID; + header->size = size; + header->prev = prev; + header->next = next; +} + +void PutFirstMemBlockHeader(void *block, u32 size) +{ + PutMemBlockHeader(block, (struct MemBlock *)block, (struct MemBlock *)block, size - sizeof(struct MemBlock)); +} + +void *AllocInternal(void *heapStart, u32 size) +{ + struct MemBlock *pos = (struct MemBlock *)heapStart; + struct MemBlock *head = pos; + struct MemBlock *splitBlock; + u32 foundBlockSize; + + // Alignment + if (size & 3) + size = 4 * ((size / 4) + 1); + + for (;;) { + // Loop through the blocks looking for unused block that's big enough. + + if (!pos->flag) { + foundBlockSize = pos->size; + + if (foundBlockSize >= size) { + if (foundBlockSize - size < 2 * sizeof(struct MemBlock)) { + // The block isn't much bigger than the requested size, + // so just use it. + pos->flag = TRUE; + } else { + // The block is significantly bigger than the requested + // size, so split the rest into a separate block. + foundBlockSize -= sizeof(struct MemBlock); + foundBlockSize -= size; + + splitBlock = (struct MemBlock *)(pos->data + size); + + pos->flag = TRUE; + pos->size = size; + + PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize); + + pos->next = splitBlock; + + if (splitBlock->next != head) + splitBlock->next->prev = splitBlock; + } + + return pos->data; + } + } + + if (pos->next == head) + return NULL; + + pos = pos->next; + } +} + +void FreeInternal(void *heapStart, void *pointer) +{ + if (pointer) { + struct MemBlock *head = (struct MemBlock *)heapStart; + struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); + block->flag = FALSE; + + // If the freed block isn't the last one, merge with the next block + // if it's not in use. + if (block->next != head) { + if (!block->next->flag) { + block->size += sizeof(struct MemBlock) + block->next->size; + block->next->magic = 0; + block->next = block->next->next; + if (block->next != head) + block->next->prev = block; + } + } + + // If the freed block isn't the first one, merge with the previous block + // if it's not in use. + if (block != head) { + if (!block->prev->flag) { + block->prev->next = block->next; + + if (block->next != head) + block->next->prev = block->prev; + + block->magic = 0; + block->prev->size += sizeof(struct MemBlock) + block->size; + } + } + } +} + +void *AllocZeroedInternal(void *heapStart, u32 size) +{ + void *mem = AllocInternal(heapStart, size); + + if (mem != NULL) { + if (size & 3) + size = 4 * ((size / 4) + 1); + + CpuFill32(0, mem, size); + } + + return mem; +} + +bool32 CheckMemBlockInternal(void *heapStart, void *pointer) +{ + struct MemBlock *head = (struct MemBlock *)heapStart; + struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); + + if (block->magic != MALLOC_SYSTEM_ID) + return FALSE; + + if (block->next->magic != MALLOC_SYSTEM_ID) + return FALSE; + + if (block->next != head && block->next->prev != block) + return FALSE; + + if (block->prev->magic != MALLOC_SYSTEM_ID) + return FALSE; + + if (block->prev != head && block->prev->next != block) + return FALSE; + + if (block->next != head && block->next != (struct MemBlock *)(block->data + block->size)) + return FALSE; + + return TRUE; +} + +void InitHeap(void *heapStart, u32 heapSize) +{ + sHeapStart = heapStart; + sHeapSize = heapSize; + PutFirstMemBlockHeader(heapStart, heapSize); +} + +void *Alloc(u32 size) +{ + return AllocInternal(sHeapStart, size); +} + +void *AllocZeroed(u32 size) +{ + return AllocZeroedInternal(sHeapStart, size); +} + +void Free(void *pointer) +{ + FreeInternal(sHeapStart, pointer); +} + +bool32 CheckMemBlock(void *pointer) +{ + return CheckMemBlockInternal(sHeapStart, pointer); +} + +bool32 CheckHeap() +{ + struct MemBlock *pos = (struct MemBlock *)sHeapStart; + + do { + if (!CheckMemBlockInternal(sHeapStart, pos->data)) + return FALSE; + pos = pos->next; + } while (pos != (struct MemBlock *)sHeapStart); + + return TRUE; +} diff --git a/gflib/malloc.h b/gflib/malloc.h new file mode 100644 index 000000000..f2dcf6d46 --- /dev/null +++ b/gflib/malloc.h @@ -0,0 +1,22 @@ +#ifndef GUARD_ALLOC_H +#define GUARD_ALLOC_H + +#define HEAP_SIZE 0x1C000 +#define malloc Alloc +#define calloc(ct, sz) AllocZeroed((ct) * (sz)) +#define free Free + +#define FREE_AND_SET_NULL(ptr) \ +{ \ + free(ptr); \ + ptr = NULL; \ +} + +extern u8 gHeap[]; + +void *Alloc(u32 size); +void *AllocZeroed(u32 size); +void Free(void *pointer); +void InitHeap(void *pointer, u32 size); + +#endif // GUARD_ALLOC_H diff --git a/gflib/sprite.c b/gflib/sprite.c new file mode 100644 index 000000000..e25eac62e --- /dev/null +++ b/gflib/sprite.c @@ -0,0 +1,1758 @@ +#include "global.h" +#include "sprite.h" +#include "main.h" +#include "palette.h" + +#define MAX_SPRITE_COPY_REQUESTS 64 + +#define OAM_MATRIX_COUNT 32 + +#define SET_SPRITE_TILE_RANGE(index, start, count) \ +{ \ + sSpriteTileRanges[index * 2] = start; \ + (sSpriteTileRanges + 1)[index * 2] = count; \ +} + +#define ALLOC_SPRITE_TILE(n) \ +{ \ + sSpriteTileAllocBitmap[(n) / 8] |= (1 << ((n) % 8)); \ +} + +#define FREE_SPRITE_TILE(n) \ +{ \ + sSpriteTileAllocBitmap[(n) / 8] &= ~(1 << ((n) % 8)); \ +} + +#define SPRITE_TILE_IS_ALLOCATED(n) ((sSpriteTileAllocBitmap[(n) / 8] >> ((n) % 8)) & 1) + + +struct SpriteCopyRequest +{ + const u8 *src; + u8 *dest; + u16 size; +}; + +struct OamDimensions +{ + s8 width; + s8 height; +}; + +static void UpdateOamCoords(void); +static void BuildSpritePriorities(void); +static void SortSprites(void); +static void CopyMatricesToOamBuffer(void); +static void AddSpritesToOamBuffer(void); +static u8 CreateSpriteAt(u8 index, const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); +static void ResetOamMatrices(void); +static void ResetSprite(struct Sprite *sprite); +static s16 AllocSpriteTiles(u16 tileCount); +static void RequestSpriteFrameImageCopy(u16 index, u16 tileNum, const struct SpriteFrameImage *images); +static void ResetAllSprites(void); +static void BeginAnim(struct Sprite *sprite); +static void ContinueAnim(struct Sprite *sprite); +static void AnimCmd_frame(struct Sprite *sprite); +static void AnimCmd_end(struct Sprite *sprite); +static void AnimCmd_jump(struct Sprite *sprite); +static void AnimCmd_loop(struct Sprite *sprite); +static void BeginAnimLoop(struct Sprite *sprite); +static void ContinueAnimLoop(struct Sprite *sprite); +static void JumpToTopOfAnimLoop(struct Sprite *sprite); +static void BeginAffineAnim(struct Sprite *sprite); +static void ContinueAffineAnim(struct Sprite *sprite); +static void AffineAnimDelay(u8 matrixNum, struct Sprite *sprite); +static void AffineAnimCmd_loop(u8 matrixNum, struct Sprite *sprite); +static void BeginAffineAnimLoop(u8 matrixNum, struct Sprite *sprite); +static void ContinueAffineAnimLoop(u8 matrixNum, struct Sprite *sprite); +static void JumpToTopOfAffineAnimLoop(u8 matrixNum, struct Sprite *sprite); +static void AffineAnimCmd_jump(u8 matrixNum, struct Sprite *sprite); +static void AffineAnimCmd_end(u8 matrixNum, struct Sprite *sprite); +static void AffineAnimCmd_frame(u8 matrixNum, struct Sprite *sprite); +static void CopyOamMatrix(u8 destMatrixIndex, struct OamMatrix *srcMatrix); +static u8 GetSpriteMatrixNum(struct Sprite *sprite); +static void SetSpriteOamFlipBits(struct Sprite *sprite, u8 hFlip, u8 vFlip); +static void AffineAnimStateRestartAnim(u8 matrixNum); +static void AffineAnimStateStartAnim(u8 matrixNum, u8 animNum); +static void AffineAnimStateReset(u8 matrixNum); +static void ApplyAffineAnimFrameAbsolute(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd); +static void DecrementAnimDelayCounter(struct Sprite *sprite); +static bool8 DecrementAffineAnimDelayCounter(struct Sprite *sprite, u8 matrixNum); +static void ApplyAffineAnimFrameRelativeAndUpdateMatrix(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd); +static s16 ConvertScaleParam(s16 scale); +static void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd); +static void ApplyAffineAnimFrame(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd); +static u8 IndexOfSpriteTileTag(u16 tag); +static void AllocSpriteTileRange(u16 tag, u16 start, u16 count); +static void DoLoadSpritePalette(const u16 *src, u16 paletteOffset); +static void obj_update_pos2(struct Sprite* sprite, s32 a1, s32 a2); + +typedef void (*AnimFunc)(struct Sprite *); +typedef void (*AnimCmdFunc)(struct Sprite *); +typedef void (*AffineAnimCmdFunc)(u8 matrixNum, struct Sprite *); + +#define DUMMY_OAM_DATA \ +{ \ + .y = 160, \ + .affineMode = 0, \ + .objMode = 0, \ + .mosaic = 0, \ + .bpp = 0, \ + .shape = SPRITE_SHAPE(8x8), \ + .x = 304, \ + .matrixNum = 0, \ + .size = SPRITE_SIZE(8x8), \ + .tileNum = 0, \ + .priority = 3, /* lowest priority */ \ + .paletteNum = 0, \ + .affineParam = 0 \ +} + +#define ANIM_END 0xFFFF +#define AFFINE_ANIM_END 0x7FFF + +// forward declarations +const union AnimCmd * const gDummySpriteAnimTable[]; +const union AffineAnimCmd * const gDummySpriteAffineAnimTable[]; +const struct SpriteTemplate gDummySpriteTemplate; + +// Unreferenced data. Also unreferenced in R/S. +static const u8 sUnknownData[24] = +{ + 0x01, 0x04, 0x10, 0x40, + 0x02, 0x04, 0x08, 0x20, + 0x02, 0x04, 0x08, 0x20, + 0x01, 0x04, 0x10, 0x40, + 0x02, 0x04, 0x08, 0x20, + 0x02, 0x04, 0x08, 0x20, +}; + +static const u8 sCenterToCornerVecTable[3][4][2] = +{ + { // square + { -4, -4 }, + { -8, -8 }, + { -16, -16 }, + { -32, -32 }, + }, + { // horizontal rectangle + { -8, -4 }, + { -16, -4 }, + { -16, -8 }, + { -32, -16 }, + }, + { // vertical rectangle + { -4, -8 }, + { -4, -16 }, + { -8, -16 }, + { -16, -32 }, + }, +}; + +static const struct Sprite sDummySprite = +{ + .oam = DUMMY_OAM_DATA, + .anims = gDummySpriteAnimTable, + .images = NULL, + .affineAnims = gDummySpriteAffineAnimTable, + .template = &gDummySpriteTemplate, + .subspriteTables = NULL, + .callback = SpriteCallbackDummy, + .pos1 = { 304, 160 }, + .pos2 = { 0, 0 }, + .centerToCornerVecX = 0, + .centerToCornerVecY = 0, + .animNum = 0, + .animCmdIndex = 0, + .animDelayCounter = 0, + .animPaused = 0, + .affineAnimPaused = 0, + .animLoopCounter = 0, + .data = {0, 0, 0, 0, 0, 0, 0}, + .inUse = 0, + .coordOffsetEnabled = 0, + .invisible = FALSE, + .flags_3 = 0, + .flags_4 = 0, + .flags_5 = 0, + .flags_6 = 0, + .flags_7 = 0, + .hFlip = 0, + .vFlip = 0, + .animBeginning = 0, + .affineAnimBeginning = 0, + .animEnded = 0, + .affineAnimEnded = 0, + .usingSheet = 0, + .flags_f = 0, + .sheetTileStart = 0, + .subspriteTableNum = 0, + .subspriteMode = 0, + .subpriority = 0xFF +}; + +const struct OamData gDummyOamData = DUMMY_OAM_DATA; + +static const union AnimCmd sDummyAnim = { ANIM_END }; + +const union AnimCmd * const gDummySpriteAnimTable[] = { &sDummyAnim }; + +static const union AffineAnimCmd sDummyAffineAnim = { AFFINE_ANIM_END }; + +const union AffineAnimCmd * const gDummySpriteAffineAnimTable[] = { &sDummyAffineAnim }; + +const struct SpriteTemplate gDummySpriteTemplate = +{ + .tileTag = 0, + .paletteTag = 0xFFFF, + .oam = &gDummyOamData, + .anims = gDummySpriteAnimTable, + .images = NULL, + .affineAnims = gDummySpriteAffineAnimTable, + .callback = SpriteCallbackDummy +}; + +static const AnimFunc sAnimFuncs[] = +{ + ContinueAnim, + BeginAnim, +}; + +static const AnimFunc sAffineAnimFuncs[] = +{ + ContinueAffineAnim, + BeginAffineAnim, +}; + +static const AnimCmdFunc sAnimCmdFuncs[] = +{ + AnimCmd_loop, + AnimCmd_jump, + AnimCmd_end, + AnimCmd_frame, +}; + +static const AffineAnimCmdFunc sAffineAnimCmdFuncs[] = +{ + AffineAnimCmd_loop, + AffineAnimCmd_jump, + AffineAnimCmd_end, + AffineAnimCmd_frame, +}; + +static const s32 sUnknown_082EC6F4[3][4][2] = +{ + { + {8, 8}, + {0x10, 0x10}, + {0x20, 0x20}, + {0x40, 0x40}, + }, + { + {0x10, 8}, + {0x20, 8}, + {0x20, 0x10}, + {0x40, 0x20}, + }, + { + {8, 0x10}, + {8, 0x20}, + {0x10, 0x20}, + {0x20, 0x40}, + }, +}; + +static const struct OamDimensions sOamDimensions[3][4] = +{ + { // square + { 8, 8 }, + { 16, 16 }, + { 32, 32 }, + { 64, 64 }, + }, + { // horizontal rectangle + { 16, 8 }, + { 32, 8 }, + { 32, 16 }, + { 64, 32 }, + }, + { // vertical rectangle + { 8, 16 }, + { 8, 32 }, + { 16, 32 }, + { 32, 64 }, + }, +}; + +// iwram bss +static u16 sSpriteTileRangeTags[MAX_SPRITES]; +static u16 sSpriteTileRanges[MAX_SPRITES * 2]; +static struct AffineAnimState sAffineAnimStates[OAM_MATRIX_COUNT]; +static u16 sSpritePaletteTags[16]; + +// iwram common +u32 gOamMatrixAllocBitmap; +u8 gReservedSpritePaletteCount; + +EWRAM_DATA struct Sprite gSprites[MAX_SPRITES + 1] = {0}; +EWRAM_DATA static u16 sSpritePriorities[MAX_SPRITES] = {0}; +EWRAM_DATA static u8 sSpriteOrder[MAX_SPRITES] = {0}; +EWRAM_DATA static bool8 sShouldProcessSpriteCopyRequests = 0; +EWRAM_DATA static u8 sSpriteCopyRequestCount = 0; +EWRAM_DATA static struct SpriteCopyRequest sSpriteCopyRequests[MAX_SPRITES] = {0}; +EWRAM_DATA u8 gOamLimit = 0; +EWRAM_DATA u16 gReservedSpriteTileCount = 0; +EWRAM_DATA static u8 sSpriteTileAllocBitmap[128] = {0}; +EWRAM_DATA s16 gSpriteCoordOffsetX = 0; +EWRAM_DATA s16 gSpriteCoordOffsetY = 0; +EWRAM_DATA struct OamMatrix gOamMatrices[OAM_MATRIX_COUNT] = {0}; +EWRAM_DATA bool8 gAffineAnimsDisabled = FALSE; + +void ResetSpriteData(void) +{ + ResetOamRange(0, 128); + ResetAllSprites(); + ClearSpriteCopyRequests(); + ResetAffineAnimData(); + FreeSpriteTileRanges(); + gOamLimit = 64; + gReservedSpriteTileCount = 0; + AllocSpriteTiles(0); + gSpriteCoordOffsetX = 0; + gSpriteCoordOffsetY = 0; +} + +void AnimateSprites(void) +{ + u8 i; + for (i = 0; i < MAX_SPRITES; i++) + { + struct Sprite *sprite = &gSprites[i]; + + if (sprite->inUse) + { + sprite->callback(sprite); + + if (sprite->inUse) + AnimateSprite(sprite); + } + } +} + +void BuildOamBuffer(void) +{ + u8 temp; + UpdateOamCoords(); + BuildSpritePriorities(); + SortSprites(); + temp = gMain.oamLoadDisabled; + gMain.oamLoadDisabled = TRUE; + AddSpritesToOamBuffer(); + CopyMatricesToOamBuffer(); + gMain.oamLoadDisabled = temp; + sShouldProcessSpriteCopyRequests = TRUE; +} + +void UpdateOamCoords(void) +{ + u8 i; + for (i = 0; i < MAX_SPRITES; i++) + { + struct Sprite *sprite = &gSprites[i]; + if (sprite->inUse && !sprite->invisible) + { + if (sprite->coordOffsetEnabled) + { + sprite->oam.x = sprite->pos1.x + sprite->pos2.x + sprite->centerToCornerVecX + gSpriteCoordOffsetX; + sprite->oam.y = sprite->pos1.y + sprite->pos2.y + sprite->centerToCornerVecY + gSpriteCoordOffsetY; + } + else + { + sprite->oam.x = sprite->pos1.x + sprite->pos2.x + sprite->centerToCornerVecX; + sprite->oam.y = sprite->pos1.y + sprite->pos2.y + sprite->centerToCornerVecY; + } + } + } +} + +void BuildSpritePriorities(void) +{ + u16 i; + for (i = 0; i < MAX_SPRITES; i++) + { + struct Sprite *sprite = &gSprites[i]; + u16 priority = sprite->subpriority | (sprite->oam.priority << 8); + sSpritePriorities[i] = priority; + } +} + +void SortSprites(void) +{ + u8 i; + for (i = 1; i < MAX_SPRITES; i++) + { + u8 j = i; + struct Sprite *sprite1 = &gSprites[sSpriteOrder[i - 1]]; + struct Sprite *sprite2 = &gSprites[sSpriteOrder[i]]; + u16 sprite1Priority = sSpritePriorities[sSpriteOrder[i - 1]]; + u16 sprite2Priority = sSpritePriorities[sSpriteOrder[i]]; + s16 sprite1Y = sprite1->oam.y; + s16 sprite2Y = sprite2->oam.y; + + if (sprite1Y >= DISPLAY_HEIGHT) + sprite1Y = sprite1Y - 256; + + if (sprite2Y >= DISPLAY_HEIGHT) + sprite2Y = sprite2Y - 256; + + if (sprite1->oam.affineMode == ST_OAM_AFFINE_DOUBLE + && sprite1->oam.size == 3) + { + u32 shape = sprite1->oam.shape; + if (shape == ST_OAM_SQUARE || shape == 2) + { + if (sprite1Y > 128) + sprite1Y = sprite1Y - 256; + } + } + + if (sprite2->oam.affineMode == ST_OAM_AFFINE_DOUBLE + && sprite2->oam.size == 3) + { + u32 shape = sprite2->oam.shape; + if (shape == ST_OAM_SQUARE || shape == ST_OAM_V_RECTANGLE) + { + if (sprite2Y > 128) + sprite2Y = sprite2Y - 256; + } + } + + while (j > 0 + && ((sprite1Priority > sprite2Priority) + || (sprite1Priority == sprite2Priority && sprite1Y < sprite2Y))) + { + u8 temp = sSpriteOrder[j]; + sSpriteOrder[j] = sSpriteOrder[j - 1]; + sSpriteOrder[j - 1] = temp; + + // UB: If j equals 1, then j-- makes j equal 0. + // Then, sSpriteOrder[-1] gets accessed below. + // Although this doesn't result in a bug in the ROM, + // the behavior is undefined. + j--; + + sprite1 = &gSprites[sSpriteOrder[j - 1]]; + sprite2 = &gSprites[sSpriteOrder[j]]; + sprite1Priority = sSpritePriorities[sSpriteOrder[j - 1]]; + sprite2Priority = sSpritePriorities[sSpriteOrder[j]]; + sprite1Y = sprite1->oam.y; + sprite2Y = sprite2->oam.y; + + if (sprite1Y >= DISPLAY_HEIGHT) + sprite1Y = sprite1Y - 256; + + if (sprite2Y >= DISPLAY_HEIGHT) + sprite2Y = sprite2Y - 256; + + if (sprite1->oam.affineMode == ST_OAM_AFFINE_DOUBLE + && sprite1->oam.size == 3) + { + u32 shape = sprite1->oam.shape; + if (shape == ST_OAM_SQUARE || shape == ST_OAM_V_RECTANGLE) + { + if (sprite1Y > 128) + sprite1Y = sprite1Y - 256; + } + } + + if (sprite2->oam.affineMode == ST_OAM_AFFINE_DOUBLE + && sprite2->oam.size == 3) + { + u32 shape = sprite2->oam.shape; + if (shape == ST_OAM_SQUARE || shape == ST_OAM_V_RECTANGLE) + { + if (sprite2Y > 128) + sprite2Y = sprite2Y - 256; + } + } + } + } +} + +void CopyMatricesToOamBuffer(void) +{ + u8 i; + for (i = 0; i < OAM_MATRIX_COUNT; i++) + { + u32 base = 4 * i; + gMain.oamBuffer[base + 0].affineParam = gOamMatrices[i].a; + gMain.oamBuffer[base + 1].affineParam = gOamMatrices[i].b; + gMain.oamBuffer[base + 2].affineParam = gOamMatrices[i].c; + gMain.oamBuffer[base + 3].affineParam = gOamMatrices[i].d; + } +} + +void AddSpritesToOamBuffer(void) +{ + u8 i = 0; + u8 oamIndex = 0; + + while (i < MAX_SPRITES) + { + struct Sprite *sprite = &gSprites[sSpriteOrder[i]]; + if (sprite->inUse && !sprite->invisible && AddSpriteToOamBuffer(sprite, &oamIndex)) + return; + i++; + } + + while (oamIndex < gOamLimit) + { + gMain.oamBuffer[oamIndex] = gDummyOamData; + oamIndex++; + } +} + +u8 CreateSprite(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) +{ + u8 i; + + for (i = 0; i < MAX_SPRITES; i++) + if (!gSprites[i].inUse) + return CreateSpriteAt(i, template, x, y, subpriority); + + return MAX_SPRITES; +} + +u8 CreateSpriteAtEnd(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) +{ + s16 i; + + for (i = MAX_SPRITES - 1; i > -1; i--) + if (!gSprites[i].inUse) + return CreateSpriteAt(i, template, x, y, subpriority); + + return MAX_SPRITES; +} + +u8 CreateInvisibleSprite(void (*callback)(struct Sprite *)) +{ + u8 index = CreateSprite(&gDummySpriteTemplate, 0, 0, 31); + + if (index == MAX_SPRITES) + { + return MAX_SPRITES; + } + else + { + gSprites[index].invisible = TRUE; + gSprites[index].callback = callback; + return index; + } +} + +u8 CreateSpriteAt(u8 index, const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) +{ + struct Sprite *sprite = &gSprites[index]; + + ResetSprite(sprite); + + sprite->inUse = TRUE; + sprite->animBeginning = TRUE; + sprite->affineAnimBeginning = TRUE; + sprite->usingSheet = TRUE; + + sprite->subpriority = subpriority; + sprite->oam = *template->oam; + sprite->anims = template->anims; + sprite->affineAnims = template->affineAnims; + sprite->template = template; + sprite->callback = template->callback; + sprite->pos1.x = x; + sprite->pos1.y = y; + + CalcCenterToCornerVec(sprite, sprite->oam.shape, sprite->oam.size, sprite->oam.affineMode); + + if (template->tileTag == 0xFFFF) + { + s16 tileNum; + sprite->images = template->images; + tileNum = AllocSpriteTiles((u8)(sprite->images->size / TILE_SIZE_4BPP)); + if (tileNum == -1) + { + ResetSprite(sprite); + return MAX_SPRITES; + } + sprite->oam.tileNum = tileNum; + sprite->usingSheet = FALSE; + sprite->sheetTileStart = 0; + } + else + { + sprite->sheetTileStart = GetSpriteTileStartByTag(template->tileTag); + SetSpriteSheetFrameTileNum(sprite); + } + + if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) + InitSpriteAffineAnim(sprite); + + if (template->paletteTag != 0xFFFF) + sprite->oam.paletteNum = IndexOfSpritePaletteTag(template->paletteTag); + + return index; +} + +u8 CreateSpriteAndAnimate(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) +{ + u8 i; + + for (i = 0; i < MAX_SPRITES; i++) + { + struct Sprite *sprite = &gSprites[i]; + + if (!gSprites[i].inUse) + { + u8 index = CreateSpriteAt(i, template, x, y, subpriority); + + if (index == MAX_SPRITES) + return MAX_SPRITES; + + gSprites[i].callback(sprite); + + if (gSprites[i].inUse) + AnimateSprite(sprite); + + return index; + } + } + + return MAX_SPRITES; +} + +void DestroySprite(struct Sprite *sprite) +{ + if (sprite->inUse) + { + if (!sprite->usingSheet) + { + u16 i; + u16 tileEnd = (sprite->images->size / TILE_SIZE_4BPP) + sprite->oam.tileNum; + for (i = sprite->oam.tileNum; i < tileEnd; i++) + FREE_SPRITE_TILE(i); + } + ResetSprite(sprite); + } +} + +void ResetOamRange(u8 a, u8 b) +{ + u8 i; + + for (i = a; i < b; i++) + { + struct OamData *oamBuffer = gMain.oamBuffer; + oamBuffer[i] = *(struct OamData *)&gDummyOamData; + } +} + +void LoadOam(void) +{ + if (!gMain.oamLoadDisabled) + CpuCopy32(gMain.oamBuffer, (void *)OAM, sizeof(gMain.oamBuffer)); +} + +void ClearSpriteCopyRequests(void) +{ + u8 i; + + sShouldProcessSpriteCopyRequests = FALSE; + sSpriteCopyRequestCount = 0; + + for (i = 0; i < MAX_SPRITE_COPY_REQUESTS; i++) + { + sSpriteCopyRequests[i].src = 0; + sSpriteCopyRequests[i].dest = 0; + sSpriteCopyRequests[i].size = 0; + } +} + +void ResetOamMatrices(void) +{ + u8 i; + for (i = 0; i < OAM_MATRIX_COUNT; i++) + { + // set to identity matrix + gOamMatrices[i].a = 0x0100; + gOamMatrices[i].b = 0x0000; + gOamMatrices[i].c = 0x0000; + gOamMatrices[i].d = 0x0100; + } +} + +void SetOamMatrix(u8 matrixNum, u16 a, u16 b, u16 c, u16 d) +{ + gOamMatrices[matrixNum].a = a; + gOamMatrices[matrixNum].b = b; + gOamMatrices[matrixNum].c = c; + gOamMatrices[matrixNum].d = d; +} + +void ResetSprite(struct Sprite *sprite) +{ + *sprite = sDummySprite; +} + +void CalcCenterToCornerVec(struct Sprite *sprite, u8 shape, u8 size, u8 affineMode) +{ + u8 x = sCenterToCornerVecTable[shape][size][0]; + u8 y = sCenterToCornerVecTable[shape][size][1]; + + if (affineMode & ST_OAM_AFFINE_DOUBLE_MASK) + { + x *= 2; + y *= 2; + } + + sprite->centerToCornerVecX = x; + sprite->centerToCornerVecY = y; +} + +s16 AllocSpriteTiles(u16 tileCount) +{ + u16 i; + s16 start; + u16 numTilesFound; + + if (tileCount == 0) + { + // Free all unreserved tiles if the tile count is 0. + for (i = gReservedSpriteTileCount; i < TOTAL_OBJ_TILE_COUNT; i++) + FREE_SPRITE_TILE(i); + + return 0; + } + + i = gReservedSpriteTileCount; + + for (;;) + { + while (SPRITE_TILE_IS_ALLOCATED(i)) + { + i++; + + if (i == TOTAL_OBJ_TILE_COUNT) + return -1; + } + + start = i; + numTilesFound = 1; + + while (numTilesFound != tileCount) + { + i++; + + if (i == TOTAL_OBJ_TILE_COUNT) + return -1; + + if (!SPRITE_TILE_IS_ALLOCATED(i)) + numTilesFound++; + else + break; + } + + if (numTilesFound == tileCount) + break; + } + + for (i = start; i < tileCount + start; i++) + ALLOC_SPRITE_TILE(i); + + return start; +} + +u8 SpriteTileAllocBitmapOp(u16 bit, u8 op) +{ + u8 index = bit / 8; + u8 shift = bit % 8; + u8 val = bit % 8; + u8 retVal = 0; + + if (op == 0) + { + val = ~(1 << val); + sSpriteTileAllocBitmap[index] &= val; + } + else if (op == 1) + { + val = (1 << val); + sSpriteTileAllocBitmap[index] |= val; + } + else + { + retVal = 1 << shift; + retVal &= sSpriteTileAllocBitmap[index]; + } + + return retVal; +} + +void SpriteCallbackDummy(struct Sprite *sprite) +{ +} + +void ProcessSpriteCopyRequests(void) +{ + if (sShouldProcessSpriteCopyRequests) + { + u8 i = 0; + + while (sSpriteCopyRequestCount > 0) + { + CpuCopy16(sSpriteCopyRequests[i].src, sSpriteCopyRequests[i].dest, sSpriteCopyRequests[i].size); + sSpriteCopyRequestCount--; + i++; + } + + sShouldProcessSpriteCopyRequests = FALSE; + } +} + +void RequestSpriteFrameImageCopy(u16 index, u16 tileNum, const struct SpriteFrameImage *images) +{ + if (sSpriteCopyRequestCount < MAX_SPRITE_COPY_REQUESTS) + { + sSpriteCopyRequests[sSpriteCopyRequestCount].src = images[index].data; + sSpriteCopyRequests[sSpriteCopyRequestCount].dest = (u8 *)OBJ_VRAM0 + TILE_SIZE_4BPP * tileNum; + sSpriteCopyRequests[sSpriteCopyRequestCount].size = images[index].size; + sSpriteCopyRequestCount++; + } +} + +void RequestSpriteCopy(const u8 *src, u8 *dest, u16 size) +{ + if (sSpriteCopyRequestCount < MAX_SPRITE_COPY_REQUESTS) + { + sSpriteCopyRequests[sSpriteCopyRequestCount].src = src; + sSpriteCopyRequests[sSpriteCopyRequestCount].dest = dest; + sSpriteCopyRequests[sSpriteCopyRequestCount].size = size; + sSpriteCopyRequestCount++; + } +} + +void CopyFromSprites(u8 *dest) +{ + u32 i; + u8 *src = (u8 *)gSprites; + for (i = 0; i < sizeof(struct Sprite) * MAX_SPRITES; i++) + { + *dest = *src; + dest++; + src++; + } +} + +void CopyToSprites(u8 *src) +{ + u32 i; + u8 *dest = (u8 *)gSprites; + for (i = 0; i < sizeof(struct Sprite) * MAX_SPRITES; i++) + { + *dest = *src; + src++; + dest++; + } +} + +void ResetAllSprites(void) +{ + u8 i; + + for (i = 0; i < MAX_SPRITES; i++) + { + ResetSprite(&gSprites[i]); + sSpriteOrder[i] = i; + } + + ResetSprite(&gSprites[i]); +} + +void FreeSpriteTiles(struct Sprite *sprite) +{ + if (sprite->template->tileTag != 0xFFFF) + FreeSpriteTilesByTag(sprite->template->tileTag); +} + +void FreeSpritePalette(struct Sprite *sprite) +{ + FreeSpritePaletteByTag(sprite->template->paletteTag); +} + +void FreeSpriteOamMatrix(struct Sprite *sprite) +{ + if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) + { + FreeOamMatrix(sprite->oam.matrixNum); + sprite->oam.affineMode = ST_OAM_AFFINE_OFF; + } +} + +void DestroySpriteAndFreeResources(struct Sprite *sprite) +{ + FreeSpriteTiles(sprite); + FreeSpritePalette(sprite); + FreeSpriteOamMatrix(sprite); + DestroySprite(sprite); +} + +void AnimateSprite(struct Sprite *sprite) +{ + sAnimFuncs[sprite->animBeginning](sprite); + + if (!gAffineAnimsDisabled) + sAffineAnimFuncs[sprite->affineAnimBeginning](sprite); +} + +void BeginAnim(struct Sprite *sprite) +{ + s16 imageValue; + u8 duration; + u8 hFlip; + u8 vFlip; + + sprite->animCmdIndex = 0; + sprite->animEnded = FALSE; + sprite->animLoopCounter = 0; + imageValue = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; + + if (imageValue != -1) + { + sprite->animBeginning = FALSE; + duration = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.duration; + hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; + vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; + + if (duration) + duration--; + + sprite->animDelayCounter = duration; + + if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) + SetSpriteOamFlipBits(sprite, hFlip, vFlip); + + if (sprite->usingSheet) + sprite->oam.tileNum = sprite->sheetTileStart + imageValue; + else + RequestSpriteFrameImageCopy(imageValue, sprite->oam.tileNum, sprite->images); + } +} + +void ContinueAnim(struct Sprite *sprite) +{ + if (sprite->animDelayCounter) + { + u8 hFlip; + u8 vFlip; + DecrementAnimDelayCounter(sprite); + hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; + vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; + if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) + SetSpriteOamFlipBits(sprite, hFlip, vFlip); + } + else if (!sprite->animPaused) + { + s16 type; + s16 funcIndex; + sprite->animCmdIndex++; + type = sprite->anims[sprite->animNum][sprite->animCmdIndex].type; + funcIndex = 3; + if (type < 0) + funcIndex = type + 3; + sAnimCmdFuncs[funcIndex](sprite); + } +} + +void AnimCmd_frame(struct Sprite *sprite) +{ + s16 imageValue; + u8 duration; + u8 hFlip; + u8 vFlip; + + imageValue = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; + duration = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.duration; + hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; + vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; + + if (duration) + duration--; + + sprite->animDelayCounter = duration; + + if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) + SetSpriteOamFlipBits(sprite, hFlip, vFlip); + + if (sprite->usingSheet) + sprite->oam.tileNum = sprite->sheetTileStart + imageValue; + else + RequestSpriteFrameImageCopy(imageValue, sprite->oam.tileNum, sprite->images); +} + +void AnimCmd_end(struct Sprite *sprite) +{ + sprite->animCmdIndex--; + sprite->animEnded = TRUE; +} + +void AnimCmd_jump(struct Sprite *sprite) +{ + s16 imageValue; + u8 duration; + u8 hFlip; + u8 vFlip; + + sprite->animCmdIndex = sprite->anims[sprite->animNum][sprite->animCmdIndex].jump.target; + + imageValue = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; + duration = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.duration; + hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; + vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; + + if (duration) + duration--; + + sprite->animDelayCounter = duration; + + if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) + SetSpriteOamFlipBits(sprite, hFlip, vFlip); + + if (sprite->usingSheet) + sprite->oam.tileNum = sprite->sheetTileStart + imageValue; + else + RequestSpriteFrameImageCopy(imageValue, sprite->oam.tileNum, sprite->images); +} + +void AnimCmd_loop(struct Sprite *sprite) +{ + if (sprite->animLoopCounter) + ContinueAnimLoop(sprite); + else + BeginAnimLoop(sprite); +} + +void BeginAnimLoop(struct Sprite *sprite) +{ + sprite->animLoopCounter = sprite->anims[sprite->animNum][sprite->animCmdIndex].loop.count; + JumpToTopOfAnimLoop(sprite); + ContinueAnim(sprite); +} + +void ContinueAnimLoop(struct Sprite *sprite) +{ + sprite->animLoopCounter--; + JumpToTopOfAnimLoop(sprite); + ContinueAnim(sprite); +} + +void JumpToTopOfAnimLoop(struct Sprite *sprite) +{ + if (sprite->animLoopCounter) + { + sprite->animCmdIndex--; + + while (sprite->anims[sprite->animNum][sprite->animCmdIndex - 1].type != -3) + { + if (sprite->animCmdIndex == 0) + break; + sprite->animCmdIndex--; + } + + sprite->animCmdIndex--; + } +} + +void BeginAffineAnim(struct Sprite *sprite) +{ + if ((sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) && sprite->affineAnims[0][0].type != 32767) + { + struct AffineAnimFrameCmd frameCmd; + u8 matrixNum = GetSpriteMatrixNum(sprite); + AffineAnimStateRestartAnim(matrixNum); + GetAffineAnimFrame(matrixNum, sprite, &frameCmd); + sprite->affineAnimBeginning = FALSE; + sprite->affineAnimEnded = FALSE; + ApplyAffineAnimFrame(matrixNum, &frameCmd); + sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; + if (sprite->flags_f) + obj_update_pos2(sprite, sprite->data[6], sprite->data[7]); + } +} + +void ContinueAffineAnim(struct Sprite *sprite) +{ + if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) + { + u8 matrixNum = GetSpriteMatrixNum(sprite); + + if (sAffineAnimStates[matrixNum].delayCounter) + AffineAnimDelay(matrixNum, sprite); + else if (sprite->affineAnimPaused) + return; + else + { + s16 type; + s16 funcIndex; + sAffineAnimStates[matrixNum].animCmdIndex++; + type = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].type; + funcIndex = 3; + if (type >= 32765) + funcIndex = type - 32765; + sAffineAnimCmdFuncs[funcIndex](matrixNum, sprite); + } + if (sprite->flags_f) + obj_update_pos2(sprite, sprite->data[6], sprite->data[7]); + } +} + +void AffineAnimDelay(u8 matrixNum, struct Sprite *sprite) +{ + if (!DecrementAffineAnimDelayCounter(sprite, matrixNum)) + { + struct AffineAnimFrameCmd frameCmd; + GetAffineAnimFrame(matrixNum, sprite, &frameCmd); + ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, &frameCmd); + } +} + +void AffineAnimCmd_loop(u8 matrixNum, struct Sprite *sprite) +{ + if (sAffineAnimStates[matrixNum].loopCounter) + ContinueAffineAnimLoop(matrixNum, sprite); + else + BeginAffineAnimLoop(matrixNum, sprite); +} + +void BeginAffineAnimLoop(u8 matrixNum, struct Sprite *sprite) +{ + sAffineAnimStates[matrixNum].loopCounter = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].loop.count; + JumpToTopOfAffineAnimLoop(matrixNum, sprite); + ContinueAffineAnim(sprite); +} + +void ContinueAffineAnimLoop(u8 matrixNum, struct Sprite *sprite) +{ + sAffineAnimStates[matrixNum].loopCounter--; + JumpToTopOfAffineAnimLoop(matrixNum, sprite); + ContinueAffineAnim(sprite); +} + +void JumpToTopOfAffineAnimLoop(u8 matrixNum, struct Sprite *sprite) +{ + if (sAffineAnimStates[matrixNum].loopCounter) + { + sAffineAnimStates[matrixNum].animCmdIndex--; + + while (sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex - 1].type != 32765) + { + if (sAffineAnimStates[matrixNum].animCmdIndex == 0) + break; + sAffineAnimStates[matrixNum].animCmdIndex--; + } + + sAffineAnimStates[matrixNum].animCmdIndex--; + } +} + +void AffineAnimCmd_jump(u8 matrixNum, struct Sprite *sprite) +{ + struct AffineAnimFrameCmd frameCmd; + sAffineAnimStates[matrixNum].animCmdIndex = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].jump.target; + GetAffineAnimFrame(matrixNum, sprite, &frameCmd); + ApplyAffineAnimFrame(matrixNum, &frameCmd); + sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; +} + +void AffineAnimCmd_end(u8 matrixNum, struct Sprite *sprite) +{ + struct AffineAnimFrameCmd dummyFrameCmd = {0}; + sprite->affineAnimEnded = TRUE; + sAffineAnimStates[matrixNum].animCmdIndex--; + ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, &dummyFrameCmd); +} + +void AffineAnimCmd_frame(u8 matrixNum, struct Sprite *sprite) +{ + struct AffineAnimFrameCmd frameCmd; + GetAffineAnimFrame(matrixNum, sprite, &frameCmd); + ApplyAffineAnimFrame(matrixNum, &frameCmd); + sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; +} + +void CopyOamMatrix(u8 destMatrixIndex, struct OamMatrix *srcMatrix) +{ + gOamMatrices[destMatrixIndex].a = srcMatrix->a; + gOamMatrices[destMatrixIndex].b = srcMatrix->b; + gOamMatrices[destMatrixIndex].c = srcMatrix->c; + gOamMatrices[destMatrixIndex].d = srcMatrix->d; +} + +u8 GetSpriteMatrixNum(struct Sprite *sprite) +{ + u8 matrixNum = 0; + if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) + matrixNum = sprite->oam.matrixNum; + return matrixNum; +} + +void sub_8007E18(struct Sprite* sprite, s16 a2, s16 a3) +{ + sprite->data[6] = a2; + sprite->data[7] = a3; + sprite->flags_f = 1; +} + +s32 sub_8007E28(s32 a0, s32 a1, s32 a2) +{ + s32 subResult, var1; + + subResult = a1 - a0; + if (subResult < 0) + var1 = -(subResult) >> 9; + else + var1 = -(subResult >> 9); + return a2 - ((u32)(a2 * a1) / (u32)(a0) + var1); +} + +void obj_update_pos2(struct Sprite *sprite, s32 a1, s32 a2) +{ + s32 var0, var1, var2; + + u32 matrixNum = sprite->oam.matrixNum; + if (a1 != 0x800) + { + var0 = sUnknown_082EC6F4[sprite->oam.shape][sprite->oam.size][0]; + var1 = var0 << 8; + var2 = (var0 << 16) / gOamMatrices[matrixNum].a; + sprite->pos2.x = sub_8007E28(var1, var2, a1); + } + if (a2 != 0x800) + { + var0 = sUnknown_082EC6F4[sprite->oam.shape][sprite->oam.size][1]; + var1 = var0 << 8; + var2 = (var0 << 16) / gOamMatrices[matrixNum].d; + sprite->pos2.y = sub_8007E28(var1, var2, a2); + } +} + +void SetSpriteOamFlipBits(struct Sprite *sprite, u8 hFlip, u8 vFlip) +{ + sprite->oam.matrixNum &= 0x7; + sprite->oam.matrixNum |= (((hFlip ^ sprite->hFlip) & 1) << 3); + sprite->oam.matrixNum |= (((vFlip ^ sprite->vFlip) & 1) << 4); +} + +void AffineAnimStateRestartAnim(u8 matrixNum) +{ + sAffineAnimStates[matrixNum].animCmdIndex = 0; + sAffineAnimStates[matrixNum].delayCounter = 0; + sAffineAnimStates[matrixNum].loopCounter = 0; +} + +void AffineAnimStateStartAnim(u8 matrixNum, u8 animNum) +{ + sAffineAnimStates[matrixNum].animNum = animNum; + sAffineAnimStates[matrixNum].animCmdIndex = 0; + sAffineAnimStates[matrixNum].delayCounter = 0; + sAffineAnimStates[matrixNum].loopCounter = 0; + sAffineAnimStates[matrixNum].xScale = 0x0100; + sAffineAnimStates[matrixNum].yScale = 0x0100; + sAffineAnimStates[matrixNum].rotation = 0; +} + +void AffineAnimStateReset(u8 matrixNum) +{ + sAffineAnimStates[matrixNum].animNum = 0; + sAffineAnimStates[matrixNum].animCmdIndex = 0; + sAffineAnimStates[matrixNum].delayCounter = 0; + sAffineAnimStates[matrixNum].loopCounter = 0; + sAffineAnimStates[matrixNum].xScale = 0x0100; + sAffineAnimStates[matrixNum].yScale = 0x0100; + sAffineAnimStates[matrixNum].rotation = 0; +} + +void ApplyAffineAnimFrameAbsolute(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd) +{ + sAffineAnimStates[matrixNum].xScale = frameCmd->xScale; + sAffineAnimStates[matrixNum].yScale = frameCmd->yScale; + sAffineAnimStates[matrixNum].rotation = frameCmd->rotation << 8; +} + +void DecrementAnimDelayCounter(struct Sprite *sprite) +{ + if (!sprite->animPaused) + sprite->animDelayCounter--; +} + +bool8 DecrementAffineAnimDelayCounter(struct Sprite *sprite, u8 matrixNum) +{ + if (!sprite->affineAnimPaused) + --sAffineAnimStates[matrixNum].delayCounter; + return sprite->affineAnimPaused; +} + +void ApplyAffineAnimFrameRelativeAndUpdateMatrix(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd) +{ + struct ObjAffineSrcData srcData; + struct OamMatrix matrix; + sAffineAnimStates[matrixNum].xScale += frameCmd->xScale; + sAffineAnimStates[matrixNum].yScale += frameCmd->yScale; + sAffineAnimStates[matrixNum].rotation = (sAffineAnimStates[matrixNum].rotation + (frameCmd->rotation << 8)) & ~0xFF; + srcData.xScale = ConvertScaleParam(sAffineAnimStates[matrixNum].xScale); + srcData.yScale = ConvertScaleParam(sAffineAnimStates[matrixNum].yScale); + srcData.rotation = sAffineAnimStates[matrixNum].rotation; + ObjAffineSet(&srcData, &matrix, 1, 2); + CopyOamMatrix(matrixNum, &matrix); +} + +s16 ConvertScaleParam(s16 scale) +{ + s32 val = 0x10000; + return val / scale; +} + +void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd) +{ + frameCmd->xScale = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.xScale; + frameCmd->yScale = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.yScale; + frameCmd->rotation = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.rotation; + frameCmd->duration = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.duration; +} + +void ApplyAffineAnimFrame(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd) +{ + struct AffineAnimFrameCmd dummyFrameCmd = {0}; + + if (frameCmd->duration) + { + frameCmd->duration--; + ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, frameCmd); + } + else + { + ApplyAffineAnimFrameAbsolute(matrixNum, frameCmd); + ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, &dummyFrameCmd); + } +} + +void StartSpriteAnim(struct Sprite *sprite, u8 animNum) +{ + sprite->animNum = animNum; + sprite->animBeginning = TRUE; + sprite->animEnded = FALSE; +} + +void StartSpriteAnimIfDifferent(struct Sprite *sprite, u8 animNum) +{ + if (sprite->animNum != animNum) + StartSpriteAnim(sprite, animNum); +} + +void SeekSpriteAnim(struct Sprite *sprite, u8 animCmdIndex) +{ + u8 temp = sprite->animPaused; + sprite->animCmdIndex = animCmdIndex - 1; + sprite->animDelayCounter = 0; + sprite->animBeginning = FALSE; + sprite->animEnded = FALSE; + sprite->animPaused = FALSE; + ContinueAnim(sprite); + if (sprite->animDelayCounter) + sprite->animDelayCounter++; + sprite->animPaused = temp; +} + +void StartSpriteAffineAnim(struct Sprite *sprite, u8 animNum) +{ + u8 matrixNum = GetSpriteMatrixNum(sprite); + AffineAnimStateStartAnim(matrixNum, animNum); + sprite->affineAnimBeginning = TRUE; + sprite->affineAnimEnded = FALSE; +} + +void StartSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum) +{ + u8 matrixNum = GetSpriteMatrixNum(sprite); + if (sAffineAnimStates[matrixNum].animNum != animNum) + StartSpriteAffineAnim(sprite, animNum); +} + +void ChangeSpriteAffineAnim(struct Sprite *sprite, u8 animNum) +{ + u8 matrixNum = GetSpriteMatrixNum(sprite); + sAffineAnimStates[matrixNum].animNum = animNum; + sprite->affineAnimBeginning = TRUE; + sprite->affineAnimEnded = FALSE; +} + +void ChangeSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum) +{ + u8 matrixNum = GetSpriteMatrixNum(sprite); + if (sAffineAnimStates[matrixNum].animNum != animNum) + ChangeSpriteAffineAnim(sprite, animNum); +} + +void SetSpriteSheetFrameTileNum(struct Sprite *sprite) +{ + if (sprite->usingSheet) + { + s16 tileOffset = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; + if (tileOffset < 0) + tileOffset = 0; + sprite->oam.tileNum = sprite->sheetTileStart + tileOffset; + } +} + +void ResetAffineAnimData(void) +{ + u8 i; + + gAffineAnimsDisabled = FALSE; + gOamMatrixAllocBitmap = 0; + + ResetOamMatrices(); + + for (i = 0; i < OAM_MATRIX_COUNT; i++) + AffineAnimStateReset(i); +} + +u8 AllocOamMatrix(void) +{ + u8 i = 0; + u32 bit = 1; + u32 bitmap = gOamMatrixAllocBitmap; + + while (i < OAM_MATRIX_COUNT) + { + if (!(bitmap & bit)) + { + gOamMatrixAllocBitmap |= bit; + return i; + } + + i++; + bit <<= 1; + } + + return 0xFF; +} + +void FreeOamMatrix(u8 matrixNum) +{ + u8 i = 0; + u32 bit = 1; + + while (i < matrixNum) + { + i++; + bit <<= 1; + } + + gOamMatrixAllocBitmap &= ~bit; + SetOamMatrix(matrixNum, 0x100, 0, 0, 0x100); +} + +void InitSpriteAffineAnim(struct Sprite *sprite) +{ + u8 matrixNum = AllocOamMatrix(); + if (matrixNum != 0xFF) + { + CalcCenterToCornerVec(sprite, sprite->oam.shape, sprite->oam.size, sprite->oam.affineMode); + sprite->oam.matrixNum = matrixNum; + sprite->affineAnimBeginning = TRUE; + AffineAnimStateReset(matrixNum); + } +} + +void SetOamMatrixRotationScaling(u8 matrixNum, s16 xScale, s16 yScale, u16 rotation) +{ + struct ObjAffineSrcData srcData; + struct OamMatrix matrix; + srcData.xScale = ConvertScaleParam(xScale); + srcData.yScale = ConvertScaleParam(yScale); + srcData.rotation = rotation; + ObjAffineSet(&srcData, &matrix, 1, 2); + CopyOamMatrix(matrixNum, &matrix); +} + +u16 LoadSpriteSheet(const struct SpriteSheet *sheet) +{ + s16 tileStart = AllocSpriteTiles(sheet->size / TILE_SIZE_4BPP); + + if (tileStart < 0) + { + return 0; + } + else + { + AllocSpriteTileRange(sheet->tag, (u16)tileStart, sheet->size / TILE_SIZE_4BPP); + CpuCopy16(sheet->data, (u8 *)OBJ_VRAM0 + TILE_SIZE_4BPP * tileStart, sheet->size); + return (u16)tileStart; + } +} + +void LoadSpriteSheets(const struct SpriteSheet *sheets) +{ + u8 i; + for (i = 0; sheets[i].data != NULL; i++) + LoadSpriteSheet(&sheets[i]); +} + +void FreeSpriteTilesByTag(u16 tag) +{ + u8 index = IndexOfSpriteTileTag(tag); + if (index != 0xFF) + { + u16 i; + u16 *rangeStarts; + u16 *rangeCounts; + u16 start; + u16 count; + rangeStarts = sSpriteTileRanges; + start = rangeStarts[index * 2]; + rangeCounts = sSpriteTileRanges + 1; + count = rangeCounts[index * 2]; + + for (i = start; i < start + count; i++) + FREE_SPRITE_TILE(i); + + sSpriteTileRangeTags[index] = 0xFFFF; + } +} + +void FreeSpriteTileRanges(void) +{ + u8 i; + + for (i = 0; i < MAX_SPRITES; i++) + { + sSpriteTileRangeTags[i] = 0xFFFF; + SET_SPRITE_TILE_RANGE(i, 0, 0); + } +} + +u16 GetSpriteTileStartByTag(u16 tag) +{ + u8 index = IndexOfSpriteTileTag(tag); + if (index == 0xFF) + return 0xFFFF; + return sSpriteTileRanges[index * 2]; +} + +u8 IndexOfSpriteTileTag(u16 tag) +{ + u8 i; + + for (i = 0; i < MAX_SPRITES; i++) + if (sSpriteTileRangeTags[i] == tag) + return i; + + return 0xFF; +} + +u16 GetSpriteTileTagByTileStart(u16 start) +{ + u8 i; + + for (i = 0; i < MAX_SPRITES; i++) + { + if (sSpriteTileRangeTags[i] != 0xFFFF && sSpriteTileRanges[i * 2] == start) + return sSpriteTileRangeTags[i]; + } + + return 0xFFFF; +} + +void AllocSpriteTileRange(u16 tag, u16 start, u16 count) +{ + u8 freeIndex = IndexOfSpriteTileTag(0xFFFF); + sSpriteTileRangeTags[freeIndex] = tag; + SET_SPRITE_TILE_RANGE(freeIndex, start, count); +} + +void FreeAllSpritePalettes(void) +{ + u8 i; + gReservedSpritePaletteCount = 0; + for (i = 0; i < 16; i++) + sSpritePaletteTags[i] = 0xFFFF; +} + +u8 LoadSpritePalette(const struct SpritePalette *palette) +{ + u8 index = IndexOfSpritePaletteTag(palette->tag); + + if (index != 0xFF) + return index; + + index = IndexOfSpritePaletteTag(0xFFFF); + + if (index == 0xFF) + { + return 0xFF; + } + else + { + sSpritePaletteTags[index] = palette->tag; + DoLoadSpritePalette(palette->data, index * 16); + return index; + } +} + +void LoadSpritePalettes(const struct SpritePalette *palettes) +{ + u8 i; + for (i = 0; palettes[i].data != NULL; i++) + if (LoadSpritePalette(&palettes[i]) == 0xFF) + break; +} + +void DoLoadSpritePalette(const u16 *src, u16 paletteOffset) +{ + LoadPalette(src, paletteOffset + 0x100, 32); +} + +u8 AllocSpritePalette(u16 tag) +{ + u8 index = IndexOfSpritePaletteTag(0xFFFF); + if (index == 0xFF) + { + return 0xFF; + } + else + { + sSpritePaletteTags[index] = tag; + return index; + } +} + +u8 IndexOfSpritePaletteTag(u16 tag) +{ + u8 i; + for (i = gReservedSpritePaletteCount; i < 16; i++) + if (sSpritePaletteTags[i] == tag) + return i; + + return 0xFF; +} + +u16 GetSpritePaletteTagByPaletteNum(u8 paletteNum) +{ + return sSpritePaletteTags[paletteNum]; +} + +void FreeSpritePaletteByTag(u16 tag) +{ + u8 index = IndexOfSpritePaletteTag(tag); + if (index != 0xFF) + sSpritePaletteTags[index] = 0xFFFF; +} + +void SetSubspriteTables(struct Sprite *sprite, const struct SubspriteTable *subspriteTables) +{ + sprite->subspriteTables = subspriteTables; + sprite->subspriteTableNum = 0; + sprite->subspriteMode = SUBSPRITES_ON; +} + +bool8 AddSpriteToOamBuffer(struct Sprite *sprite, u8 *oamIndex) +{ + if (*oamIndex >= gOamLimit) + return 1; + + if (!sprite->subspriteTables || sprite->subspriteMode == SUBSPRITES_OFF) + { + gMain.oamBuffer[*oamIndex] = sprite->oam; + (*oamIndex)++; + return 0; + } + else + { + return AddSubspritesToOamBuffer(sprite, &gMain.oamBuffer[*oamIndex], oamIndex); + } +} + +bool8 AddSubspritesToOamBuffer(struct Sprite *sprite, struct OamData *destOam, u8 *oamIndex) +{ + const struct SubspriteTable *subspriteTable; + struct OamData *oam; + + if (*oamIndex >= gOamLimit) + return 1; + + subspriteTable = &sprite->subspriteTables[sprite->subspriteTableNum]; + oam = &sprite->oam; + + if (!subspriteTable || !subspriteTable->subsprites) + { + *destOam = *oam; + (*oamIndex)++; + return 0; + } + else + { + u16 tileNum; + u16 baseX; + u16 baseY; + u8 subspriteCount; + u8 hFlip; + u8 vFlip; + u8 i; + + tileNum = oam->tileNum; + subspriteCount = subspriteTable->subspriteCount; + hFlip = ((s32)oam->matrixNum >> 3) & 1; + vFlip = ((s32)oam->matrixNum >> 4) & 1; + baseX = oam->x - sprite->centerToCornerVecX; + baseY = oam->y - sprite->centerToCornerVecY; + + for (i = 0; i < subspriteCount; i++, (*oamIndex)++) + { + u16 x; + u16 y; + + if (*oamIndex >= gOamLimit) + return 1; + + x = subspriteTable->subsprites[i].x; + y = subspriteTable->subsprites[i].y; + + if (hFlip) + { + s8 width = sOamDimensions[subspriteTable->subsprites[i].shape][subspriteTable->subsprites[i].size].width; + s16 right = x; + right += width; + x = right; + x = ~x + 1; + } + + if (vFlip) + { + s8 height = sOamDimensions[subspriteTable->subsprites[i].shape][subspriteTable->subsprites[i].size].height; + s16 bottom = y; + bottom += height; + y = bottom; + y = ~y + 1; + } + + destOam[i] = *oam; + destOam[i].shape = subspriteTable->subsprites[i].shape; + destOam[i].size = subspriteTable->subsprites[i].size; + destOam[i].x = (s16)baseX + (s16)x; + destOam[i].y = baseY + y; + destOam[i].tileNum = tileNum + subspriteTable->subsprites[i].tileOffset; + + if (sprite->subspriteMode != SUBSPRITES_IGNORE_PRIORITY) + destOam[i].priority = subspriteTable->subsprites[i].priority; + } + } + + return 0; +} diff --git a/gflib/sprite.h b/gflib/sprite.h new file mode 100644 index 000000000..9753837fd --- /dev/null +++ b/gflib/sprite.h @@ -0,0 +1,319 @@ +#ifndef GUARD_SPRITE_H +#define GUARD_SPRITE_H + +#define MAX_SPRITES 64 +#define SPRITE_INVALID_TAG 0xFFFF + +struct SpriteSheet +{ + const void *data; // Raw uncompressed pixel data + u16 size; + u16 tag; +}; + +struct CompressedSpriteSheet +{ + const u32 *data; // LZ77 compressed pixel data + u16 size; // Uncompressed size of pixel data + u16 tag; +}; + +struct SpriteFrameImage +{ + const void *data; + u16 size; +}; + +#define obj_frame_tiles(ptr) {.data = (u8 *)ptr, .size = sizeof ptr} + +#define overworld_frame(ptr, width, height, frame) {.data = (u8 *)ptr + (width * height * frame * 64)/2, .size = (width * height * 64)/2} + +struct SpritePalette +{ + const u16 *data; // Raw uncompressed palette data + u16 tag; +}; + +struct CompressedSpritePalette +{ + const u32 *data; // LZ77 compressed palette data + u16 tag; +}; + +struct AnimFrameCmd +{ + // If the sprite has an array of images, this is the array index. + // If the sprite has a sheet, this is the tile offset. + u32 imageValue:16; + + u32 duration:6; + u32 hFlip:1; + u32 vFlip:1; +}; + +struct AnimLoopCmd +{ + u32 type:16; + u32 count:6; +}; + +struct AnimJumpCmd +{ + u32 type:16; + u32 target:6; +}; + +// The first halfword of this union specifies the type of command. +// If it -2, then it is a jump command. If it is -1, then it is the end of the script. +// Otherwise, it is the imageValue for a frame command. +union AnimCmd +{ + s16 type; + struct AnimFrameCmd frame; + struct AnimLoopCmd loop; + struct AnimJumpCmd jump; +}; + +#define ANIMCMD_FRAME(...) \ + {.frame = {__VA_ARGS__}} +#define ANIMCMD_LOOP(_count) \ + {.loop = {.type = -3, .count = _count}} +#define ANIMCMD_JUMP(_target) \ + {.jump = {.type = -2, .target = _target}} +#define ANIMCMD_END \ + {.type = -1} + +struct AffineAnimFrameCmd +{ + s16 xScale; + s16 yScale; + u8 rotation; + u8 duration; +}; + +struct AffineAnimLoopCmd +{ + s16 type; + s16 count; +}; + +struct AffineAnimJumpCmd +{ + s16 type; + u16 target; +}; + +struct AffineAnimEndCmdAlt +{ + s16 type; + u16 val; +}; + +union AffineAnimCmd +{ + s16 type; + struct AffineAnimFrameCmd frame; + struct AffineAnimLoopCmd loop; + struct AffineAnimJumpCmd jump; + struct AffineAnimEndCmdAlt end; // unused in code +}; + +#define AFFINEANIMCMDTYPE_LOOP 0x7FFD +#define AFFINEANIMCMDTYPE_JUMP 0x7FFE +#define AFFINEANIMCMDTYPE_END 0x7FFF + +#define AFFINEANIMCMD_FRAME(_xScale, _yScale, _rotation, _duration) \ + {.frame = {.xScale = _xScale, .yScale = _yScale, .rotation = _rotation, .duration = _duration}} +#define AFFINEANIMCMD_LOOP(_count) \ + {.loop = {.type = AFFINEANIMCMDTYPE_LOOP, .count = _count}} +#define AFFINEANIMCMD_JUMP(_target) \ + {.jump = {.type = AFFINEANIMCMDTYPE_JUMP, .target = _target}} +#define AFFINEANIMCMD_END \ + {.type = AFFINEANIMCMDTYPE_END} +#define AFFINEANIMCMD_END_ALT(_val) \ + {.end = {.type = AFFINEANIMCMDTYPE_END, .val = _val}} + +struct AffineAnimState +{ + u8 animNum; + u8 animCmdIndex; + u8 delayCounter; + u8 loopCounter; + s16 xScale; + s16 yScale; + u16 rotation; +}; + +enum +{ + SUBSPRITES_OFF, + SUBSPRITES_ON, + SUBSPRITES_IGNORE_PRIORITY, // on but priority is ignored +}; + +struct Subsprite +{ + s8 x; // was u16 in R/S + s8 y; // was u16 in R/S + u16 shape:2; + u16 size:2; + u16 tileOffset:10; + u16 priority:2; +}; + +struct SubspriteTable +{ + u8 subspriteCount; + const struct Subsprite *subsprites; +}; + +struct Sprite; + +typedef void (*SpriteCallback)(struct Sprite *); + +struct SpriteTemplate +{ + u16 tileTag; + u16 paletteTag; + const struct OamData *oam; + const union AnimCmd *const *anims; + const struct SpriteFrameImage *images; + const union AffineAnimCmd *const *affineAnims; + SpriteCallback callback; +}; + +struct Sprite +{ + /*0x00*/ struct OamData oam; + /*0x08*/ const union AnimCmd *const *anims; + /*0x0C*/ const struct SpriteFrameImage *images; + /*0x10*/ const union AffineAnimCmd *const *affineAnims; + /*0x14*/ const struct SpriteTemplate *template; + /*0x18*/ const struct SubspriteTable *subspriteTables; + /*0x1C*/ SpriteCallback callback; + + /*0x20*/ struct Coords16 pos1; + /*0x24*/ struct Coords16 pos2; + /*0x28*/ s8 centerToCornerVecX; + /*0x29*/ s8 centerToCornerVecY; + + /*0x2A*/ u8 animNum; + /*0x2B*/ u8 animCmdIndex; + /*0x2C*/ u8 animDelayCounter:6; + bool8 animPaused:1; + bool8 affineAnimPaused:1; + /*0x2D*/ u8 animLoopCounter; + + // general purpose data fields + /*0x2E*/ s16 data[8]; + + /*0x3E*/ bool16 inUse:1; //1 + bool16 coordOffsetEnabled:1; //2 + bool16 invisible:1; //4 + bool16 flags_3:1; //8 + bool16 flags_4:1; //0x10 + bool16 flags_5:1; //0x20 + bool16 flags_6:1; //0x40 + bool16 flags_7:1; //0x80 + /*0x3F*/ bool16 hFlip:1; //1 + bool16 vFlip:1; //2 + bool16 animBeginning:1; //4 + bool16 affineAnimBeginning:1; //8 + bool16 animEnded:1; //0x10 + bool16 affineAnimEnded:1; //0x20 + bool16 usingSheet:1; //0x40 + bool16 flags_f:1; //0x80 + + /*0x40*/ u16 sheetTileStart; + + /*0x42*/ u8 subspriteTableNum:6; + u8 subspriteMode:2; + + /*0x43*/ u8 subpriority; +}; + +struct OamMatrix +{ + s16 a; + s16 b; + s16 c; + s16 d; +}; + +extern const struct OamData gDummyOamData; +extern const union AnimCmd *const gDummySpriteAnimTable[]; +extern const union AffineAnimCmd *const gDummySpriteAffineAnimTable[]; +extern const struct SpriteTemplate gDummySpriteTemplate; + +extern u8 gReservedSpritePaletteCount; +extern struct Sprite gSprites[]; +extern u8 gOamLimit; +extern u16 gReservedSpriteTileCount; +extern s16 gSpriteCoordOffsetX; +extern s16 gSpriteCoordOffsetY; +extern struct OamMatrix gOamMatrices[]; +extern bool8 gAffineAnimsDisabled; + +void ResetSpriteData(void); +void AnimateSprites(void); +void BuildOamBuffer(void); +u8 CreateSprite(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); +u8 CreateSpriteAtEnd(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); +u8 CreateInvisibleSprite(void (*callback)(struct Sprite *)); +u8 CreateSpriteAndAnimate(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); +void DestroySprite(struct Sprite *sprite); +void ResetOamRange(u8 a, u8 b); +void LoadOam(void); +void SetOamMatrix(u8 matrixNum, u16 a, u16 b, u16 c, u16 d); +void CalcCenterToCornerVec(struct Sprite *sprite, u8 shape, u8 size, u8 affineMode); +void SpriteCallbackDummy(struct Sprite *sprite); +void ProcessSpriteCopyRequests(void); +void RequestSpriteCopy(const u8 *src, u8 *dest, u16 size); +void FreeSpriteTiles(struct Sprite *sprite); +void FreeSpritePalette(struct Sprite *sprite); +void FreeSpriteOamMatrix(struct Sprite *sprite); +void DestroySpriteAndFreeResources(struct Sprite *sprite); +void sub_800142C(u32 a1, u32 a2, u16 *a3, u16 a4, u32 a5); +void AnimateSprite(struct Sprite *sprite); +void sub_8007E18(struct Sprite* sprite, s16 a2, s16 a3); +void StartSpriteAnim(struct Sprite *sprite, u8 animNum); +void StartSpriteAnimIfDifferent(struct Sprite *sprite, u8 animNum); +void SeekSpriteAnim(struct Sprite *sprite, u8 animCmdIndex); +void StartSpriteAffineAnim(struct Sprite *sprite, u8 animNum); +void StartSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum); +void ChangeSpriteAffineAnim(struct Sprite *sprite, u8 animNum); +void ChangeSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum); +void SetSpriteSheetFrameTileNum(struct Sprite *sprite); +u8 AllocOamMatrix(void); +void FreeOamMatrix(u8 matrixNum); +void InitSpriteAffineAnim(struct Sprite *sprite); +void SetOamMatrixRotationScaling(u8 matrixNum, s16 xScale, s16 yScale, u16 rotation); +u16 LoadSpriteSheet(const struct SpriteSheet *sheet); +void LoadSpriteSheets(const struct SpriteSheet *sheets); +u16 AllocTilesForSpriteSheet(struct SpriteSheet *sheet); +void AllocTilesForSpriteSheets(struct SpriteSheet *sheets); +void LoadTilesForSpriteSheet(const struct SpriteSheet *sheet); +void LoadTilesForSpriteSheets(struct SpriteSheet *sheets); +void FreeSpriteTilesByTag(u16 tag); +void FreeSpriteTileRanges(void); +u16 GetSpriteTileStartByTag(u16 tag); +u16 GetSpriteTileTagByTileStart(u16 start); +void RequestSpriteSheetCopy(const struct SpriteSheet *sheet); +u16 LoadSpriteSheetDeferred(const struct SpriteSheet *sheet); +void FreeAllSpritePalettes(void); +u8 LoadSpritePalette(const struct SpritePalette *palette); +void LoadSpritePalettes(const struct SpritePalette *palettes); +u8 AllocSpritePalette(u16 tag); +u8 IndexOfSpritePaletteTag(u16 tag); +u16 GetSpritePaletteTagByPaletteNum(u8 paletteNum); +void FreeSpritePaletteByTag(u16 tag); +void SetSubspriteTables(struct Sprite *sprite, const struct SubspriteTable *subspriteTables); +bool8 AddSpriteToOamBuffer(struct Sprite *object, u8 *oamIndex); +bool8 AddSubspritesToOamBuffer(struct Sprite *sprite, struct OamData *destOam, u8 *oamIndex); +void CopyToSprites(u8 *src); +void CopyFromSprites(u8 *dest); +u8 SpriteTileAllocBitmapOp(u16 bit, u8 op); +void ClearSpriteCopyRequests(void); +void ResetAffineAnimData(void); + +#endif //GUARD_SPRITE_H diff --git a/gflib/string_util.c b/gflib/string_util.c new file mode 100644 index 000000000..39d235ab8 --- /dev/null +++ b/gflib/string_util.c @@ -0,0 +1,784 @@ +#include "global.h" +#include "string_util.h" +#include "text.h" + +EWRAM_DATA u8 gStringVar1[0x100] = {0}; +EWRAM_DATA u8 gStringVar2[0x100] = {0}; +EWRAM_DATA u8 gStringVar3[0x100] = {0}; +EWRAM_DATA u8 gStringVar4[0x3E8] = {0}; +EWRAM_DATA static u8 sUnknownStringVar[16] = {0}; + +static const u8 sDigits[] = __("0123456789ABCDEF"); + +static const s32 sPowersOfTen[] = +{ + 1, + 10, + 100, + 1000, + 10000, + 100000, + 1000000, + 10000000, + 100000000, + 1000000000, +}; + +extern const u8 gExpandedPlaceholder_Empty[]; +extern const u8 gExpandedPlaceholder_Kun[]; +extern const u8 gExpandedPlaceholder_Chan[]; +extern const u8 gExpandedPlaceholder_Sapphire[]; +extern const u8 gExpandedPlaceholder_Ruby[]; +extern const u8 gExpandedPlaceholder_Emerald[]; +extern const u8 gExpandedPlaceholder_Aqua[]; +extern const u8 gExpandedPlaceholder_Magma[]; +extern const u8 gExpandedPlaceholder_Archie[]; +extern const u8 gExpandedPlaceholder_Maxie[]; +extern const u8 gExpandedPlaceholder_Kyogre[]; +extern const u8 gExpandedPlaceholder_Groudon[]; +extern const u8 gExpandedPlaceholder_Brendan[]; +extern const u8 gExpandedPlaceholder_May[]; + +u8 *StringCopy10(u8 *dest, const u8 *src) +{ + u8 i; + u32 limit = 10; + + for (i = 0; i < limit; i++) + { + dest[i] = src[i]; + + if (dest[i] == EOS) + return &dest[i]; + } + + dest[i] = EOS; + return &dest[i]; +} + +u8 *StringGetEnd10(u8 *str) +{ + u8 i; + u32 limit = 10; + + for (i = 0; i < limit; i++) + if (str[i] == EOS) + return &str[i]; + + str[i] = EOS; + return &str[i]; +} + +u8 *StringCopy7(u8 *dest, const u8 *src) +{ + s32 i; + s32 limit = 7; + + for (i = 0; i < limit; i++) + { + dest[i] = src[i]; + + if (dest[i] == EOS) + return &dest[i]; + } + + dest[i] = EOS; + return &dest[i]; +} + +u8 *StringCopy(u8 *dest, const u8 *src) +{ + while (*src != EOS) + { + *dest = *src; + dest++; + src++; + } + + *dest = EOS; + return dest; +} + +u8 *StringAppend(u8 *dest, const u8 *src) +{ + while (*dest != EOS) + dest++; + + return StringCopy(dest, src); +} + +u8 *StringCopyN(u8 *dest, const u8 *src, u8 n) +{ + u16 i; + + for (i = 0; i < n; i++) + dest[i] = src[i]; + + return &dest[n]; +} + +u8 *StringAppendN(u8 *dest, const u8 *src, u8 n) +{ + while (*dest != EOS) + dest++; + + return StringCopyN(dest, src, n); +} + +u16 StringLength(const u8 *str) +{ + u16 length = 0; + + while (str[length] != EOS) + length++; + + return length; +} + +s32 StringCompare(const u8 *str1, const u8 *str2) +{ + while (*str1 == *str2) + { + if (*str1 == EOS) + return 0; + str1++; + str2++; + } + + return *str1 - *str2; +} + +s32 StringCompareN(const u8 *str1, const u8 *str2, u32 n) +{ + while (*str1 == *str2) + { + if (*str1 == EOS) + return 0; + str1++; + str2++; + if (--n == 0) + return 0; + } + + return *str1 - *str2; +} + +bool8 IsStringLengthAtLeast(const u8 *str, s32 n) +{ + u8 i; + + for (i = 0; i < n; i++) + if (str[i] && str[i] != EOS) + return TRUE; + + return FALSE; +} + +u8 *ConvertIntToDecimalStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n) +{ + enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state; + s32 powerOfTen; + s32 largestPowerOfTen = sPowersOfTen[n - 1]; + + state = WAITING_FOR_NONZERO_DIGIT; + + if (mode == STR_CONV_MODE_RIGHT_ALIGN) + state = WRITING_SPACES; + + if (mode == STR_CONV_MODE_LEADING_ZEROS) + state = WRITING_DIGITS; + + for (powerOfTen = largestPowerOfTen; powerOfTen > 0; powerOfTen /= 10) + { + u8 c; + u16 digit = value / powerOfTen; + s32 temp = value - (powerOfTen * digit); + + if (state == WRITING_DIGITS) + { + u8 *out = dest++; + + if (digit <= 9) + c = sDigits[digit]; + else + c = CHAR_QUESTION_MARK; + + *out = c; + } + else if (digit != 0 || powerOfTen == 1) + { + u8 *out; + state = WRITING_DIGITS; + out = dest++; + + if (digit <= 9) + c = sDigits[digit]; + else + c = CHAR_QUESTION_MARK; + + *out = c; + } + else if (state == WRITING_SPACES) + { + *dest++ = 0x77; + } + + value = temp; + } + + *dest = EOS; + return dest; +} + +u8 *ConvertUIntToDecimalStringN(u8 *dest, u32 value, enum StringConvertMode mode, u8 n) +{ + enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state; + s32 powerOfTen; + s32 largestPowerOfTen = sPowersOfTen[n - 1]; + + state = WAITING_FOR_NONZERO_DIGIT; + + if (mode == STR_CONV_MODE_RIGHT_ALIGN) + state = WRITING_SPACES; + + if (mode == STR_CONV_MODE_LEADING_ZEROS) + state = WRITING_DIGITS; + + for (powerOfTen = largestPowerOfTen; powerOfTen > 0; powerOfTen /= 10) + { + u8 c; + u16 digit = value / powerOfTen; + u32 temp = value - (powerOfTen * digit); + + if (state == WRITING_DIGITS) + { + u8 *out = dest++; + + if (digit <= 9) + c = sDigits[digit]; + else + c = CHAR_QUESTION_MARK; + + *out = c; + } + else if (digit != 0 || powerOfTen == 1) + { + u8 *out; + state = WRITING_DIGITS; + out = dest++; + + if (digit <= 9) + c = sDigits[digit]; + else + c = CHAR_QUESTION_MARK; + + *out = c; + } + else if (state == WRITING_SPACES) + { + *dest++ = 0x77; + } + + value = temp; + } + + *dest = EOS; + return dest; +} + +u8 *ConvertIntToHexStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n) +{ + enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state; + u8 i; + s32 powerOfSixteen; + s32 largestPowerOfSixteen = 1; + + for (i = 1; i < n; i++) + largestPowerOfSixteen *= 16; + + state = WAITING_FOR_NONZERO_DIGIT; + + if (mode == STR_CONV_MODE_RIGHT_ALIGN) + state = WRITING_SPACES; + + if (mode == STR_CONV_MODE_LEADING_ZEROS) + state = WRITING_DIGITS; + + for (powerOfSixteen = largestPowerOfSixteen; powerOfSixteen > 0; powerOfSixteen /= 16) + { + u8 c; + u32 digit = value / powerOfSixteen; + s32 temp = value % powerOfSixteen; + + if (state == WRITING_DIGITS) + { + char *out = dest++; + + if (digit <= 0xF) + c = sDigits[digit]; + else + c = CHAR_QUESTION_MARK; + + *out = c; + } + else if (digit != 0 || powerOfSixteen == 1) + { + char *out; + state = WRITING_DIGITS; + out = dest++; + + if (digit <= 0xF) + c = sDigits[digit]; + else + c = CHAR_QUESTION_MARK; + + *out = c; + } + else if (state == WRITING_SPACES) + { + *dest++ = 0x77; + } + + value = temp; + } + + *dest = EOS; + return dest; +} + +u8 *StringExpandPlaceholders(u8 *dest, const u8 *src) +{ + for (;;) + { + u8 c = *src++; + u8 placeholderId; + const u8 *expandedString; + + switch (c) + { + case PLACEHOLDER_BEGIN: + placeholderId = *src++; + expandedString = GetExpandedPlaceholder(placeholderId); + dest = StringExpandPlaceholders(dest, expandedString); + break; + case EXT_CTRL_CODE_BEGIN: + *dest++ = c; + c = *src++; + *dest++ = c; + + switch (c) + { + case 0x07: + case 0x09: + case 0x0F: + case 0x15: + case 0x16: + case 0x17: + case 0x18: + break; + case 0x04: + *dest++ = *src++; + case 0x0B: + *dest++ = *src++; + default: + *dest++ = *src++; + } + break; + case EOS: + *dest = EOS; + return dest; + case CHAR_PROMPT_SCROLL: + case CHAR_PROMPT_CLEAR: + case CHAR_NEWLINE: + default: + *dest++ = c; + } + } +} + +u8 *StringBraille(u8 *dest, const u8 *src) +{ + u8 setBrailleFont[] = { EXT_CTRL_CODE_BEGIN, 0x06, 0x06, EOS }; + u8 gotoLine2[] = { CHAR_NEWLINE, EXT_CTRL_CODE_BEGIN, 0x0E, 0x02, EOS }; + + dest = StringCopy(dest, setBrailleFont); + + for (;;) + { + u8 c = *src++; + + switch (c) + { + case EOS: + *dest = c; + return dest; + case CHAR_NEWLINE: + dest = StringCopy(dest, gotoLine2); + break; + default: + *dest++ = c; + *dest++ = c + 0x40; + break; + } + } +} + +static const u8 *ExpandPlaceholder_UnknownStringVar(void) +{ + return sUnknownStringVar; +} + +static const u8 *ExpandPlaceholder_PlayerName(void) +{ + return gSaveBlock2Ptr->playerName; +} + +static const u8 *ExpandPlaceholder_StringVar1(void) +{ + return gStringVar1; +} + +static const u8 *ExpandPlaceholder_StringVar2(void) +{ + return gStringVar2; +} + +static const u8 *ExpandPlaceholder_StringVar3(void) +{ + return gStringVar3; +} + +static const u8 *ExpandPlaceholder_KunChan(void) +{ + if (gSaveBlock2Ptr->playerGender == MALE) + return gExpandedPlaceholder_Kun; + else + return gExpandedPlaceholder_Chan; +} + +static const u8 *ExpandPlaceholder_RivalName(void) +{ + if (gSaveBlock2Ptr->playerGender == MALE) + return gExpandedPlaceholder_May; + else + return gExpandedPlaceholder_Brendan; +} + +static const u8 *ExpandPlaceholder_Version(void) +{ + return gExpandedPlaceholder_Emerald; +} + +static const u8 *ExpandPlaceholder_Aqua(void) +{ + return gExpandedPlaceholder_Aqua; +} + +static const u8 *ExpandPlaceholder_Magma(void) +{ + return gExpandedPlaceholder_Magma; +} + +static const u8 *ExpandPlaceholder_Archie(void) +{ + return gExpandedPlaceholder_Archie; +} + +static const u8 *ExpandPlaceholder_Maxie(void) +{ + return gExpandedPlaceholder_Maxie; +} + +static const u8 *ExpandPlaceholder_Kyogre(void) +{ + return gExpandedPlaceholder_Kyogre; +} + +static const u8 *ExpandPlaceholder_Groudon(void) +{ + return gExpandedPlaceholder_Groudon; +} + +const u8 *GetExpandedPlaceholder(u32 id) +{ + typedef const u8 *(*ExpandPlaceholderFunc)(void); + + static const ExpandPlaceholderFunc funcs[] = + { + ExpandPlaceholder_UnknownStringVar, + ExpandPlaceholder_PlayerName, + ExpandPlaceholder_StringVar1, + ExpandPlaceholder_StringVar2, + ExpandPlaceholder_StringVar3, + ExpandPlaceholder_KunChan, + ExpandPlaceholder_RivalName, + ExpandPlaceholder_Version, + ExpandPlaceholder_Aqua, + ExpandPlaceholder_Magma, + ExpandPlaceholder_Archie, + ExpandPlaceholder_Maxie, + ExpandPlaceholder_Kyogre, + ExpandPlaceholder_Groudon, + }; + + if (id >= ARRAY_COUNT(funcs)) + return gExpandedPlaceholder_Empty; + else + return funcs[id](); +} + +u8 *StringFill(u8 *dest, u8 c, u16 n) +{ + u16 i; + + for (i = 0; i < n; i++) + *dest++ = c; + + *dest = EOS; + return dest; +} + +u8 *StringCopyPadded(u8 *dest, const u8 *src, u8 c, u16 n) +{ + while (*src != EOS) + { + *dest++ = *src++; + + if (n) + n--; + } + + n--; + + while (n != (u16)-1) + { + *dest++ = c; + n--; + } + + *dest = EOS; + return dest; +} + +u8 *StringFillWithTerminator(u8 *dest, u16 n) +{ + return StringFill(dest, EOS, n); +} + +u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n) +{ + u32 i; + + for (i = n - 1; i != (u32)-1; i--) + { + if (*src == EOS) + { + break; + } + else + { + *dest++ = *src++; + if (*(src - 1) == CHAR_SPECIAL_F9) + *dest++ = *src++; + } + } + + *dest = EOS; + return dest; +} + +u32 StringLength_Multibyte(const u8 *str) +{ + u32 length = 0; + + while (*str != EOS) + { + if (*str == CHAR_SPECIAL_F9) + str++; + str++; + length++; + } + + return length; +} + +u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color) +{ + *dest = EXT_CTRL_CODE_BEGIN; + dest++; + + switch (colorType) + { + case 0: + *dest = 1; + dest++; + break; + case 1: + *dest = 3; + dest++; + break; + case 2: + *dest = 2; + dest++; + break; + } + + *dest = color; + dest++; + *dest = EOS; + return dest; +} + +bool32 IsStringJapanese(u8 *str) +{ + while (*str != EOS) + { + if (*str <= 0xA0) + if (*str != CHAR_SPACE) + return TRUE; + str++; + } + + return FALSE; +} + +bool32 sub_800924C(u8 *str, s32 n) +{ + s32 i; + + for (i = 0; *str != EOS && i < n; i++) + { + if (*str <= 0xA0) + if (*str != CHAR_SPACE) + return TRUE; + str++; + } + + return FALSE; +} + +u8 GetExtCtrlCodeLength(u8 code) +{ + static const u8 lengths[] = + { + 1, + 2, + 2, + 2, + 4, + 2, + 2, + 1, + 2, + 1, + 1, + 3, + 2, + 2, + 2, + 1, + 3, + 2, + 2, + 2, + 2, + 1, + 1, + 1, + 1, + }; + + u8 length = 0; + if (code < ARRAY_COUNT(lengths)) + length = lengths[code]; + return length; +} + +static const u8 *SkipExtCtrlCode(const u8 *s) +{ + while (*s == EXT_CTRL_CODE_BEGIN) + { + s++; + s += GetExtCtrlCodeLength(*s); + } + + return s; +} + +s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2) +{ + s32 retVal = 0; + + while (1) + { + str1 = SkipExtCtrlCode(str1); + str2 = SkipExtCtrlCode(str2); + + if (*str1 > *str2) + break; + + if (*str1 < *str2) + { + retVal = -1; + if (*str2 == EOS) + retVal = 1; + } + + if (*str1 == EOS) + return retVal; + + str1++; + str2++; + } + + retVal = 1; + + if (*str1 == EOS) + retVal = -1; + + return retVal; +} + +void ConvertInternationalString(u8 *s, u8 language) +{ + if (language == LANGUAGE_JAPANESE) + { + u8 i; + + StripExtCtrlCodes(s); + i = StringLength(s); + s[i++] = EXT_CTRL_CODE_BEGIN; + s[i++] = 22; + s[i++] = EOS; + + i--; + + while (i != (u8)-1) + { + s[i + 2] = s[i]; + i--; + } + + s[0] = EXT_CTRL_CODE_BEGIN; + s[1] = 21; + } +} + +void StripExtCtrlCodes(u8 *str) +{ + u16 srcIndex = 0; + u16 destIndex = 0; + while (str[srcIndex] != EOS) + { + if (str[srcIndex] == EXT_CTRL_CODE_BEGIN) + { + srcIndex++; + srcIndex += GetExtCtrlCodeLength(str[srcIndex]); + } + else + { + str[destIndex++] = str[srcIndex++]; + } + } + str[destIndex] = EOS; +} diff --git a/gflib/string_util.h b/gflib/string_util.h new file mode 100644 index 000000000..b921d2391 --- /dev/null +++ b/gflib/string_util.h @@ -0,0 +1,46 @@ +#ifndef GUARD_STRING_UTIL_H +#define GUARD_STRING_UTIL_H + +extern u8 gStringVar1[]; +extern u8 gStringVar2[]; +extern u8 gStringVar3[]; +extern u8 gStringVar4[]; + +enum StringConvertMode +{ + STR_CONV_MODE_LEFT_ALIGN, + STR_CONV_MODE_RIGHT_ALIGN, + STR_CONV_MODE_LEADING_ZEROS +}; + +u8 *StringCopy10(u8 *dest, const u8 *src); +u8 *StringGetEnd10(u8 *str); +u8 *StringCopy7(u8 *dest, const u8 *src); +u8 *StringCopy(u8 *dest, const u8 *src); +u8 *StringAppend(u8 *dest, const u8 *src); +u8 *StringCopyN(u8 *dest, const u8 *src, u8 n); +u8 *StringAppendN(u8 *dest, const u8 *src, u8 n); +u16 StringLength(const u8 *str); +s32 StringCompare(const u8 *str1, const u8 *str2); +s32 StringCompareN(const u8 *str1, const u8 *str2, u32 n); +bool8 IsStringLengthAtLeast(const u8 *str, s32 n); +u8 *ConvertIntToDecimalStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); +u8 *ConvertUIntToDecimalStringN(u8 *dest, u32 value, enum StringConvertMode mode, u8 n); +u8 *ConvertIntToHexStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); +u8 *StringExpandPlaceholders(u8 *dest, const u8 *src); +u8 *StringBraille(u8 *dest, const u8 *src); +const u8 *GetExpandedPlaceholder(u32 id); +u8 *StringFill(u8 *dest, u8 c, u16 n); +u8 *StringCopyPadded(u8 *dest, const u8 *src, u8 c, u16 n); +u8 *StringFillWithTerminator(u8 *dest, u16 n); +u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n); +u32 StringLength_Multibyte(const u8 *str); +u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color); +bool32 IsStringJapanese(u8 *str); +bool32 sub_800924C(u8 *str, s32 n); +u8 GetExtCtrlCodeLength(u8 code); +s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2); +void ConvertInternationalString(u8 *s, u8 language); +void StripExtCtrlCodes(u8 *str); + +#endif // GUARD_STRING_UTIL_H diff --git a/gflib/text.c b/gflib/text.c new file mode 100644 index 000000000..7e4fa7104 --- /dev/null +++ b/gflib/text.c @@ -0,0 +1,2540 @@ +#include "global.h" +#include "battle.h" +#include "main.h" +#include "m4a.h" +#include "palette.h" +#include "sound.h" +#include "constants/songs.h" +#include "string_util.h" +#include "window.h" +#include "text.h" +#include "blit.h" +#include "menu.h" +#include "dynamic_placeholder_text_util.h" + +EWRAM_DATA struct TextPrinter gTempTextPrinter = {0}; +EWRAM_DATA struct TextPrinter gTextPrinters[NUM_TEXT_PRINTERS] = {0}; + +static u16 gFontHalfRowLookupTable[0x51]; +static u16 gLastTextBgColor; +static u16 gLastTextFgColor; +static u16 gLastTextShadowColor; + +const struct FontInfo *gFonts; +u8 gUnknown_03002F84; +struct Struct_03002F90 gUnknown_03002F90; +TextFlags gTextFlags; + +const u8 gFontHalfRowOffsets[] = +{ + 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00, + 0x09, 0x0A, 0x0B, 0x09, 0x0C, 0x0D, 0x0E, 0x0C, 0x0F, 0x10, 0x11, 0x0F, 0x09, 0x0A, 0x0B, 0x09, + 0x12, 0x13, 0x14, 0x12, 0x15, 0x16, 0x17, 0x15, 0x18, 0x19, 0x1A, 0x18, 0x12, 0x13, 0x14, 0x12, + 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00, + 0x1B, 0x1C, 0x1D, 0x1B, 0x1E, 0x1F, 0x20, 0x1E, 0x21, 0x22, 0x23, 0x21, 0x1B, 0x1C, 0x1D, 0x1B, + 0x24, 0x25, 0x26, 0x24, 0x27, 0x28, 0x29, 0x27, 0x2A, 0x2B, 0x2C, 0x2A, 0x24, 0x25, 0x26, 0x24, + 0x2D, 0x2E, 0x2F, 0x2D, 0x30, 0x31, 0x32, 0x30, 0x33, 0x34, 0x35, 0x33, 0x2D, 0x2E, 0x2F, 0x2D, + 0x1B, 0x1C, 0x1D, 0x1B, 0x1E, 0x1F, 0x20, 0x1E, 0x21, 0x22, 0x23, 0x21, 0x1B, 0x1C, 0x1D, 0x1B, + 0x36, 0x37, 0x38, 0x36, 0x39, 0x3A, 0x3B, 0x39, 0x3C, 0x3D, 0x3E, 0x3C, 0x36, 0x37, 0x38, 0x36, + 0x3F, 0x40, 0x41, 0x3F, 0x42, 0x43, 0x44, 0x42, 0x45, 0x46, 0x47, 0x45, 0x3F, 0x40, 0x41, 0x3F, + 0x48, 0x49, 0x4A, 0x48, 0x4B, 0x4C, 0x4D, 0x4B, 0x4E, 0x4F, 0x50, 0x4E, 0x48, 0x49, 0x4A, 0x48, + 0x36, 0x37, 0x38, 0x36, 0x39, 0x3A, 0x3B, 0x39, 0x3C, 0x3D, 0x3E, 0x3C, 0x36, 0x37, 0x38, 0x36, + 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00, + 0x09, 0x0A, 0x0B, 0x09, 0x0C, 0x0D, 0x0E, 0x0C, 0x0F, 0x10, 0x11, 0x0F, 0x09, 0x0A, 0x0B, 0x09, + 0x12, 0x13, 0x14, 0x12, 0x15, 0x16, 0x17, 0x15, 0x18, 0x19, 0x1A, 0x18, 0x12, 0x13, 0x14, 0x12, + 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00 +}; + +const u8 gDownArrowTiles[] = INCBIN_U8("data/graphics/fonts/down_arrow.4bpp"); +const u8 gDarkDownArrowTiles[] = INCBIN_U8("data/graphics/fonts/down_arrow_RS.4bpp"); +const u8 gUnusedFRLGBlankedDownArrow[] = INCBIN_U8("data/graphics/fonts/unused_frlg_blanked_down_arrow.4bpp"); +const u8 gUnusedFRLGDownArrow[] = INCBIN_U8("data/graphics/fonts/unused_frlg_down_arrow.4bpp"); +const u8 gDownArrowYCoords[] = { 0x0, 0x1, 0x2, 0x1 }; +const u8 gWindowVerticalScrollSpeeds[] = { 0x1, 0x2, 0x4, 0x0 }; + +const struct GlyphWidthFunc gGlyphWidthFuncs[] = +{ + { 0x0, GetGlyphWidthFont0 }, + { 0x1, GetGlyphWidthFont1 }, + { 0x2, GetGlyphWidthFont2 }, + { 0x3, GetGlyphWidthFont2 }, + { 0x4, GetGlyphWidthFont2 }, + { 0x5, GetGlyphWidthFont2 }, + { 0x6, GetGlyphWidthFont6 }, + { 0x7, GetGlyphWidthFont7 }, + { 0x8, GetGlyphWidthFont8 } +}; + +const struct KeypadIcon gKeypadIcons[] = +{ + { 0x0, 0x8, 0xC }, + { 0x1, 0x8, 0xC }, + { 0x2, 0x10, 0xC }, + { 0x4, 0x10, 0xC }, + { 0x6, 0x18, 0xC }, + { 0x9, 0x18, 0xC }, + { 0xC, 0x8, 0xC }, + { 0xD, 0x8, 0xC }, + { 0xE, 0x8, 0xC }, + { 0xF, 0x8, 0xC }, + { 0x20, 0x8, 0xC }, + { 0x21, 0x8, 0xC }, + { 0x22, 0x8, 0xC } +}; + +const u8 gKeypadIconTiles[] = INCBIN_U8("data/graphics/fonts/keypad_icons.4bpp"); + +const struct FontInfo gFontInfos[] = +{ + { Font0Func, 0x5, 0xC, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font1Func, 0x6, 0x10, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font2Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font3Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font4Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font5Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font6Func, 0x8, 0x10, 0x0, 0x8, 0x0, 0x2, 0x1, 0x3 }, + { Font7Func, 0x5, 0x10, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { Font8Func, 0x5, 0x8, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, + { NULL, 0x8, 0x8, 0x0, 0x0, 0x0, 0x1, 0x2, 0xF } +}; + +const u8 gMenuCursorDimensions[][2] = +{ + { 0x8, 0xC }, + { 0x8, 0xF }, + { 0x8, 0xE }, + { 0x8, 0xE }, + { 0x8, 0xE }, + { 0x8, 0xE }, + { 0x8, 0x10 }, + { 0x8, 0xF }, + { 0x8, 0x8 }, + { 0x0, 0x0 } +}; + +const u16 gFont9JapaneseGlyphs[] = INCBIN_U16("data/graphics/fonts/font9.hwjpnfont"); + +extern const u16 gFont8LatinGlyphs[]; +extern const u8 gFont8LatinGlyphWidths[]; +extern const u16 gFont0LatinGlyphs[]; +extern const u8 gFont0LatinGlyphWidths[]; +extern const u16 gFont7LatinGlyphs[]; +extern const u8 gFont7LatinGlyphWidths[]; +extern const u16 gFont2LatinGlyphs[]; +extern const u8 gFont2LatinGlyphWidths[]; +extern const u16 gFont1LatinGlyphs[]; +extern const u8 gFont1LatinGlyphWidths[]; +extern const u16 gFont0JapaneseGlyphs[]; +extern const u16 gFont1JapaneseGlyphs[]; +extern const u16 gFont2JapaneseGlyphs[]; +extern const u8 gFont2JapaneseGlyphWidths[]; + +void SetFontsPointer(const struct FontInfo *fonts) +{ + gFonts = fonts; +} + +void DeactivateAllTextPrinters(void) +{ + int printer; + for (printer = 0; printer < NUM_TEXT_PRINTERS; ++printer) + gTextPrinters[printer].active = 0; +} + +u16 AddTextPrinterParameterized(u8 windowId, u8 fontId, const u8 *str, u8 x, u8 y, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)) +{ + struct TextPrinterTemplate printerTemplate; + + printerTemplate.currentChar = str; + printerTemplate.windowId = windowId; + printerTemplate.fontId = fontId; + printerTemplate.x = x; + printerTemplate.y = y; + printerTemplate.currentX = x; + printerTemplate.currentY = y; + printerTemplate.letterSpacing = gFonts[fontId].letterSpacing; + printerTemplate.lineSpacing = gFonts[fontId].lineSpacing; + printerTemplate.unk = gFonts[fontId].unk; + printerTemplate.fgColor = gFonts[fontId].fgColor; + printerTemplate.bgColor = gFonts[fontId].bgColor; + printerTemplate.shadowColor = gFonts[fontId].shadowColor; + return AddTextPrinter(&printerTemplate, speed, callback); +} + +bool16 AddTextPrinter(struct TextPrinterTemplate *printerTemplate, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)) +{ + int i; + u16 j; + u8 *ptr; + + if (!gFonts) + return FALSE; + + gTempTextPrinter.active = 1; + gTempTextPrinter.state = 0; + gTempTextPrinter.textSpeed = speed; + gTempTextPrinter.delayCounter = 0; + gTempTextPrinter.scrollDistance = 0; + + for (i = 0; i < 7; i++) + { + gTempTextPrinter.subUnion.fields[i] = 0; + } + + gTempTextPrinter.printerTemplate = *printerTemplate; + gTempTextPrinter.callback = callback; + gTempTextPrinter.minLetterSpacing = 0; + gTempTextPrinter.japanese = 0; + + GenerateFontHalfRowLookupTable(printerTemplate->fgColor, printerTemplate->bgColor, printerTemplate->shadowColor); + if (speed != TEXT_SPEED_FF && speed != 0x0) + { + --gTempTextPrinter.textSpeed; + gTextPrinters[printerTemplate->windowId] = gTempTextPrinter; + } + else + { + gTempTextPrinter.textSpeed = 0; + for (j = 0; j < 0x400; ++j) + { + if (RenderFont(&gTempTextPrinter) == 1) + break; + } + + if (speed != TEXT_SPEED_FF) + CopyWindowToVram(gTempTextPrinter.printerTemplate.windowId, 2); + gTextPrinters[printerTemplate->windowId].active = 0; + } + gUnknown_03002F84 = 0; + return TRUE; +} + +void RunTextPrinters(void) +{ + int i; + + if (gUnknown_03002F84 == 0) + { + for (i = 0; i < 0x20; ++i) + { + if (gTextPrinters[i].active) + { + u16 temp = RenderFont(&gTextPrinters[i]); + switch (temp) + { + case 0: + CopyWindowToVram(gTextPrinters[i].printerTemplate.windowId, 2); + case 3: + if (gTextPrinters[i].callback != 0) + gTextPrinters[i].callback(&gTextPrinters[i].printerTemplate, temp); + break; + case 1: + gTextPrinters[i].active = 0; + break; + } + } + } + } +} + +bool16 IsTextPrinterActive(u8 id) +{ + return gTextPrinters[id].active; +} + +u32 RenderFont(struct TextPrinter *textPrinter) +{ + u32 ret; + while (TRUE) + { + ret = gFonts[textPrinter->printerTemplate.fontId].fontFunction(textPrinter); + if (ret != 2) + return ret; + } +} + +void GenerateFontHalfRowLookupTable(u8 fgColor, u8 bgColor, u8 shadowColor) +{ + u32 fg12, bg12, shadow12; + u32 temp; + + u16 *current = gFontHalfRowLookupTable; + + gLastTextBgColor = bgColor; + gLastTextFgColor = fgColor; + gLastTextShadowColor = shadowColor; + + bg12 = bgColor << 12; + fg12 = fgColor << 12; + shadow12 = shadowColor << 12; + + temp = (bgColor << 8) | (bgColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (bgColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (bgColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (fgColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (fgColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (fgColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (shadowColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (shadowColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (shadowColor << 4) | bgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (bgColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (bgColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (bgColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (fgColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (fgColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (fgColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (shadowColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (shadowColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (shadowColor << 4) | fgColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (bgColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (bgColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (bgColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (fgColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (fgColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (fgColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (bgColor << 8) | (shadowColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (fgColor << 8) | (shadowColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; + + temp = (shadowColor << 8) | (shadowColor << 4) | shadowColor; + *(current++) = (bg12) | temp; + *(current++) = (fg12) | temp; + *(current++) = (shadow12) | temp; +} + +void SaveTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor) +{ + *bgColor = gLastTextBgColor; + *fgColor = gLastTextFgColor; + *shadowColor = gLastTextShadowColor; +} + +void RestoreTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor) +{ + GenerateFontHalfRowLookupTable(*fgColor, *bgColor, *shadowColor); +} + +void DecompressGlyphTile(const void *src_, void *dest_) +{ + u32 temp; + const u16 *src = src_; + u32 *dest = dest_; + + temp = *(src++); + *(dest)++ = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); + + temp = *(src++); + *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); +} + +u8 GetLastTextColor(u8 colorType) +{ + switch (colorType) + { + case 0: + return gLastTextFgColor; + case 2: + return gLastTextBgColor; + case 1: + return gLastTextShadowColor; + default: + return 0; + } +} + +#ifdef NONMATCHING + +#define GLYPH_COPY(fromY_, toY_, fromX_, toX_, unk) \ +{ \ + u32 i, j, *ptr, toY, fromX, toX, r5, toOrr, bits; \ + u8 *dst; \ + \ + j = fromX_; \ + i = fromY_; \ + ptr = unk; \ + toX = toX_; \ + toY = toY_; \ + fromX = fromX_; \ + \ + for (; i < toY; i++) \ + { \ + r5 = *(ptr++); \ + for (j = fromX; j < toX; j++) \ + { \ + toOrr = r5 & 0xF; \ + if (toOrr) \ + { \ + dst = windowTiles + ((j / 8) * 32) + ((j & 7) / 2) + ((i / 8) * widthOffset) + ((i & 7) * 4); \ + bits = ((j & 1) << 2); \ + *dst = ((toOrr << bits) | (*dst & (0xF0 >> bits))); \ + } \ + r5 >>= 4; \ + } \ + } \ +} + +void CopyGlyphToWindow(struct TextPrinter *textPrinter) +{ + struct Window *win; + struct WindowTemplate *winTempl; + struct Struct_03002F90 *unkStruct; + u32 currX, widthOffset, currY; + s32 r4, r0; + u8 *windowTiles; + + win = &gWindows[textPrinter->printerTemplate.windowId]; + winTempl = &win->window; + + r4 = (winTempl->width * 8) - textPrinter->printerTemplate.currentX; + if (r4 > gUnknown_03002F90.unk80) + r4 = gUnknown_03002F90.unk80; + + r0 = (winTempl->height * 8) - textPrinter->printerTemplate.currentY; + if (r0 > gUnknown_03002F90.unk81) + r0 = gUnknown_03002F90.unk81; + + currX = textPrinter->printerTemplate.currentX; + currY = textPrinter->printerTemplate.currentY; + unkStruct = &gUnknown_03002F90; + windowTiles = win->tileData; + widthOffset = winTempl->width * 32; + + if (r4 <= 8) + { + if (r0 <= 8) + { + GLYPH_COPY(currY, currY + r0, currX, currX + r4, unkStruct->unk0); + } + else + { + u32 temp; + GLYPH_COPY(currY, currY + 8, currX, currX + r4, unkStruct->unk0); + + temp = currY + 8; + GLYPH_COPY(temp, (temp - 8) + r0, currX, currX + r4, unkStruct->unk40); + } + } + else + { + if (r0 <= 8) + { + u32 temp; + GLYPH_COPY(currY, currY + r0, currX, currX + 8, unkStruct->unk0); + + temp = currX + 8; + GLYPH_COPY(currY, currY + r0, temp, (temp - 8) + r4, unkStruct->unk20); + } + else + { + u32 temp; + GLYPH_COPY(currY, currY + 8, currX, currX + 8, unkStruct->unk0); + + temp = currX + 8; + GLYPH_COPY(currY, currY + 8, temp, temp - 8 + r4, unkStruct->unk20); + + temp = currY + 8; + GLYPH_COPY(temp, temp - 8 + r0, currX, currX + 8, unkStruct->unk40); + { + u32 tempX, tempY; + tempX = currX + 8; + tempY = currY + 8; + GLYPH_COPY(tempY, tempY - 8 + r0, tempX, tempX - 8 + r4, unkStruct->unk60); + } + } + } +} +#else +NAKED +void CopyGlyphToWindow(struct TextPrinter *x) +{ + asm("push {r4-r7,lr}\n\ + mov r7, r10\n\ + mov r6, r9\n\ + mov r5, r8\n\ + push {r5-r7}\n\ + sub sp, #0x8C\n\ + add r3, r0, #0\n\ + ldrb r1, [r3, #0x4]\n\ + lsl r0, r1, #1\n\ + add r0, r1\n\ + lsl r0, #2\n\ + ldr r1, =gWindows\n\ + add r1, r0, r1\n\ + add r2, r1, #0\n\ + ldrb r7, [r1, #0x3]\n\ + lsl r0, r7, #3\n\ + ldrb r6, [r3, #0x8]\n\ + sub r4, r0, r6\n\ + ldr r5, =gUnknown_03002F90\n\ + add r0, r5, #0\n\ + add r0, #0x80\n\ + ldrb r0, [r0]\n\ + cmp r4, r0\n\ + ble _08004DD2\n\ + add r4, r0, #0\n\ +_08004DD2:\n\ + ldrb r0, [r1, #0x4]\n\ + lsl r0, #3\n\ + ldrb r3, [r3, #0x9]\n\ + sub r0, r3\n\ + add r1, r5, #0\n\ + add r1, #0x81\n\ + ldrb r1, [r1]\n\ + cmp r0, r1\n\ + ble _08004DE6\n\ + add r0, r1, #0\n\ +_08004DE6:\n\ + str r6, [sp]\n\ + mov r8, r3\n\ + add r3, r5, #0\n\ + ldr r2, [r2, #0x8]\n\ + mov r9, r2\n\ + lsl r1, r7, #5\n\ + str r1, [sp, #0x4]\n\ + cmp r4, #0x8\n\ + ble _08004DFA\n\ + b _08004F94\n\ +_08004DFA:\n\ + cmp r0, #0x8\n\ + bgt _08004E84\n\ + mov r1, r8\n\ + str r3, [sp, #0x8]\n\ + add r2, r6, #0\n\ + add r2, r4\n\ + mov r8, r2\n\ + add r0, r1, r0\n\ + str r0, [sp, #0xC]\n\ + str r6, [sp, #0x10]\n\ + cmp r1, r0\n\ + bcc _08004E14\n\ + b _080052AA\n\ +_08004E14:\n\ + ldr r3, [sp, #0x8]\n\ + ldm r3!, {r5}\n\ + str r3, [sp, #0x8]\n\ + ldr r4, [sp, #0x10]\n\ + add r0, r1, #0x1\n\ + mov r10, r0\n\ + cmp r4, r8\n\ + bcs _08004E72\n\ + mov r2, #0x7\n\ + mov r12, r2\n\ + lsr r0, r1, #3\n\ + ldr r2, [sp, #0x4]\n\ + add r3, r0, #0\n\ + mul r3, r2\n\ + add r7, r3, #0\n\ + mov r3, r12\n\ + and r1, r3\n\ + lsl r6, r1, #2\n\ +_08004E38:\n\ + add r3, r5, #0\n\ + mov r0, #0xF\n\ + and r3, r0\n\ + cmp r3, #0\n\ + beq _08004E6A\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r12\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_08004E6A:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r8\n\ + bcc _08004E38\n\ +_08004E72:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0xC]\n\ + cmp r1, r2\n\ + bcc _08004E14\n\ + b _080052AA\n\ + .pool\n\ +_08004E84:\n\ + mov r1, r8\n\ + str r3, [sp, #0x14]\n\ + ldr r3, [sp]\n\ + add r3, r4\n\ + mov r12, r3\n\ + add r2, r1, #0\n\ + add r2, #0x8\n\ + str r2, [sp, #0x18]\n\ + ldr r3, [sp]\n\ + str r3, [sp, #0x1C]\n\ + mov r2, r12\n\ + str r2, [sp, #0x74]\n\ + ldr r3, [sp, #0x18]\n\ + str r3, [sp, #0x88]\n\ + sub r0, #0x8\n\ + str r0, [sp, #0x80]\n\ + cmp r1, r3\n\ + bcs _08004F0E\n\ +_08004EA8:\n\ + ldr r0, [sp, #0x14]\n\ + ldm r0!, {r5}\n\ + str r0, [sp, #0x14]\n\ + ldr r4, [sp, #0x1C]\n\ + add r2, r1, #0x1\n\ + mov r8, r2\n\ + cmp r4, r12\n\ + bcs _08004F06\n\ + mov r3, #0x7\n\ + mov r10, r3\n\ + lsr r0, r1, #3\n\ + ldr r3, [sp, #0x4]\n\ + add r2, r0, #0\n\ + mul r2, r3\n\ + add r7, r2, #0\n\ + mov r0, r10\n\ + and r1, r0\n\ + lsl r6, r1, #2\n\ +_08004ECC:\n\ + add r3, r5, #0\n\ + mov r1, #0xF\n\ + and r3, r1\n\ + cmp r3, #0\n\ + beq _08004EFE\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r10\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_08004EFE:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r12\n\ + bcc _08004ECC\n\ +_08004F06:\n\ + mov r1, r8\n\ + ldr r2, [sp, #0x18]\n\ + cmp r1, r2\n\ + bcc _08004EA8\n\ +_08004F0E:\n\ + ldr r1, [sp, #0x88]\n\ + ldr r3, =gUnknown_03002F90 + 0x40\n\ + str r3, [sp, #0x20]\n\ + ldr r0, [sp, #0x74]\n\ + mov r8, r0\n\ + ldr r2, [sp, #0x80]\n\ + add r2, r1, r2\n\ + str r2, [sp, #0x24]\n\ + ldr r3, [sp]\n\ + str r3, [sp, #0x28]\n\ + cmp r1, r2\n\ + bcc _08004F28\n\ + b _080052AA\n\ +_08004F28:\n\ + ldr r0, [sp, #0x20]\n\ + ldm r0!, {r5}\n\ + str r0, [sp, #0x20]\n\ + ldr r4, [sp, #0x28]\n\ + add r2, r1, #0x1\n\ + mov r10, r2\n\ + cmp r4, r8\n\ + bcs _08004F86\n\ + mov r3, #0x7\n\ + mov r12, r3\n\ + lsr r0, r1, #3\n\ + ldr r3, [sp, #0x4]\n\ + add r2, r0, #0\n\ + mul r2, r3\n\ + add r7, r2, #0\n\ + mov r0, r12\n\ + and r1, r0\n\ + lsl r6, r1, #2\n\ +_08004F4C:\n\ + add r3, r5, #0\n\ + mov r1, #0xF\n\ + and r3, r1\n\ + cmp r3, #0\n\ + beq _08004F7E\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r12\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_08004F7E:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r8\n\ + bcc _08004F4C\n\ +_08004F86:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x24]\n\ + cmp r1, r2\n\ + bcc _08004F28\n\ + b _080052AA\n\ + .pool\n\ +_08004F94:\n\ + cmp r0, #0x8\n\ + ble _08004F9A\n\ + b _080050A4\n\ +_08004F9A:\n\ + mov r1, r8\n\ + str r3, [sp, #0x2C]\n\ + ldr r3, [sp]\n\ + add r3, #0x8\n\ + mov r12, r3\n\ + add r0, r8\n\ + str r0, [sp, #0x30]\n\ + ldr r0, [sp]\n\ + str r0, [sp, #0x34]\n\ + ldr r2, [sp, #0x30]\n\ + str r2, [sp, #0x78]\n\ + str r3, [sp, #0x84]\n\ + sub r4, #0x8\n\ + str r4, [sp, #0x7C]\n\ + cmp r8, r2\n\ + bcs _0800501C\n\ +_08004FBA:\n\ + ldr r0, [sp, #0x2C]\n\ + ldm r0!, {r5}\n\ + str r0, [sp, #0x2C]\n\ + ldr r4, [sp, #0x34]\n\ + add r2, r1, #0x1\n\ + mov r10, r2\n\ + cmp r4, r12\n\ + bcs _08005014\n\ + lsr r0, r1, #3\n\ + ldr r2, [sp, #0x4]\n\ + add r3, r0, #0\n\ + mul r3, r2\n\ + add r7, r3, #0\n\ + mov r3, #0x7\n\ + and r1, r3\n\ + lsl r6, r1, #2\n\ +_08004FDA:\n\ + add r3, r5, #0\n\ + mov r0, #0xF\n\ + and r3, r0\n\ + cmp r3, #0\n\ + beq _0800500C\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, #0x7\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_0800500C:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r12\n\ + bcc _08004FDA\n\ +_08005014:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x30]\n\ + cmp r1, r2\n\ + bcc _08004FBA\n\ +_0800501C:\n\ + mov r1, r8\n\ + ldr r3, =gUnknown_03002F90 + 0x20\n\ + str r3, [sp, #0x38]\n\ + ldr r0, [sp, #0x84]\n\ + ldr r2, [sp, #0x7C]\n\ + add r0, r2\n\ + mov r8, r0\n\ + ldr r3, [sp, #0x78]\n\ + str r3, [sp, #0x3C]\n\ + ldr r0, [sp, #0x84]\n\ + str r0, [sp, #0x40]\n\ + cmp r1, r3\n\ + bcc _08005038\n\ + b _080052AA\n\ +_08005038:\n\ + ldr r2, [sp, #0x38]\n\ + ldm r2!, {r5}\n\ + str r2, [sp, #0x38]\n\ + ldr r4, [sp, #0x40]\n\ + add r3, r1, #0x1\n\ + mov r10, r3\n\ + cmp r4, r8\n\ + bcs _08005096\n\ + mov r0, #0x7\n\ + mov r12, r0\n\ + lsr r0, r1, #3\n\ + ldr r3, [sp, #0x4]\n\ + add r2, r0, #0\n\ + mul r2, r3\n\ + add r7, r2, #0\n\ + mov r0, r12\n\ + and r1, r0\n\ + lsl r6, r1, #2\n\ +_0800505C:\n\ + add r3, r5, #0\n\ + mov r1, #0xF\n\ + and r3, r1\n\ + cmp r3, #0\n\ + beq _0800508E\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r12\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_0800508E:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r8\n\ + bcc _0800505C\n\ +_08005096:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x3C]\n\ + cmp r1, r2\n\ + bcc _08005038\n\ + b _080052AA\n\ + .pool\n\ +_080050A4:\n\ + mov r1, r8\n\ + str r5, [sp, #0x44]\n\ + ldr r3, [sp]\n\ + add r3, #0x8\n\ + mov r12, r3\n\ + mov r2, r8\n\ + add r2, #0x8\n\ + str r2, [sp, #0x48]\n\ + ldr r3, [sp]\n\ + str r3, [sp, #0x4C]\n\ + str r2, [sp, #0x88]\n\ + sub r0, #0x8\n\ + str r0, [sp, #0x80]\n\ + mov r0, r12\n\ + str r0, [sp, #0x84]\n\ + sub r4, #0x8\n\ + str r4, [sp, #0x7C]\n\ + cmp r8, r2\n\ + bcs _0800512C\n\ +_080050CA:\n\ + ldr r2, [sp, #0x44]\n\ + ldm r2!, {r5}\n\ + str r2, [sp, #0x44]\n\ + ldr r4, [sp, #0x4C]\n\ + add r3, r1, #0x1\n\ + mov r10, r3\n\ + cmp r4, r12\n\ + bcs _08005124\n\ + lsr r0, r1, #3\n\ + ldr r3, [sp, #0x4]\n\ + add r2, r0, #0\n\ + mul r2, r3\n\ + add r7, r2, #0\n\ + mov r0, #0x7\n\ + and r1, r0\n\ + lsl r6, r1, #2\n\ +_080050EA:\n\ + add r3, r5, #0\n\ + mov r1, #0xF\n\ + and r3, r1\n\ + cmp r3, #0\n\ + beq _0800511C\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, #0x7\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_0800511C:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r12\n\ + bcc _080050EA\n\ +_08005124:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x48]\n\ + cmp r1, r2\n\ + bcc _080050CA\n\ +_0800512C:\n\ + mov r1, r8\n\ + ldr r3, =gUnknown_03002F90 + 0x20\n\ + str r3, [sp, #0x50]\n\ + ldr r0, [sp, #0x84]\n\ + ldr r2, [sp, #0x7C]\n\ + add r0, r2\n\ + mov r8, r0\n\ + ldr r3, [sp, #0x88]\n\ + str r3, [sp, #0x54]\n\ + ldr r0, [sp, #0x84]\n\ + str r0, [sp, #0x58]\n\ + cmp r1, r3\n\ + bcs _080051AC\n\ +_08005146:\n\ + ldr r2, [sp, #0x50]\n\ + ldm r2!, {r5}\n\ + str r2, [sp, #0x50]\n\ + ldr r4, [sp, #0x58]\n\ + add r3, r1, #0x1\n\ + mov r10, r3\n\ + cmp r4, r8\n\ + bcs _080051A4\n\ + mov r0, #0x7\n\ + mov r12, r0\n\ + lsr r0, r1, #3\n\ + ldr r3, [sp, #0x4]\n\ + add r2, r0, #0\n\ + mul r2, r3\n\ + add r7, r2, #0\n\ + mov r0, r12\n\ + and r1, r0\n\ + lsl r6, r1, #2\n\ +_0800516A:\n\ + add r3, r5, #0\n\ + mov r1, #0xF\n\ + and r3, r1\n\ + cmp r3, #0\n\ + beq _0800519C\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r12\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_0800519C:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r8\n\ + bcc _0800516A\n\ +_080051A4:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x54]\n\ + cmp r1, r2\n\ + bcc _08005146\n\ +_080051AC:\n\ + ldr r1, [sp, #0x88]\n\ + ldr r3, =gUnknown_03002F90 + 0x40\n\ + str r3, [sp, #0x5C]\n\ + ldr r0, [sp, #0x84]\n\ + mov r8, r0\n\ + ldr r2, [sp, #0x80]\n\ + add r2, r1, r2\n\ + str r2, [sp, #0x60]\n\ + ldr r3, [sp]\n\ + str r3, [sp, #0x64]\n\ + cmp r1, r2\n\ + bcs _0800522A\n\ +_080051C4:\n\ + ldr r0, [sp, #0x5C]\n\ + ldm r0!, {r5}\n\ + str r0, [sp, #0x5C]\n\ + ldr r4, [sp, #0x64]\n\ + add r2, r1, #0x1\n\ + mov r10, r2\n\ + cmp r4, r8\n\ + bcs _08005222\n\ + mov r3, #0x7\n\ + mov r12, r3\n\ + lsr r0, r1, #3\n\ + ldr r3, [sp, #0x4]\n\ + add r2, r0, #0\n\ + mul r2, r3\n\ + add r7, r2, #0\n\ + mov r0, r12\n\ + and r1, r0\n\ + lsl r6, r1, #2\n\ +_080051E8:\n\ + add r3, r5, #0\n\ + mov r1, #0xF\n\ + and r3, r1\n\ + cmp r3, #0\n\ + beq _0800521A\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r12\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_0800521A:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r8\n\ + bcc _080051E8\n\ +_08005222:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x60]\n\ + cmp r1, r2\n\ + bcc _080051C4\n\ +_0800522A:\n\ + ldr r4, [sp, #0x84]\n\ + ldr r1, [sp, #0x88]\n\ + ldr r3, =gUnknown_03002F90 + 0x60\n\ + str r3, [sp, #0x68]\n\ + ldr r0, [sp, #0x7C]\n\ + add r0, r4\n\ + mov r8, r0\n\ + ldr r2, [sp, #0x80]\n\ + add r2, r1, r2\n\ + str r2, [sp, #0x6C]\n\ + str r4, [sp, #0x70]\n\ + cmp r1, r2\n\ + bcs _080052AA\n\ +_08005244:\n\ + ldr r3, [sp, #0x68]\n\ + ldm r3!, {r5}\n\ + str r3, [sp, #0x68]\n\ + ldr r4, [sp, #0x70]\n\ + add r0, r1, #0x1\n\ + mov r10, r0\n\ + cmp r4, r8\n\ + bcs _080052A2\n\ + mov r2, #0x7\n\ + mov r12, r2\n\ + lsr r0, r1, #3\n\ + ldr r2, [sp, #0x4]\n\ + add r3, r0, #0\n\ + mul r3, r2\n\ + add r7, r3, #0\n\ + mov r3, r12\n\ + and r1, r3\n\ + lsl r6, r1, #2\n\ +_08005268:\n\ + add r3, r5, #0\n\ + mov r0, #0xF\n\ + and r3, r0\n\ + cmp r3, #0\n\ + beq _0800529A\n\ + lsr r2, r4, #3\n\ + lsl r2, #5\n\ + add r2, r9\n\ + add r0, r4, #0\n\ + mov r1, r12\n\ + and r0, r1\n\ + lsr r0, #1\n\ + add r2, r0\n\ + add r2, r7\n\ + add r2, r6\n\ + mov r1, #0x1\n\ + and r1, r4\n\ + lsl r1, #2\n\ + lsl r3, r1\n\ + mov r0, #0xF0\n\ + asr r0, r1\n\ + ldrb r1, [r2]\n\ + and r0, r1\n\ + orr r3, r0\n\ + strb r3, [r2]\n\ +_0800529A:\n\ + lsr r5, #4\n\ + add r4, #0x1\n\ + cmp r4, r8\n\ + bcc _08005268\n\ +_080052A2:\n\ + mov r1, r10\n\ + ldr r2, [sp, #0x6C]\n\ + cmp r1, r2\n\ + bcc _08005244\n\ +_080052AA:\n\ + add sp, #0x8C\n\ + pop {r3-r5}\n\ + mov r8, r3\n\ + mov r9, r4\n\ + mov r10, r5\n\ + pop {r4-r7}\n\ + pop {r0}\n\ + bx r0\n\ + .pool"); +} +#endif // NONMATCHING + +void ClearTextSpan(struct TextPrinter *textPrinter, u32 width) +{ + struct Window *window; + struct Bitmap pixels_data; + struct Struct_03002F90 *gUnk; + u8* glyphHeight; + + if (gLastTextBgColor != 0) + { + window = &gWindows[textPrinter->printerTemplate.windowId]; + pixels_data.pixels = window->tileData; + pixels_data.width = window->window.width << 3; + pixels_data.height = window->window.height << 3; + + gUnk = &gUnknown_03002F90; + glyphHeight = &gUnk->unk81; + + FillBitmapRect4Bit( + &pixels_data, + textPrinter->printerTemplate.currentX, + textPrinter->printerTemplate.currentY, + width, + *glyphHeight, + gLastTextBgColor); + } +} + +u16 Font0Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 0; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font1Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 1; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font2Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 2; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font3Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 3; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font4Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 4; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font5Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 5; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font7Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 7; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +u16 Font8Func(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->hasGlyphIdBeenSet == FALSE) + { + textPrinter->subUnion.sub.glyphId = 8; + subStruct->hasGlyphIdBeenSet = TRUE; + } + return RenderText(textPrinter); +} + +void TextPrinterInitDownArrowCounters(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (gTextFlags.autoScroll == 1) + subStruct->autoScrollDelay = 0; + else + { + subStruct->downArrowYPosIdx = 0; + subStruct->downArrowDelay = 0; + } +} + +void TextPrinterDrawDownArrow(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + const u8 *arrowTiles; + + if (gTextFlags.autoScroll == 0) + { + if (subStruct->downArrowDelay != 0) + { + subStruct->downArrowDelay = ((*(u32*)&textPrinter->subUnion.sub) << 19 >> 27) - 1; // convoluted way of getting downArrowDelay, necessary to match + } + else + { + FillWindowPixelRect( + textPrinter->printerTemplate.windowId, + textPrinter->printerTemplate.bgColor << 4 | textPrinter->printerTemplate.bgColor, + textPrinter->printerTemplate.currentX, + textPrinter->printerTemplate.currentY, + 0x8, + 0x10); + + switch (gTextFlags.useAlternateDownArrow) + { + case 0: + default: + arrowTiles = gDownArrowTiles; + break; + case 1: + arrowTiles = gDarkDownArrowTiles; + break; + } + + BlitBitmapRectToWindow( + textPrinter->printerTemplate.windowId, + arrowTiles, + 0, + gDownArrowYCoords[*(u32*)subStruct << 17 >> 30], // subStruct->downArrowYPosIdx but again, stupidly retrieved + 0x8, + 0x10, + textPrinter->printerTemplate.currentX, + textPrinter->printerTemplate.currentY, + 0x8, + 0x10); + CopyWindowToVram(textPrinter->printerTemplate.windowId, 0x2); + + subStruct->downArrowDelay = 0x8; + subStruct->downArrowYPosIdx = (*(u32*)subStruct << 17 >> 30) + 1; + } + } +} + +void TextPrinterClearDownArrow(struct TextPrinter *textPrinter) +{ + FillWindowPixelRect( + textPrinter->printerTemplate.windowId, + textPrinter->printerTemplate.bgColor << 4 | textPrinter->printerTemplate.bgColor, + textPrinter->printerTemplate.currentX, + textPrinter->printerTemplate.currentY, + 0x8, + 0x10); + CopyWindowToVram(textPrinter->printerTemplate.windowId, 0x2); +} + +bool8 TextPrinterWaitAutoMode(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + + if (subStruct->autoScrollDelay == 49) + { + return TRUE; + } + else + { + ++subStruct->autoScrollDelay; + return FALSE; + } +} + +bool16 TextPrinterWaitWithDownArrow(struct TextPrinter *textPrinter) +{ + bool8 result = FALSE; + if (gTextFlags.autoScroll != 0) + { + result = TextPrinterWaitAutoMode(textPrinter); + } + else + { + TextPrinterDrawDownArrow(textPrinter); + if (gMain.newKeys & (A_BUTTON | B_BUTTON)) + { + result = TRUE; + PlaySE(SE_SELECT); + } + } + return result; +} + +bool16 TextPrinterWait(struct TextPrinter *textPrinter) +{ + bool16 result = FALSE; + if (gTextFlags.autoScroll != 0) + { + result = TextPrinterWaitAutoMode(textPrinter); + } + else + { + if (gMain.newKeys & (A_BUTTON | B_BUTTON)) + { + result = TRUE; + PlaySE(SE_SELECT); + } + } + return result; +} + +void DrawDownArrow(u8 windowId, u16 x, u16 y, u8 bgColor, bool8 drawArrow, u8 *counter, u8 *yCoordIndex) +{ + const u8 *arrowTiles; + + if (*counter != 0) + { + --*counter; + } + else + { + FillWindowPixelRect(windowId, (bgColor << 4) | bgColor, x, y, 0x8, 0x10); + if (drawArrow == 0) + { + switch (gTextFlags.useAlternateDownArrow) + { + case 0: + default: + arrowTiles = gDownArrowTiles; + break; + case 1: + arrowTiles = gDarkDownArrowTiles; + break; + } + + BlitBitmapRectToWindow( + windowId, + arrowTiles, + 0, + gDownArrowYCoords[*yCoordIndex & 3], + 0x8, + 0x10, + x, + y - 2, + 0x8, + 0x10); + CopyWindowToVram(windowId, 0x2); + *counter = 8; + ++*yCoordIndex; + } + } +} + +u16 RenderText(struct TextPrinter *textPrinter) +{ + struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; + u16 currChar; + s32 width; + s32 widthHelper; + + switch (textPrinter->state) + { + case 0: + if ((gMain.heldKeys & (A_BUTTON | B_BUTTON)) && subStruct->hasPrintBeenSpedUp) + textPrinter->delayCounter = 0; + + if (textPrinter->delayCounter && textPrinter->textSpeed) + { + textPrinter->delayCounter--; + if (gTextFlags.canABSpeedUpPrint && (gMain.newKeys & (A_BUTTON | B_BUTTON))) + { + subStruct->hasPrintBeenSpedUp = TRUE; + textPrinter->delayCounter = 0; + } + return 3; + } + + if (!(gBattleTypeFlags & BATTLE_TYPE_RECORDED) && gTextFlags.autoScroll) + textPrinter->delayCounter = 3; + else + textPrinter->delayCounter = textPrinter->textSpeed; + + currChar = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + + switch (currChar) + { + case CHAR_NEWLINE: + textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; + textPrinter->printerTemplate.currentY += (gFonts[textPrinter->printerTemplate.fontId].maxLetterHeight + textPrinter->printerTemplate.lineSpacing); + return 2; + case PLACEHOLDER_BEGIN: + textPrinter->printerTemplate.currentChar++; + return 2; + case EXT_CTRL_CODE_BEGIN: + currChar = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + switch (currChar) + { + case 1: + textPrinter->printerTemplate.fgColor = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); + return 2; + case 2: + textPrinter->printerTemplate.bgColor = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); + return 2; + case 3: + textPrinter->printerTemplate.shadowColor = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); + return 2; + case 4: + textPrinter->printerTemplate.fgColor = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + textPrinter->printerTemplate.bgColor = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + textPrinter->printerTemplate.shadowColor = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); + return 2; + case 5: + textPrinter->printerTemplate.currentChar++; + return 2; + case 6: + subStruct->glyphId = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + return 2; + case EXT_CTRL_CODE_UNKNOWN_7: + return 2; + case 8: + textPrinter->delayCounter = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + textPrinter->state = 6; + return 2; + case 9: + textPrinter->state = 1; + if (gTextFlags.autoScroll) + subStruct->autoScrollDelay = 0; + return 3; + case 10: + textPrinter->state = 5; + return 3; + case 11: + currChar = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + currChar |= *textPrinter->printerTemplate.currentChar << 8; + textPrinter->printerTemplate.currentChar++; + PlayBGM(currChar); + return 2; + case 12: + currChar = *textPrinter->printerTemplate.currentChar | 0x100; + textPrinter->printerTemplate.currentChar++; + break; + case 16: + currChar = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + currChar |= (*textPrinter->printerTemplate.currentChar << 8); + textPrinter->printerTemplate.currentChar++; + PlaySE(currChar); + return 2; + case 13: + textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x + *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + return 2; + case 14: + textPrinter->printerTemplate.currentY = textPrinter->printerTemplate.y + *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + return 2; + case 15: + FillWindowPixelBuffer(textPrinter->printerTemplate.windowId, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); + textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; + textPrinter->printerTemplate.currentY = textPrinter->printerTemplate.y; + return 2; + case 23: + m4aMPlayStop(&gMPlayInfo_BGM); + return 2; + case 24: + m4aMPlayContinue(&gMPlayInfo_BGM); + return 2; + case EXT_CTRL_CODE_CLEAR: + width = *textPrinter->printerTemplate.currentChar; + textPrinter->printerTemplate.currentChar++; + if (width > 0) + { + ClearTextSpan(textPrinter, width); + textPrinter->printerTemplate.currentX += width; + return 0; + } + return 2; + case 18: + textPrinter->printerTemplate.currentX = *textPrinter->printerTemplate.currentChar + textPrinter->printerTemplate.x; + textPrinter->printerTemplate.currentChar++; + return 2; + case EXT_CTRL_CODE_CLEAR_TO: + { + widthHelper = *textPrinter->printerTemplate.currentChar; + widthHelper += textPrinter->printerTemplate.x; + textPrinter->printerTemplate.currentChar++; + width = widthHelper - textPrinter->printerTemplate.currentX; + if (width > 0) + { + ClearTextSpan(textPrinter, width); + textPrinter->printerTemplate.currentX += width; + return 0; + } + } + return 2; + case EXT_CTRL_CODE_MIN_LETTER_SPACING: + textPrinter->minLetterSpacing = *textPrinter->printerTemplate.currentChar++; + return 2; + case EXT_CTRL_CODE_JPN: + textPrinter->japanese = 1; + return 2; + case EXT_CTRL_CODE_ENG: + textPrinter->japanese = 0; + return 2; + } + break; + case CHAR_PROMPT_CLEAR: + textPrinter->state = 2; + TextPrinterInitDownArrowCounters(textPrinter); + return 3; + case CHAR_PROMPT_SCROLL: + textPrinter->state = 3; + TextPrinterInitDownArrowCounters(textPrinter); + return 3; + case CHAR_SPECIAL_F9: + currChar = *textPrinter->printerTemplate.currentChar | 0x100; + textPrinter->printerTemplate.currentChar++; + break; + case CHAR_SPECIAL_F8: + currChar = *textPrinter->printerTemplate.currentChar++; + gUnknown_03002F90.unk80 = DrawKeypadIcon(textPrinter->printerTemplate.windowId, currChar, textPrinter->printerTemplate.currentX, textPrinter->printerTemplate.currentY); + textPrinter->printerTemplate.currentX += gUnknown_03002F90.unk80 + textPrinter->printerTemplate.letterSpacing; + return 0; + case EOS: + return 1; + } + + switch (subStruct->glyphId) + { + case 0: + DecompressGlyphFont0(currChar, textPrinter->japanese); + break; + case 1: + DecompressGlyphFont1(currChar, textPrinter->japanese); + break; + case 2: + case 3: + case 4: + case 5: + DecompressGlyphFont2(currChar, textPrinter->japanese); + break; + case 7: + DecompressGlyphFont7(currChar, textPrinter->japanese); + break; + case 8: + DecompressGlyphFont8(currChar, textPrinter->japanese); + break; + case 6: + break; + } + + CopyGlyphToWindow(textPrinter); + + if (textPrinter->minLetterSpacing) + { + textPrinter->printerTemplate.currentX += gUnknown_03002F90.unk80; + width = textPrinter->minLetterSpacing - gUnknown_03002F90.unk80; + if (width > 0) + { + ClearTextSpan(textPrinter, width); + textPrinter->printerTemplate.currentX += width; + } + } + else + { + if (textPrinter->japanese) + textPrinter->printerTemplate.currentX += (gUnknown_03002F90.unk80 + textPrinter->printerTemplate.letterSpacing); + else + textPrinter->printerTemplate.currentX += gUnknown_03002F90.unk80; + } + return 0; + case 1: + if (TextPrinterWait(textPrinter)) + textPrinter->state = 0; + return 3; + case 2: + if (TextPrinterWaitWithDownArrow(textPrinter)) + { + FillWindowPixelBuffer(textPrinter->printerTemplate.windowId, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); + textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; + textPrinter->printerTemplate.currentY = textPrinter->printerTemplate.y; + textPrinter->state = 0; + } + return 3; + case 3: + if (TextPrinterWaitWithDownArrow(textPrinter)) + { + TextPrinterClearDownArrow(textPrinter); + textPrinter->scrollDistance = gFonts[textPrinter->printerTemplate.fontId].maxLetterHeight + textPrinter->printerTemplate.lineSpacing; + textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; + textPrinter->state = 4; + } + return 3; + case 4: + if (textPrinter->scrollDistance) + { + int scrollSpeed = GetPlayerTextSpeed(); + int speed = gWindowVerticalScrollSpeeds[scrollSpeed]; + if (textPrinter->scrollDistance < speed) + { + ScrollWindow(textPrinter->printerTemplate.windowId, 0, textPrinter->scrollDistance, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); + textPrinter->scrollDistance = 0; + } + else + { + ScrollWindow(textPrinter->printerTemplate.windowId, 0, speed, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); + textPrinter->scrollDistance -= speed; + } + CopyWindowToVram(textPrinter->printerTemplate.windowId, 2); + } + else + { + textPrinter->state = 0; + } + return 3; + case 5: + if (!IsSEPlaying()) + textPrinter->state = 0; + return 3; + case 6: + if (textPrinter->delayCounter != 0) + textPrinter->delayCounter--; + else + textPrinter->state = 0; + return 3; + } + + return 1; +} + +u32 GetStringWidthFixedWidthFont(const u8 *str, u8 fontId, u8 letterSpacing) +{ + int i; + u8 width; + int temp; + int temp2; + u8 line; + int strPos; + u8 lineWidths[8]; + const u8 *strLocal; + + for (i = 0; i < 8; i++) + { + lineWidths[i] = 0; + } + + width = 0; + line = 0; + strLocal = str; + strPos = 0; + + do + { + temp = strLocal[strPos++]; + switch (temp) + { + case CHAR_NEWLINE: + case EOS: + lineWidths[line] = width; + width = 0; + line++; + break; + case EXT_CTRL_CODE_BEGIN: + temp2 = strLocal[strPos++]; + switch (temp2) + { + case 0x4: + ++strPos; + case 0xB: + case 0x10: + ++strPos; + case 0x1: + case 0x2: + case 0x3: + case 0x5: + case 0x6: + case 0x8: + case 0xC: + case 0xD: + case 0xE: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + ++strPos; + break; + case EXT_CTRL_CODE_UNKNOWN_7: + case 0x9: + case 0xA: + case 0xF: + case EXT_CTRL_CODE_JPN: + case EXT_CTRL_CODE_ENG: + default: + break; + } + break; + case CHAR_SPECIAL_F7: + case PLACEHOLDER_BEGIN: + ++strPos; + break; + case CHAR_PROMPT_SCROLL: + case CHAR_PROMPT_CLEAR: + break; + case CHAR_SPECIAL_F8: + case CHAR_SPECIAL_F9: + ++strPos; + default: + ++width; + break; + } + } while (temp != EOS); + + for (width = 0, strPos = 0; strPos < 8; ++strPos) + { + if (width < lineWidths[strPos]) + width = lineWidths[strPos]; + } + + return (u8)(GetFontAttribute(fontId, FONTATTR_MAX_LETTER_WIDTH) + letterSpacing) * width; +} + +u32 (*GetFontWidthFunc(u8 glyphId))(u16, bool32) +{ + u32 i; + + for (i = 0; i < 9; ++i) + { + if (glyphId == gGlyphWidthFuncs[i].fontId) + return gGlyphWidthFuncs[i].func; + } + + return NULL; +} + +s32 GetStringWidth(u8 fontId, const u8 *str, s16 letterSpacing) +{ + bool8 isJapanese; + int minGlyphWidth; + u32 (*func)(u16 glyphId, bool32 isJapanese); + s32 result; + int localLetterSpacing; + u32 lineWidth; + const u8 *bufferPointer; + int glyphWidth; + s32 width; + + isJapanese = 0; + minGlyphWidth = 0; + + func = GetFontWidthFunc(fontId); + if (func == NULL) + return 0; + + if (letterSpacing == -1) + localLetterSpacing = GetFontAttribute(fontId, FONTATTR_LETTER_SPACING); + else + localLetterSpacing = letterSpacing; + + width = 0; + lineWidth = 0; + bufferPointer = 0; + + while (*str != EOS) + { + switch (*str) + { + case CHAR_NEWLINE: + if (lineWidth > width) + width = lineWidth; + lineWidth = 0; + break; + case PLACEHOLDER_BEGIN: + switch (*++str) + { + case 0x2: + bufferPointer = gStringVar1; + break; + case 0x3: + bufferPointer = gStringVar2; + break; + case 0x4: + bufferPointer = gStringVar3; + break; + default: + return 0; + } + case CHAR_SPECIAL_F7: + if (bufferPointer == NULL) + bufferPointer = DynamicPlaceholderTextUtil_GetPlaceholderPtr(*++str); + while (*bufferPointer != EOS) + { + glyphWidth = func(*bufferPointer++, isJapanese); + if (minGlyphWidth > 0) + { + if (glyphWidth < minGlyphWidth) + glyphWidth = minGlyphWidth; + lineWidth += glyphWidth; + } + else + { + lineWidth += glyphWidth; + if (isJapanese && str[1] != EOS) + lineWidth += localLetterSpacing; + } + } + bufferPointer = 0; + break; + case EXT_CTRL_CODE_BEGIN: + switch (*++str) + { + case 0x4: + ++str; + case 0xB: + case 0x10: + ++str; + case 0x1: + case 0x2: + case 0x3: + case 0x5: + case 0x8: + case 0xC: + case 0xD: + case 0xE: + ++str; + break; + case 0x6: + func = GetFontWidthFunc(*++str); + if (func == NULL) + return 0; + if (letterSpacing == -1) + localLetterSpacing = GetFontAttribute(*str, FONTATTR_LETTER_SPACING); + break; + case 0x11: + glyphWidth = *++str; + lineWidth += glyphWidth; + break; + case 0x12: + lineWidth = *++str; + break; + case 0x13: + if (*++str > lineWidth) + lineWidth = *str; + break; + case 0x14: + minGlyphWidth = *++str; + break; + case EXT_CTRL_CODE_JPN: + isJapanese = 1; + break; + case EXT_CTRL_CODE_ENG: + isJapanese = 0; + break; + case EXT_CTRL_CODE_UNKNOWN_7: + case 0x9: + case 0xA: + case 0xF: + default: + break; + } + break; + case CHAR_SPECIAL_F8: + case CHAR_SPECIAL_F9: + if (*str == CHAR_SPECIAL_F9) + glyphWidth = func(*++str | 0x100, isJapanese); + else + glyphWidth = GetKeypadIconWidth(*++str); + + if (minGlyphWidth > 0) + { + if (glyphWidth < minGlyphWidth) + glyphWidth = minGlyphWidth; + lineWidth += glyphWidth; + } + else + { + lineWidth += glyphWidth; + if (isJapanese && str[1] != EOS) + lineWidth += localLetterSpacing; + } + break; + case CHAR_PROMPT_SCROLL: + case CHAR_PROMPT_CLEAR: + break; + default: + glyphWidth = func(*str, isJapanese); + if (minGlyphWidth > 0) + { + if (glyphWidth < minGlyphWidth) + glyphWidth = minGlyphWidth; + lineWidth += glyphWidth; + } + else + { + lineWidth += glyphWidth; + if (isJapanese && str[1] != EOS) + lineWidth += localLetterSpacing; + } + break; + } + ++str; + } + + if (lineWidth > width) + return lineWidth; + else + return width; +} + +u8 RenderTextFont9(u8 *pixels, u8 fontId, u8 *str) +{ + u8 shadowColor; + u8 *strLocal; + int strPos; + int temp; + int temp2; + u8 colorBackup[3]; + u8 fgColor; + u8 bgColor; + + SaveTextColors(&colorBackup[0], &colorBackup[1], &colorBackup[2]); + + fgColor = 1; + bgColor = 0; + shadowColor = 3; + + GenerateFontHalfRowLookupTable(1, 0, 3); + strLocal = str; + strPos = 0; + + do + { + temp = strLocal[strPos++]; + switch (temp) + { + case EXT_CTRL_CODE_BEGIN: + temp2 = strLocal[strPos++]; + switch (temp2) + { + case 0x4: + fgColor = strLocal[strPos++]; + bgColor = strLocal[strPos++]; + shadowColor = strLocal[strPos++]; + GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); + continue; + case 0x1: + fgColor = strLocal[strPos++]; + GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); + continue; + case 0x2: + bgColor = strLocal[strPos++]; + GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); + continue; + case 0x3: + shadowColor = strLocal[strPos++]; + GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); + continue; + case 0x6: + fontId = strLocal[strPos++]; + break; + case 0xB: + case 0x10: + ++strPos; + case 0x5: + case 0x8: + case 0xC: + case 0xD: + case 0xE: + case 0x11: + case 0x12: + case 0x13: + case 0x14: + ++strPos; + break; + case EXT_CTRL_CODE_UNKNOWN_7: + case 0x9: + case 0xA: + case 0xF: + case EXT_CTRL_CODE_JPN: + case EXT_CTRL_CODE_ENG: + default: + continue; + } + break; + case CHAR_SPECIAL_F7: + case CHAR_SPECIAL_F8: + case CHAR_SPECIAL_F9: + case PLACEHOLDER_BEGIN: + ++strPos; + break; + case CHAR_PROMPT_SCROLL: + case CHAR_PROMPT_CLEAR: + case CHAR_NEWLINE: + case EOS: + break; + default: + switch (fontId) + { + case 9: + DecompressGlyphFont9(temp); + break; + case 1: + default: + DecompressGlyphFont1(temp, 1); + break; + } + CpuCopy32(gUnknown_03002F90.unk0, pixels, 0x20); + CpuCopy32(gUnknown_03002F90.unk40, pixels + 0x20, 0x20); + pixels += 0x40; + break; + } + } + while (temp != EOS); + + RestoreTextColors(&colorBackup[0], &colorBackup[1], &colorBackup[2]); + return 1; +} + +u8 DrawKeypadIcon(u8 windowId, u8 keypadIconId, u16 x, u16 y) +{ + BlitBitmapRectToWindow( + windowId, + gKeypadIconTiles + (gKeypadIcons[keypadIconId].tileOffset * 0x20), + 0, + 0, + 0x80, + 0x80, + x, + y, + gKeypadIcons[keypadIconId].width, + gKeypadIcons[keypadIconId].height); + return gKeypadIcons[keypadIconId].width; +} + +u8 GetKeypadIconTileOffset(u8 keypadIconId) +{ + return gKeypadIcons[keypadIconId].tileOffset; +} + +u8 GetKeypadIconWidth(u8 keypadIconId) +{ + return gKeypadIcons[keypadIconId].width; +} + +u8 GetKeypadIconHeight(u8 keypadIconId) +{ + return gKeypadIcons[keypadIconId].height; +} + +void SetDefaultFontsPointer(void) +{ + SetFontsPointer(&gFontInfos[0]); +} + +u8 GetFontAttribute(u8 fontId, u8 attributeId) +{ + int result = 0; + switch (attributeId) + { + case FONTATTR_MAX_LETTER_WIDTH: + result = gFontInfos[fontId].maxLetterWidth; + break; + case FONTATTR_MAX_LETTER_HEIGHT: + result = gFontInfos[fontId].maxLetterHeight; + break; + case FONTATTR_LETTER_SPACING: + result = gFontInfos[fontId].letterSpacing; + break; + case FONTATTR_LINE_SPACING: + result = gFontInfos[fontId].lineSpacing; + break; + case FONTATTR_UNKNOWN: + result = gFontInfos[fontId].unk; + break; + case FONTATTR_COLOR_FOREGROUND: + result = gFontInfos[fontId].fgColor; + break; + case FONTATTR_COLOR_BACKGROUND: + result = gFontInfos[fontId].bgColor; + break; + case FONTATTR_COLOR_SHADOW: + result = gFontInfos[fontId].shadowColor; + break; + } + return result; +} + +u8 GetMenuCursorDimensionByFont(u8 fontId, u8 whichDimension) +{ + return gMenuCursorDimensions[fontId][whichDimension]; +} + +void DecompressGlyphFont0(u16 glyphId, bool32 isJapanese) +{ + const u16* glyphs; + + if (isJapanese == 1) + { + glyphs = gFont0JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & 0xF)); + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 + gUnknown_03002F90.unk80 = 8; // gGlyphWidth + gUnknown_03002F90.unk81 = 12; // gGlyphHeight + } + else + { + glyphs = gFont0LatinGlyphs + (0x20 * glyphId); + gUnknown_03002F90.unk80 = gFont0LatinGlyphWidths[glyphId]; + + if (gUnknown_03002F90.unk80 <= 8) + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + } + else + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); + } + + gUnknown_03002F90.unk81 = 13; + } +} + +u32 GetGlyphWidthFont0(u16 glyphId, bool32 isJapanese) +{ + if (isJapanese == TRUE) + return 8; + else + return gFont0LatinGlyphWidths[glyphId]; +} + +void DecompressGlyphFont7(u16 glyphId, bool32 isJapanese) +{ + const u16* glyphs; + + if (isJapanese == TRUE) + { + int eff; + glyphs = gFont1JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & (eff = 0xF))); // shh, no questions, only matching now + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 + gUnknown_03002F90.unk80 = 8; // gGlyphWidth + gUnknown_03002F90.unk81 = 15; // gGlyphHeight + } + else + { + glyphs = gFont7LatinGlyphs + (0x20 * glyphId); + gUnknown_03002F90.unk80 = gFont7LatinGlyphWidths[glyphId]; + + if (gUnknown_03002F90.unk80 <= 8) + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + } + else + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); + } + + gUnknown_03002F90.unk81 = 15; + } +} + +u32 GetGlyphWidthFont7(u16 glyphId, bool32 isJapanese) +{ + if (isJapanese == TRUE) + return 8; + else + return gFont7LatinGlyphWidths[glyphId]; +} + +void DecompressGlyphFont8(u16 glyphId, bool32 isJapanese) +{ + const u16* glyphs; + + if (isJapanese == TRUE) + { + glyphs = gFont0JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & 0xF)); + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 + gUnknown_03002F90.unk80 = 8; // gGlyphWidth + gUnknown_03002F90.unk81 = 12; // gGlyphHeight + } + else + { + glyphs = gFont8LatinGlyphs + (0x20 * glyphId); + gUnknown_03002F90.unk80 = gFont8LatinGlyphWidths[glyphId]; + + if (gUnknown_03002F90.unk80 <= 8) + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + } + else + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); + } + + gUnknown_03002F90.unk81 = 12; + } +} + +u32 GetGlyphWidthFont8(u16 glyphId, bool32 isJapanese) +{ + if (isJapanese == TRUE) + return 8; + else + return gFont8LatinGlyphWidths[glyphId]; +} + +void DecompressGlyphFont2(u16 glyphId, bool32 isJapanese) +{ + const u16* glyphs; + + if (isJapanese == TRUE) + { + glyphs = gFont2JapaneseGlyphs + (0x100 * (glyphId >> 0x3)) + (0x10 * (glyphId & 0x7)); + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); // gUnknown_03002F90 + 0x40 + DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x20 + DecompressGlyphTile(glyphs + 0x88, gUnknown_03002F90.unk60); // gUnknown_03002F90 + 0x60 + gUnknown_03002F90.unk80 = gFont2JapaneseGlyphWidths[glyphId]; // gGlyphWidth + gUnknown_03002F90.unk81 = 14; // gGlyphHeight + } + else + { + glyphs = gFont2LatinGlyphs + (0x20 * glyphId); + gUnknown_03002F90.unk80 = gFont2LatinGlyphWidths[glyphId]; + + if (gUnknown_03002F90.unk80 <= 8) + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + } + else + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); + } + + gUnknown_03002F90.unk81 = 14; + } +} + +u32 GetGlyphWidthFont2(u16 glyphId, bool32 isJapanese) +{ + if (isJapanese == TRUE) + return gFont2JapaneseGlyphWidths[glyphId]; + else + return gFont2LatinGlyphWidths[glyphId]; +} + +void DecompressGlyphFont1(u16 glyphId, bool32 isJapanese) +{ + const u16* glyphs; + + if (isJapanese == TRUE) + { + int eff; + glyphs = gFont1JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & (eff = 0xF))); // shh, no questions, only matching now + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 + gUnknown_03002F90.unk80 = 8; // gGlyphWidth + gUnknown_03002F90.unk81 = 15; // gGlyphHeight + } + else + { + glyphs = gFont1LatinGlyphs + (0x20 * glyphId); + gUnknown_03002F90.unk80 = gFont1LatinGlyphWidths[glyphId]; + + if (gUnknown_03002F90.unk80 <= 8) + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + } + else + { + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); + DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); + DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); + } + + gUnknown_03002F90.unk81 = 15; + } +} + +u32 GetGlyphWidthFont1(u16 glyphId, bool32 isJapanese) +{ + if (isJapanese == TRUE) + return 8; + else + return gFont1LatinGlyphWidths[glyphId]; +} + +void DecompressGlyphFont9(u16 glyphId) +{ + const u16* glyphs; + + glyphs = gFont9JapaneseGlyphs + (0x100 * (glyphId >> 4)) + (0x8 * (glyphId & 0xF)); + DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); + DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); + gUnknown_03002F90.unk80 = 8; + gUnknown_03002F90.unk81 = 12; +} diff --git a/gflib/text.h b/gflib/text.h new file mode 100644 index 000000000..d3ff663bb --- /dev/null +++ b/gflib/text.h @@ -0,0 +1,297 @@ +#ifndef GUARD_TEXT_H +#define GUARD_TEXT_H + +#define CHAR_SPACE 0x00 +#define CHAR_PLUS 0x2E +#define CHAR_0 0xA1 +#define CHAR_1 0xA2 +#define CHAR_2 0xA3 +#define CHAR_3 0xA4 +#define CHAR_4 0xA5 +#define CHAR_5 0xA6 +#define CHAR_6 0xA7 +#define CHAR_7 0xA8 +#define CHAR_8 0xA9 +#define CHAR_9 0xAA +#define CHAR_EXCL_MARK 0xAB +#define CHAR_QUESTION_MARK 0xAC +#define CHAR_PERIOD 0xAD +#define CHAR_HYPHEN 0xAE +#define CHAR_ELLIPSIS 0xB0 +#define CHAR_DBL_QUOT_LEFT 0xB1 +#define CHAR_DBL_QUOT_RIGHT 0xB2 +#define CHAR_SGL_QUOT_LEFT 0xB3 +#define CHAR_SGL_QUOT_RIGHT 0xB4 +#define CHAR_MALE 0xB5 +#define CHAR_FEMALE 0xB6 +#define CHAR_CURRENCY 0xB7 +#define CHAR_COMMA 0xB8 +#define CHAR_MULT_SIGN 0xB9 +#define CHAR_SLASH 0xBA +#define CHAR_A 0xBB +#define CHAR_B 0xBC +#define CHAR_C 0xBD +#define CHAR_D 0xBE +#define CHAR_E 0xBF +#define CHAR_F 0xC0 +#define CHAR_G 0xC1 +#define CHAR_H 0xC2 +#define CHAR_I 0xC3 +#define CHAR_J 0xC4 +#define CHAR_K 0xC5 +#define CHAR_L 0xC6 +#define CHAR_M 0xC7 +#define CHAR_N 0xC8 +#define CHAR_O 0xC9 +#define CHAR_P 0xCA +#define CHAR_Q 0xCB +#define CHAR_R 0xCC +#define CHAR_S 0xCD +#define CHAR_T 0xCE +#define CHAR_U 0xCF +#define CHAR_V 0xD0 +#define CHAR_W 0xD1 +#define CHAR_X 0xD2 +#define CHAR_Y 0xD3 +#define CHAR_Z 0xD4 +#define CHAR_a 0xD5 +#define CHAR_b 0xD6 +#define CHAR_c 0xD7 +#define CHAR_d 0xD8 +#define CHAR_e 0xD9 +#define CHAR_f 0xDA +#define CHAR_g 0xDB +#define CHAR_h 0xDC +#define CHAR_i 0xDD +#define CHAR_j 0xDE +#define CHAR_k 0xDF +#define CHAR_l 0xE0 +#define CHAR_m 0xE1 +#define CHAR_n 0xE2 +#define CHAR_o 0xE3 +#define CHAR_p 0xE4 +#define CHAR_q 0xE5 +#define CHAR_r 0xE6 +#define CHAR_s 0xE7 +#define CHAR_t 0xE8 +#define CHAR_u 0xE9 +#define CHAR_v 0xEA +#define CHAR_w 0xEB +#define CHAR_x 0xEC +#define CHAR_y 0xED +#define CHAR_z 0xEE +#define CHAR_SPECIAL_F7 0xF7 +#define CHAR_SPECIAL_F8 0xF8 +#define CHAR_SPECIAL_F9 0xF9 +#define CHAR_COLON 0xF0 +#define CHAR_PROMPT_SCROLL 0xFA // waits for button press and scrolls dialog +#define CHAR_PROMPT_CLEAR 0xFB // waits for button press and clears dialog +#define EXT_CTRL_CODE_BEGIN 0xFC // extended control code +#define PLACEHOLDER_BEGIN 0xFD // string placeholder +#define CHAR_NEWLINE 0xFE +#define EOS 0xFF // end of string + +#define EXT_CTRL_CODE_COLOR 0x1 +#define EXT_CTRL_CODE_HIGHLIGHT 0x2 +#define EXT_CTRL_CODE_SHADOW 0x3 +// +#define EXT_CTRL_CODE_UNKNOWN_7 0x7 +// +#define EXT_CTRL_CODE_CLEAR 0x11 +// +#define EXT_CTRL_CODE_CLEAR_TO 0x13 +#define EXT_CTRL_CODE_MIN_LETTER_SPACING 0x14 +#define EXT_CTRL_CODE_JPN 0x15 +#define EXT_CTRL_CODE_ENG 0x16 + +#define TEXT_COLOR_TRANSPARENT 0x0 +#define TEXT_COLOR_WHITE 0x1 +#define TEXT_COLOR_DARK_GREY 0x2 +// 0x3 +#define TEXT_COLOR_RED 0x4 +// 0x5 +#define TEXT_COLOR_GREEN 0x6 +// 0x7 +#define TEXT_COLOR_BLUE 0x8 + +// battle placeholders are located in battle_message.h + +#define NUM_TEXT_PRINTERS 32 + +#define TEXT_SPEED_FF 0xFF + +enum +{ + FONTATTR_MAX_LETTER_WIDTH, + FONTATTR_MAX_LETTER_HEIGHT, + FONTATTR_LETTER_SPACING, + FONTATTR_LINE_SPACING, + FONTATTR_UNKNOWN, // dunno what this is yet + FONTATTR_COLOR_FOREGROUND, + FONTATTR_COLOR_BACKGROUND, + FONTATTR_COLOR_SHADOW +}; + +struct TextPrinterSubStruct +{ + u8 glyphId:4; // 0x14 + bool8 hasPrintBeenSpedUp:1; + u8 unk:3; + u8 downArrowDelay:5; + u8 downArrowYPosIdx:2; + bool8 hasGlyphIdBeenSet:1; + u8 autoScrollDelay; +}; + +struct TextPrinterTemplate +{ + const u8* currentChar; + u8 windowId; + u8 fontId; + u8 x; + u8 y; + u8 currentX; // 0x8 + u8 currentY; + u8 letterSpacing; + u8 lineSpacing; + u8 unk:4; // 0xC + u8 fgColor:4; + u8 bgColor:4; + u8 shadowColor:4; +}; + +struct TextPrinter +{ + struct TextPrinterTemplate printerTemplate; + + void (*callback)(struct TextPrinterTemplate *, u16); // 0x10 + + union +#if !MODERN + __attribute__((packed)) +#endif + { + struct TextPrinterSubStruct sub; + u8 fields[7]; + } subUnion; + + u8 active; + u8 state; // 0x1C + u8 textSpeed; + u8 delayCounter; + u8 scrollDistance; + u8 minLetterSpacing; // 0x20 + u8 japanese; +}; + +struct FontInfo +{ + u16 (*fontFunction)(struct TextPrinter *x); + u8 maxLetterWidth; + u8 maxLetterHeight; + u8 letterSpacing; + u8 lineSpacing; + u8 unk:4; + u8 fgColor:4; + u8 bgColor:4; + u8 shadowColor:4; +}; + +extern const struct FontInfo *gFonts; + +struct GlyphWidthFunc +{ + u32 fontId; + u32 (*func)(u16 glyphId, bool32 isJapanese); +}; + +struct KeypadIcon +{ + u16 tileOffset; + u8 width; + u8 height; +}; + +typedef struct { + bool8 canABSpeedUpPrint:1; + bool8 useAlternateDownArrow:1; + bool8 autoScroll:1; + bool8 forceMidTextSpeed:1; +} TextFlags; + +struct Struct_03002F90 +{ + u32 unk0[8]; + u32 unk20[8]; + u32 unk40[8]; + u32 unk60[8]; + u8 unk80; + u8 unk81; +}; + +extern TextFlags gTextFlags; + +extern u8 gUnknown_03002F84; +extern struct Struct_03002F90 gUnknown_03002F90; + +void SetFontsPointer(const struct FontInfo *fonts); +void DeactivateAllTextPrinters(void); +u16 AddTextPrinterParameterized(u8 windowId, u8 fontId, const u8 *str, u8 x, u8 y, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)); +bool16 AddTextPrinter(struct TextPrinterTemplate *template, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)); +void RunTextPrinters(void); +bool16 IsTextPrinterActive(u8 id); +u32 RenderFont(struct TextPrinter *textPrinter); +void GenerateFontHalfRowLookupTable(u8 fgColor, u8 bgColor, u8 shadowColor); +void SaveTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor); +void RestoreTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor); +void DecompressGlyphTile(const void *src_, void *dest_); +u8 GetLastTextColor(u8 colorType); +void CopyGlyphToWindow(struct TextPrinter *x); +void ClearTextSpan(struct TextPrinter *textPrinter, u32 width); +u8 GetMenuCursorDimensionByFont(u8, u8); + +u16 Font0Func(struct TextPrinter *textPrinter); +u16 Font1Func(struct TextPrinter *textPrinter); +u16 Font2Func(struct TextPrinter *textPrinter); +u16 Font3Func(struct TextPrinter *textPrinter); +u16 Font4Func(struct TextPrinter *textPrinter); +u16 Font5Func(struct TextPrinter *textPrinter); +u16 Font7Func(struct TextPrinter *textPrinter); +u16 Font8Func(struct TextPrinter *textPrinter); + +void TextPrinterInitDownArrowCounters(struct TextPrinter *textPrinter); +void TextPrinterDrawDownArrow(struct TextPrinter *textPrinter); +void TextPrinterClearDownArrow(struct TextPrinter *textPrinter); +bool8 TextPrinterWaitAutoMode(struct TextPrinter *textPrinter); +bool16 TextPrinterWaitWithDownArrow(struct TextPrinter *textPrinter); +bool16 TextPrinterWait(struct TextPrinter *textPrinter); +void DrawDownArrow(u8 windowId, u16 x, u16 y, u8 bgColor, bool8 drawArrow, u8 *counter, u8 *yCoordIndex); +u16 RenderText(struct TextPrinter *textPrinter); +u32 GetStringWidthFixedWidthFont(const u8 *str, u8 fontId, u8 letterSpacing); +u32 (*GetFontWidthFunc(u8 glyphId))(u16, bool32); +s32 GetStringWidth(u8 fontId, const u8 *str, s16 letterSpacing); +u8 RenderTextFont9(u8 *pixels, u8 fontId, u8 *str); +u8 DrawKeypadIcon(u8 windowId, u8 keypadIconId, u16 x, u16 y); +u8 GetKeypadIconTileOffset(u8 keypadIconId); +u8 GetKeypadIconWidth(u8 keypadIconId); +u8 GetKeypadIconHeight(u8 keypadIconId); +void SetDefaultFontsPointer(void); +u8 GetFontAttribute(u8 fontId, u8 attributeId); +u8 GetMenuCursorDimensionByFont(u8 fontId, u8 whichDimension); +void DecompressGlyphFont0(u16 glyphId, bool32 isJapanese); +u32 GetGlyphWidthFont0(u16 glyphId, bool32 isJapanese); +void DecompressGlyphFont7(u16 glyphId, bool32 isJapanese); +u32 GetGlyphWidthFont7(u16 glyphId, bool32 isJapanese); +void DecompressGlyphFont8(u16 glyphId, bool32 isJapanese); +u32 GetGlyphWidthFont8(u16 glyphId, bool32 isJapanese); +void DecompressGlyphFont2(u16 glyphId, bool32 isJapanese); +u32 GetGlyphWidthFont2(u16 glyphId, bool32 isJapanese); +void DecompressGlyphFont1(u16 glyphId, bool32 isJapanese); +u32 GetGlyphWidthFont1(u16 glyphId, bool32 isJapanese); +void DecompressGlyphFont9(u16 glyphId); + +// unk_text_util_2.c +u16 Font6Func(struct TextPrinter *textPrinter); +u32 GetGlyphWidthFont6(u16 glyphId, bool32 isJapanese); + +#endif // GUARD_TEXT_H diff --git a/gflib/window.c b/gflib/window.c new file mode 100644 index 000000000..7c87ea86d --- /dev/null +++ b/gflib/window.c @@ -0,0 +1,721 @@ +#include "global.h" +#include "window.h" +#include "malloc.h" +#include "bg.h" +#include "blit.h" + +u32 filler_03002F58; +u32 filler_03002F5C; +// This global is set to 0 and never changed. +u8 gTransparentTileNumber; +u32 filler_03002F64; +void *gUnknown_03002F70[4]; +extern u32 gUnneededFireRedVariable; + +#define WINDOWS_MAX 32 + +EWRAM_DATA struct Window gWindows[WINDOWS_MAX] = {0}; +EWRAM_DATA static struct Window* sWindowPtr = NULL; +EWRAM_DATA static u16 sWindowSize = 0; + +static u8 GetNumActiveWindowsOnBg(u8 bgId); +static u8 GetNumActiveWindowsOnBg8Bit(u8 bgId); + +static const struct WindowTemplate sDummyWindowTemplate = DUMMY_WIN_TEMPLATE; + +static void nullsub_8(void) +{ + +} + +bool16 InitWindows(const struct WindowTemplate *templates) +{ + int i; + void *bgTilemapBuffer; + int j; + u8 bgLayer; + u16 attrib; + u8* allocatedTilemapBuffer; + int allocatedBaseBlock; + + for (i = 0; i < 0x4; ++i) + { + bgTilemapBuffer = GetBgTilemapBuffer(i); + if (bgTilemapBuffer != NULL) + gUnknown_03002F70[i] = nullsub_8; + else + gUnknown_03002F70[i] = bgTilemapBuffer; + } + + for (i = 0; i < 0x20; ++i) + { + gWindows[i].window = sDummyWindowTemplate; + gWindows[i].tileData = NULL; + } + + for (i = 0, allocatedBaseBlock = 0, bgLayer = templates[i].bg; bgLayer != 0xFF && i < 0x20; ++i, bgLayer = templates[i].bg) + { + if (gUnneededFireRedVariable == 1) + { + allocatedBaseBlock = DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, 0, templates[i].width * templates[i].height, 0); + if (allocatedBaseBlock == -1) + return FALSE; + } + + if (gUnknown_03002F70[bgLayer] == NULL) + { + attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC); + + if (attrib != 0xFFFF) + { + allocatedTilemapBuffer = AllocZeroed(attrib); + + if (allocatedTilemapBuffer == NULL) + { + FreeAllWindowBuffers(); + return FALSE; + } + + for (j = 0; j < attrib; ++j) + allocatedTilemapBuffer[j] = 0; + + gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; + SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer); + } + } + + allocatedTilemapBuffer = AllocZeroed((u16)(0x20 * (templates[i].width * templates[i].height))); + + if (allocatedTilemapBuffer == NULL) + { + if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gUnknown_03002F70[bgLayer] != nullsub_8)) + { + Free(gUnknown_03002F70[bgLayer]); + gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; + } + + return FALSE; + } + + gWindows[i].tileData = allocatedTilemapBuffer; + gWindows[i].window = templates[i]; + + if (gUnneededFireRedVariable == 1) + { + gWindows[i].window.baseBlock = allocatedBaseBlock; + DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, allocatedBaseBlock, templates[i].width * templates[i].height, 1); + } + } + + gTransparentTileNumber = 0; + return TRUE; +} + +u16 AddWindow(const struct WindowTemplate *template) +{ + u16 win; + u8 bgLayer; + int allocatedBaseBlock; + u16 attrib; + u8 *allocatedTilemapBuffer; + int i; + + for (win = 0; win < WINDOWS_MAX; ++win) + { + if ((bgLayer = gWindows[win].window.bg) == 0xFF) + break; + } + + if (win == WINDOWS_MAX) + return 0xFF; + + bgLayer = template->bg; + allocatedBaseBlock = 0; + + if (gUnneededFireRedVariable == 1) + { + allocatedBaseBlock = DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, 0, template->width * template->height, 0); + + if (allocatedBaseBlock == -1) + return 0xFF; + } + + if (gUnknown_03002F70[bgLayer] == NULL) + { + attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC); + + if (attrib != 0xFFFF) + { + allocatedTilemapBuffer = AllocZeroed(attrib); + + if (allocatedTilemapBuffer == NULL) + return 0xFF; + + for (i = 0; i < attrib; ++i) + allocatedTilemapBuffer[i] = 0; + + gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; + SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer); + } + } + + allocatedTilemapBuffer = AllocZeroed((u16)(0x20 * (template->width * template->height))); + + if (allocatedTilemapBuffer == NULL) + { + if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gUnknown_03002F70[bgLayer] != nullsub_8)) + { + Free(gUnknown_03002F70[bgLayer]); + gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; + } + return 0xFF; + } + + gWindows[win].tileData = allocatedTilemapBuffer; + gWindows[win].window = *template; + + if (gUnneededFireRedVariable == 1) + { + gWindows[win].window.baseBlock = allocatedBaseBlock; + DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, allocatedBaseBlock, gWindows[win].window.width * gWindows[win].window.height, 1); + } + + return win; +} + +int AddWindowWithoutTileMap(const struct WindowTemplate *template) +{ + u16 win; + u8 bgLayer; + int allocatedBaseBlock; + + for (win = 0; win < WINDOWS_MAX; ++win) + { + if (gWindows[win].window.bg == 0xFF) + break; + } + + if (win == WINDOWS_MAX) + return 0xFF; + + bgLayer = template->bg; + allocatedBaseBlock = 0; + + if (gUnneededFireRedVariable == 1) + { + allocatedBaseBlock = DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, 0, template->width * template->height, 0); + + if (allocatedBaseBlock == -1) + return 0xFF; + } + + gWindows[win].window = *template; + + if (gUnneededFireRedVariable == 1) + { + gWindows[win].window.baseBlock = allocatedBaseBlock; + DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, allocatedBaseBlock, gWindows[win].window.width * gWindows[win].window.height, 1); + } + + return win; +} + +void RemoveWindow(u8 windowId) +{ + u8 bgLayer = gWindows[windowId].window.bg; + + if (gUnneededFireRedVariable == 1) + { + DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, gWindows[windowId].window.baseBlock, gWindows[windowId].window.width * gWindows[windowId].window.height, 2); + } + + gWindows[windowId].window = sDummyWindowTemplate; + + if (GetNumActiveWindowsOnBg(bgLayer) == 0) + { + if (gUnknown_03002F70[bgLayer] != nullsub_8) + { + Free(gUnknown_03002F70[bgLayer]); + gUnknown_03002F70[bgLayer] = 0; + } + } + + if (gWindows[windowId].tileData != NULL) + { + Free(gWindows[windowId].tileData); + gWindows[windowId].tileData = NULL; + } +} + +void FreeAllWindowBuffers(void) +{ + int i; + + for (i = 0; i < 4; ++i) + { + if (gUnknown_03002F70[i] != NULL && gUnknown_03002F70[i] != nullsub_8) + { + Free(gUnknown_03002F70[i]); + gUnknown_03002F70[i] = NULL; + } + } + + for (i = 0; i < 0x20; ++i) + { + if (gWindows[i].tileData != NULL) + { + Free(gWindows[i].tileData); + gWindows[i].tileData = NULL; + } + } +} + +void CopyWindowToVram(u8 windowId, u8 mode) +{ + struct Window windowLocal = gWindows[windowId]; + u16 windowSize = 32 * (windowLocal.window.width * windowLocal.window.height); + + switch (mode) + { + case 1: + CopyBgTilemapBufferToVram(windowLocal.window.bg); + break; + case 2: + LoadBgTiles(windowLocal.window.bg, windowLocal.tileData, windowSize, windowLocal.window.baseBlock); + break; + case 3: + LoadBgTiles(windowLocal.window.bg, windowLocal.tileData, windowSize, windowLocal.window.baseBlock); + CopyBgTilemapBufferToVram(windowLocal.window.bg); + break; + } +} + +void CopyWindowRectToVram(u32 windowId, u32 mode, u32 x, u32 y, u32 w, u32 h) +{ + struct Window windowLocal; + int rectSize; + int rectPos; + + if (w != 0 && h != 0) + { + windowLocal = gWindows[windowId]; + + rectSize = ((h - 1) * windowLocal.window.width); + rectSize += (windowLocal.window.width - x); + rectSize -= (windowLocal.window.width - (x + w)); + rectSize *= 32; + + rectPos = (y * windowLocal.window.width) + x; + + switch (mode) + { + case 1: + CopyBgTilemapBufferToVram(windowLocal.window.bg); + break; + case 2: + LoadBgTiles(windowLocal.window.bg, windowLocal.tileData + (rectPos * 32), rectSize, windowLocal.window.baseBlock + rectPos); + break; + case 3: + LoadBgTiles(windowLocal.window.bg, windowLocal.tileData + (rectPos * 32), rectSize, windowLocal.window.baseBlock + rectPos); + CopyBgTilemapBufferToVram(windowLocal.window.bg); + break; + } + } +} + +void PutWindowTilemap(u8 windowId) +{ + struct Window windowLocal = gWindows[windowId]; + + WriteSequenceToBgTilemapBuffer( + windowLocal.window.bg, + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE) + windowLocal.window.baseBlock, + windowLocal.window.tilemapLeft, + windowLocal.window.tilemapTop, + windowLocal.window.width, + windowLocal.window.height, + windowLocal.window.paletteNum, + 1); +} + +void PutWindowRectTilemapOverridePalette(u8 windowId, u8 x, u8 y, u8 width, u8 height, u8 palette) +{ + struct Window windowLocal = gWindows[windowId]; + u16 currentRow = windowLocal.window.baseBlock + (y * windowLocal.window.width) + x + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE); + int i; + + for (i = 0; i < height; ++i) + { + WriteSequenceToBgTilemapBuffer( + windowLocal.window.bg, + currentRow, + windowLocal.window.tilemapLeft + x, + windowLocal.window.tilemapTop + y + i, + width, + 1, + palette, + 1); + + currentRow += windowLocal.window.width; + } +} + +// Fills a window with transparent tiles. +void ClearWindowTilemap(u8 windowId) +{ + struct Window windowLocal = gWindows[windowId]; + + FillBgTilemapBufferRect( + windowLocal.window.bg, + gTransparentTileNumber, + windowLocal.window.tilemapLeft, + windowLocal.window.tilemapTop, + windowLocal.window.width, + windowLocal.window.height, + windowLocal.window.paletteNum); +} + +void PutWindowRectTilemap(u8 windowId, u8 x, u8 y, u8 width, u8 height) +{ + struct Window windowLocal = gWindows[windowId]; + u16 currentRow = windowLocal.window.baseBlock + (y * windowLocal.window.width) + x + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE); + int i; + + for (i = 0; i < height; ++i) + { + WriteSequenceToBgTilemapBuffer( + windowLocal.window.bg, + currentRow, + windowLocal.window.tilemapLeft + x, + windowLocal.window.tilemapTop + y + i, + width, + 1, + windowLocal.window.paletteNum, + 1); + + currentRow += windowLocal.window.width; + } +} + +void BlitBitmapToWindow(u8 windowId, const u8 *pixels, u16 x, u16 y, u16 width, u16 height) +{ + BlitBitmapRectToWindow(windowId, pixels, 0, 0, width, height, x, y, width, height); +} + +void BlitBitmapRectToWindow(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight) +{ + struct Bitmap sourceRect; + struct Bitmap destRect; + + sourceRect.pixels = (u8*)pixels; + sourceRect.width = srcWidth; + sourceRect.height = srcHeight; + + destRect.pixels = gWindows[windowId].tileData; + destRect.width = 8 * gWindows[windowId].window.width; + destRect.height = 8 * gWindows[windowId].window.height; + + BlitBitmapRect4Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, 0); +} + +static void BlitBitmapRectToWindowWithColorKey(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 colorKey) +{ + struct Bitmap sourceRect; + struct Bitmap destRect; + + sourceRect.pixels = (u8*)pixels; + sourceRect.width = srcWidth; + sourceRect.height = srcHeight; + + destRect.pixels = gWindows[windowId].tileData; + destRect.width = 8 * gWindows[windowId].window.width; + destRect.height = 8 * gWindows[windowId].window.height; + + BlitBitmapRect4Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, colorKey); +} + +void FillWindowPixelRect(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height) +{ + struct Bitmap pixelRect; + + pixelRect.pixels = gWindows[windowId].tileData; + pixelRect.width = 8 * gWindows[windowId].window.width; + pixelRect.height = 8 * gWindows[windowId].window.height; + + FillBitmapRect4Bit(&pixelRect, x, y, width, height, fillValue); +} + +void CopyToWindowPixelBuffer(u8 windowId, const void *src, u16 size, u16 tileOffset) +{ + if (size != 0) + CpuCopy16(src, gWindows[windowId].tileData + (0x20 * tileOffset), size); + else + LZ77UnCompWram(src, gWindows[windowId].tileData + (0x20 * tileOffset)); +} + +// Sets all pixels within the window to the fillValue color. +void FillWindowPixelBuffer(u8 windowId, u8 fillValue) +{ + int fillSize = gWindows[windowId].window.width * gWindows[windowId].window.height; + CpuFastFill8(fillValue, gWindows[windowId].tileData, 0x20 * fillSize); +} + +#define MOVE_TILES_DOWN(a) \ +{ \ + destOffset = i + (a); \ + srcOffset = i + (((width * (distanceLoop & ~7)) | (distanceLoop & 7)) * 4); \ + if (srcOffset < size) \ + *(u32*)(tileData + destOffset) = *(u32*)(tileData + srcOffset); \ + else \ + *(u32*)(tileData + destOffset) = fillValue32; \ + distanceLoop++; \ +} + +#define MOVE_TILES_UP(a) \ +{ \ + destOffset = i + (a); \ + srcOffset = i + (((width * (distanceLoop & ~7)) | (distanceLoop & 7)) * 4); \ + if (srcOffset < size) \ + *(u32*)(tileData - destOffset) = *(u32*)(tileData - srcOffset); \ + else \ + *(u32*)(tileData - destOffset) = fillValue32; \ + distanceLoop++; \ +} + +void ScrollWindow(u8 windowId, u8 direction, u8 distance, u8 fillValue) +{ + struct WindowTemplate window = gWindows[windowId].window; + u8 *tileData = gWindows[windowId].tileData; + u32 fillValue32 = (fillValue << 24) | (fillValue << 16) | (fillValue << 8) | fillValue; + s32 size = window.height * window.width * 32; + u32 width = window.width; + s32 i; + s32 srcOffset, destOffset; + u32 distanceLoop; + + switch (direction) + { + case 0: + for (i = 0; i < size; i += 32) + { + distanceLoop = distance; + MOVE_TILES_DOWN(0) + MOVE_TILES_DOWN(4) + MOVE_TILES_DOWN(8) + MOVE_TILES_DOWN(12) + MOVE_TILES_DOWN(16) + MOVE_TILES_DOWN(20) + MOVE_TILES_DOWN(24) + MOVE_TILES_DOWN(28) + } + break; + case 1: + tileData += size - 4; + for (i = 0; i < size; i += 32) + { + distanceLoop = distance; + MOVE_TILES_UP(0) + MOVE_TILES_UP(4) + MOVE_TILES_UP(8) + MOVE_TILES_UP(12) + MOVE_TILES_UP(16) + MOVE_TILES_UP(20) + MOVE_TILES_UP(24) + MOVE_TILES_UP(28) + } + break; + case 2: + break; + } +} + +void CallWindowFunction(u8 windowId, void ( *func)(u8, u8, u8, u8, u8, u8)) +{ + struct WindowTemplate window = gWindows[windowId].window; + func(window.bg, window.tilemapLeft, window.tilemapTop, window.width, window.height, window.paletteNum); +} + +bool8 SetWindowAttribute(u8 windowId, u8 attributeId, u32 value) +{ + switch (attributeId) + { + case WINDOW_TILEMAP_LEFT: + gWindows[windowId].window.tilemapLeft = value; + return FALSE; + case WINDOW_TILEMAP_TOP: + gWindows[windowId].window.tilemapTop = value; + return FALSE; + case WINDOW_PALETTE_NUM: + gWindows[windowId].window.paletteNum = value; + return FALSE; + case WINDOW_BASE_BLOCK: + gWindows[windowId].window.baseBlock = value; + return FALSE; + case WINDOW_TILE_DATA: + gWindows[windowId].tileData = (u8*)(value); + return TRUE; + case WINDOW_BG: + case WINDOW_WIDTH: + case WINDOW_HEIGHT: + default: + return TRUE; + } +} + +u32 GetWindowAttribute(u8 windowId, u8 attributeId) +{ + switch (attributeId) + { + case WINDOW_BG: + return gWindows[windowId].window.bg; + case WINDOW_TILEMAP_LEFT: + return gWindows[windowId].window.tilemapLeft; + case WINDOW_TILEMAP_TOP: + return gWindows[windowId].window.tilemapTop; + case WINDOW_WIDTH: + return gWindows[windowId].window.width; + case WINDOW_HEIGHT: + return gWindows[windowId].window.height; + case WINDOW_PALETTE_NUM: + return gWindows[windowId].window.paletteNum; + case WINDOW_BASE_BLOCK: + return gWindows[windowId].window.baseBlock; + case WINDOW_TILE_DATA: + return (u32)(gWindows[windowId].tileData); + default: + return 0; + } +} + +static u8 GetNumActiveWindowsOnBg(u8 bgId) +{ + u8 windowsNum = 0; + s32 i; + for (i = 0; i < WINDOWS_MAX; i++) + { + if (gWindows[i].window.bg == bgId) + windowsNum++; + } + return windowsNum; +} + +static void nullsub_9(void) +{ + +} + +u16 AddWindow8Bit(const struct WindowTemplate *template) +{ + u16 windowId; + u8* memAddress; + u8 bgLayer; + + for (windowId = 0; windowId < 32; windowId++) + { + if (gWindows[windowId].window.bg == 0xFF) + break; + } + if (windowId == WINDOWS_MAX) + return 0xFF; + bgLayer = template->bg; + if (gUnknown_03002F70[bgLayer] == 0) + { + u16 attribute = GetBgAttribute(bgLayer, BG_ATTR_METRIC); + if (attribute != 0xFFFF) + { + s32 i; + memAddress = Alloc(attribute); + if (memAddress == NULL) + return 0xFF; + for (i = 0; i < attribute; i++) // if we're going to zero out the memory anyway, why not call AllocZeroed? + memAddress[i] = 0; + gUnknown_03002F70[bgLayer] = memAddress; + SetBgTilemapBuffer(bgLayer, memAddress); + } + } + memAddress = Alloc((u16)(0x40 * (template->width * template->height))); + if (memAddress == NULL) + { + if (GetNumActiveWindowsOnBg8Bit(bgLayer) == 0 && gUnknown_03002F70[bgLayer] != nullsub_9) + { + Free(gUnknown_03002F70[bgLayer]); + gUnknown_03002F70[bgLayer] = NULL; + } + return 0xFF; + } + else + { + gWindows[windowId].tileData = memAddress; + gWindows[windowId].window = *template; + return windowId; + } +} + +void FillWindowPixelBuffer8Bit(u8 windowId, u8 fillValue) +{ + s32 i; + s32 size; + + size = (u16)(0x40 * (gWindows[windowId].window.width * gWindows[windowId].window.height)); + for (i = 0; i < size; i++) + gWindows[windowId].tileData[i] = fillValue; +} + +void FillWindowPixelRect8Bit(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height) +{ + struct Bitmap pixelRect; + + pixelRect.pixels = gWindows[windowId].tileData; + pixelRect.width = 8 * gWindows[windowId].window.width; + pixelRect.height = 8 * gWindows[windowId].window.height; + + FillBitmapRect8Bit(&pixelRect, x, y, width, height, fillValue); +} + +void BlitBitmapRectToWindow4BitTo8Bit(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 paletteNum) +{ + struct Bitmap sourceRect; + struct Bitmap destRect; + + sourceRect.pixels = (u8*) pixels; + sourceRect.width = srcWidth; + sourceRect.height = srcHeight; + + destRect.pixels = gWindows[windowId].tileData; + destRect.width = 8 * gWindows[windowId].window.width; + destRect.height = 8 * gWindows[windowId].window.height; + + BlitBitmapRect4BitTo8Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, 0, paletteNum); +} + +void CopyWindowToVram8Bit(u8 windowId, u8 mode) +{ + sWindowPtr = &gWindows[windowId]; + sWindowSize = 0x40 * (sWindowPtr->window.width * sWindowPtr->window.height); + + switch (mode) + { + case 1: + CopyBgTilemapBufferToVram(sWindowPtr->window.bg); + break; + case 2: + LoadBgTiles(sWindowPtr->window.bg, sWindowPtr->tileData, sWindowSize, sWindowPtr->window.baseBlock); + break; + case 3: + LoadBgTiles(sWindowPtr->window.bg, sWindowPtr->tileData, sWindowSize, sWindowPtr->window.baseBlock); + CopyBgTilemapBufferToVram(sWindowPtr->window.bg); + break; + } +} + +static u8 GetNumActiveWindowsOnBg8Bit(u8 bgId) +{ + u8 windowsNum = 0; + s32 i; + for (i = 0; i < WINDOWS_MAX; i++) + { + if (gWindows[i].window.bg == bgId) + windowsNum++; + } + return windowsNum; +} diff --git a/gflib/window.h b/gflib/window.h new file mode 100644 index 000000000..10e447789 --- /dev/null +++ b/gflib/window.h @@ -0,0 +1,78 @@ +#ifndef GUARD_WINDOW_H +#define GUARD_WINDOW_H + +#define PIXEL_FILL(num) ((num) | ((num) << 4)) + +enum +{ + WINDOW_BG, + WINDOW_TILEMAP_LEFT, + WINDOW_TILEMAP_TOP, + WINDOW_WIDTH, + WINDOW_HEIGHT, + WINDOW_PALETTE_NUM, + WINDOW_BASE_BLOCK, + WINDOW_TILE_DATA +}; + +struct WindowTemplate +{ + u8 bg; + u8 tilemapLeft; + u8 tilemapTop; + u8 width; + u8 height; + u8 paletteNum; + u16 baseBlock; +}; + +#define DUMMY_WIN_TEMPLATE \ +{ \ + 0xFF, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ + 0, \ +} + +struct Window +{ + struct WindowTemplate window; + u8 *tileData; +}; + +bool16 InitWindows(const struct WindowTemplate *templates); +u16 AddWindow(const struct WindowTemplate *template); +int AddWindowWithoutTileMap(const struct WindowTemplate *template); +void RemoveWindow(u8 windowId); +void FreeAllWindowBuffers(void); +void CopyWindowToVram(u8 windowId, u8 mode); +void CopyWindowRectToVram(u32 windowId, u32 mode, u32 x, u32 y, u32 w, u32 h); +void PutWindowTilemap(u8 windowId); +void PutWindowRectTilemapOverridePalette(u8 windowId, u8 x, u8 y, u8 width, u8 height, u8 palette); +void ClearWindowTilemap(u8 windowId); +void PutWindowRectTilemap(u8 windowId, u8 x, u8 y, u8 width, u8 height); +void BlitBitmapToWindow(u8 windowId, const u8 *pixels, u16 x, u16 y, u16 width, u16 height); +void BlitBitmapRectToWindow(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight); +void FillWindowPixelRect(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height); +void CopyToWindowPixelBuffer(u8 windowId, const void *src, u16 size, u16 tileOffset); +void FillWindowPixelBuffer(u8 windowId, u8 fillValue); +void ScrollWindow(u8 windowId, u8 direction, u8 distance, u8 fillValue); +void CallWindowFunction(u8 windowId, void ( *func)(u8, u8, u8, u8, u8, u8)); +bool8 SetWindowAttribute(u8 windowId, u8 attributeId, u32 value); +u32 GetWindowAttribute(u8 windowId, u8 attributeId); +u16 AddWindow8Bit(const struct WindowTemplate *template); +void FillWindowPixelBuffer8Bit(u8 windowId, u8 fillValue); +void FillWindowPixelRect8Bit(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height); +void BlitBitmapRectToWindow4BitTo8Bit(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 paletteNum); +void CopyWindowToVram8Bit(u8 windowId, u8 mode); + +extern struct Window gWindows[]; +extern void* gUnknown_03002F70[]; +extern u32 filler_03002F58; +extern u32 filler_03002F5C; +extern u32 filler_03002F64; + +#endif // GUARD_WINDOW_H diff --git a/include/alloc.h b/include/alloc.h deleted file mode 100644 index f2dcf6d46..000000000 --- a/include/alloc.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef GUARD_ALLOC_H -#define GUARD_ALLOC_H - -#define HEAP_SIZE 0x1C000 -#define malloc Alloc -#define calloc(ct, sz) AllocZeroed((ct) * (sz)) -#define free Free - -#define FREE_AND_SET_NULL(ptr) \ -{ \ - free(ptr); \ - ptr = NULL; \ -} - -extern u8 gHeap[]; - -void *Alloc(u32 size); -void *AllocZeroed(u32 size); -void Free(void *pointer); -void InitHeap(void *pointer, u32 size); - -#endif // GUARD_ALLOC_H diff --git a/include/bg.h b/include/bg.h deleted file mode 100644 index 3c7eee292..000000000 --- a/include/bg.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef GUARD_BG_H -#define GUARD_BG_H - -struct BGCntrlBitfield // for the I/O registers -{ - volatile u16 priority:2; - volatile u16 charBaseBlock:2; - volatile u16 field_0_2:4; - volatile u16 field_1_0:5; - volatile u16 areaOverflowMode:1; - volatile u16 screenSize:2; -}; - -enum -{ - BG_ATTR_CHARBASEINDEX = 1, - BG_ATTR_MAPBASEINDEX, - BG_ATTR_SCREENSIZE, - BG_ATTR_PALETTEMODE, - BG_ATTR_MOSAIC, - BG_ATTR_WRAPAROUND, - BG_ATTR_PRIORITY, - BG_ATTR_METRIC, - BG_ATTR_TYPE, - BG_ATTR_BASETILE, -}; - -struct BgTemplate -{ - u16 bg:2; // 0x1, 0x2 -> 0x3 - u16 charBaseIndex:2; // 0x4, 0x8 -> 0xC - u16 mapBaseIndex:5; // 0x10, 0x20, 0x40, 0x80, 0x100 -> 0x1F0 - u16 screenSize:2; // 0x200, 0x400 -> 0x600 - u16 paletteMode:1; // 0x800 - u16 priority:2; // 0x1000, 0x2000 > 0x3000 - u16 baseTile:10; -}; - -void ResetBgs(void); -u8 GetBgMode(void); -void ResetBgControlStructs(void); -void Unused_ResetBgControlStruct(u8 bg); -u8 LoadBgVram(u8 bg, const void *src, u16 size, u16 destOffset, u8 mode); -void SetTextModeAndHideBgs(void); -bool8 IsInvalidBg(u8 bg); -int DummiedOutFireRedLeafGreenTileAllocFunc(int a1, int a2, int a3, int a4); -void ResetBgsAndClearDma3BusyFlags(u32 leftoverFireRedLeafGreenVariable); -void InitBgsFromTemplates(u8 bgMode, const struct BgTemplate *templates, u8 numTemplates); -void InitBgFromTemplate(const struct BgTemplate *template); -void SetBgMode(u8 bgMode); -u16 LoadBgTiles(u8 bg, const void* src, u16 size, u16 destOffset); -u16 LoadBgTilemap(u8 bg, const void *src, u16 size, u16 destOffset); -u16 Unused_LoadBgPalette(u8 bg, const void *src, u16 size, u16 destOffset); -bool8 IsDma3ManagerBusyWithBgCopy(void); -void ShowBg(u8 bg); -void HideBg(u8 bg); -void SetBgAttribute(u8 bg, u8 attributeId, u8 value); -u16 GetBgAttribute(u8 bg, u8 attributeId); -s32 ChangeBgX(u8 bg, s32 value, u8 op); -s32 GetBgX(u8 bg); -s32 ChangeBgY(u8 bg, s32 value, u8 op); -s32 ChangeBgY_ScreenOff(u8 bg, u32 value, u8 op); -s32 GetBgY(u8 bg); -void SetBgAffine(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle); -u8 Unused_AdjustBgMosaic(u8 a1, u8 a2); -void SetBgTilemapBuffer(u8 bg, void *tilemap); -void UnsetBgTilemapBuffer(u8 bg); -void* GetBgTilemapBuffer(u8 bg); -void CopyToBgTilemapBuffer(u8 bg, const void *src, u16 mode, u16 destOffset); -void CopyBgTilemapBufferToVram(u8 bg); -void CopyToBgTilemapBufferRect(u8 bg, const void* src, u8 destX, u8 destY, u8 width, u8 height); -void CopyToBgTilemapBufferRect_ChangePalette(u8 bg, const void *src, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, u8 palette); -void CopyRectToBgTilemapBufferRect(u8 bg, const void *src, u8 srcX, u8 srcY, u8 srcWidth, u8 unused, u8 srcHeight, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, s16 palette1, s16 tileOffset); -void FillBgTilemapBufferRect_Palette0(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height); -void FillBgTilemapBufferRect(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height, u8 palette); -void WriteSequenceToBgTilemapBuffer(u8 bg, u16 firstTileNum, u8 x, u8 y, u8 width, u8 height, u8 paletteSlot, s16 tileNumDelta); -u16 GetBgMetricTextMode(u8 bg, u8 whichMetric); -u32 GetBgMetricAffineMode(u8 bg, u8 whichMetric); -u32 GetTileMapIndexFromCoords(s32 x, s32 y, s32 screenSize, u32 screenWidth, u32 screenHeight); -void CopyTileMapEntry(const u16 *src, u16 *dest, s32 palette1, s32 tileOffset, s32 palette2); -u32 GetBgType(u8 bg); -bool32 IsInvalidBg32(u8 bg); -bool32 IsTileMapOutsideWram(u8 bg); - -#endif // GUARD_BG_H diff --git a/include/blit.h b/include/blit.h deleted file mode 100644 index 78f67766e..000000000 --- a/include/blit.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef GUARD_BLIT_H -#define GUARD_BLIT_H - -struct Bitmap -{ - u8 *pixels; - u32 width:16; - u32 height:16; -}; - -void BlitBitmapRect4BitWithoutColorKey(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height); -void BlitBitmapRect4Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey); -void FillBitmapRect4Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue); -void BlitBitmapRect4BitTo8Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey, u8 paletteOffset); -void FillBitmapRect8Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue); - -#endif // GUARD_BLIT_H diff --git a/include/dma3.h b/include/dma3.h deleted file mode 100644 index 8eff34f55..000000000 --- a/include/dma3.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef GUARD_DMA3_H -#define GUARD_DMA3_H - -// Maximum amount of data we will transfer in one operation -#define MAX_DMA_BLOCK_SIZE 0x1000 - -#define Dma3CopyLarge_(src, dest, size, bit) \ -{ \ - const void *_src = src; \ - void *_dest = dest; \ - u32 _size = size; \ - while (1) \ - { \ - if (_size <= MAX_DMA_BLOCK_SIZE) \ - { \ - DmaCopy##bit(3, _src, _dest, _size); \ - break; \ - } \ - DmaCopy##bit(3, _src, _dest, MAX_DMA_BLOCK_SIZE); \ - _src += MAX_DMA_BLOCK_SIZE; \ - _dest += MAX_DMA_BLOCK_SIZE; \ - _size -= MAX_DMA_BLOCK_SIZE; \ - } \ -} - -#define Dma3CopyLarge16_(src, dest, size) Dma3CopyLarge_(src, dest, size, 16) -#define Dma3CopyLarge32_(src, dest, size) Dma3CopyLarge_(src, dest, size, 32) - -#define Dma3FillLarge_(value, dest, size, bit) \ -{ \ - void *_dest = dest; \ - u32 _size = size; \ - while (1) \ - { \ - if (_size <= MAX_DMA_BLOCK_SIZE) \ - { \ - DmaFill##bit(3, value, _dest, _size); \ - break; \ - } \ - DmaFill##bit(3, value, _dest, MAX_DMA_BLOCK_SIZE); \ - _dest += MAX_DMA_BLOCK_SIZE; \ - _size -= MAX_DMA_BLOCK_SIZE; \ - } \ -} - -#define Dma3FillLarge16_(value, dest, size) Dma3FillLarge_(value, dest, size, 16) -#define Dma3FillLarge32_(value, dest, size) Dma3FillLarge_(value, dest, size, 32) - -void ClearDma3Requests(void); -void ProcessDma3Requests(void); -s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode); -s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode); -s16 CheckForSpaceForDma3Request(s16 index); - -#endif // GUARD_DMA3_H diff --git a/include/gpu_regs.h b/include/gpu_regs.h deleted file mode 100644 index 89e0cb64b..000000000 --- a/include/gpu_regs.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef GUARD_GPU_REGS_H -#define GUARD_GPU_REGS_H - -// Exported type declarations - -// Exported RAM declarations - -// Exported ROM declarations -void InitGpuRegManager(void); -void CopyBufferedValuesToGpuRegs(void); -void SetGpuReg(u8 regOffset, u16 value); -void SetGpuReg_ForcedBlank(u8 regOffset, u16 value); -u16 GetGpuReg(u8 regOffset); -void SetGpuRegBits(u8 regOffset, u16 mask); -void ClearGpuRegBits(u8 regOffset, u16 mask); -void EnableInterrupts(u16 mask); -void DisableInterrupts(u16 mask); - -#endif //GUARD_GPU_REGS_H diff --git a/include/sprite.h b/include/sprite.h deleted file mode 100644 index 9753837fd..000000000 --- a/include/sprite.h +++ /dev/null @@ -1,319 +0,0 @@ -#ifndef GUARD_SPRITE_H -#define GUARD_SPRITE_H - -#define MAX_SPRITES 64 -#define SPRITE_INVALID_TAG 0xFFFF - -struct SpriteSheet -{ - const void *data; // Raw uncompressed pixel data - u16 size; - u16 tag; -}; - -struct CompressedSpriteSheet -{ - const u32 *data; // LZ77 compressed pixel data - u16 size; // Uncompressed size of pixel data - u16 tag; -}; - -struct SpriteFrameImage -{ - const void *data; - u16 size; -}; - -#define obj_frame_tiles(ptr) {.data = (u8 *)ptr, .size = sizeof ptr} - -#define overworld_frame(ptr, width, height, frame) {.data = (u8 *)ptr + (width * height * frame * 64)/2, .size = (width * height * 64)/2} - -struct SpritePalette -{ - const u16 *data; // Raw uncompressed palette data - u16 tag; -}; - -struct CompressedSpritePalette -{ - const u32 *data; // LZ77 compressed palette data - u16 tag; -}; - -struct AnimFrameCmd -{ - // If the sprite has an array of images, this is the array index. - // If the sprite has a sheet, this is the tile offset. - u32 imageValue:16; - - u32 duration:6; - u32 hFlip:1; - u32 vFlip:1; -}; - -struct AnimLoopCmd -{ - u32 type:16; - u32 count:6; -}; - -struct AnimJumpCmd -{ - u32 type:16; - u32 target:6; -}; - -// The first halfword of this union specifies the type of command. -// If it -2, then it is a jump command. If it is -1, then it is the end of the script. -// Otherwise, it is the imageValue for a frame command. -union AnimCmd -{ - s16 type; - struct AnimFrameCmd frame; - struct AnimLoopCmd loop; - struct AnimJumpCmd jump; -}; - -#define ANIMCMD_FRAME(...) \ - {.frame = {__VA_ARGS__}} -#define ANIMCMD_LOOP(_count) \ - {.loop = {.type = -3, .count = _count}} -#define ANIMCMD_JUMP(_target) \ - {.jump = {.type = -2, .target = _target}} -#define ANIMCMD_END \ - {.type = -1} - -struct AffineAnimFrameCmd -{ - s16 xScale; - s16 yScale; - u8 rotation; - u8 duration; -}; - -struct AffineAnimLoopCmd -{ - s16 type; - s16 count; -}; - -struct AffineAnimJumpCmd -{ - s16 type; - u16 target; -}; - -struct AffineAnimEndCmdAlt -{ - s16 type; - u16 val; -}; - -union AffineAnimCmd -{ - s16 type; - struct AffineAnimFrameCmd frame; - struct AffineAnimLoopCmd loop; - struct AffineAnimJumpCmd jump; - struct AffineAnimEndCmdAlt end; // unused in code -}; - -#define AFFINEANIMCMDTYPE_LOOP 0x7FFD -#define AFFINEANIMCMDTYPE_JUMP 0x7FFE -#define AFFINEANIMCMDTYPE_END 0x7FFF - -#define AFFINEANIMCMD_FRAME(_xScale, _yScale, _rotation, _duration) \ - {.frame = {.xScale = _xScale, .yScale = _yScale, .rotation = _rotation, .duration = _duration}} -#define AFFINEANIMCMD_LOOP(_count) \ - {.loop = {.type = AFFINEANIMCMDTYPE_LOOP, .count = _count}} -#define AFFINEANIMCMD_JUMP(_target) \ - {.jump = {.type = AFFINEANIMCMDTYPE_JUMP, .target = _target}} -#define AFFINEANIMCMD_END \ - {.type = AFFINEANIMCMDTYPE_END} -#define AFFINEANIMCMD_END_ALT(_val) \ - {.end = {.type = AFFINEANIMCMDTYPE_END, .val = _val}} - -struct AffineAnimState -{ - u8 animNum; - u8 animCmdIndex; - u8 delayCounter; - u8 loopCounter; - s16 xScale; - s16 yScale; - u16 rotation; -}; - -enum -{ - SUBSPRITES_OFF, - SUBSPRITES_ON, - SUBSPRITES_IGNORE_PRIORITY, // on but priority is ignored -}; - -struct Subsprite -{ - s8 x; // was u16 in R/S - s8 y; // was u16 in R/S - u16 shape:2; - u16 size:2; - u16 tileOffset:10; - u16 priority:2; -}; - -struct SubspriteTable -{ - u8 subspriteCount; - const struct Subsprite *subsprites; -}; - -struct Sprite; - -typedef void (*SpriteCallback)(struct Sprite *); - -struct SpriteTemplate -{ - u16 tileTag; - u16 paletteTag; - const struct OamData *oam; - const union AnimCmd *const *anims; - const struct SpriteFrameImage *images; - const union AffineAnimCmd *const *affineAnims; - SpriteCallback callback; -}; - -struct Sprite -{ - /*0x00*/ struct OamData oam; - /*0x08*/ const union AnimCmd *const *anims; - /*0x0C*/ const struct SpriteFrameImage *images; - /*0x10*/ const union AffineAnimCmd *const *affineAnims; - /*0x14*/ const struct SpriteTemplate *template; - /*0x18*/ const struct SubspriteTable *subspriteTables; - /*0x1C*/ SpriteCallback callback; - - /*0x20*/ struct Coords16 pos1; - /*0x24*/ struct Coords16 pos2; - /*0x28*/ s8 centerToCornerVecX; - /*0x29*/ s8 centerToCornerVecY; - - /*0x2A*/ u8 animNum; - /*0x2B*/ u8 animCmdIndex; - /*0x2C*/ u8 animDelayCounter:6; - bool8 animPaused:1; - bool8 affineAnimPaused:1; - /*0x2D*/ u8 animLoopCounter; - - // general purpose data fields - /*0x2E*/ s16 data[8]; - - /*0x3E*/ bool16 inUse:1; //1 - bool16 coordOffsetEnabled:1; //2 - bool16 invisible:1; //4 - bool16 flags_3:1; //8 - bool16 flags_4:1; //0x10 - bool16 flags_5:1; //0x20 - bool16 flags_6:1; //0x40 - bool16 flags_7:1; //0x80 - /*0x3F*/ bool16 hFlip:1; //1 - bool16 vFlip:1; //2 - bool16 animBeginning:1; //4 - bool16 affineAnimBeginning:1; //8 - bool16 animEnded:1; //0x10 - bool16 affineAnimEnded:1; //0x20 - bool16 usingSheet:1; //0x40 - bool16 flags_f:1; //0x80 - - /*0x40*/ u16 sheetTileStart; - - /*0x42*/ u8 subspriteTableNum:6; - u8 subspriteMode:2; - - /*0x43*/ u8 subpriority; -}; - -struct OamMatrix -{ - s16 a; - s16 b; - s16 c; - s16 d; -}; - -extern const struct OamData gDummyOamData; -extern const union AnimCmd *const gDummySpriteAnimTable[]; -extern const union AffineAnimCmd *const gDummySpriteAffineAnimTable[]; -extern const struct SpriteTemplate gDummySpriteTemplate; - -extern u8 gReservedSpritePaletteCount; -extern struct Sprite gSprites[]; -extern u8 gOamLimit; -extern u16 gReservedSpriteTileCount; -extern s16 gSpriteCoordOffsetX; -extern s16 gSpriteCoordOffsetY; -extern struct OamMatrix gOamMatrices[]; -extern bool8 gAffineAnimsDisabled; - -void ResetSpriteData(void); -void AnimateSprites(void); -void BuildOamBuffer(void); -u8 CreateSprite(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); -u8 CreateSpriteAtEnd(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); -u8 CreateInvisibleSprite(void (*callback)(struct Sprite *)); -u8 CreateSpriteAndAnimate(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); -void DestroySprite(struct Sprite *sprite); -void ResetOamRange(u8 a, u8 b); -void LoadOam(void); -void SetOamMatrix(u8 matrixNum, u16 a, u16 b, u16 c, u16 d); -void CalcCenterToCornerVec(struct Sprite *sprite, u8 shape, u8 size, u8 affineMode); -void SpriteCallbackDummy(struct Sprite *sprite); -void ProcessSpriteCopyRequests(void); -void RequestSpriteCopy(const u8 *src, u8 *dest, u16 size); -void FreeSpriteTiles(struct Sprite *sprite); -void FreeSpritePalette(struct Sprite *sprite); -void FreeSpriteOamMatrix(struct Sprite *sprite); -void DestroySpriteAndFreeResources(struct Sprite *sprite); -void sub_800142C(u32 a1, u32 a2, u16 *a3, u16 a4, u32 a5); -void AnimateSprite(struct Sprite *sprite); -void sub_8007E18(struct Sprite* sprite, s16 a2, s16 a3); -void StartSpriteAnim(struct Sprite *sprite, u8 animNum); -void StartSpriteAnimIfDifferent(struct Sprite *sprite, u8 animNum); -void SeekSpriteAnim(struct Sprite *sprite, u8 animCmdIndex); -void StartSpriteAffineAnim(struct Sprite *sprite, u8 animNum); -void StartSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum); -void ChangeSpriteAffineAnim(struct Sprite *sprite, u8 animNum); -void ChangeSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum); -void SetSpriteSheetFrameTileNum(struct Sprite *sprite); -u8 AllocOamMatrix(void); -void FreeOamMatrix(u8 matrixNum); -void InitSpriteAffineAnim(struct Sprite *sprite); -void SetOamMatrixRotationScaling(u8 matrixNum, s16 xScale, s16 yScale, u16 rotation); -u16 LoadSpriteSheet(const struct SpriteSheet *sheet); -void LoadSpriteSheets(const struct SpriteSheet *sheets); -u16 AllocTilesForSpriteSheet(struct SpriteSheet *sheet); -void AllocTilesForSpriteSheets(struct SpriteSheet *sheets); -void LoadTilesForSpriteSheet(const struct SpriteSheet *sheet); -void LoadTilesForSpriteSheets(struct SpriteSheet *sheets); -void FreeSpriteTilesByTag(u16 tag); -void FreeSpriteTileRanges(void); -u16 GetSpriteTileStartByTag(u16 tag); -u16 GetSpriteTileTagByTileStart(u16 start); -void RequestSpriteSheetCopy(const struct SpriteSheet *sheet); -u16 LoadSpriteSheetDeferred(const struct SpriteSheet *sheet); -void FreeAllSpritePalettes(void); -u8 LoadSpritePalette(const struct SpritePalette *palette); -void LoadSpritePalettes(const struct SpritePalette *palettes); -u8 AllocSpritePalette(u16 tag); -u8 IndexOfSpritePaletteTag(u16 tag); -u16 GetSpritePaletteTagByPaletteNum(u8 paletteNum); -void FreeSpritePaletteByTag(u16 tag); -void SetSubspriteTables(struct Sprite *sprite, const struct SubspriteTable *subspriteTables); -bool8 AddSpriteToOamBuffer(struct Sprite *object, u8 *oamIndex); -bool8 AddSubspritesToOamBuffer(struct Sprite *sprite, struct OamData *destOam, u8 *oamIndex); -void CopyToSprites(u8 *src); -void CopyFromSprites(u8 *dest); -u8 SpriteTileAllocBitmapOp(u16 bit, u8 op); -void ClearSpriteCopyRequests(void); -void ResetAffineAnimData(void); - -#endif //GUARD_SPRITE_H diff --git a/include/string_util.h b/include/string_util.h deleted file mode 100644 index b921d2391..000000000 --- a/include/string_util.h +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef GUARD_STRING_UTIL_H -#define GUARD_STRING_UTIL_H - -extern u8 gStringVar1[]; -extern u8 gStringVar2[]; -extern u8 gStringVar3[]; -extern u8 gStringVar4[]; - -enum StringConvertMode -{ - STR_CONV_MODE_LEFT_ALIGN, - STR_CONV_MODE_RIGHT_ALIGN, - STR_CONV_MODE_LEADING_ZEROS -}; - -u8 *StringCopy10(u8 *dest, const u8 *src); -u8 *StringGetEnd10(u8 *str); -u8 *StringCopy7(u8 *dest, const u8 *src); -u8 *StringCopy(u8 *dest, const u8 *src); -u8 *StringAppend(u8 *dest, const u8 *src); -u8 *StringCopyN(u8 *dest, const u8 *src, u8 n); -u8 *StringAppendN(u8 *dest, const u8 *src, u8 n); -u16 StringLength(const u8 *str); -s32 StringCompare(const u8 *str1, const u8 *str2); -s32 StringCompareN(const u8 *str1, const u8 *str2, u32 n); -bool8 IsStringLengthAtLeast(const u8 *str, s32 n); -u8 *ConvertIntToDecimalStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); -u8 *ConvertUIntToDecimalStringN(u8 *dest, u32 value, enum StringConvertMode mode, u8 n); -u8 *ConvertIntToHexStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n); -u8 *StringExpandPlaceholders(u8 *dest, const u8 *src); -u8 *StringBraille(u8 *dest, const u8 *src); -const u8 *GetExpandedPlaceholder(u32 id); -u8 *StringFill(u8 *dest, u8 c, u16 n); -u8 *StringCopyPadded(u8 *dest, const u8 *src, u8 c, u16 n); -u8 *StringFillWithTerminator(u8 *dest, u16 n); -u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n); -u32 StringLength_Multibyte(const u8 *str); -u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color); -bool32 IsStringJapanese(u8 *str); -bool32 sub_800924C(u8 *str, s32 n); -u8 GetExtCtrlCodeLength(u8 code); -s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2); -void ConvertInternationalString(u8 *s, u8 language); -void StripExtCtrlCodes(u8 *str); - -#endif // GUARD_STRING_UTIL_H diff --git a/include/text.h b/include/text.h deleted file mode 100644 index d3ff663bb..000000000 --- a/include/text.h +++ /dev/null @@ -1,297 +0,0 @@ -#ifndef GUARD_TEXT_H -#define GUARD_TEXT_H - -#define CHAR_SPACE 0x00 -#define CHAR_PLUS 0x2E -#define CHAR_0 0xA1 -#define CHAR_1 0xA2 -#define CHAR_2 0xA3 -#define CHAR_3 0xA4 -#define CHAR_4 0xA5 -#define CHAR_5 0xA6 -#define CHAR_6 0xA7 -#define CHAR_7 0xA8 -#define CHAR_8 0xA9 -#define CHAR_9 0xAA -#define CHAR_EXCL_MARK 0xAB -#define CHAR_QUESTION_MARK 0xAC -#define CHAR_PERIOD 0xAD -#define CHAR_HYPHEN 0xAE -#define CHAR_ELLIPSIS 0xB0 -#define CHAR_DBL_QUOT_LEFT 0xB1 -#define CHAR_DBL_QUOT_RIGHT 0xB2 -#define CHAR_SGL_QUOT_LEFT 0xB3 -#define CHAR_SGL_QUOT_RIGHT 0xB4 -#define CHAR_MALE 0xB5 -#define CHAR_FEMALE 0xB6 -#define CHAR_CURRENCY 0xB7 -#define CHAR_COMMA 0xB8 -#define CHAR_MULT_SIGN 0xB9 -#define CHAR_SLASH 0xBA -#define CHAR_A 0xBB -#define CHAR_B 0xBC -#define CHAR_C 0xBD -#define CHAR_D 0xBE -#define CHAR_E 0xBF -#define CHAR_F 0xC0 -#define CHAR_G 0xC1 -#define CHAR_H 0xC2 -#define CHAR_I 0xC3 -#define CHAR_J 0xC4 -#define CHAR_K 0xC5 -#define CHAR_L 0xC6 -#define CHAR_M 0xC7 -#define CHAR_N 0xC8 -#define CHAR_O 0xC9 -#define CHAR_P 0xCA -#define CHAR_Q 0xCB -#define CHAR_R 0xCC -#define CHAR_S 0xCD -#define CHAR_T 0xCE -#define CHAR_U 0xCF -#define CHAR_V 0xD0 -#define CHAR_W 0xD1 -#define CHAR_X 0xD2 -#define CHAR_Y 0xD3 -#define CHAR_Z 0xD4 -#define CHAR_a 0xD5 -#define CHAR_b 0xD6 -#define CHAR_c 0xD7 -#define CHAR_d 0xD8 -#define CHAR_e 0xD9 -#define CHAR_f 0xDA -#define CHAR_g 0xDB -#define CHAR_h 0xDC -#define CHAR_i 0xDD -#define CHAR_j 0xDE -#define CHAR_k 0xDF -#define CHAR_l 0xE0 -#define CHAR_m 0xE1 -#define CHAR_n 0xE2 -#define CHAR_o 0xE3 -#define CHAR_p 0xE4 -#define CHAR_q 0xE5 -#define CHAR_r 0xE6 -#define CHAR_s 0xE7 -#define CHAR_t 0xE8 -#define CHAR_u 0xE9 -#define CHAR_v 0xEA -#define CHAR_w 0xEB -#define CHAR_x 0xEC -#define CHAR_y 0xED -#define CHAR_z 0xEE -#define CHAR_SPECIAL_F7 0xF7 -#define CHAR_SPECIAL_F8 0xF8 -#define CHAR_SPECIAL_F9 0xF9 -#define CHAR_COLON 0xF0 -#define CHAR_PROMPT_SCROLL 0xFA // waits for button press and scrolls dialog -#define CHAR_PROMPT_CLEAR 0xFB // waits for button press and clears dialog -#define EXT_CTRL_CODE_BEGIN 0xFC // extended control code -#define PLACEHOLDER_BEGIN 0xFD // string placeholder -#define CHAR_NEWLINE 0xFE -#define EOS 0xFF // end of string - -#define EXT_CTRL_CODE_COLOR 0x1 -#define EXT_CTRL_CODE_HIGHLIGHT 0x2 -#define EXT_CTRL_CODE_SHADOW 0x3 -// -#define EXT_CTRL_CODE_UNKNOWN_7 0x7 -// -#define EXT_CTRL_CODE_CLEAR 0x11 -// -#define EXT_CTRL_CODE_CLEAR_TO 0x13 -#define EXT_CTRL_CODE_MIN_LETTER_SPACING 0x14 -#define EXT_CTRL_CODE_JPN 0x15 -#define EXT_CTRL_CODE_ENG 0x16 - -#define TEXT_COLOR_TRANSPARENT 0x0 -#define TEXT_COLOR_WHITE 0x1 -#define TEXT_COLOR_DARK_GREY 0x2 -// 0x3 -#define TEXT_COLOR_RED 0x4 -// 0x5 -#define TEXT_COLOR_GREEN 0x6 -// 0x7 -#define TEXT_COLOR_BLUE 0x8 - -// battle placeholders are located in battle_message.h - -#define NUM_TEXT_PRINTERS 32 - -#define TEXT_SPEED_FF 0xFF - -enum -{ - FONTATTR_MAX_LETTER_WIDTH, - FONTATTR_MAX_LETTER_HEIGHT, - FONTATTR_LETTER_SPACING, - FONTATTR_LINE_SPACING, - FONTATTR_UNKNOWN, // dunno what this is yet - FONTATTR_COLOR_FOREGROUND, - FONTATTR_COLOR_BACKGROUND, - FONTATTR_COLOR_SHADOW -}; - -struct TextPrinterSubStruct -{ - u8 glyphId:4; // 0x14 - bool8 hasPrintBeenSpedUp:1; - u8 unk:3; - u8 downArrowDelay:5; - u8 downArrowYPosIdx:2; - bool8 hasGlyphIdBeenSet:1; - u8 autoScrollDelay; -}; - -struct TextPrinterTemplate -{ - const u8* currentChar; - u8 windowId; - u8 fontId; - u8 x; - u8 y; - u8 currentX; // 0x8 - u8 currentY; - u8 letterSpacing; - u8 lineSpacing; - u8 unk:4; // 0xC - u8 fgColor:4; - u8 bgColor:4; - u8 shadowColor:4; -}; - -struct TextPrinter -{ - struct TextPrinterTemplate printerTemplate; - - void (*callback)(struct TextPrinterTemplate *, u16); // 0x10 - - union -#if !MODERN - __attribute__((packed)) -#endif - { - struct TextPrinterSubStruct sub; - u8 fields[7]; - } subUnion; - - u8 active; - u8 state; // 0x1C - u8 textSpeed; - u8 delayCounter; - u8 scrollDistance; - u8 minLetterSpacing; // 0x20 - u8 japanese; -}; - -struct FontInfo -{ - u16 (*fontFunction)(struct TextPrinter *x); - u8 maxLetterWidth; - u8 maxLetterHeight; - u8 letterSpacing; - u8 lineSpacing; - u8 unk:4; - u8 fgColor:4; - u8 bgColor:4; - u8 shadowColor:4; -}; - -extern const struct FontInfo *gFonts; - -struct GlyphWidthFunc -{ - u32 fontId; - u32 (*func)(u16 glyphId, bool32 isJapanese); -}; - -struct KeypadIcon -{ - u16 tileOffset; - u8 width; - u8 height; -}; - -typedef struct { - bool8 canABSpeedUpPrint:1; - bool8 useAlternateDownArrow:1; - bool8 autoScroll:1; - bool8 forceMidTextSpeed:1; -} TextFlags; - -struct Struct_03002F90 -{ - u32 unk0[8]; - u32 unk20[8]; - u32 unk40[8]; - u32 unk60[8]; - u8 unk80; - u8 unk81; -}; - -extern TextFlags gTextFlags; - -extern u8 gUnknown_03002F84; -extern struct Struct_03002F90 gUnknown_03002F90; - -void SetFontsPointer(const struct FontInfo *fonts); -void DeactivateAllTextPrinters(void); -u16 AddTextPrinterParameterized(u8 windowId, u8 fontId, const u8 *str, u8 x, u8 y, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)); -bool16 AddTextPrinter(struct TextPrinterTemplate *template, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)); -void RunTextPrinters(void); -bool16 IsTextPrinterActive(u8 id); -u32 RenderFont(struct TextPrinter *textPrinter); -void GenerateFontHalfRowLookupTable(u8 fgColor, u8 bgColor, u8 shadowColor); -void SaveTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor); -void RestoreTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor); -void DecompressGlyphTile(const void *src_, void *dest_); -u8 GetLastTextColor(u8 colorType); -void CopyGlyphToWindow(struct TextPrinter *x); -void ClearTextSpan(struct TextPrinter *textPrinter, u32 width); -u8 GetMenuCursorDimensionByFont(u8, u8); - -u16 Font0Func(struct TextPrinter *textPrinter); -u16 Font1Func(struct TextPrinter *textPrinter); -u16 Font2Func(struct TextPrinter *textPrinter); -u16 Font3Func(struct TextPrinter *textPrinter); -u16 Font4Func(struct TextPrinter *textPrinter); -u16 Font5Func(struct TextPrinter *textPrinter); -u16 Font7Func(struct TextPrinter *textPrinter); -u16 Font8Func(struct TextPrinter *textPrinter); - -void TextPrinterInitDownArrowCounters(struct TextPrinter *textPrinter); -void TextPrinterDrawDownArrow(struct TextPrinter *textPrinter); -void TextPrinterClearDownArrow(struct TextPrinter *textPrinter); -bool8 TextPrinterWaitAutoMode(struct TextPrinter *textPrinter); -bool16 TextPrinterWaitWithDownArrow(struct TextPrinter *textPrinter); -bool16 TextPrinterWait(struct TextPrinter *textPrinter); -void DrawDownArrow(u8 windowId, u16 x, u16 y, u8 bgColor, bool8 drawArrow, u8 *counter, u8 *yCoordIndex); -u16 RenderText(struct TextPrinter *textPrinter); -u32 GetStringWidthFixedWidthFont(const u8 *str, u8 fontId, u8 letterSpacing); -u32 (*GetFontWidthFunc(u8 glyphId))(u16, bool32); -s32 GetStringWidth(u8 fontId, const u8 *str, s16 letterSpacing); -u8 RenderTextFont9(u8 *pixels, u8 fontId, u8 *str); -u8 DrawKeypadIcon(u8 windowId, u8 keypadIconId, u16 x, u16 y); -u8 GetKeypadIconTileOffset(u8 keypadIconId); -u8 GetKeypadIconWidth(u8 keypadIconId); -u8 GetKeypadIconHeight(u8 keypadIconId); -void SetDefaultFontsPointer(void); -u8 GetFontAttribute(u8 fontId, u8 attributeId); -u8 GetMenuCursorDimensionByFont(u8 fontId, u8 whichDimension); -void DecompressGlyphFont0(u16 glyphId, bool32 isJapanese); -u32 GetGlyphWidthFont0(u16 glyphId, bool32 isJapanese); -void DecompressGlyphFont7(u16 glyphId, bool32 isJapanese); -u32 GetGlyphWidthFont7(u16 glyphId, bool32 isJapanese); -void DecompressGlyphFont8(u16 glyphId, bool32 isJapanese); -u32 GetGlyphWidthFont8(u16 glyphId, bool32 isJapanese); -void DecompressGlyphFont2(u16 glyphId, bool32 isJapanese); -u32 GetGlyphWidthFont2(u16 glyphId, bool32 isJapanese); -void DecompressGlyphFont1(u16 glyphId, bool32 isJapanese); -u32 GetGlyphWidthFont1(u16 glyphId, bool32 isJapanese); -void DecompressGlyphFont9(u16 glyphId); - -// unk_text_util_2.c -u16 Font6Func(struct TextPrinter *textPrinter); -u32 GetGlyphWidthFont6(u16 glyphId, bool32 isJapanese); - -#endif // GUARD_TEXT_H diff --git a/include/window.h b/include/window.h deleted file mode 100644 index 10e447789..000000000 --- a/include/window.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef GUARD_WINDOW_H -#define GUARD_WINDOW_H - -#define PIXEL_FILL(num) ((num) | ((num) << 4)) - -enum -{ - WINDOW_BG, - WINDOW_TILEMAP_LEFT, - WINDOW_TILEMAP_TOP, - WINDOW_WIDTH, - WINDOW_HEIGHT, - WINDOW_PALETTE_NUM, - WINDOW_BASE_BLOCK, - WINDOW_TILE_DATA -}; - -struct WindowTemplate -{ - u8 bg; - u8 tilemapLeft; - u8 tilemapTop; - u8 width; - u8 height; - u8 paletteNum; - u16 baseBlock; -}; - -#define DUMMY_WIN_TEMPLATE \ -{ \ - 0xFF, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ - 0, \ -} - -struct Window -{ - struct WindowTemplate window; - u8 *tileData; -}; - -bool16 InitWindows(const struct WindowTemplate *templates); -u16 AddWindow(const struct WindowTemplate *template); -int AddWindowWithoutTileMap(const struct WindowTemplate *template); -void RemoveWindow(u8 windowId); -void FreeAllWindowBuffers(void); -void CopyWindowToVram(u8 windowId, u8 mode); -void CopyWindowRectToVram(u32 windowId, u32 mode, u32 x, u32 y, u32 w, u32 h); -void PutWindowTilemap(u8 windowId); -void PutWindowRectTilemapOverridePalette(u8 windowId, u8 x, u8 y, u8 width, u8 height, u8 palette); -void ClearWindowTilemap(u8 windowId); -void PutWindowRectTilemap(u8 windowId, u8 x, u8 y, u8 width, u8 height); -void BlitBitmapToWindow(u8 windowId, const u8 *pixels, u16 x, u16 y, u16 width, u16 height); -void BlitBitmapRectToWindow(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight); -void FillWindowPixelRect(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height); -void CopyToWindowPixelBuffer(u8 windowId, const void *src, u16 size, u16 tileOffset); -void FillWindowPixelBuffer(u8 windowId, u8 fillValue); -void ScrollWindow(u8 windowId, u8 direction, u8 distance, u8 fillValue); -void CallWindowFunction(u8 windowId, void ( *func)(u8, u8, u8, u8, u8, u8)); -bool8 SetWindowAttribute(u8 windowId, u8 attributeId, u32 value); -u32 GetWindowAttribute(u8 windowId, u8 attributeId); -u16 AddWindow8Bit(const struct WindowTemplate *template); -void FillWindowPixelBuffer8Bit(u8 windowId, u8 fillValue); -void FillWindowPixelRect8Bit(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height); -void BlitBitmapRectToWindow4BitTo8Bit(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 paletteNum); -void CopyWindowToVram8Bit(u8 windowId, u8 mode); - -extern struct Window gWindows[]; -extern void* gUnknown_03002F70[]; -extern u32 filler_03002F58; -extern u32 filler_03002F5C; -extern u32 filler_03002F64; - -#endif // GUARD_WINDOW_H diff --git a/ld_script.txt b/ld_script.txt index 4247fca4d..cb3a7fa16 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -49,15 +49,15 @@ SECTIONS { { src/crt0.o(.text); src/main.o(.text); - src/alloc.o(.text); - src/dma3_manager.o(.text); - src/gpu_regs.o(.text); - src/bg.o(.text); - src/blit.o(.text); - src/window.o(.text); - src/text.o(.text); - src/sprite.o(.text); - src/string_util.o(.text); + gflib/malloc.o(.text); + gflib/dma3_manager.o(.text); + gflib/gpu_regs.o(.text); + gflib/bg.o(.text); + gflib/blit.o(.text); + gflib/window.o(.text); + gflib/text.o(.text); + gflib/sprite.o(.text); + gflib/string_util.o(.text); src/link.o(.text); src/link_rfu.o(.text); src/union_room.o(.text); @@ -420,12 +420,12 @@ SECTIONS { ALIGN(4) { src/main.o(.rodata); - src/bg.o(.rodata); - src/window.o(.rodata); - src/text.o(.rodata); - src/sprite.o(.rodata); + gflib/bg.o(.rodata); + gflib/window.o(.rodata); + gflib/text.o(.rodata); + gflib/sprite.o(.rodata); data/io_reg.o(.rodata); - src/string_util.o(.rodata); + gflib/string_util.o(.rodata); src/link.o(.rodata); src/link.o(.rodata.str1.4); src/link_rfu.o(.rodata); diff --git a/src/alloc.c b/src/alloc.c deleted file mode 100644 index 4d1a9fe5c..000000000 --- a/src/alloc.c +++ /dev/null @@ -1,210 +0,0 @@ -#include "global.h" - -static void *sHeapStart; -static u32 sHeapSize; -static u32 malloc_c_unused_0300000c; // needed to align dma3_manager.o(.bss) - -#define MALLOC_SYSTEM_ID 0xA3A3 - -struct MemBlock { - // Whether this block is currently allocated. - bool16 flag; - - // Magic number used for error checking. Should equal MALLOC_SYSTEM_ID. - u16 magic; - - // Size of the block (not including this header struct). - u32 size; - - // Previous block pointer. Equals sHeapStart if this is the first block. - struct MemBlock *prev; - - // Next block pointer. Equals sHeapStart if this is the last block. - struct MemBlock *next; - - // Data in the memory block. (Arrays of length 0 are a GNU extension.) - u8 data[0]; -}; - -void PutMemBlockHeader(void *block, struct MemBlock *prev, struct MemBlock *next, u32 size) -{ - struct MemBlock *header = (struct MemBlock *)block; - - header->flag = FALSE; - header->magic = MALLOC_SYSTEM_ID; - header->size = size; - header->prev = prev; - header->next = next; -} - -void PutFirstMemBlockHeader(void *block, u32 size) -{ - PutMemBlockHeader(block, (struct MemBlock *)block, (struct MemBlock *)block, size - sizeof(struct MemBlock)); -} - -void *AllocInternal(void *heapStart, u32 size) -{ - struct MemBlock *pos = (struct MemBlock *)heapStart; - struct MemBlock *head = pos; - struct MemBlock *splitBlock; - u32 foundBlockSize; - - // Alignment - if (size & 3) - size = 4 * ((size / 4) + 1); - - for (;;) { - // Loop through the blocks looking for unused block that's big enough. - - if (!pos->flag) { - foundBlockSize = pos->size; - - if (foundBlockSize >= size) { - if (foundBlockSize - size < 2 * sizeof(struct MemBlock)) { - // The block isn't much bigger than the requested size, - // so just use it. - pos->flag = TRUE; - } else { - // The block is significantly bigger than the requested - // size, so split the rest into a separate block. - foundBlockSize -= sizeof(struct MemBlock); - foundBlockSize -= size; - - splitBlock = (struct MemBlock *)(pos->data + size); - - pos->flag = TRUE; - pos->size = size; - - PutMemBlockHeader(splitBlock, pos, pos->next, foundBlockSize); - - pos->next = splitBlock; - - if (splitBlock->next != head) - splitBlock->next->prev = splitBlock; - } - - return pos->data; - } - } - - if (pos->next == head) - return NULL; - - pos = pos->next; - } -} - -void FreeInternal(void *heapStart, void *pointer) -{ - if (pointer) { - struct MemBlock *head = (struct MemBlock *)heapStart; - struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); - block->flag = FALSE; - - // If the freed block isn't the last one, merge with the next block - // if it's not in use. - if (block->next != head) { - if (!block->next->flag) { - block->size += sizeof(struct MemBlock) + block->next->size; - block->next->magic = 0; - block->next = block->next->next; - if (block->next != head) - block->next->prev = block; - } - } - - // If the freed block isn't the first one, merge with the previous block - // if it's not in use. - if (block != head) { - if (!block->prev->flag) { - block->prev->next = block->next; - - if (block->next != head) - block->next->prev = block->prev; - - block->magic = 0; - block->prev->size += sizeof(struct MemBlock) + block->size; - } - } - } -} - -void *AllocZeroedInternal(void *heapStart, u32 size) -{ - void *mem = AllocInternal(heapStart, size); - - if (mem != NULL) { - if (size & 3) - size = 4 * ((size / 4) + 1); - - CpuFill32(0, mem, size); - } - - return mem; -} - -bool32 CheckMemBlockInternal(void *heapStart, void *pointer) -{ - struct MemBlock *head = (struct MemBlock *)heapStart; - struct MemBlock *block = (struct MemBlock *)((u8 *)pointer - sizeof(struct MemBlock)); - - if (block->magic != MALLOC_SYSTEM_ID) - return FALSE; - - if (block->next->magic != MALLOC_SYSTEM_ID) - return FALSE; - - if (block->next != head && block->next->prev != block) - return FALSE; - - if (block->prev->magic != MALLOC_SYSTEM_ID) - return FALSE; - - if (block->prev != head && block->prev->next != block) - return FALSE; - - if (block->next != head && block->next != (struct MemBlock *)(block->data + block->size)) - return FALSE; - - return TRUE; -} - -void InitHeap(void *heapStart, u32 heapSize) -{ - sHeapStart = heapStart; - sHeapSize = heapSize; - PutFirstMemBlockHeader(heapStart, heapSize); -} - -void *Alloc(u32 size) -{ - return AllocInternal(sHeapStart, size); -} - -void *AllocZeroed(u32 size) -{ - return AllocZeroedInternal(sHeapStart, size); -} - -void Free(void *pointer) -{ - FreeInternal(sHeapStart, pointer); -} - -bool32 CheckMemBlock(void *pointer) -{ - return CheckMemBlockInternal(sHeapStart, pointer); -} - -bool32 CheckHeap() -{ - struct MemBlock *pos = (struct MemBlock *)sHeapStart; - - do { - if (!CheckMemBlockInternal(sHeapStart, pos->data)) - return FALSE; - pos = pos->next; - } while (pos != (struct MemBlock *)sHeapStart); - - return TRUE; -} diff --git a/src/apprentice.c b/src/apprentice.c index 4ad295e1a..6c4ac86aa 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -10,7 +10,7 @@ #include "item.h" #include "item_menu.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "new_game.h" #include "party_menu.h" diff --git a/src/battle_anim_effects_1.c b/src/battle_anim_effects_1.c index db9057cb4..b2999a127 100644 --- a/src/battle_anim_effects_1.c +++ b/src/battle_anim_effects_1.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle_anim.h" #include "battle_interface.h" #include "decompress.h" diff --git a/src/battle_anim_effects_2.c b/src/battle_anim_effects_2.c index 1add76bf3..87023e7e1 100755 --- a/src/battle_anim_effects_2.c +++ b/src/battle_anim_effects_2.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle_anim.h" #include "battle_interface.h" #include "decompress.h" diff --git a/src/battle_anim_effects_3.c b/src/battle_anim_effects_3.c index 79b728e61..ea69780ef 100755 --- a/src/battle_anim_effects_3.c +++ b/src/battle_anim_effects_3.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_anim.h" #include "bg.h" diff --git a/src/battle_anim_mons.c b/src/battle_anim_mons.c index a667daecf..9a588a9ca 100644 --- a/src/battle_anim_mons.c +++ b/src/battle_anim_mons.c @@ -7,7 +7,7 @@ #include "decompress.h" #include "dma3.h" #include "gpu_regs.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "pokemon_icon.h" #include "sprite.h" diff --git a/src/battle_anim_utility_funcs.c b/src/battle_anim_utility_funcs.c index efe35e1b4..1a0200271 100644 --- a/src/battle_anim_utility_funcs.c +++ b/src/battle_anim_utility_funcs.c @@ -3,7 +3,7 @@ #include "contest.h" #include "gpu_regs.h" #include "graphics.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "sound.h" #include "sprite.h" diff --git a/src/battle_dome.c b/src/battle_dome.c index 1c116467d..22a1ecaf2 100644 --- a/src/battle_dome.c +++ b/src/battle_dome.c @@ -8,7 +8,7 @@ #include "event_data.h" #include "overworld.h" #include "util.h" -#include "alloc.h" +#include "malloc.h" #include "string_util.h" #include "random.h" #include "task.h" diff --git a/src/battle_factory_screen.c b/src/battle_factory_screen.c index f56442432..36839cde5 100644 --- a/src/battle_factory_screen.c +++ b/src/battle_factory_screen.c @@ -11,7 +11,7 @@ #include "palette.h" #include "task.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "gpu_regs.h" #include "string_util.h" diff --git a/src/battle_gfx_sfx_util.c b/src/battle_gfx_sfx_util.c index de403aec7..737e6b61e 100644 --- a/src/battle_gfx_sfx_util.c +++ b/src/battle_gfx_sfx_util.c @@ -6,7 +6,7 @@ #include "constants/battle_anim.h" #include "battle_interface.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "graphics.h" #include "random.h" #include "util.h" diff --git a/src/battle_main.c b/src/battle_main.c index 87e29dd57..c8b2d17cc 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -26,7 +26,7 @@ #include "link_rfu.h" #include "load_save.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "m4a.h" #include "palette.h" #include "party_menu.h" diff --git a/src/battle_pike.c b/src/battle_pike.c index 8846e2d1a..7d1257fb3 100644 --- a/src/battle_pike.c +++ b/src/battle_pike.c @@ -9,7 +9,7 @@ #include "task.h" #include "battle_tower.h" #include "party_menu.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "script.h" #include "battle_setup.h" diff --git a/src/battle_pyramid.c b/src/battle_pyramid.c index cc722da8d..e0a00d28c 100644 --- a/src/battle_pyramid.c +++ b/src/battle_pyramid.c @@ -23,7 +23,7 @@ #include "main.h" #include "load_save.h" #include "script.h" -#include "alloc.h" +#include "malloc.h" #include "overworld.h" #include "event_scripts.h" #include "constants/battle_frontier.h" diff --git a/src/battle_pyramid_bag.c b/src/battle_pyramid_bag.c index b4ec6cd62..a17a93499 100644 --- a/src/battle_pyramid_bag.c +++ b/src/battle_pyramid_bag.c @@ -17,7 +17,7 @@ #include "list_menu.h" #include "mail.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "menu_helpers.h" #include "overworld.h" diff --git a/src/battle_records.c b/src/battle_records.c index 3d4cabcb0..ccd16e219 100644 --- a/src/battle_records.c +++ b/src/battle_records.c @@ -18,7 +18,7 @@ #include "international_string_util.h" #include "sound.h" #include "constants/songs.h" -#include "alloc.h" +#include "malloc.h" #include "gpu_regs.h" #include "constants/game_stat.h" #include "trainer_hill.h" diff --git a/src/battle_transition.c b/src/battle_transition.c index 3e86fcf51..ae6c28711 100644 --- a/src/battle_transition.c +++ b/src/battle_transition.c @@ -9,7 +9,7 @@ #include "field_weather.h" #include "gpu_regs.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "overworld.h" #include "palette.h" #include "random.h" diff --git a/src/battle_util2.c b/src/battle_util2.c index f840cd58b..ce2153b2d 100644 --- a/src/battle_util2.c +++ b/src/battle_util2.c @@ -2,7 +2,7 @@ #include "battle.h" #include "battle_anim.h" #include "battle_controllers.h" -#include "alloc.h" +#include "malloc.h" #include "pokemon.h" #include "trainer_hill.h" #include "party_menu.h" diff --git a/src/berry_blender.c b/src/berry_blender.c index 03764b2f8..3c58ca1b4 100644 --- a/src/berry_blender.c +++ b/src/berry_blender.c @@ -12,7 +12,7 @@ #include "bg.h" #include "palette.h" #include "decompress.h" -#include "alloc.h" +#include "malloc.h" #include "gpu_regs.h" #include "text.h" #include "text_window.h" diff --git a/src/berry_crush.c b/src/berry_crush.c index 6259e0a41..8b4c7a545 100755 --- a/src/berry_crush.c +++ b/src/berry_crush.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "berry_powder.h" #include "bg.h" #include "event_data.h" diff --git a/src/berry_fix_program.c b/src/berry_fix_program.c index 37be569f8..e3c4e2675 100644 --- a/src/berry_fix_program.c +++ b/src/berry_fix_program.c @@ -1,7 +1,7 @@ #include "global.h" #include "gpu_regs.h" #include "multiboot.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "graphics.h" #include "main.h" diff --git a/src/berry_tag_screen.c b/src/berry_tag_screen.c index c3eac783d..8c9cde7d3 100644 --- a/src/berry_tag_screen.c +++ b/src/berry_tag_screen.c @@ -21,7 +21,7 @@ #include "string_util.h" #include "strings.h" #include "bg.h" -#include "alloc.h" +#include "malloc.h" #include "scanline_effect.h" #include "gpu_regs.h" #include "graphics.h" diff --git a/src/bg.c b/src/bg.c deleted file mode 100644 index 1678f4023..000000000 --- a/src/bg.c +++ /dev/null @@ -1,1305 +0,0 @@ -#include "global.h" -#include "bg.h" -#include "dma3.h" -#include "gpu_regs.h" - -#define DISPCNT_ALL_BG_AND_MODE_BITS (DISPCNT_BG_ALL_ON | 0x7) - -struct BgControl -{ - struct BgConfig { - u16 visible:1; - u16 unknown_1:1; - u16 screenSize:2; - u16 priority:2; - u16 mosaic:1; - u16 wraparound:1; - - u16 charBaseIndex:2; - u16 mapBaseIndex:5; - u16 paletteMode:1; - - u8 unknown_2; - u8 unknown_3; - } configs[4]; - - u16 bgVisibilityAndMode; -}; - -struct BgConfig2 -{ - u32 baseTile:10; - u32 basePalette:4; - u32 unk_3:18; - - void* tilemap; - s32 bg_x; - s32 bg_y; -}; - -static struct BgControl sGpuBgConfigs; -static struct BgConfig2 sGpuBgConfigs2[4]; -static u32 sDmaBusyBitfield[4]; - -u32 gUnneededFireRedVariable; - -static const struct BgConfig sZeroedBgControlStruct = { 0 }; - -void ResetBgs(void) -{ - ResetBgControlStructs(); - sGpuBgConfigs.bgVisibilityAndMode = 0; - SetTextModeAndHideBgs(); -} - -static void SetBgModeInternal(u8 bgMode) -{ - sGpuBgConfigs.bgVisibilityAndMode &= 0xFFF8; - sGpuBgConfigs.bgVisibilityAndMode |= bgMode; -} - -u8 GetBgMode(void) -{ - return sGpuBgConfigs.bgVisibilityAndMode & 0x7; -} - -void ResetBgControlStructs(void) -{ - struct BgConfig* bgConfigs = &sGpuBgConfigs.configs[0]; - struct BgConfig zeroedConfig = sZeroedBgControlStruct; - int i; - - for (i = 0; i < 4; i++) - { - bgConfigs[i] = zeroedConfig; - } -} - -void Unused_ResetBgControlStruct(u8 bg) -{ - if (!IsInvalidBg(bg)) - { - sGpuBgConfigs.configs[bg] = sZeroedBgControlStruct; - } -} - -enum -{ - BG_CTRL_ATTR_VISIBLE = 1, - BG_CTRL_ATTR_CHARBASEINDEX = 2, - BG_CTRL_ATTR_MAPBASEINDEX = 3, - BG_CTRL_ATTR_SCREENSIZE = 4, - BG_CTRL_ATTR_PALETTEMODE = 5, - BG_CTRL_ATTR_PRIORITY = 6, - BG_CTRL_ATTR_MOSAIC = 7, - BG_CTRL_ATTR_WRAPAROUND = 8, -}; - -static void SetBgControlAttributes(u8 bg, u8 charBaseIndex, u8 mapBaseIndex, u8 screenSize, u8 paletteMode, u8 priority, u8 mosaic, u8 wraparound) -{ - if (!IsInvalidBg(bg)) - { - if (charBaseIndex != 0xFF) - { - sGpuBgConfigs.configs[bg].charBaseIndex = charBaseIndex & 0x3; - } - - if (mapBaseIndex != 0xFF) - { - sGpuBgConfigs.configs[bg].mapBaseIndex = mapBaseIndex & 0x1F; - } - - if (screenSize != 0xFF) - { - sGpuBgConfigs.configs[bg].screenSize = screenSize & 0x3; - } - - if (paletteMode != 0xFF) - { - sGpuBgConfigs.configs[bg].paletteMode = paletteMode; - } - - if (priority != 0xFF) - { - sGpuBgConfigs.configs[bg].priority = priority & 0x3; - } - - if (mosaic != 0xFF) - { - sGpuBgConfigs.configs[bg].mosaic = mosaic & 0x1; - } - - if (wraparound != 0xFF) - { - sGpuBgConfigs.configs[bg].wraparound = wraparound; - } - - sGpuBgConfigs.configs[bg].unknown_2 = 0; - sGpuBgConfigs.configs[bg].unknown_3 = 0; - - sGpuBgConfigs.configs[bg].visible = 1; - } -} - -static u16 GetBgControlAttribute(u8 bg, u8 attributeId) -{ - if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) - { - switch (attributeId) - { - case BG_CTRL_ATTR_VISIBLE: - return sGpuBgConfigs.configs[bg].visible; - case BG_CTRL_ATTR_CHARBASEINDEX: - return sGpuBgConfigs.configs[bg].charBaseIndex; - case BG_CTRL_ATTR_MAPBASEINDEX: - return sGpuBgConfigs.configs[bg].mapBaseIndex; - case BG_CTRL_ATTR_SCREENSIZE: - return sGpuBgConfigs.configs[bg].screenSize; - case BG_CTRL_ATTR_PALETTEMODE: - return sGpuBgConfigs.configs[bg].paletteMode; - case BG_CTRL_ATTR_PRIORITY: - return sGpuBgConfigs.configs[bg].priority; - case BG_CTRL_ATTR_MOSAIC: - return sGpuBgConfigs.configs[bg].mosaic; - case BG_CTRL_ATTR_WRAPAROUND: - return sGpuBgConfigs.configs[bg].wraparound; - } - } - - return 0xFF; -} - -u8 LoadBgVram(u8 bg, const void *src, u16 size, u16 destOffset, u8 mode) -{ - u16 offset; - s8 cursor; - - if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) - { - switch (mode) - { - case 0x1: - offset = sGpuBgConfigs.configs[bg].charBaseIndex * BG_CHAR_SIZE; - break; - case 0x2: - offset = sGpuBgConfigs.configs[bg].mapBaseIndex * BG_SCREEN_SIZE; - break; - default: - cursor = -1; - goto end; - } - - offset = destOffset + offset; - - cursor = RequestDma3Copy(src, (void*)(offset + BG_VRAM), size, 0); - - if (cursor == -1) - { - return -1; - } - } - else - { - return -1; - } - -end: - return cursor; -} - -static void ShowBgInternal(u8 bg) -{ - u16 value; - if (!IsInvalidBg(bg) && sGpuBgConfigs.configs[bg].visible) - { - value = sGpuBgConfigs.configs[bg].priority | - (sGpuBgConfigs.configs[bg].charBaseIndex << 2) | - (sGpuBgConfigs.configs[bg].mosaic << 6) | - (sGpuBgConfigs.configs[bg].paletteMode << 7) | - (sGpuBgConfigs.configs[bg].mapBaseIndex << 8) | - (sGpuBgConfigs.configs[bg].wraparound << 13) | - (sGpuBgConfigs.configs[bg].screenSize << 14); - - SetGpuReg((bg << 1) + REG_OFFSET_BG0CNT, value); - - sGpuBgConfigs.bgVisibilityAndMode |= 1 << (bg + 8); - sGpuBgConfigs.bgVisibilityAndMode &= DISPCNT_ALL_BG_AND_MODE_BITS; - } -} - -static void HideBgInternal(u8 bg) -{ - if (!IsInvalidBg(bg)) - { - sGpuBgConfigs.bgVisibilityAndMode &= ~(1 << (bg + 8)); - sGpuBgConfigs.bgVisibilityAndMode &= DISPCNT_ALL_BG_AND_MODE_BITS; - } -} - -static void SyncBgVisibilityAndMode(void) -{ - SetGpuReg(REG_OFFSET_DISPCNT, (GetGpuReg(REG_OFFSET_DISPCNT) & ~DISPCNT_ALL_BG_AND_MODE_BITS) | sGpuBgConfigs.bgVisibilityAndMode); -} - -void SetTextModeAndHideBgs(void) -{ - SetGpuReg(REG_OFFSET_DISPCNT, GetGpuReg(REG_OFFSET_DISPCNT) & ~DISPCNT_ALL_BG_AND_MODE_BITS); -} - -static void SetBgAffineInternal(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle) -{ - struct BgAffineSrcData src; - struct BgAffineDstData dest; - - switch (sGpuBgConfigs.bgVisibilityAndMode & 0x7) - { - case 1: - if (bg != 2) - return; - break; - case 2: - if (bg < 2 || bg > 3) - return; - break; - case 0: - default: - return; - } - - src.texX = srcCenterX; - src.texY = srcCenterY; - src.scrX = dispCenterX; - src.scrY = dispCenterY; - src.sx = scaleX; - src.sy = scaleY; - src.alpha = rotationAngle; - - BgAffineSet(&src, &dest, 1); - - SetGpuReg(REG_OFFSET_BG2PA, dest.pa); - SetGpuReg(REG_OFFSET_BG2PB, dest.pb); - SetGpuReg(REG_OFFSET_BG2PC, dest.pc); - SetGpuReg(REG_OFFSET_BG2PD, dest.pd); - SetGpuReg(REG_OFFSET_BG2PA, dest.pa); - SetGpuReg(REG_OFFSET_BG2X_L, (s16)(dest.dx)); - SetGpuReg(REG_OFFSET_BG2X_H, (s16)(dest.dx >> 16)); - SetGpuReg(REG_OFFSET_BG2Y_L, (s16)(dest.dy)); - SetGpuReg(REG_OFFSET_BG2Y_H, (s16)(dest.dy >> 16)); -} - -bool8 IsInvalidBg(u8 bg) -{ - if (bg > 3) - return TRUE; - else - return FALSE; -} - -int DummiedOutFireRedLeafGreenTileAllocFunc(int a1, int a2, int a3, int a4) -{ - return 0; -} - -void ResetBgsAndClearDma3BusyFlags(u32 leftoverFireRedLeafGreenVariable) -{ - int i; - ResetBgs(); - - for (i = 0; i < 4; i++) - { - sDmaBusyBitfield[i] = 0; - } - - gUnneededFireRedVariable = leftoverFireRedLeafGreenVariable; -} - -void InitBgsFromTemplates(u8 bgMode, const struct BgTemplate *templates, u8 numTemplates) -{ - int i; - u8 bg; - - SetBgModeInternal(bgMode); - ResetBgControlStructs(); - - for (i = 0; i < numTemplates; i++) - { - bg = templates[i].bg; - if (bg < 4) - { - SetBgControlAttributes(bg, - templates[i].charBaseIndex, - templates[i].mapBaseIndex, - templates[i].screenSize, - templates[i].paletteMode, - templates[i].priority, - 0, - 0); - - sGpuBgConfigs2[bg].baseTile = templates[i].baseTile; - sGpuBgConfigs2[bg].basePalette = 0; - sGpuBgConfigs2[bg].unk_3 = 0; - - sGpuBgConfigs2[bg].tilemap = NULL; - sGpuBgConfigs2[bg].bg_x = 0; - sGpuBgConfigs2[bg].bg_y = 0; - } - } -} - -void InitBgFromTemplate(const struct BgTemplate *template) -{ - u8 bg = template->bg; - - if (bg < 4) - { - SetBgControlAttributes(bg, - template->charBaseIndex, - template->mapBaseIndex, - template->screenSize, - template->paletteMode, - template->priority, - 0, - 0); - - sGpuBgConfigs2[bg].baseTile = template->baseTile; - sGpuBgConfigs2[bg].basePalette = 0; - sGpuBgConfigs2[bg].unk_3 = 0; - - sGpuBgConfigs2[bg].tilemap = NULL; - sGpuBgConfigs2[bg].bg_x = 0; - sGpuBgConfigs2[bg].bg_y = 0; - } -} - -void SetBgMode(u8 bgMode) -{ - SetBgModeInternal(bgMode); -} - -u16 LoadBgTiles(u8 bg, const void* src, u16 size, u16 destOffset) -{ - u16 tileOffset; - u8 cursor; - - if (GetBgControlAttribute(bg, BG_CTRL_ATTR_PALETTEMODE) == 0) - { - tileOffset = (sGpuBgConfigs2[bg].baseTile + destOffset) * 0x20; - } - else - { - tileOffset = (sGpuBgConfigs2[bg].baseTile + destOffset) * 0x40; - } - - cursor = LoadBgVram(bg, src, size, tileOffset, DISPCNT_MODE_1); - - if (cursor == 0xFF) - { - return -1; - } - - sDmaBusyBitfield[cursor / 0x20] |= (1 << (cursor % 0x20)); - - if (gUnneededFireRedVariable == 1) - { - DummiedOutFireRedLeafGreenTileAllocFunc(bg, tileOffset / 0x20, size / 0x20, 1); - } - - return cursor; -} - -u16 LoadBgTilemap(u8 bg, const void *src, u16 size, u16 destOffset) -{ - u8 cursor = LoadBgVram(bg, src, size, destOffset * 2, DISPCNT_MODE_2); - - if (cursor == 0xFF) - { - return -1; - } - - sDmaBusyBitfield[cursor / 0x20] |= (1 << (cursor % 0x20)); - - return cursor; -} - -u16 Unused_LoadBgPalette(u8 bg, const void *src, u16 size, u16 destOffset) -{ - s8 cursor; - - if (!IsInvalidBg32(bg)) - { - u16 paletteOffset = (sGpuBgConfigs2[bg].basePalette * 0x20) + (destOffset * 2); - cursor = RequestDma3Copy(src, (void*)(paletteOffset + BG_PLTT), size, 0); - - if (cursor == -1) - { - return -1; - } - } - else - { - return -1; - } - - sDmaBusyBitfield[cursor / 0x20] |= (1 << (cursor % 0x20)); - - return (u8)cursor; -} - -bool8 IsDma3ManagerBusyWithBgCopy(void) -{ - int i; - - for (i = 0; i < 0x80; i++) - { - u8 div = i / 0x20; - u8 mod = i % 0x20; - - if ((sDmaBusyBitfield[div] & (1 << mod))) - { - s8 reqSpace = CheckForSpaceForDma3Request(i); - if (reqSpace == -1) - { - return TRUE; - } - - sDmaBusyBitfield[div] &= ~(1 << mod); - } - } - - return FALSE; -} - -void ShowBg(u8 bg) -{ - ShowBgInternal(bg); - SyncBgVisibilityAndMode(); -} - -void HideBg(u8 bg) -{ - HideBgInternal(bg); - SyncBgVisibilityAndMode(); -} - -void SetBgAttribute(u8 bg, u8 attributeId, u8 value) -{ - switch (attributeId) - { - case BG_ATTR_CHARBASEINDEX: - SetBgControlAttributes(bg, value, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); - break; - case BG_ATTR_MAPBASEINDEX: - SetBgControlAttributes(bg, 0xFF, value, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF); - break; - case BG_ATTR_SCREENSIZE: - SetBgControlAttributes(bg, 0xFF, 0xFF, value, 0xFF, 0xFF, 0xFF, 0xFF); - break; - case BG_ATTR_PALETTEMODE: - SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, value, 0xFF, 0xFF, 0xFF); - break; - case BG_ATTR_PRIORITY: - SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, 0xFF, value, 0xFF, 0xFF); - break; - case BG_ATTR_MOSAIC: - SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, value, 0xFF); - break; - case BG_ATTR_WRAPAROUND: - SetBgControlAttributes(bg, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, value); - break; - } -} - -u16 GetBgAttribute(u8 bg, u8 attributeId) -{ - switch (attributeId) - { - case BG_ATTR_CHARBASEINDEX: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_CHARBASEINDEX); - case BG_ATTR_MAPBASEINDEX: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_MAPBASEINDEX); - case BG_ATTR_SCREENSIZE: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); - case BG_ATTR_PALETTEMODE: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_PALETTEMODE); - case BG_ATTR_PRIORITY: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_PRIORITY); - case BG_ATTR_MOSAIC: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_MOSAIC); - case BG_ATTR_WRAPAROUND: - return GetBgControlAttribute(bg, BG_CTRL_ATTR_WRAPAROUND); - case BG_ATTR_METRIC: - switch (GetBgType(bg)) - { - case 0: - return GetBgMetricTextMode(bg, 0) * 0x800; - case 1: - return GetBgMetricAffineMode(bg, 0) * 0x100; - default: - return 0; - } - case BG_ATTR_TYPE: - return GetBgType(bg); - case BG_ATTR_BASETILE: - return sGpuBgConfigs2[bg].baseTile; - default: - return -1; - } -} - -s32 ChangeBgX(u8 bg, s32 value, u8 op) -{ - u8 mode; - u16 temp1; - u16 temp2; - - if (IsInvalidBg32(bg) || !GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - { - return -1; - } - - switch (op) - { - case 0: - default: - sGpuBgConfigs2[bg].bg_x = value; - break; - case 1: - sGpuBgConfigs2[bg].bg_x += value; - break; - case 2: - sGpuBgConfigs2[bg].bg_x -= value; - break; - } - - mode = GetBgMode(); - - switch (bg) - { - case 0: - temp1 = sGpuBgConfigs2[0].bg_x >> 0x8; - SetGpuReg(REG_OFFSET_BG0HOFS, temp1); - break; - case 1: - temp1 = sGpuBgConfigs2[1].bg_x >> 0x8; - SetGpuReg(REG_OFFSET_BG1HOFS, temp1); - break; - case 2: - if (mode == 0) - { - temp1 = sGpuBgConfigs2[2].bg_x >> 0x8; - SetGpuReg(REG_OFFSET_BG2HOFS, temp1); - } - else - { - temp1 = sGpuBgConfigs2[2].bg_x >> 0x10; - temp2 = sGpuBgConfigs2[2].bg_x & 0xFFFF; - SetGpuReg(REG_OFFSET_BG2X_H, temp1); - SetGpuReg(REG_OFFSET_BG2X_L, temp2); - } - break; - case 3: - if (mode == 0) - { - temp1 = sGpuBgConfigs2[3].bg_x >> 0x8; - SetGpuReg(REG_OFFSET_BG3HOFS, temp1); - } - else if (mode == 2) - { - temp1 = sGpuBgConfigs2[3].bg_x >> 0x10; - temp2 = sGpuBgConfigs2[3].bg_x & 0xFFFF; - SetGpuReg(REG_OFFSET_BG3X_H, temp1); - SetGpuReg(REG_OFFSET_BG3X_L, temp2); - } - break; - } - - return sGpuBgConfigs2[bg].bg_x; -} - -s32 GetBgX(u8 bg) -{ - if (IsInvalidBg32(bg)) - return -1; - else if (!GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - return -1; - else - return sGpuBgConfigs2[bg].bg_x; -} - -s32 ChangeBgY(u8 bg, s32 value, u8 op) -{ - u8 mode; - u16 temp1; - u16 temp2; - - if (IsInvalidBg32(bg) || !GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - { - return -1; - } - - switch (op) - { - case 0: - default: - sGpuBgConfigs2[bg].bg_y = value; - break; - case 1: - sGpuBgConfigs2[bg].bg_y += value; - break; - case 2: - sGpuBgConfigs2[bg].bg_y -= value; - break; - } - - mode = GetBgMode(); - - switch (bg) - { - case 0: - temp1 = sGpuBgConfigs2[0].bg_y >> 0x8; - SetGpuReg(REG_OFFSET_BG0VOFS, temp1); - break; - case 1: - temp1 = sGpuBgConfigs2[1].bg_y >> 0x8; - SetGpuReg(REG_OFFSET_BG1VOFS, temp1); - break; - case 2: - if (mode == 0) - { - temp1 = sGpuBgConfigs2[2].bg_y >> 0x8; - SetGpuReg(REG_OFFSET_BG2VOFS, temp1); - } - else - { - temp1 = sGpuBgConfigs2[2].bg_y >> 0x10; - temp2 = sGpuBgConfigs2[2].bg_y & 0xFFFF; - SetGpuReg(REG_OFFSET_BG2Y_H, temp1); - SetGpuReg(REG_OFFSET_BG2Y_L, temp2); - } - break; - case 3: - if (mode == 0) - { - temp1 = sGpuBgConfigs2[3].bg_y >> 0x8; - SetGpuReg(REG_OFFSET_BG3VOFS, temp1); - } - else if (mode == 2) - { - temp1 = sGpuBgConfigs2[3].bg_y >> 0x10; - temp2 = sGpuBgConfigs2[3].bg_y & 0xFFFF; - SetGpuReg(REG_OFFSET_BG3Y_H, temp1); - SetGpuReg(REG_OFFSET_BG3Y_L, temp2); - } - break; - } - - return sGpuBgConfigs2[bg].bg_y; -} - -s32 ChangeBgY_ScreenOff(u8 bg, u32 value, u8 op) -{ - u8 mode; - u16 temp1; - u16 temp2; - - if (IsInvalidBg32(bg) || !GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - { - return -1; - } - - switch (op) - { - case 0: - default: - sGpuBgConfigs2[bg].bg_y = value; - break; - case 1: - sGpuBgConfigs2[bg].bg_y += value; - break; - case 2: - sGpuBgConfigs2[bg].bg_y -= value; - break; - } - - mode = GetBgMode(); - - switch (bg) - { - case 0: - temp1 = sGpuBgConfigs2[0].bg_y >> 0x8; - SetGpuReg_ForcedBlank(REG_OFFSET_BG0VOFS, temp1); - break; - case 1: - temp1 = sGpuBgConfigs2[1].bg_y >> 0x8; - SetGpuReg_ForcedBlank(REG_OFFSET_BG1VOFS, temp1); - break; - case 2: - if (mode == 0) - { - temp1 = sGpuBgConfigs2[2].bg_y >> 0x8; - SetGpuReg_ForcedBlank(REG_OFFSET_BG2VOFS, temp1); - - } - else - { - temp1 = sGpuBgConfigs2[2].bg_y >> 0x10; - temp2 = sGpuBgConfigs2[2].bg_y & 0xFFFF; - SetGpuReg_ForcedBlank(REG_OFFSET_BG2Y_H, temp1); - SetGpuReg_ForcedBlank(REG_OFFSET_BG2Y_L, temp2); - } - break; - case 3: - if (mode == 0) - { - temp1 = sGpuBgConfigs2[3].bg_y >> 0x8; - SetGpuReg_ForcedBlank(REG_OFFSET_BG3VOFS, temp1); - } - else if (mode == 2) - { - temp1 = sGpuBgConfigs2[3].bg_y >> 0x10; - temp2 = sGpuBgConfigs2[3].bg_y & 0xFFFF; - SetGpuReg_ForcedBlank(REG_OFFSET_BG3Y_H, temp1); - SetGpuReg_ForcedBlank(REG_OFFSET_BG3Y_L, temp2); - } - break; - } - - return sGpuBgConfigs2[bg].bg_y; -} - -s32 GetBgY(u8 bg) -{ - if (IsInvalidBg32(bg)) - return -1; - else if (!GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - return -1; - else - return sGpuBgConfigs2[bg].bg_y; -} - -void SetBgAffine(u8 bg, s32 srcCenterX, s32 srcCenterY, s16 dispCenterX, s16 dispCenterY, s16 scaleX, s16 scaleY, u16 rotationAngle) -{ - SetBgAffineInternal(bg, srcCenterX, srcCenterY, dispCenterX, dispCenterY, scaleX, scaleY, rotationAngle); -} - -u8 Unused_AdjustBgMosaic(u8 a1, u8 a2) -{ - u16 result = GetGpuReg(REG_OFFSET_MOSAIC); - s16 test1 = result & 0xF; - s16 test2 = (result >> 4) & 0xF; - - result &= 0xFF00; - - switch (a2) - { - case 0: - default: - test1 = a1 & 0xF; - test2 = a1 >> 0x4; - break; - case 1: - test1 = a1 & 0xF; - break; - case 2: - if ((test1 + a1) > 0xF) - { - test1 = 0xF; - } - else - { - test1 += a1; - } - break; - case 3: - if ((test1 - a1) < 0) - { - test1 = 0x0; - } - else - { - test1 -= a1; - } - break; - case 4: - test2 = a1 & 0xF; - break; - case 5: - if ((test2 + a1) > 0xF) - { - test2 = 0xF; - } - else - { - test2 += a1; - } - break; - case 6: - if ((test2 - a1) < 0) - { - test2 = 0x0; - } - else - { - test2 -= a1; - } - break; - } - - result |= ((test2 << 0x4) & 0xF0); - result |= (test1 & 0xF); - - SetGpuReg(REG_OFFSET_MOSAIC, result); - - return result; -} - -void SetBgTilemapBuffer(u8 bg, void *tilemap) -{ - if (!IsInvalidBg32(bg) && GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - { - sGpuBgConfigs2[bg].tilemap = tilemap; - } -} - -void UnsetBgTilemapBuffer(u8 bg) -{ - if (!IsInvalidBg32(bg) && GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - { - sGpuBgConfigs2[bg].tilemap = NULL; - } -} - -void* GetBgTilemapBuffer(u8 bg) -{ - if (IsInvalidBg32(bg)) - return NULL; - else if (!GetBgControlAttribute(bg, BG_CTRL_ATTR_VISIBLE)) - return NULL; - else - return sGpuBgConfigs2[bg].tilemap; -} - -void CopyToBgTilemapBuffer(u8 bg, const void *src, u16 mode, u16 destOffset) -{ - if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) - { - if (mode != 0) - CpuCopy16(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2)), mode); - else - LZ77UnCompWram(src, (void *)(sGpuBgConfigs2[bg].tilemap + (destOffset * 2))); - } -} - -void CopyBgTilemapBufferToVram(u8 bg) -{ - u16 sizeToLoad; - - if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) - { - switch (GetBgType(bg)) - { - case 0: - sizeToLoad = GetBgMetricTextMode(bg, 0) * 0x800; - break; - case 1: - sizeToLoad = GetBgMetricAffineMode(bg, 0) * 0x100; - break; - default: - sizeToLoad = 0; - break; - } - LoadBgVram(bg, sGpuBgConfigs2[bg].tilemap, sizeToLoad, 0, 2); - } -} - -void CopyToBgTilemapBufferRect(u8 bg, const void* src, u8 destX, u8 destY, u8 width, u8 height) -{ - u16 destX16; - u16 destY16; - u16 mode; - - if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) - { - switch (GetBgType(bg)) - { - case 0: - { - const u16 * srcCopy = src; - for (destY16 = destY; destY16 < (destY + height); destY16++) - { - for (destX16 = destX; destX16 < (destX + width); destX16++) - { - ((u16*)sGpuBgConfigs2[bg].tilemap)[((destY16 * 0x20) + destX16)] = *srcCopy++; - } - } - break; - } - case 1: - { - const u8 * srcCopy = src; - mode = GetBgMetricAffineMode(bg, 0x1); - for (destY16 = destY; destY16 < (destY + height); destY16++) - { - for (destX16 = destX; destX16 < (destX + width); destX16++) - { - ((u8*)sGpuBgConfigs2[bg].tilemap)[((destY16 * mode) + destX16)] = *srcCopy++; - } - } - break; - } - } - } -} - -void CopyToBgTilemapBufferRect_ChangePalette(u8 bg, const void *src, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, u8 palette) -{ - CopyRectToBgTilemapBufferRect(bg, src, 0, 0, rectWidth, rectHeight, destX, destY, rectWidth, rectHeight, palette, 0, 0); -} - -void CopyRectToBgTilemapBufferRect(u8 bg, const void *src, u8 srcX, u8 srcY, u8 srcWidth, u8 unused, u8 srcHeight, u8 destX, u8 destY, u8 rectWidth, u8 rectHeight, s16 palette1, s16 tileOffset) -{ - u16 screenWidth, screenHeight, screenSize; - u16 var; - const void *srcPtr; - u16 i, j; - - if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) - { - screenSize = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); - screenWidth = GetBgMetricTextMode(bg, 0x1) * 0x20; - screenHeight = GetBgMetricTextMode(bg, 0x2) * 0x20; - switch (GetBgType(bg)) - { - case 0: - srcPtr = src + ((srcY * srcWidth) + srcX) * 2; - for (i = destX; i < (destX + rectWidth); i++) - { - for (j = srcHeight; j < (srcHeight + destY); j++) - { - u16 index = GetTileMapIndexFromCoords(j, i, screenSize, screenWidth, screenHeight); - CopyTileMapEntry(srcPtr, sGpuBgConfigs2[bg].tilemap + (index * 2), rectHeight, palette1, tileOffset); - srcPtr += 2; - } - srcPtr += (srcWidth - destY) * 2; - } - break; - case 1: - srcPtr = src + ((srcY * srcWidth) + srcX); - var = GetBgMetricAffineMode(bg, 0x1); - for (i = destX; i < (destX + rectWidth); i++) - { - for (j = srcHeight; j < (srcHeight + destY); j++) - { - *(u8*)(sGpuBgConfigs2[bg].tilemap + ((var * i) + j)) = *(u8*)(srcPtr) + palette1; - srcPtr++; - } - srcPtr += (srcWidth - destY); - } - break; - } - } -} - -void FillBgTilemapBufferRect_Palette0(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height) -{ - u16 x16; - u16 y16; - u16 mode; - - if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) - { - switch (GetBgType(bg)) - { - case 0: - for (y16 = y; y16 < (y + height); y16++) - { - for (x16 = x; x16 < (x + width); x16++) - { - ((u16*)sGpuBgConfigs2[bg].tilemap)[((y16 * 0x20) + x16)] = tileNum; - } - } - break; - case 1: - mode = GetBgMetricAffineMode(bg, 0x1); - for (y16 = y; y16 < (y + height); y16++) - { - for (x16 = x; x16 < (x + width); x16++) - { - ((u8*)sGpuBgConfigs2[bg].tilemap)[((y16 * mode) + x16)] = tileNum; - } - } - break; - } - } -} - -void FillBgTilemapBufferRect(u8 bg, u16 tileNum, u8 x, u8 y, u8 width, u8 height, u8 palette) -{ - WriteSequenceToBgTilemapBuffer(bg, tileNum, x, y, width, height, palette, 0); -} - -void WriteSequenceToBgTilemapBuffer(u8 bg, u16 firstTileNum, u8 x, u8 y, u8 width, u8 height, u8 paletteSlot, s16 tileNumDelta) -{ - u16 mode; - u16 mode2; - u16 attribute; - u16 mode3; - u16 x16, y16; - - if (!IsInvalidBg32(bg) && !IsTileMapOutsideWram(bg)) - { - attribute = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); - mode = GetBgMetricTextMode(bg, 0x1) * 0x20; - mode2 = GetBgMetricTextMode(bg, 0x2) * 0x20; - switch (GetBgType(bg)) - { - case 0: - for (y16 = y; y16 < (y + height); y16++) - { - for (x16 = x; x16 < (x + width); x16++) - { - CopyTileMapEntry(&firstTileNum, &((u16*)sGpuBgConfigs2[bg].tilemap)[(u16)GetTileMapIndexFromCoords(x16, y16, attribute, mode, mode2)], paletteSlot, 0, 0); - firstTileNum = (firstTileNum & (METATILE_COLLISION_MASK | METATILE_ELEVATION_MASK)) + ((firstTileNum + tileNumDelta) & METATILE_ID_MASK); - } - } - break; - case 1: - mode3 = GetBgMetricAffineMode(bg, 0x1); - for (y16 = y; y16 < (y + height); y16++) - { - for (x16 = x; x16 < (x + width); x16++) - { - ((u8*)sGpuBgConfigs2[bg].tilemap)[(y16 * mode3) + x16] = firstTileNum; - firstTileNum = (firstTileNum & (METATILE_COLLISION_MASK | METATILE_ELEVATION_MASK)) + ((firstTileNum + tileNumDelta) & METATILE_ID_MASK); - } - } - break; - } - } -} - -u16 GetBgMetricTextMode(u8 bg, u8 whichMetric) -{ - u8 screenSize = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); - - switch (whichMetric) - { - case 0: - switch (screenSize) - { - case 0: - return 1; - case 1: - case 2: - return 2; - case 3: - return 4; - } - break; - case 1: - switch (screenSize) - { - case 0: - return 1; - case 1: - return 2; - case 2: - return 1; - case 3: - return 2; - } - break; - case 2: - switch (screenSize) - { - case 0: - case 1: - return 1; - case 2: - case 3: - return 2; - } - break; - } - return 0; -} - -u32 GetBgMetricAffineMode(u8 bg, u8 whichMetric) -{ - u8 screenSize = GetBgControlAttribute(bg, BG_CTRL_ATTR_SCREENSIZE); - - switch (whichMetric) - { - case 0: - switch (screenSize) - { - case 0: - return 0x1; - case 1: - return 0x4; - case 2: - return 0x10; - case 3: - return 0x40; - } - break; - case 1: - case 2: - return 0x10 << screenSize; - } - return 0; -} - -u32 GetTileMapIndexFromCoords(s32 x, s32 y, s32 screenSize, u32 screenWidth, u32 screenHeight) -{ - x = x & (screenWidth - 1); - y = y & (screenHeight - 1); - - switch (screenSize) - { - case 0: - case 2: - break; - case 3: - if (y >= 0x20) - y += 0x20; - case 1: - if (x >= 0x20) - { - x -= 0x20; - y += 0x20; - } - break; - } - return (y * 0x20) + x; -} - -#ifdef NONMATCHING // This one has some weird switch statement cases that refuse to cooperate -void CopyTileMapEntry(const u16 *src, u16 *dest, s32 palette1, s32 tileOffset, s32 palette2) -{ - u16 var; - switch (palette1) - { - case 0 ... 16: - if (palette1 != 16) - var = ((*src + tileOffset) & 0xFFF) + ((palette1 + palette2) << 12); - else - var = ((*dest & 0xFC00) + (palette2 << 12)) | ((*src + tileOffset) & 0x3FF); - break; - default: - var = *src + tileOffset + (palette2 << 12); - break; - } - - *dest = var; -} -#else -NAKED -void CopyTileMapEntry(const u16 *src, u16 *dest, s32 palette1, s32 tileOffset, s32 palette2) -{ - asm("push {r4-r6,lr}\n\ - add r4, r0, #0\n\ - add r6, r1, #0\n\ - ldr r5, [sp, #0x10]\n\ - cmp r2, #0x10\n\ - beq _08002B14\n\ - cmp r2, #0x10\n\ - bgt _08002B34\n\ - cmp r2, #0\n\ - blt _08002B34\n\ - ldrh r0, [r4]\n\ - add r0, r3\n\ - ldr r3, =0x00000fff\n\ - add r1, r3, #0\n\ - and r0, r1\n\ - add r1, r2, r5\n\ - lsl r1, #12\n\ - b _08002B3A\n\ - .pool\n\ -_08002B14:\n\ - ldrh r1, [r6]\n\ - mov r0, #0xFC\n\ - lsl r0, #8\n\ - and r1, r0\n\ - lsl r2, r5, #12\n\ - add r2, r1, r2\n\ - ldrh r0, [r4]\n\ - add r0, r3\n\ - ldr r3, =0x000003ff\n\ - add r1, r3, #0\n\ - and r0, r1\n\ - orr r0, r2\n\ - b _08002B3C\n\ - .pool\n\ -_08002B34:\n\ - ldrh r0, [r4]\n\ - add r0, r3\n\ - lsl r1, r5, #12\n\ -_08002B3A:\n\ - add r0, r1\n\ -_08002B3C:\n\ - lsl r0, #16\n\ - lsr r1, r0, #16\n\ - strh r1, [r6]\n\ - pop {r4-r6}\n\ - pop {r0}\n\ - bx r0\n"); -} -#endif // NONMATCHING - -u32 GetBgType(u8 bg) -{ - u8 mode = GetBgMode(); - - switch (bg) - { - case 0: - case 1: - switch (mode) - { - case 0: - case 1: - return 0; - } - break; - case 2: - switch (mode) - { - case 0: - return 0; - case 1: - case 2: - return 1; - } - break; - case 3: - switch (mode) - { - case 0: - return 0; - case 2: - return 1; - } - break; - } - - return 0xFFFF; -} - -bool32 IsInvalidBg32(u8 bg) -{ - if (bg > 3) - return TRUE; - else - return FALSE; -} - -bool32 IsTileMapOutsideWram(u8 bg) -{ - if (sGpuBgConfigs2[bg].tilemap > (void*)IWRAM_END) - return TRUE; - else if (sGpuBgConfigs2[bg].tilemap == NULL) - return TRUE; - else - return FALSE; -} diff --git a/src/blit.c b/src/blit.c deleted file mode 100644 index 26a63fe9b..000000000 --- a/src/blit.c +++ /dev/null @@ -1,209 +0,0 @@ -#include "global.h" -#include "blit.h" - -void BlitBitmapRect4BitWithoutColorKey(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height) -{ - BlitBitmapRect4Bit(src, dst, srcX, srcY, dstX, dstY, width, height, 0xFF); -} - -void BlitBitmapRect4Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey) -{ - s32 xEnd; - s32 yEnd; - s32 multiplierSrcY; - s32 multiplierDstY; - s32 loopSrcY, loopDstY; - s32 loopSrcX, loopDstX; - const u8 *pixelsSrc; - u8 *pixelsDst; - s32 toOrr; - s32 toAnd; - s32 toShift; - - if (dst->width - dstX < width) - xEnd = (dst->width - dstX) + srcX; - else - xEnd = srcX + width; - - if (dst->height - dstY < height) - yEnd = (dst->height - dstY) + srcY; - else - yEnd = height + srcY; - - multiplierSrcY = (src->width + (src->width & 7)) >> 3; - multiplierDstY = (dst->width + (dst->width & 7)) >> 3; - - if (colorKey == 0xFF) - { - for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) - { - for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) - { - pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B); - pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B); - toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF); - toShift = ((loopDstX & 1) << 2); - toOrr <<= toShift; - toAnd = 0xF0 >> (toShift); - *pixelsDst = toOrr | (*pixelsDst & toAnd); - } - } - } - else - { - for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) - { - for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) - { - pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1B); - pixelsDst = dst->pixels + ((loopDstX >> 1) & 3) + ((loopDstX >> 3) << 5) + (((loopDstY >> 3) * multiplierDstY) << 5) + ((u32)(loopDstY << 0x1d) >> 0x1B); - toOrr = ((*pixelsSrc >> ((loopSrcX & 1) << 2)) & 0xF); - if (toOrr != colorKey) - { - toShift = ((loopDstX & 1) << 2); - toOrr <<= toShift; - toAnd = 0xF0 >> (toShift); - *pixelsDst = toOrr | (*pixelsDst & toAnd); - } - } - } - } -} - -void FillBitmapRect4Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue) -{ - s32 xEnd; - s32 yEnd; - s32 multiplierY; - s32 loopX, loopY; - s32 toOrr1, toOrr2; - - xEnd = x + width; - if (xEnd > surface->width) - xEnd = surface->width; - - yEnd = y + height; - if (yEnd > surface->height) - yEnd = surface->height; - - multiplierY = (surface->width + (surface->width & 7)) >> 3; - toOrr1 = (u32)(fillValue << 0x1C) >> 0x18; - toOrr2 = (fillValue & 0xF); - - for (loopY = y; loopY < yEnd; loopY++) - { - for (loopX = x; loopX < xEnd; loopX++) - { - u8 *pixels = surface->pixels + ((loopX >> 1) & 3) + ((loopX >> 3) << 5) + (((loopY >> 3) * multiplierY) << 5) + ((u32)(loopY << 0x1d) >> 0x1B); - if ((loopX << 0x1F) != 0) - *pixels = toOrr1 | (*pixels & 0xF); - else - *pixels = toOrr2 | (*pixels & 0xF0); - } - } -} - -void BlitBitmapRect4BitTo8Bit(const struct Bitmap *src, struct Bitmap *dst, u16 srcX, u16 srcY, u16 dstX, u16 dstY, u16 width, u16 height, u8 colorKey, u8 paletteOffset) -{ - s32 palOffsetBits; - s32 xEnd; - s32 yEnd; - s32 multiplierSrcY; - s32 multiplierDstY; - s32 loopSrcY, loopDstY; - s32 loopSrcX, loopDstX; - const u8 *pixelsSrc; - u8 *pixelsDst; - s32 colorKeyBits; - - palOffsetBits = (u32)(paletteOffset << 0x1C) >> 0x18; - colorKeyBits = (u32)(colorKey << 0x1C) >> 0x18; - - if (dst->width - dstX < width) - xEnd = (dst->width - dstX) + srcX; - else - xEnd = width + srcX; - - if (dst->height - dstY < height) - yEnd = (srcY + dst->height) - dstY; - else - yEnd = srcY + height; - - multiplierSrcY = (src->width + (src->width & 7)) >> 3; - multiplierDstY = (dst->width + (dst->width & 7)) >> 3; - - if (colorKey == 0xFF) - { - for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) - { - pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); - for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) - { - pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a); - if (loopSrcX & 1) - { - *pixelsDst = palOffsetBits + (*pixelsSrc >> 4); - } - else - { - pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); - *pixelsDst = palOffsetBits + (*pixelsSrc & 0xF); - } - } - } - } - else - { - for (loopSrcY = srcY, loopDstY = dstY; loopSrcY < yEnd; loopSrcY++, loopDstY++) - { - pixelsSrc = src->pixels + ((srcX >> 1) & 3) + ((srcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); - for (loopSrcX = srcX, loopDstX = dstX; loopSrcX < xEnd; loopSrcX++, loopDstX++) - { - if (loopSrcX & 1) - { - if ((*pixelsSrc & 0xF0) != colorKeyBits) - { - pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a); - *pixelsDst = palOffsetBits + (*pixelsSrc >> 4); - } - } - else - { - pixelsSrc = src->pixels + ((loopSrcX >> 1) & 3) + ((loopSrcX >> 3) << 5) + (((loopSrcY >> 3) * multiplierSrcY) << 5) + ((u32)(loopSrcY << 0x1d) >> 0x1b); - if ((*pixelsSrc & 0xF) != colorKey) - { - pixelsDst = dst->pixels + (loopDstX & 7) + ((loopDstX >> 3) << 6) + (((loopDstY >> 3) * multiplierDstY) << 6) + ((u32)(loopDstY << 0x1d) >> 0x1a); - *pixelsDst = palOffsetBits + (*pixelsSrc & 0xF); - } - } - } - } - } -} - -void FillBitmapRect8Bit(struct Bitmap *surface, u16 x, u16 y, u16 width, u16 height, u8 fillValue) -{ - s32 xEnd; - s32 yEnd; - s32 multiplierY; - s32 loopX, loopY; - - xEnd = x + width; - if (xEnd > surface->width) - xEnd = surface->width; - - yEnd = y + height; - if (yEnd > surface->height) - yEnd = surface->height; - - multiplierY = (surface->width + (surface->width & 7)) >> 3; - - for (loopY = y; loopY < yEnd; loopY++) - { - for (loopX = x; loopX < xEnd; loopX++) - { - u8 *pixels = surface->pixels + (loopX & 7) + ((loopX >> 3) << 6) + (((loopY >> 3) * multiplierY) << 6) + ((u32)(loopY << 0x1d) >> 0x1a); - *pixels = fillValue; - } - } -} diff --git a/src/cable_car.c b/src/cable_car.c index adeee2647..c6e7e724e 100644 --- a/src/cable_car.c +++ b/src/cable_car.c @@ -6,7 +6,7 @@ #include "field_weather.h" #include "gpu_regs.h" #include "graphics.h" -#include "alloc.h" +#include "malloc.h" #include "main.h" #include "menu.h" #include "overworld.h" diff --git a/src/contest.c b/src/contest.c index 79c1616c2..e9a2f456f 100644 --- a/src/contest.c +++ b/src/contest.c @@ -1,7 +1,7 @@ #include "global.h" #include "gpu_regs.h" #include "bg.h" -#include "alloc.h" +#include "malloc.h" #include "constants/items.h" #include "constants/event_objects.h" #include "constants/moves.h" diff --git a/src/contest_link_80F57C4.c b/src/contest_link_80F57C4.c index a0a29f0ad..54b2326d0 100644 --- a/src/contest_link_80F57C4.c +++ b/src/contest_link_80F57C4.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_gfx_sfx_util.h" #include "bg.h" diff --git a/src/contest_painting.c b/src/contest_painting.c index 1b95e1ed9..2b36d5e38 100644 --- a/src/contest_painting.c +++ b/src/contest_painting.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_gfx_sfx_util.h" #include "bg.h" diff --git a/src/credits.c b/src/credits.c index 44fa2002c..e2c125d3f 100644 --- a/src/credits.c +++ b/src/credits.c @@ -3,7 +3,7 @@ #include "main.h" #include "task.h" #include "bg.h" -#include "alloc.h" +#include "malloc.h" #include "window.h" #include "text.h" #include "menu.h" diff --git a/src/data.c b/src/data.c index 7aebd63ed..8d6640223 100644 --- a/src/data.c +++ b/src/data.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "data.h" #include "graphics.h" diff --git a/src/decompress.c b/src/decompress.c index 46e4ef614..5e85e065f 100644 --- a/src/decompress.c +++ b/src/decompress.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "data.h" #include "decompress.h" #include "pokemon.h" diff --git a/src/decoration.c b/src/decoration.c index 974859c88..3e6428501 100644 --- a/src/decoration.c +++ b/src/decoration.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "decoration.h" #include "decoration_inventory.h" diff --git a/src/dewford_trend.c b/src/dewford_trend.c index ee310af04..57a97d8f3 100644 --- a/src/dewford_trend.c +++ b/src/dewford_trend.c @@ -3,7 +3,7 @@ #include "easy_chat.h" #include "event_data.h" #include "link.h" -#include "alloc.h" +#include "malloc.h" #include "random.h" #include "text.h" #include "tv.h" diff --git a/src/diploma.c b/src/diploma.c index 8e0901fa8..3c03c057f 100644 --- a/src/diploma.c +++ b/src/diploma.c @@ -5,7 +5,7 @@ #include "gpu_regs.h" #include "scanline_effect.h" #include "task.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "bg.h" #include "window.h" diff --git a/src/dma3_manager.c b/src/dma3_manager.c deleted file mode 100644 index 43744883f..000000000 --- a/src/dma3_manager.c +++ /dev/null @@ -1,181 +0,0 @@ -#include "global.h" -#include "dma3.h" - -#define MAX_DMA_REQUESTS 128 - -#define DMA_REQUEST_COPY32 1 -#define DMA_REQUEST_FILL32 2 -#define DMA_REQUEST_COPY16 3 -#define DMA_REQUEST_FILL16 4 - -BSS_DATA struct -{ - const u8 *src; - u8 *dest; - u16 size; - u16 mode; - u32 value; -} gDma3Requests[MAX_DMA_REQUESTS]; - -static volatile bool8 gDma3ManagerLocked; -static u8 gDma3RequestCursor; - -void ClearDma3Requests(void) -{ - int i; - - gDma3ManagerLocked = TRUE; - gDma3RequestCursor = 0; - - for (i = 0; i < MAX_DMA_REQUESTS; i++) - { - gDma3Requests[i].size = 0; - gDma3Requests[i].src = NULL; - gDma3Requests[i].dest = NULL; - } - - gDma3ManagerLocked = FALSE; -} - -void ProcessDma3Requests(void) -{ - u16 bytesTransferred; - - if (gDma3ManagerLocked) - return; - - bytesTransferred = 0; - - // as long as there are DMA requests to process (unless size or vblank is an issue), do not exit - while (gDma3Requests[gDma3RequestCursor].size != 0) - { - bytesTransferred += gDma3Requests[gDma3RequestCursor].size; - - if (bytesTransferred > 40 * 1024) - return; // don't transfer more than 40 KiB - if (*(u8 *)REG_ADDR_VCOUNT > 224) - return; // we're about to leave vblank, stop - - switch (gDma3Requests[gDma3RequestCursor].mode) - { - case DMA_REQUEST_COPY32: // regular 32-bit copy - Dma3CopyLarge32_(gDma3Requests[gDma3RequestCursor].src, - gDma3Requests[gDma3RequestCursor].dest, - gDma3Requests[gDma3RequestCursor].size); - break; - case DMA_REQUEST_FILL32: // repeat a single 32-bit value across RAM - Dma3FillLarge32_(gDma3Requests[gDma3RequestCursor].value, - gDma3Requests[gDma3RequestCursor].dest, - gDma3Requests[gDma3RequestCursor].size); - break; - case DMA_REQUEST_COPY16: // regular 16-bit copy - Dma3CopyLarge16_(gDma3Requests[gDma3RequestCursor].src, - gDma3Requests[gDma3RequestCursor].dest, - gDma3Requests[gDma3RequestCursor].size); - break; - case DMA_REQUEST_FILL16: // repeat a single 16-bit value across RAM - Dma3FillLarge16_(gDma3Requests[gDma3RequestCursor].value, - gDma3Requests[gDma3RequestCursor].dest, - gDma3Requests[gDma3RequestCursor].size); - break; - } - - // Free the request - gDma3Requests[gDma3RequestCursor].src = NULL; - gDma3Requests[gDma3RequestCursor].dest = NULL; - gDma3Requests[gDma3RequestCursor].size = 0; - gDma3Requests[gDma3RequestCursor].mode = 0; - gDma3Requests[gDma3RequestCursor].value = 0; - gDma3RequestCursor++; - - if (gDma3RequestCursor >= MAX_DMA_REQUESTS) // loop back to the first DMA request - gDma3RequestCursor = 0; - } -} - -s16 RequestDma3Copy(const void *src, void *dest, u16 size, u8 mode) -{ - int cursor; - int i = 0; - - gDma3ManagerLocked = TRUE; - cursor = gDma3RequestCursor; - - while (i < MAX_DMA_REQUESTS) - { - if (gDma3Requests[cursor].size == 0) // an empty request was found. - { - gDma3Requests[cursor].src = src; - gDma3Requests[cursor].dest = dest; - gDma3Requests[cursor].size = size; - - if (mode == 1) - gDma3Requests[cursor].mode = DMA_REQUEST_COPY32; - else - gDma3Requests[cursor].mode = DMA_REQUEST_COPY16; - - gDma3ManagerLocked = FALSE; - return cursor; - } - if (++cursor >= MAX_DMA_REQUESTS) // loop back to start. - cursor = 0; - i++; - } - gDma3ManagerLocked = FALSE; - return -1; // no free DMA request was found -} - -s16 RequestDma3Fill(s32 value, void *dest, u16 size, u8 mode) -{ - int cursor; - int i = 0; - - cursor = gDma3RequestCursor; - gDma3ManagerLocked = TRUE; - - while (i < MAX_DMA_REQUESTS) - { - if (gDma3Requests[cursor].size == 0) // an empty request was found. - { - gDma3Requests[cursor].dest = dest; - gDma3Requests[cursor].size = size; - gDma3Requests[cursor].mode = mode; - gDma3Requests[cursor].value = value; - - if(mode == 1) - gDma3Requests[cursor].mode = DMA_REQUEST_FILL32; - else - gDma3Requests[cursor].mode = DMA_REQUEST_FILL16; - - gDma3ManagerLocked = FALSE; - return cursor; - } - if (++cursor >= MAX_DMA_REQUESTS) // loop back to start. - cursor = 0; - i++; - } - gDma3ManagerLocked = FALSE; - return -1; // no free DMA request was found -} - -s16 CheckForSpaceForDma3Request(s16 index) -{ - int i = 0; - - if (index == -1) // check if all requests are free - { - while (i < MAX_DMA_REQUESTS) - { - if (gDma3Requests[i].size != 0) - return -1; - i++; - } - return 0; - } - else // check the specified request - { - if (gDma3Requests[index].size != 0) - return -1; - return 0; - } -} diff --git a/src/dodrio_berry_picking.c b/src/dodrio_berry_picking.c index 714b10ef6..4b636e2b9 100644 --- a/src/dodrio_berry_picking.c +++ b/src/dodrio_berry_picking.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "dodrio_berry_picking.h" #include "dynamic_placeholder_text_util.h" diff --git a/src/easy_chat.c b/src/easy_chat.c index ba54de783..f4ac92d06 100644 --- a/src/easy_chat.c +++ b/src/easy_chat.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bard_music.h" #include "bg.h" #include "data.h" diff --git a/src/egg_hatch.c b/src/egg_hatch.c index 5e16f1479..c796a0423 100644 --- a/src/egg_hatch.c +++ b/src/egg_hatch.c @@ -17,7 +17,7 @@ #include "menu.h" #include "trig.h" #include "random.h" -#include "alloc.h" +#include "malloc.h" #include "dma3.h" #include "gpu_regs.h" #include "bg.h" diff --git a/src/ereader_helpers.c b/src/ereader_helpers.c index f184dea82..7f0887790 100755 --- a/src/ereader_helpers.c +++ b/src/ereader_helpers.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "ereader_helpers.h" #include "link.h" diff --git a/src/ereader_screen.c b/src/ereader_screen.c index 13e964224..0e865558a 100755 --- a/src/ereader_screen.c +++ b/src/ereader_screen.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "ereader_helpers.h" #include "link.h" diff --git a/src/event_object_movement.c b/src/event_object_movement.c index 75f758a12..c493c6d87 100644 --- a/src/event_object_movement.c +++ b/src/event_object_movement.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle_pyramid.h" #include "berry.h" #include "decoration.h" diff --git a/src/evolution_scene.c b/src/evolution_scene.c index 96ca2ed1c..5ba979453 100644 --- a/src/evolution_scene.c +++ b/src/evolution_scene.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_message.h" #include "bg.h" diff --git a/src/field_region_map.c b/src/field_region_map.c index ac1b26154..184c49b40 100644 --- a/src/field_region_map.c +++ b/src/field_region_map.c @@ -3,7 +3,7 @@ #include "gpu_regs.h" #include "international_string_util.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "palette.h" #include "region_map.h" diff --git a/src/field_specials.c b/src/field_specials.c index 99e8e93a9..5af0fd88a 100644 --- a/src/field_specials.c +++ b/src/field_specials.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_tower.h" #include "cable_club.h" diff --git a/src/fldeff_cut.c b/src/fldeff_cut.c index 3115ff231..00eed7624 100644 --- a/src/fldeff_cut.c +++ b/src/fldeff_cut.c @@ -7,7 +7,7 @@ #include "field_player_avatar.h" #include "fieldmap.h" #include "fldeff.h" -#include "alloc.h" +#include "malloc.h" #include "metatile_behavior.h" #include "overworld.h" #include "party_menu.h" diff --git a/src/frontier_pass.c b/src/frontier_pass.c index fff93c3c7..a4ef500d7 100644 --- a/src/frontier_pass.c +++ b/src/frontier_pass.c @@ -5,7 +5,7 @@ #include "battle_anim.h" #include "event_data.h" #include "recorded_battle.h" -#include "alloc.h" +#include "malloc.h" #include "sprite.h" #include "scanline_effect.h" #include "text_window.h" diff --git a/src/frontier_util.c b/src/frontier_util.c index 536e23e30..69db5f762 100644 --- a/src/frontier_util.c +++ b/src/frontier_util.c @@ -24,7 +24,7 @@ #include "data.h" #include "record_mixing.h" #include "strings.h" -#include "alloc.h" +#include "malloc.h" #include "save.h" #include "load_save.h" #include "battle_dome.h" diff --git a/src/gpu_regs.c b/src/gpu_regs.c deleted file mode 100644 index 3bcc4fd93..000000000 --- a/src/gpu_regs.c +++ /dev/null @@ -1,195 +0,0 @@ -#include "global.h" -#include "gpu_regs.h" - -#define GPU_REG_BUF_SIZE 0x60 - -#define GPU_REG_BUF(offset) (*(u16 *)(&sGpuRegBuffer[offset])) -#define GPU_REG(offset) (*(vu16 *)(REG_BASE + offset)) - -#define EMPTY_SLOT 0xFF - -static u8 sGpuRegBuffer[GPU_REG_BUF_SIZE]; -static u8 sGpuRegWaitingList[GPU_REG_BUF_SIZE]; -static volatile bool8 sGpuRegBufferLocked; -static volatile bool8 sShouldSyncRegIE; -static vu16 sRegIE; - -static void CopyBufferedValueToGpuReg(u8 regOffset); -static void SyncRegIE(void); -static void UpdateRegDispstatIntrBits(u16 regIE); - -void InitGpuRegManager(void) -{ - s32 i; - - for (i = 0; i < GPU_REG_BUF_SIZE; i++) - { - sGpuRegBuffer[i] = 0; - sGpuRegWaitingList[i] = EMPTY_SLOT; - } - - sGpuRegBufferLocked = FALSE; - sShouldSyncRegIE = FALSE; - sRegIE = 0; -} - -static void CopyBufferedValueToGpuReg(u8 regOffset) -{ - if (regOffset == REG_OFFSET_DISPSTAT) - { - REG_DISPSTAT &= ~(DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR); - REG_DISPSTAT |= GPU_REG_BUF(REG_OFFSET_DISPSTAT); - } - else - { - GPU_REG(regOffset) = GPU_REG_BUF(regOffset); - } -} - -void CopyBufferedValuesToGpuRegs(void) -{ - if (!sGpuRegBufferLocked) - { - s32 i; - - for (i = 0; i < GPU_REG_BUF_SIZE; i++) - { - u8 regOffset = sGpuRegWaitingList[i]; - if (regOffset == EMPTY_SLOT) - return; - CopyBufferedValueToGpuReg(regOffset); - sGpuRegWaitingList[i] = EMPTY_SLOT; - } - } -} - -void SetGpuReg(u8 regOffset, u16 value) -{ - if (regOffset < GPU_REG_BUF_SIZE) - { - u16 vcount; - - GPU_REG_BUF(regOffset) = value; - vcount = REG_VCOUNT & 0xFF; - - if ((vcount >= 161 && vcount <= 225) || (REG_DISPCNT & DISPCNT_FORCED_BLANK)) - { - CopyBufferedValueToGpuReg(regOffset); - } - else - { - s32 i; - - sGpuRegBufferLocked = TRUE; - - for (i = 0; i < GPU_REG_BUF_SIZE && sGpuRegWaitingList[i] != EMPTY_SLOT; i++) - { - if (sGpuRegWaitingList[i] == regOffset) - { - sGpuRegBufferLocked = FALSE; - return; - } - } - - sGpuRegWaitingList[i] = regOffset; - sGpuRegBufferLocked = FALSE; - } - } -} - -void SetGpuReg_ForcedBlank(u8 regOffset, u16 value) -{ - if (regOffset < GPU_REG_BUF_SIZE) - { - GPU_REG_BUF(regOffset) = value; - - if (REG_DISPCNT & DISPCNT_FORCED_BLANK) - { - CopyBufferedValueToGpuReg(regOffset); - } - else - { - s32 i; - - sGpuRegBufferLocked = TRUE; - - for (i = 0; i < GPU_REG_BUF_SIZE && sGpuRegWaitingList[i] != EMPTY_SLOT; i++) - { - if (sGpuRegWaitingList[i] == regOffset) - { - sGpuRegBufferLocked = FALSE; - return; - } - } - - sGpuRegWaitingList[i] = regOffset; - sGpuRegBufferLocked = FALSE; - } - } -} - -u16 GetGpuReg(u8 regOffset) -{ - if (regOffset == REG_OFFSET_DISPSTAT) - return REG_DISPSTAT; - - if (regOffset == REG_OFFSET_VCOUNT) - return REG_VCOUNT; - - return GPU_REG_BUF(regOffset); -} - -void SetGpuRegBits(u8 regOffset, u16 mask) -{ - u16 regValue = GPU_REG_BUF(regOffset); - SetGpuReg(regOffset, regValue | mask); -} - -void ClearGpuRegBits(u8 regOffset, u16 mask) -{ - u16 regValue = GPU_REG_BUF(regOffset); - SetGpuReg(regOffset, regValue & ~mask); -} - -static void SyncRegIE(void) -{ - if (sShouldSyncRegIE) - { - u16 temp = REG_IME; - REG_IME = 0; - REG_IE = sRegIE; - REG_IME = temp; - sShouldSyncRegIE = FALSE; - } -} - -void EnableInterrupts(u16 mask) -{ - sRegIE |= mask; - sShouldSyncRegIE = TRUE; - SyncRegIE(); - UpdateRegDispstatIntrBits(sRegIE); -} - -void DisableInterrupts(u16 mask) -{ - sRegIE &= ~mask; - sShouldSyncRegIE = TRUE; - SyncRegIE(); - UpdateRegDispstatIntrBits(sRegIE); -} - -static void UpdateRegDispstatIntrBits(u16 regIE) -{ - u16 oldValue = GetGpuReg(REG_OFFSET_DISPSTAT) & (DISPSTAT_HBLANK_INTR | DISPSTAT_VBLANK_INTR); - u16 newValue = 0; - - if (regIE & INTR_FLAG_VBLANK) - newValue |= DISPSTAT_VBLANK_INTR; - - if (regIE & INTR_FLAG_HBLANK) - newValue |= DISPSTAT_HBLANK_INTR; - - if (oldValue != newValue) - SetGpuReg(REG_OFFSET_DISPSTAT, newValue); -} diff --git a/src/hall_of_fame.c b/src/hall_of_fame.c index ac37b2ece..d138abbb1 100644 --- a/src/hall_of_fame.c +++ b/src/hall_of_fame.c @@ -6,7 +6,7 @@ #include "pokemon.h" #include "text.h" #include "text_window.h" -#include "alloc.h" +#include "malloc.h" #include "gpu_regs.h" #include "graphics.h" #include "main.h" diff --git a/src/intro.c b/src/intro.c index e97fbedb0..58f2509cc 100644 --- a/src/intro.c +++ b/src/intro.c @@ -5,7 +5,7 @@ #include "task.h" #include "title_screen.h" #include "libgcnmultiboot.h" -#include "alloc.h" +#include "malloc.h" #include "gpu_regs.h" #include "link.h" #include "multiboot_pokemon_colosseum.h" diff --git a/src/item.c b/src/item.c index 5e505724c..6b8f51f12 100644 --- a/src/item.c +++ b/src/item.c @@ -4,7 +4,7 @@ #include "string_util.h" #include "text.h" #include "event_data.h" -#include "alloc.h" +#include "malloc.h" #include "secret_base.h" #include "item_menu.h" #include "strings.h" diff --git a/src/item_icon.c b/src/item_icon.c index 91c32951a..6ae7eac58 100644 --- a/src/item_icon.c +++ b/src/item_icon.c @@ -2,7 +2,7 @@ #include "decompress.h" #include "graphics.h" #include "item_icon.h" -#include "alloc.h" +#include "malloc.h" #include "sprite.h" #include "constants/items.h" diff --git a/src/item_menu.c b/src/item_menu.c index 12f03b328..c004ed443 100755 --- a/src/item_menu.c +++ b/src/item_menu.c @@ -25,7 +25,7 @@ #include "link.h" #include "mail.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "map_name_popup.h" #include "menu.h" #include "money.h" diff --git a/src/link.c b/src/link.c index 9928b48da..db72be19a 100644 --- a/src/link.c +++ b/src/link.c @@ -2,7 +2,7 @@ // Includes #include "global.h" #include "m4a.h" -#include "alloc.h" +#include "malloc.h" #include "reset_save_heap.h" #include "save.h" #include "bg.h" diff --git a/src/link_rfu.c b/src/link_rfu.c index 4b0b0a2a5..3b533a3a4 100644 --- a/src/link_rfu.c +++ b/src/link_rfu.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "berry_blender.h" #include "decompress.h" diff --git a/src/list_menu.c b/src/list_menu.c index 6d51559f7..797aff5f8 100644 --- a/src/list_menu.c +++ b/src/list_menu.c @@ -8,7 +8,7 @@ #include "trig.h" #include "decompress.h" #include "palette.h" -#include "alloc.h" +#include "malloc.h" #include "strings.h" #include "sound.h" #include "constants/songs.h" diff --git a/src/load_save.c b/src/load_save.c index 5857f2b46..24dfda20c 100644 --- a/src/load_save.c +++ b/src/load_save.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "berry_powder.h" #include "item.h" #include "load_save.h" diff --git a/src/mail.c b/src/mail.c index 0078bc4fc..cdf965545 100644 --- a/src/mail.c +++ b/src/mail.c @@ -17,7 +17,7 @@ #include "bg.h" #include "pokemon_icon.h" #include "constants/species.h" -#include "alloc.h" +#include "malloc.h" #include "easy_chat.h" #include "constants/rgb.h" diff --git a/src/main.c b/src/main.c index 06425e661..f488e7f6d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,6 @@ #include "global.h" #include "crt0.h" -#include "alloc.h" +#include "malloc.h" #include "link.h" #include "link_rfu.h" #include "librfu.h" diff --git a/src/match_call.c b/src/match_call.c index b02af9977..bf5642a83 100644 --- a/src/match_call.c +++ b/src/match_call.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_setup.h" #include "bg.h" diff --git a/src/menu.c b/src/menu.c index 8d50d4dcc..177c37694 100644 --- a/src/menu.c +++ b/src/menu.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "blit.h" #include "dma3.h" diff --git a/src/menu_specialized.c b/src/menu_specialized.c index 1536cf413..f46c93e05 100644 --- a/src/menu_specialized.c +++ b/src/menu_specialized.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle_main.h" #include "contest_effect.h" #include "data.h" diff --git a/src/mevent_801BAAC.c b/src/mevent_801BAAC.c index 8f5b2d636..24d55c511 100644 --- a/src/mevent_801BAAC.c +++ b/src/mevent_801BAAC.c @@ -4,7 +4,7 @@ #include "gpu_regs.h" #include "palette.h" #include "decompress.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "pokemon_icon.h" #include "union_room.h" diff --git a/src/mevent_client.c b/src/mevent_client.c index ec8908af2..659b772df 100644 --- a/src/mevent_client.c +++ b/src/mevent_client.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "overworld.h" #include "script.h" diff --git a/src/mevent_server.c b/src/mevent_server.c index 06b10dd95..0d3afea30 100644 --- a/src/mevent_server.c +++ b/src/mevent_server.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "script.h" #include "mevent.h" #include "mevent_server.h" diff --git a/src/mevent_server_helpers.c b/src/mevent_server_helpers.c index 9d47ba745..616f394f7 100644 --- a/src/mevent_server_helpers.c +++ b/src/mevent_server_helpers.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "util.h" #include "link.h" diff --git a/src/mirage_tower.c b/src/mirage_tower.c index bb9c6e958..0c2b8ce74 100644 --- a/src/mirage_tower.c +++ b/src/mirage_tower.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "event_data.h" #include "event_object_movement.h" diff --git a/src/mossdeep_gym.c b/src/mossdeep_gym.c index d8adbe0c6..e4e807c4e 100644 --- a/src/mossdeep_gym.c +++ b/src/mossdeep_gym.c @@ -1,7 +1,7 @@ #include "global.h" #include "event_object_movement.h" #include "fieldmap.h" -#include "alloc.h" +#include "malloc.h" #include "mossdeep_gym.h" #include "script_movement.h" #include "constants/event_object_movement_constants.h" diff --git a/src/move_relearner.c b/src/move_relearner.c index 0b0b4a5aa..0d1362232 100644 --- a/src/move_relearner.c +++ b/src/move_relearner.c @@ -9,7 +9,7 @@ #include "gpu_regs.h" #include "move_relearner.h" #include "list_menu.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "menu_helpers.h" #include "menu_specialized.h" diff --git a/src/mystery_gift.c b/src/mystery_gift.c index ab3df9020..aae7d127e 100644 --- a/src/mystery_gift.c +++ b/src/mystery_gift.c @@ -2,7 +2,7 @@ #include "main.h" #include "text.h" #include "task.h" -#include "alloc.h" +#include "malloc.h" #include "gpu_regs.h" #include "scanline_effect.h" #include "text_window.h" diff --git a/src/naming_screen.c b/src/naming_screen.c index 7da7d99b0..b259d9a24 100644 --- a/src/naming_screen.c +++ b/src/naming_screen.c @@ -1,6 +1,6 @@ #include "global.h" #include "naming_screen.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "task.h" #include "sprite.h" diff --git a/src/overworld.c b/src/overworld.c index cefbd8159..2f6898ee5 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -26,7 +26,7 @@ #include "link_rfu.h" #include "load_save.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "m4a.h" #include "map_name_popup.h" #include "match_call.h" diff --git a/src/party_menu.c b/src/party_menu.c index f4b749bde..e4235b29f 100755 --- a/src/party_menu.c +++ b/src/party_menu.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_anim.h" #include "battle_controllers.h" diff --git a/src/player_pc.c b/src/player_pc.c index 08f24d379..dd90bbd8a 100644 --- a/src/player_pc.c +++ b/src/player_pc.c @@ -14,7 +14,7 @@ #include "list_menu.h" #include "mail.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "menu_helpers.h" #include "overworld.h" diff --git a/src/pokeblock.c b/src/pokeblock.c index 0d8469132..d9f5c5498 100644 --- a/src/pokeblock.c +++ b/src/pokeblock.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_controllers.h" #include "battle_message.h" diff --git a/src/pokeblock_feed.c b/src/pokeblock_feed.c index 4ad15414d..2c0b68dc6 100644 --- a/src/pokeblock_feed.c +++ b/src/pokeblock_feed.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "bg.h" #include "data.h" diff --git a/src/pokedex.c b/src/pokedex.c index c1787ef6a..0710bdecf 100644 --- a/src/pokedex.c +++ b/src/pokedex.c @@ -8,7 +8,7 @@ #include "graphics.h" #include "international_string_util.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "m4a.h" #include "overworld.h" diff --git a/src/pokedex_area_screen.c b/src/pokedex_area_screen.c index dc69b5d54..697fdb792 100755 --- a/src/pokedex_area_screen.c +++ b/src/pokedex_area_screen.c @@ -4,7 +4,7 @@ #include "gpu_regs.h" #include "graphics.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "overworld.h" #include "palette.h" diff --git a/src/pokedex_cry_screen.c b/src/pokedex_cry_screen.c index 487db6d58..0b35b00ee 100644 --- a/src/pokedex_cry_screen.c +++ b/src/pokedex_cry_screen.c @@ -2,7 +2,7 @@ #include "bg.h" #include "m4a.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "pokedex_cry_screen.h" #include "sound.h" diff --git a/src/pokemon.c b/src/pokemon.c index 074ee2d19..b4d791e27 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "apprentice.h" #include "battle.h" #include "battle_anim.h" diff --git a/src/pokemon_jump.c b/src/pokemon_jump.c index 8f16e1ce2..cc9c2f4ff 100755 --- a/src/pokemon_jump.c +++ b/src/pokemon_jump.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle_anim.h" #include "bg.h" #include "data.h" diff --git a/src/pokemon_storage_system.c b/src/pokemon_storage_system.c index 4cd1a4772..06531be0e 100644 --- a/src/pokemon_storage_system.c +++ b/src/pokemon_storage_system.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "data.h" #include "decompress.h" diff --git a/src/pokemon_summary_screen.c b/src/pokemon_summary_screen.c index 79aac4b5e..8018e4fe0 100644 --- a/src/pokemon_summary_screen.c +++ b/src/pokemon_summary_screen.c @@ -20,7 +20,7 @@ #include "item.h" #include "link.h" #include "m4a.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "menu_helpers.h" #include "mon_markings.h" diff --git a/src/pokenav.c b/src/pokenav.c index a5bb51789..2a1647a6f 100644 --- a/src/pokenav.c +++ b/src/pokenav.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "task.h" #include "main.h" #include "overworld.h" diff --git a/src/pokenav_unk_2.c b/src/pokenav_unk_2.c index 1c40e146f..cb9467a9c 100644 --- a/src/pokenav_unk_2.c +++ b/src/pokenav_unk_2.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "bg.h" #include "palette.h" diff --git a/src/rayquaza_scene.c b/src/rayquaza_scene.c index fc2860797..4e5e492f2 100644 --- a/src/rayquaza_scene.c +++ b/src/rayquaza_scene.c @@ -5,7 +5,7 @@ #include "graphics.h" #include "bg.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "scanline_effect.h" #include "menu.h" diff --git a/src/record_mixing.c b/src/record_mixing.c index 32e0404b9..8df0a8a8b 100644 --- a/src/record_mixing.c +++ b/src/record_mixing.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "random.h" #include "constants/items.h" #include "text.h" diff --git a/src/recorded_battle.c b/src/recorded_battle.c index 9e03d57f6..b2aa3c008 100644 --- a/src/recorded_battle.c +++ b/src/recorded_battle.c @@ -10,7 +10,7 @@ #include "string_util.h" #include "palette.h" #include "save.h" -#include "alloc.h" +#include "malloc.h" #include "util.h" #include "task.h" #include "text.h" diff --git a/src/region_map.c b/src/region_map.c index a98964e5c..4957f1cac 100644 --- a/src/region_map.c +++ b/src/region_map.c @@ -2,7 +2,7 @@ #include "main.h" #include "text.h" #include "menu.h" -#include "alloc.h" +#include "malloc.h" #include "gpu_regs.h" #include "palette.h" #include "party_menu.h" diff --git a/src/reset_save_heap.c b/src/reset_save_heap.c index beba742f5..5a54a2177 100644 --- a/src/reset_save_heap.c +++ b/src/reset_save_heap.c @@ -6,7 +6,7 @@ #include "save.h" #include "new_game.h" #include "overworld.h" -#include "alloc.h" +#include "malloc.h" void sub_81700F8(void) { diff --git a/src/rom_8034C54.c b/src/rom_8034C54.c index 42817fc2b..7cc30c775 100644 --- a/src/rom_8034C54.c +++ b/src/rom_8034C54.c @@ -1,6 +1,6 @@ #include "global.h" #include "rom_8034C54.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "main.h" #include "battle_main.h" diff --git a/src/rom_81520A8.c b/src/rom_81520A8.c index f06823594..f6d14d49c 100644 --- a/src/rom_81520A8.c +++ b/src/rom_81520A8.c @@ -1,6 +1,6 @@ #include "global.h" #include "rom_81520A8.h" -#include "alloc.h" +#include "malloc.h" #include "main.h" #include "rom_8034C54.h" diff --git a/src/roulette.c b/src/roulette.c index 662522aae..bef14a364 100644 --- a/src/roulette.c +++ b/src/roulette.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "coins.h" #include "decompress.h" diff --git a/src/secret_base.c b/src/secret_base.c index 12cb8cd61..71346d389 100644 --- a/src/secret_base.c +++ b/src/secret_base.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_setup.h" #include "decoration.h" diff --git a/src/shop.c b/src/shop.c index bf15568fb..ded88a2a6 100755 --- a/src/shop.c +++ b/src/shop.c @@ -17,7 +17,7 @@ #include "item_menu.h" #include "list_menu.h" #include "main.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "menu_helpers.h" #include "money.h" diff --git a/src/slot_machine.c b/src/slot_machine.c index 003ac3327..37071926e 100644 --- a/src/slot_machine.c +++ b/src/slot_machine.c @@ -14,7 +14,7 @@ #include "util.h" #include "text.h" #include "menu.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "gpu_regs.h" #include "coins.h" diff --git a/src/sprite.c b/src/sprite.c deleted file mode 100644 index e25eac62e..000000000 --- a/src/sprite.c +++ /dev/null @@ -1,1758 +0,0 @@ -#include "global.h" -#include "sprite.h" -#include "main.h" -#include "palette.h" - -#define MAX_SPRITE_COPY_REQUESTS 64 - -#define OAM_MATRIX_COUNT 32 - -#define SET_SPRITE_TILE_RANGE(index, start, count) \ -{ \ - sSpriteTileRanges[index * 2] = start; \ - (sSpriteTileRanges + 1)[index * 2] = count; \ -} - -#define ALLOC_SPRITE_TILE(n) \ -{ \ - sSpriteTileAllocBitmap[(n) / 8] |= (1 << ((n) % 8)); \ -} - -#define FREE_SPRITE_TILE(n) \ -{ \ - sSpriteTileAllocBitmap[(n) / 8] &= ~(1 << ((n) % 8)); \ -} - -#define SPRITE_TILE_IS_ALLOCATED(n) ((sSpriteTileAllocBitmap[(n) / 8] >> ((n) % 8)) & 1) - - -struct SpriteCopyRequest -{ - const u8 *src; - u8 *dest; - u16 size; -}; - -struct OamDimensions -{ - s8 width; - s8 height; -}; - -static void UpdateOamCoords(void); -static void BuildSpritePriorities(void); -static void SortSprites(void); -static void CopyMatricesToOamBuffer(void); -static void AddSpritesToOamBuffer(void); -static u8 CreateSpriteAt(u8 index, const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority); -static void ResetOamMatrices(void); -static void ResetSprite(struct Sprite *sprite); -static s16 AllocSpriteTiles(u16 tileCount); -static void RequestSpriteFrameImageCopy(u16 index, u16 tileNum, const struct SpriteFrameImage *images); -static void ResetAllSprites(void); -static void BeginAnim(struct Sprite *sprite); -static void ContinueAnim(struct Sprite *sprite); -static void AnimCmd_frame(struct Sprite *sprite); -static void AnimCmd_end(struct Sprite *sprite); -static void AnimCmd_jump(struct Sprite *sprite); -static void AnimCmd_loop(struct Sprite *sprite); -static void BeginAnimLoop(struct Sprite *sprite); -static void ContinueAnimLoop(struct Sprite *sprite); -static void JumpToTopOfAnimLoop(struct Sprite *sprite); -static void BeginAffineAnim(struct Sprite *sprite); -static void ContinueAffineAnim(struct Sprite *sprite); -static void AffineAnimDelay(u8 matrixNum, struct Sprite *sprite); -static void AffineAnimCmd_loop(u8 matrixNum, struct Sprite *sprite); -static void BeginAffineAnimLoop(u8 matrixNum, struct Sprite *sprite); -static void ContinueAffineAnimLoop(u8 matrixNum, struct Sprite *sprite); -static void JumpToTopOfAffineAnimLoop(u8 matrixNum, struct Sprite *sprite); -static void AffineAnimCmd_jump(u8 matrixNum, struct Sprite *sprite); -static void AffineAnimCmd_end(u8 matrixNum, struct Sprite *sprite); -static void AffineAnimCmd_frame(u8 matrixNum, struct Sprite *sprite); -static void CopyOamMatrix(u8 destMatrixIndex, struct OamMatrix *srcMatrix); -static u8 GetSpriteMatrixNum(struct Sprite *sprite); -static void SetSpriteOamFlipBits(struct Sprite *sprite, u8 hFlip, u8 vFlip); -static void AffineAnimStateRestartAnim(u8 matrixNum); -static void AffineAnimStateStartAnim(u8 matrixNum, u8 animNum); -static void AffineAnimStateReset(u8 matrixNum); -static void ApplyAffineAnimFrameAbsolute(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd); -static void DecrementAnimDelayCounter(struct Sprite *sprite); -static bool8 DecrementAffineAnimDelayCounter(struct Sprite *sprite, u8 matrixNum); -static void ApplyAffineAnimFrameRelativeAndUpdateMatrix(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd); -static s16 ConvertScaleParam(s16 scale); -static void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd); -static void ApplyAffineAnimFrame(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd); -static u8 IndexOfSpriteTileTag(u16 tag); -static void AllocSpriteTileRange(u16 tag, u16 start, u16 count); -static void DoLoadSpritePalette(const u16 *src, u16 paletteOffset); -static void obj_update_pos2(struct Sprite* sprite, s32 a1, s32 a2); - -typedef void (*AnimFunc)(struct Sprite *); -typedef void (*AnimCmdFunc)(struct Sprite *); -typedef void (*AffineAnimCmdFunc)(u8 matrixNum, struct Sprite *); - -#define DUMMY_OAM_DATA \ -{ \ - .y = 160, \ - .affineMode = 0, \ - .objMode = 0, \ - .mosaic = 0, \ - .bpp = 0, \ - .shape = SPRITE_SHAPE(8x8), \ - .x = 304, \ - .matrixNum = 0, \ - .size = SPRITE_SIZE(8x8), \ - .tileNum = 0, \ - .priority = 3, /* lowest priority */ \ - .paletteNum = 0, \ - .affineParam = 0 \ -} - -#define ANIM_END 0xFFFF -#define AFFINE_ANIM_END 0x7FFF - -// forward declarations -const union AnimCmd * const gDummySpriteAnimTable[]; -const union AffineAnimCmd * const gDummySpriteAffineAnimTable[]; -const struct SpriteTemplate gDummySpriteTemplate; - -// Unreferenced data. Also unreferenced in R/S. -static const u8 sUnknownData[24] = -{ - 0x01, 0x04, 0x10, 0x40, - 0x02, 0x04, 0x08, 0x20, - 0x02, 0x04, 0x08, 0x20, - 0x01, 0x04, 0x10, 0x40, - 0x02, 0x04, 0x08, 0x20, - 0x02, 0x04, 0x08, 0x20, -}; - -static const u8 sCenterToCornerVecTable[3][4][2] = -{ - { // square - { -4, -4 }, - { -8, -8 }, - { -16, -16 }, - { -32, -32 }, - }, - { // horizontal rectangle - { -8, -4 }, - { -16, -4 }, - { -16, -8 }, - { -32, -16 }, - }, - { // vertical rectangle - { -4, -8 }, - { -4, -16 }, - { -8, -16 }, - { -16, -32 }, - }, -}; - -static const struct Sprite sDummySprite = -{ - .oam = DUMMY_OAM_DATA, - .anims = gDummySpriteAnimTable, - .images = NULL, - .affineAnims = gDummySpriteAffineAnimTable, - .template = &gDummySpriteTemplate, - .subspriteTables = NULL, - .callback = SpriteCallbackDummy, - .pos1 = { 304, 160 }, - .pos2 = { 0, 0 }, - .centerToCornerVecX = 0, - .centerToCornerVecY = 0, - .animNum = 0, - .animCmdIndex = 0, - .animDelayCounter = 0, - .animPaused = 0, - .affineAnimPaused = 0, - .animLoopCounter = 0, - .data = {0, 0, 0, 0, 0, 0, 0}, - .inUse = 0, - .coordOffsetEnabled = 0, - .invisible = FALSE, - .flags_3 = 0, - .flags_4 = 0, - .flags_5 = 0, - .flags_6 = 0, - .flags_7 = 0, - .hFlip = 0, - .vFlip = 0, - .animBeginning = 0, - .affineAnimBeginning = 0, - .animEnded = 0, - .affineAnimEnded = 0, - .usingSheet = 0, - .flags_f = 0, - .sheetTileStart = 0, - .subspriteTableNum = 0, - .subspriteMode = 0, - .subpriority = 0xFF -}; - -const struct OamData gDummyOamData = DUMMY_OAM_DATA; - -static const union AnimCmd sDummyAnim = { ANIM_END }; - -const union AnimCmd * const gDummySpriteAnimTable[] = { &sDummyAnim }; - -static const union AffineAnimCmd sDummyAffineAnim = { AFFINE_ANIM_END }; - -const union AffineAnimCmd * const gDummySpriteAffineAnimTable[] = { &sDummyAffineAnim }; - -const struct SpriteTemplate gDummySpriteTemplate = -{ - .tileTag = 0, - .paletteTag = 0xFFFF, - .oam = &gDummyOamData, - .anims = gDummySpriteAnimTable, - .images = NULL, - .affineAnims = gDummySpriteAffineAnimTable, - .callback = SpriteCallbackDummy -}; - -static const AnimFunc sAnimFuncs[] = -{ - ContinueAnim, - BeginAnim, -}; - -static const AnimFunc sAffineAnimFuncs[] = -{ - ContinueAffineAnim, - BeginAffineAnim, -}; - -static const AnimCmdFunc sAnimCmdFuncs[] = -{ - AnimCmd_loop, - AnimCmd_jump, - AnimCmd_end, - AnimCmd_frame, -}; - -static const AffineAnimCmdFunc sAffineAnimCmdFuncs[] = -{ - AffineAnimCmd_loop, - AffineAnimCmd_jump, - AffineAnimCmd_end, - AffineAnimCmd_frame, -}; - -static const s32 sUnknown_082EC6F4[3][4][2] = -{ - { - {8, 8}, - {0x10, 0x10}, - {0x20, 0x20}, - {0x40, 0x40}, - }, - { - {0x10, 8}, - {0x20, 8}, - {0x20, 0x10}, - {0x40, 0x20}, - }, - { - {8, 0x10}, - {8, 0x20}, - {0x10, 0x20}, - {0x20, 0x40}, - }, -}; - -static const struct OamDimensions sOamDimensions[3][4] = -{ - { // square - { 8, 8 }, - { 16, 16 }, - { 32, 32 }, - { 64, 64 }, - }, - { // horizontal rectangle - { 16, 8 }, - { 32, 8 }, - { 32, 16 }, - { 64, 32 }, - }, - { // vertical rectangle - { 8, 16 }, - { 8, 32 }, - { 16, 32 }, - { 32, 64 }, - }, -}; - -// iwram bss -static u16 sSpriteTileRangeTags[MAX_SPRITES]; -static u16 sSpriteTileRanges[MAX_SPRITES * 2]; -static struct AffineAnimState sAffineAnimStates[OAM_MATRIX_COUNT]; -static u16 sSpritePaletteTags[16]; - -// iwram common -u32 gOamMatrixAllocBitmap; -u8 gReservedSpritePaletteCount; - -EWRAM_DATA struct Sprite gSprites[MAX_SPRITES + 1] = {0}; -EWRAM_DATA static u16 sSpritePriorities[MAX_SPRITES] = {0}; -EWRAM_DATA static u8 sSpriteOrder[MAX_SPRITES] = {0}; -EWRAM_DATA static bool8 sShouldProcessSpriteCopyRequests = 0; -EWRAM_DATA static u8 sSpriteCopyRequestCount = 0; -EWRAM_DATA static struct SpriteCopyRequest sSpriteCopyRequests[MAX_SPRITES] = {0}; -EWRAM_DATA u8 gOamLimit = 0; -EWRAM_DATA u16 gReservedSpriteTileCount = 0; -EWRAM_DATA static u8 sSpriteTileAllocBitmap[128] = {0}; -EWRAM_DATA s16 gSpriteCoordOffsetX = 0; -EWRAM_DATA s16 gSpriteCoordOffsetY = 0; -EWRAM_DATA struct OamMatrix gOamMatrices[OAM_MATRIX_COUNT] = {0}; -EWRAM_DATA bool8 gAffineAnimsDisabled = FALSE; - -void ResetSpriteData(void) -{ - ResetOamRange(0, 128); - ResetAllSprites(); - ClearSpriteCopyRequests(); - ResetAffineAnimData(); - FreeSpriteTileRanges(); - gOamLimit = 64; - gReservedSpriteTileCount = 0; - AllocSpriteTiles(0); - gSpriteCoordOffsetX = 0; - gSpriteCoordOffsetY = 0; -} - -void AnimateSprites(void) -{ - u8 i; - for (i = 0; i < MAX_SPRITES; i++) - { - struct Sprite *sprite = &gSprites[i]; - - if (sprite->inUse) - { - sprite->callback(sprite); - - if (sprite->inUse) - AnimateSprite(sprite); - } - } -} - -void BuildOamBuffer(void) -{ - u8 temp; - UpdateOamCoords(); - BuildSpritePriorities(); - SortSprites(); - temp = gMain.oamLoadDisabled; - gMain.oamLoadDisabled = TRUE; - AddSpritesToOamBuffer(); - CopyMatricesToOamBuffer(); - gMain.oamLoadDisabled = temp; - sShouldProcessSpriteCopyRequests = TRUE; -} - -void UpdateOamCoords(void) -{ - u8 i; - for (i = 0; i < MAX_SPRITES; i++) - { - struct Sprite *sprite = &gSprites[i]; - if (sprite->inUse && !sprite->invisible) - { - if (sprite->coordOffsetEnabled) - { - sprite->oam.x = sprite->pos1.x + sprite->pos2.x + sprite->centerToCornerVecX + gSpriteCoordOffsetX; - sprite->oam.y = sprite->pos1.y + sprite->pos2.y + sprite->centerToCornerVecY + gSpriteCoordOffsetY; - } - else - { - sprite->oam.x = sprite->pos1.x + sprite->pos2.x + sprite->centerToCornerVecX; - sprite->oam.y = sprite->pos1.y + sprite->pos2.y + sprite->centerToCornerVecY; - } - } - } -} - -void BuildSpritePriorities(void) -{ - u16 i; - for (i = 0; i < MAX_SPRITES; i++) - { - struct Sprite *sprite = &gSprites[i]; - u16 priority = sprite->subpriority | (sprite->oam.priority << 8); - sSpritePriorities[i] = priority; - } -} - -void SortSprites(void) -{ - u8 i; - for (i = 1; i < MAX_SPRITES; i++) - { - u8 j = i; - struct Sprite *sprite1 = &gSprites[sSpriteOrder[i - 1]]; - struct Sprite *sprite2 = &gSprites[sSpriteOrder[i]]; - u16 sprite1Priority = sSpritePriorities[sSpriteOrder[i - 1]]; - u16 sprite2Priority = sSpritePriorities[sSpriteOrder[i]]; - s16 sprite1Y = sprite1->oam.y; - s16 sprite2Y = sprite2->oam.y; - - if (sprite1Y >= DISPLAY_HEIGHT) - sprite1Y = sprite1Y - 256; - - if (sprite2Y >= DISPLAY_HEIGHT) - sprite2Y = sprite2Y - 256; - - if (sprite1->oam.affineMode == ST_OAM_AFFINE_DOUBLE - && sprite1->oam.size == 3) - { - u32 shape = sprite1->oam.shape; - if (shape == ST_OAM_SQUARE || shape == 2) - { - if (sprite1Y > 128) - sprite1Y = sprite1Y - 256; - } - } - - if (sprite2->oam.affineMode == ST_OAM_AFFINE_DOUBLE - && sprite2->oam.size == 3) - { - u32 shape = sprite2->oam.shape; - if (shape == ST_OAM_SQUARE || shape == ST_OAM_V_RECTANGLE) - { - if (sprite2Y > 128) - sprite2Y = sprite2Y - 256; - } - } - - while (j > 0 - && ((sprite1Priority > sprite2Priority) - || (sprite1Priority == sprite2Priority && sprite1Y < sprite2Y))) - { - u8 temp = sSpriteOrder[j]; - sSpriteOrder[j] = sSpriteOrder[j - 1]; - sSpriteOrder[j - 1] = temp; - - // UB: If j equals 1, then j-- makes j equal 0. - // Then, sSpriteOrder[-1] gets accessed below. - // Although this doesn't result in a bug in the ROM, - // the behavior is undefined. - j--; - - sprite1 = &gSprites[sSpriteOrder[j - 1]]; - sprite2 = &gSprites[sSpriteOrder[j]]; - sprite1Priority = sSpritePriorities[sSpriteOrder[j - 1]]; - sprite2Priority = sSpritePriorities[sSpriteOrder[j]]; - sprite1Y = sprite1->oam.y; - sprite2Y = sprite2->oam.y; - - if (sprite1Y >= DISPLAY_HEIGHT) - sprite1Y = sprite1Y - 256; - - if (sprite2Y >= DISPLAY_HEIGHT) - sprite2Y = sprite2Y - 256; - - if (sprite1->oam.affineMode == ST_OAM_AFFINE_DOUBLE - && sprite1->oam.size == 3) - { - u32 shape = sprite1->oam.shape; - if (shape == ST_OAM_SQUARE || shape == ST_OAM_V_RECTANGLE) - { - if (sprite1Y > 128) - sprite1Y = sprite1Y - 256; - } - } - - if (sprite2->oam.affineMode == ST_OAM_AFFINE_DOUBLE - && sprite2->oam.size == 3) - { - u32 shape = sprite2->oam.shape; - if (shape == ST_OAM_SQUARE || shape == ST_OAM_V_RECTANGLE) - { - if (sprite2Y > 128) - sprite2Y = sprite2Y - 256; - } - } - } - } -} - -void CopyMatricesToOamBuffer(void) -{ - u8 i; - for (i = 0; i < OAM_MATRIX_COUNT; i++) - { - u32 base = 4 * i; - gMain.oamBuffer[base + 0].affineParam = gOamMatrices[i].a; - gMain.oamBuffer[base + 1].affineParam = gOamMatrices[i].b; - gMain.oamBuffer[base + 2].affineParam = gOamMatrices[i].c; - gMain.oamBuffer[base + 3].affineParam = gOamMatrices[i].d; - } -} - -void AddSpritesToOamBuffer(void) -{ - u8 i = 0; - u8 oamIndex = 0; - - while (i < MAX_SPRITES) - { - struct Sprite *sprite = &gSprites[sSpriteOrder[i]]; - if (sprite->inUse && !sprite->invisible && AddSpriteToOamBuffer(sprite, &oamIndex)) - return; - i++; - } - - while (oamIndex < gOamLimit) - { - gMain.oamBuffer[oamIndex] = gDummyOamData; - oamIndex++; - } -} - -u8 CreateSprite(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) -{ - u8 i; - - for (i = 0; i < MAX_SPRITES; i++) - if (!gSprites[i].inUse) - return CreateSpriteAt(i, template, x, y, subpriority); - - return MAX_SPRITES; -} - -u8 CreateSpriteAtEnd(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) -{ - s16 i; - - for (i = MAX_SPRITES - 1; i > -1; i--) - if (!gSprites[i].inUse) - return CreateSpriteAt(i, template, x, y, subpriority); - - return MAX_SPRITES; -} - -u8 CreateInvisibleSprite(void (*callback)(struct Sprite *)) -{ - u8 index = CreateSprite(&gDummySpriteTemplate, 0, 0, 31); - - if (index == MAX_SPRITES) - { - return MAX_SPRITES; - } - else - { - gSprites[index].invisible = TRUE; - gSprites[index].callback = callback; - return index; - } -} - -u8 CreateSpriteAt(u8 index, const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) -{ - struct Sprite *sprite = &gSprites[index]; - - ResetSprite(sprite); - - sprite->inUse = TRUE; - sprite->animBeginning = TRUE; - sprite->affineAnimBeginning = TRUE; - sprite->usingSheet = TRUE; - - sprite->subpriority = subpriority; - sprite->oam = *template->oam; - sprite->anims = template->anims; - sprite->affineAnims = template->affineAnims; - sprite->template = template; - sprite->callback = template->callback; - sprite->pos1.x = x; - sprite->pos1.y = y; - - CalcCenterToCornerVec(sprite, sprite->oam.shape, sprite->oam.size, sprite->oam.affineMode); - - if (template->tileTag == 0xFFFF) - { - s16 tileNum; - sprite->images = template->images; - tileNum = AllocSpriteTiles((u8)(sprite->images->size / TILE_SIZE_4BPP)); - if (tileNum == -1) - { - ResetSprite(sprite); - return MAX_SPRITES; - } - sprite->oam.tileNum = tileNum; - sprite->usingSheet = FALSE; - sprite->sheetTileStart = 0; - } - else - { - sprite->sheetTileStart = GetSpriteTileStartByTag(template->tileTag); - SetSpriteSheetFrameTileNum(sprite); - } - - if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) - InitSpriteAffineAnim(sprite); - - if (template->paletteTag != 0xFFFF) - sprite->oam.paletteNum = IndexOfSpritePaletteTag(template->paletteTag); - - return index; -} - -u8 CreateSpriteAndAnimate(const struct SpriteTemplate *template, s16 x, s16 y, u8 subpriority) -{ - u8 i; - - for (i = 0; i < MAX_SPRITES; i++) - { - struct Sprite *sprite = &gSprites[i]; - - if (!gSprites[i].inUse) - { - u8 index = CreateSpriteAt(i, template, x, y, subpriority); - - if (index == MAX_SPRITES) - return MAX_SPRITES; - - gSprites[i].callback(sprite); - - if (gSprites[i].inUse) - AnimateSprite(sprite); - - return index; - } - } - - return MAX_SPRITES; -} - -void DestroySprite(struct Sprite *sprite) -{ - if (sprite->inUse) - { - if (!sprite->usingSheet) - { - u16 i; - u16 tileEnd = (sprite->images->size / TILE_SIZE_4BPP) + sprite->oam.tileNum; - for (i = sprite->oam.tileNum; i < tileEnd; i++) - FREE_SPRITE_TILE(i); - } - ResetSprite(sprite); - } -} - -void ResetOamRange(u8 a, u8 b) -{ - u8 i; - - for (i = a; i < b; i++) - { - struct OamData *oamBuffer = gMain.oamBuffer; - oamBuffer[i] = *(struct OamData *)&gDummyOamData; - } -} - -void LoadOam(void) -{ - if (!gMain.oamLoadDisabled) - CpuCopy32(gMain.oamBuffer, (void *)OAM, sizeof(gMain.oamBuffer)); -} - -void ClearSpriteCopyRequests(void) -{ - u8 i; - - sShouldProcessSpriteCopyRequests = FALSE; - sSpriteCopyRequestCount = 0; - - for (i = 0; i < MAX_SPRITE_COPY_REQUESTS; i++) - { - sSpriteCopyRequests[i].src = 0; - sSpriteCopyRequests[i].dest = 0; - sSpriteCopyRequests[i].size = 0; - } -} - -void ResetOamMatrices(void) -{ - u8 i; - for (i = 0; i < OAM_MATRIX_COUNT; i++) - { - // set to identity matrix - gOamMatrices[i].a = 0x0100; - gOamMatrices[i].b = 0x0000; - gOamMatrices[i].c = 0x0000; - gOamMatrices[i].d = 0x0100; - } -} - -void SetOamMatrix(u8 matrixNum, u16 a, u16 b, u16 c, u16 d) -{ - gOamMatrices[matrixNum].a = a; - gOamMatrices[matrixNum].b = b; - gOamMatrices[matrixNum].c = c; - gOamMatrices[matrixNum].d = d; -} - -void ResetSprite(struct Sprite *sprite) -{ - *sprite = sDummySprite; -} - -void CalcCenterToCornerVec(struct Sprite *sprite, u8 shape, u8 size, u8 affineMode) -{ - u8 x = sCenterToCornerVecTable[shape][size][0]; - u8 y = sCenterToCornerVecTable[shape][size][1]; - - if (affineMode & ST_OAM_AFFINE_DOUBLE_MASK) - { - x *= 2; - y *= 2; - } - - sprite->centerToCornerVecX = x; - sprite->centerToCornerVecY = y; -} - -s16 AllocSpriteTiles(u16 tileCount) -{ - u16 i; - s16 start; - u16 numTilesFound; - - if (tileCount == 0) - { - // Free all unreserved tiles if the tile count is 0. - for (i = gReservedSpriteTileCount; i < TOTAL_OBJ_TILE_COUNT; i++) - FREE_SPRITE_TILE(i); - - return 0; - } - - i = gReservedSpriteTileCount; - - for (;;) - { - while (SPRITE_TILE_IS_ALLOCATED(i)) - { - i++; - - if (i == TOTAL_OBJ_TILE_COUNT) - return -1; - } - - start = i; - numTilesFound = 1; - - while (numTilesFound != tileCount) - { - i++; - - if (i == TOTAL_OBJ_TILE_COUNT) - return -1; - - if (!SPRITE_TILE_IS_ALLOCATED(i)) - numTilesFound++; - else - break; - } - - if (numTilesFound == tileCount) - break; - } - - for (i = start; i < tileCount + start; i++) - ALLOC_SPRITE_TILE(i); - - return start; -} - -u8 SpriteTileAllocBitmapOp(u16 bit, u8 op) -{ - u8 index = bit / 8; - u8 shift = bit % 8; - u8 val = bit % 8; - u8 retVal = 0; - - if (op == 0) - { - val = ~(1 << val); - sSpriteTileAllocBitmap[index] &= val; - } - else if (op == 1) - { - val = (1 << val); - sSpriteTileAllocBitmap[index] |= val; - } - else - { - retVal = 1 << shift; - retVal &= sSpriteTileAllocBitmap[index]; - } - - return retVal; -} - -void SpriteCallbackDummy(struct Sprite *sprite) -{ -} - -void ProcessSpriteCopyRequests(void) -{ - if (sShouldProcessSpriteCopyRequests) - { - u8 i = 0; - - while (sSpriteCopyRequestCount > 0) - { - CpuCopy16(sSpriteCopyRequests[i].src, sSpriteCopyRequests[i].dest, sSpriteCopyRequests[i].size); - sSpriteCopyRequestCount--; - i++; - } - - sShouldProcessSpriteCopyRequests = FALSE; - } -} - -void RequestSpriteFrameImageCopy(u16 index, u16 tileNum, const struct SpriteFrameImage *images) -{ - if (sSpriteCopyRequestCount < MAX_SPRITE_COPY_REQUESTS) - { - sSpriteCopyRequests[sSpriteCopyRequestCount].src = images[index].data; - sSpriteCopyRequests[sSpriteCopyRequestCount].dest = (u8 *)OBJ_VRAM0 + TILE_SIZE_4BPP * tileNum; - sSpriteCopyRequests[sSpriteCopyRequestCount].size = images[index].size; - sSpriteCopyRequestCount++; - } -} - -void RequestSpriteCopy(const u8 *src, u8 *dest, u16 size) -{ - if (sSpriteCopyRequestCount < MAX_SPRITE_COPY_REQUESTS) - { - sSpriteCopyRequests[sSpriteCopyRequestCount].src = src; - sSpriteCopyRequests[sSpriteCopyRequestCount].dest = dest; - sSpriteCopyRequests[sSpriteCopyRequestCount].size = size; - sSpriteCopyRequestCount++; - } -} - -void CopyFromSprites(u8 *dest) -{ - u32 i; - u8 *src = (u8 *)gSprites; - for (i = 0; i < sizeof(struct Sprite) * MAX_SPRITES; i++) - { - *dest = *src; - dest++; - src++; - } -} - -void CopyToSprites(u8 *src) -{ - u32 i; - u8 *dest = (u8 *)gSprites; - for (i = 0; i < sizeof(struct Sprite) * MAX_SPRITES; i++) - { - *dest = *src; - src++; - dest++; - } -} - -void ResetAllSprites(void) -{ - u8 i; - - for (i = 0; i < MAX_SPRITES; i++) - { - ResetSprite(&gSprites[i]); - sSpriteOrder[i] = i; - } - - ResetSprite(&gSprites[i]); -} - -void FreeSpriteTiles(struct Sprite *sprite) -{ - if (sprite->template->tileTag != 0xFFFF) - FreeSpriteTilesByTag(sprite->template->tileTag); -} - -void FreeSpritePalette(struct Sprite *sprite) -{ - FreeSpritePaletteByTag(sprite->template->paletteTag); -} - -void FreeSpriteOamMatrix(struct Sprite *sprite) -{ - if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) - { - FreeOamMatrix(sprite->oam.matrixNum); - sprite->oam.affineMode = ST_OAM_AFFINE_OFF; - } -} - -void DestroySpriteAndFreeResources(struct Sprite *sprite) -{ - FreeSpriteTiles(sprite); - FreeSpritePalette(sprite); - FreeSpriteOamMatrix(sprite); - DestroySprite(sprite); -} - -void AnimateSprite(struct Sprite *sprite) -{ - sAnimFuncs[sprite->animBeginning](sprite); - - if (!gAffineAnimsDisabled) - sAffineAnimFuncs[sprite->affineAnimBeginning](sprite); -} - -void BeginAnim(struct Sprite *sprite) -{ - s16 imageValue; - u8 duration; - u8 hFlip; - u8 vFlip; - - sprite->animCmdIndex = 0; - sprite->animEnded = FALSE; - sprite->animLoopCounter = 0; - imageValue = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; - - if (imageValue != -1) - { - sprite->animBeginning = FALSE; - duration = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.duration; - hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; - vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; - - if (duration) - duration--; - - sprite->animDelayCounter = duration; - - if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) - SetSpriteOamFlipBits(sprite, hFlip, vFlip); - - if (sprite->usingSheet) - sprite->oam.tileNum = sprite->sheetTileStart + imageValue; - else - RequestSpriteFrameImageCopy(imageValue, sprite->oam.tileNum, sprite->images); - } -} - -void ContinueAnim(struct Sprite *sprite) -{ - if (sprite->animDelayCounter) - { - u8 hFlip; - u8 vFlip; - DecrementAnimDelayCounter(sprite); - hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; - vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; - if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) - SetSpriteOamFlipBits(sprite, hFlip, vFlip); - } - else if (!sprite->animPaused) - { - s16 type; - s16 funcIndex; - sprite->animCmdIndex++; - type = sprite->anims[sprite->animNum][sprite->animCmdIndex].type; - funcIndex = 3; - if (type < 0) - funcIndex = type + 3; - sAnimCmdFuncs[funcIndex](sprite); - } -} - -void AnimCmd_frame(struct Sprite *sprite) -{ - s16 imageValue; - u8 duration; - u8 hFlip; - u8 vFlip; - - imageValue = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; - duration = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.duration; - hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; - vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; - - if (duration) - duration--; - - sprite->animDelayCounter = duration; - - if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) - SetSpriteOamFlipBits(sprite, hFlip, vFlip); - - if (sprite->usingSheet) - sprite->oam.tileNum = sprite->sheetTileStart + imageValue; - else - RequestSpriteFrameImageCopy(imageValue, sprite->oam.tileNum, sprite->images); -} - -void AnimCmd_end(struct Sprite *sprite) -{ - sprite->animCmdIndex--; - sprite->animEnded = TRUE; -} - -void AnimCmd_jump(struct Sprite *sprite) -{ - s16 imageValue; - u8 duration; - u8 hFlip; - u8 vFlip; - - sprite->animCmdIndex = sprite->anims[sprite->animNum][sprite->animCmdIndex].jump.target; - - imageValue = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; - duration = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.duration; - hFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.hFlip; - vFlip = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.vFlip; - - if (duration) - duration--; - - sprite->animDelayCounter = duration; - - if (!(sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK)) - SetSpriteOamFlipBits(sprite, hFlip, vFlip); - - if (sprite->usingSheet) - sprite->oam.tileNum = sprite->sheetTileStart + imageValue; - else - RequestSpriteFrameImageCopy(imageValue, sprite->oam.tileNum, sprite->images); -} - -void AnimCmd_loop(struct Sprite *sprite) -{ - if (sprite->animLoopCounter) - ContinueAnimLoop(sprite); - else - BeginAnimLoop(sprite); -} - -void BeginAnimLoop(struct Sprite *sprite) -{ - sprite->animLoopCounter = sprite->anims[sprite->animNum][sprite->animCmdIndex].loop.count; - JumpToTopOfAnimLoop(sprite); - ContinueAnim(sprite); -} - -void ContinueAnimLoop(struct Sprite *sprite) -{ - sprite->animLoopCounter--; - JumpToTopOfAnimLoop(sprite); - ContinueAnim(sprite); -} - -void JumpToTopOfAnimLoop(struct Sprite *sprite) -{ - if (sprite->animLoopCounter) - { - sprite->animCmdIndex--; - - while (sprite->anims[sprite->animNum][sprite->animCmdIndex - 1].type != -3) - { - if (sprite->animCmdIndex == 0) - break; - sprite->animCmdIndex--; - } - - sprite->animCmdIndex--; - } -} - -void BeginAffineAnim(struct Sprite *sprite) -{ - if ((sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) && sprite->affineAnims[0][0].type != 32767) - { - struct AffineAnimFrameCmd frameCmd; - u8 matrixNum = GetSpriteMatrixNum(sprite); - AffineAnimStateRestartAnim(matrixNum); - GetAffineAnimFrame(matrixNum, sprite, &frameCmd); - sprite->affineAnimBeginning = FALSE; - sprite->affineAnimEnded = FALSE; - ApplyAffineAnimFrame(matrixNum, &frameCmd); - sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; - if (sprite->flags_f) - obj_update_pos2(sprite, sprite->data[6], sprite->data[7]); - } -} - -void ContinueAffineAnim(struct Sprite *sprite) -{ - if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) - { - u8 matrixNum = GetSpriteMatrixNum(sprite); - - if (sAffineAnimStates[matrixNum].delayCounter) - AffineAnimDelay(matrixNum, sprite); - else if (sprite->affineAnimPaused) - return; - else - { - s16 type; - s16 funcIndex; - sAffineAnimStates[matrixNum].animCmdIndex++; - type = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].type; - funcIndex = 3; - if (type >= 32765) - funcIndex = type - 32765; - sAffineAnimCmdFuncs[funcIndex](matrixNum, sprite); - } - if (sprite->flags_f) - obj_update_pos2(sprite, sprite->data[6], sprite->data[7]); - } -} - -void AffineAnimDelay(u8 matrixNum, struct Sprite *sprite) -{ - if (!DecrementAffineAnimDelayCounter(sprite, matrixNum)) - { - struct AffineAnimFrameCmd frameCmd; - GetAffineAnimFrame(matrixNum, sprite, &frameCmd); - ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, &frameCmd); - } -} - -void AffineAnimCmd_loop(u8 matrixNum, struct Sprite *sprite) -{ - if (sAffineAnimStates[matrixNum].loopCounter) - ContinueAffineAnimLoop(matrixNum, sprite); - else - BeginAffineAnimLoop(matrixNum, sprite); -} - -void BeginAffineAnimLoop(u8 matrixNum, struct Sprite *sprite) -{ - sAffineAnimStates[matrixNum].loopCounter = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].loop.count; - JumpToTopOfAffineAnimLoop(matrixNum, sprite); - ContinueAffineAnim(sprite); -} - -void ContinueAffineAnimLoop(u8 matrixNum, struct Sprite *sprite) -{ - sAffineAnimStates[matrixNum].loopCounter--; - JumpToTopOfAffineAnimLoop(matrixNum, sprite); - ContinueAffineAnim(sprite); -} - -void JumpToTopOfAffineAnimLoop(u8 matrixNum, struct Sprite *sprite) -{ - if (sAffineAnimStates[matrixNum].loopCounter) - { - sAffineAnimStates[matrixNum].animCmdIndex--; - - while (sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex - 1].type != 32765) - { - if (sAffineAnimStates[matrixNum].animCmdIndex == 0) - break; - sAffineAnimStates[matrixNum].animCmdIndex--; - } - - sAffineAnimStates[matrixNum].animCmdIndex--; - } -} - -void AffineAnimCmd_jump(u8 matrixNum, struct Sprite *sprite) -{ - struct AffineAnimFrameCmd frameCmd; - sAffineAnimStates[matrixNum].animCmdIndex = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].jump.target; - GetAffineAnimFrame(matrixNum, sprite, &frameCmd); - ApplyAffineAnimFrame(matrixNum, &frameCmd); - sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; -} - -void AffineAnimCmd_end(u8 matrixNum, struct Sprite *sprite) -{ - struct AffineAnimFrameCmd dummyFrameCmd = {0}; - sprite->affineAnimEnded = TRUE; - sAffineAnimStates[matrixNum].animCmdIndex--; - ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, &dummyFrameCmd); -} - -void AffineAnimCmd_frame(u8 matrixNum, struct Sprite *sprite) -{ - struct AffineAnimFrameCmd frameCmd; - GetAffineAnimFrame(matrixNum, sprite, &frameCmd); - ApplyAffineAnimFrame(matrixNum, &frameCmd); - sAffineAnimStates[matrixNum].delayCounter = frameCmd.duration; -} - -void CopyOamMatrix(u8 destMatrixIndex, struct OamMatrix *srcMatrix) -{ - gOamMatrices[destMatrixIndex].a = srcMatrix->a; - gOamMatrices[destMatrixIndex].b = srcMatrix->b; - gOamMatrices[destMatrixIndex].c = srcMatrix->c; - gOamMatrices[destMatrixIndex].d = srcMatrix->d; -} - -u8 GetSpriteMatrixNum(struct Sprite *sprite) -{ - u8 matrixNum = 0; - if (sprite->oam.affineMode & ST_OAM_AFFINE_ON_MASK) - matrixNum = sprite->oam.matrixNum; - return matrixNum; -} - -void sub_8007E18(struct Sprite* sprite, s16 a2, s16 a3) -{ - sprite->data[6] = a2; - sprite->data[7] = a3; - sprite->flags_f = 1; -} - -s32 sub_8007E28(s32 a0, s32 a1, s32 a2) -{ - s32 subResult, var1; - - subResult = a1 - a0; - if (subResult < 0) - var1 = -(subResult) >> 9; - else - var1 = -(subResult >> 9); - return a2 - ((u32)(a2 * a1) / (u32)(a0) + var1); -} - -void obj_update_pos2(struct Sprite *sprite, s32 a1, s32 a2) -{ - s32 var0, var1, var2; - - u32 matrixNum = sprite->oam.matrixNum; - if (a1 != 0x800) - { - var0 = sUnknown_082EC6F4[sprite->oam.shape][sprite->oam.size][0]; - var1 = var0 << 8; - var2 = (var0 << 16) / gOamMatrices[matrixNum].a; - sprite->pos2.x = sub_8007E28(var1, var2, a1); - } - if (a2 != 0x800) - { - var0 = sUnknown_082EC6F4[sprite->oam.shape][sprite->oam.size][1]; - var1 = var0 << 8; - var2 = (var0 << 16) / gOamMatrices[matrixNum].d; - sprite->pos2.y = sub_8007E28(var1, var2, a2); - } -} - -void SetSpriteOamFlipBits(struct Sprite *sprite, u8 hFlip, u8 vFlip) -{ - sprite->oam.matrixNum &= 0x7; - sprite->oam.matrixNum |= (((hFlip ^ sprite->hFlip) & 1) << 3); - sprite->oam.matrixNum |= (((vFlip ^ sprite->vFlip) & 1) << 4); -} - -void AffineAnimStateRestartAnim(u8 matrixNum) -{ - sAffineAnimStates[matrixNum].animCmdIndex = 0; - sAffineAnimStates[matrixNum].delayCounter = 0; - sAffineAnimStates[matrixNum].loopCounter = 0; -} - -void AffineAnimStateStartAnim(u8 matrixNum, u8 animNum) -{ - sAffineAnimStates[matrixNum].animNum = animNum; - sAffineAnimStates[matrixNum].animCmdIndex = 0; - sAffineAnimStates[matrixNum].delayCounter = 0; - sAffineAnimStates[matrixNum].loopCounter = 0; - sAffineAnimStates[matrixNum].xScale = 0x0100; - sAffineAnimStates[matrixNum].yScale = 0x0100; - sAffineAnimStates[matrixNum].rotation = 0; -} - -void AffineAnimStateReset(u8 matrixNum) -{ - sAffineAnimStates[matrixNum].animNum = 0; - sAffineAnimStates[matrixNum].animCmdIndex = 0; - sAffineAnimStates[matrixNum].delayCounter = 0; - sAffineAnimStates[matrixNum].loopCounter = 0; - sAffineAnimStates[matrixNum].xScale = 0x0100; - sAffineAnimStates[matrixNum].yScale = 0x0100; - sAffineAnimStates[matrixNum].rotation = 0; -} - -void ApplyAffineAnimFrameAbsolute(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd) -{ - sAffineAnimStates[matrixNum].xScale = frameCmd->xScale; - sAffineAnimStates[matrixNum].yScale = frameCmd->yScale; - sAffineAnimStates[matrixNum].rotation = frameCmd->rotation << 8; -} - -void DecrementAnimDelayCounter(struct Sprite *sprite) -{ - if (!sprite->animPaused) - sprite->animDelayCounter--; -} - -bool8 DecrementAffineAnimDelayCounter(struct Sprite *sprite, u8 matrixNum) -{ - if (!sprite->affineAnimPaused) - --sAffineAnimStates[matrixNum].delayCounter; - return sprite->affineAnimPaused; -} - -void ApplyAffineAnimFrameRelativeAndUpdateMatrix(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd) -{ - struct ObjAffineSrcData srcData; - struct OamMatrix matrix; - sAffineAnimStates[matrixNum].xScale += frameCmd->xScale; - sAffineAnimStates[matrixNum].yScale += frameCmd->yScale; - sAffineAnimStates[matrixNum].rotation = (sAffineAnimStates[matrixNum].rotation + (frameCmd->rotation << 8)) & ~0xFF; - srcData.xScale = ConvertScaleParam(sAffineAnimStates[matrixNum].xScale); - srcData.yScale = ConvertScaleParam(sAffineAnimStates[matrixNum].yScale); - srcData.rotation = sAffineAnimStates[matrixNum].rotation; - ObjAffineSet(&srcData, &matrix, 1, 2); - CopyOamMatrix(matrixNum, &matrix); -} - -s16 ConvertScaleParam(s16 scale) -{ - s32 val = 0x10000; - return val / scale; -} - -void GetAffineAnimFrame(u8 matrixNum, struct Sprite *sprite, struct AffineAnimFrameCmd *frameCmd) -{ - frameCmd->xScale = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.xScale; - frameCmd->yScale = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.yScale; - frameCmd->rotation = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.rotation; - frameCmd->duration = sprite->affineAnims[sAffineAnimStates[matrixNum].animNum][sAffineAnimStates[matrixNum].animCmdIndex].frame.duration; -} - -void ApplyAffineAnimFrame(u8 matrixNum, struct AffineAnimFrameCmd *frameCmd) -{ - struct AffineAnimFrameCmd dummyFrameCmd = {0}; - - if (frameCmd->duration) - { - frameCmd->duration--; - ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, frameCmd); - } - else - { - ApplyAffineAnimFrameAbsolute(matrixNum, frameCmd); - ApplyAffineAnimFrameRelativeAndUpdateMatrix(matrixNum, &dummyFrameCmd); - } -} - -void StartSpriteAnim(struct Sprite *sprite, u8 animNum) -{ - sprite->animNum = animNum; - sprite->animBeginning = TRUE; - sprite->animEnded = FALSE; -} - -void StartSpriteAnimIfDifferent(struct Sprite *sprite, u8 animNum) -{ - if (sprite->animNum != animNum) - StartSpriteAnim(sprite, animNum); -} - -void SeekSpriteAnim(struct Sprite *sprite, u8 animCmdIndex) -{ - u8 temp = sprite->animPaused; - sprite->animCmdIndex = animCmdIndex - 1; - sprite->animDelayCounter = 0; - sprite->animBeginning = FALSE; - sprite->animEnded = FALSE; - sprite->animPaused = FALSE; - ContinueAnim(sprite); - if (sprite->animDelayCounter) - sprite->animDelayCounter++; - sprite->animPaused = temp; -} - -void StartSpriteAffineAnim(struct Sprite *sprite, u8 animNum) -{ - u8 matrixNum = GetSpriteMatrixNum(sprite); - AffineAnimStateStartAnim(matrixNum, animNum); - sprite->affineAnimBeginning = TRUE; - sprite->affineAnimEnded = FALSE; -} - -void StartSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum) -{ - u8 matrixNum = GetSpriteMatrixNum(sprite); - if (sAffineAnimStates[matrixNum].animNum != animNum) - StartSpriteAffineAnim(sprite, animNum); -} - -void ChangeSpriteAffineAnim(struct Sprite *sprite, u8 animNum) -{ - u8 matrixNum = GetSpriteMatrixNum(sprite); - sAffineAnimStates[matrixNum].animNum = animNum; - sprite->affineAnimBeginning = TRUE; - sprite->affineAnimEnded = FALSE; -} - -void ChangeSpriteAffineAnimIfDifferent(struct Sprite *sprite, u8 animNum) -{ - u8 matrixNum = GetSpriteMatrixNum(sprite); - if (sAffineAnimStates[matrixNum].animNum != animNum) - ChangeSpriteAffineAnim(sprite, animNum); -} - -void SetSpriteSheetFrameTileNum(struct Sprite *sprite) -{ - if (sprite->usingSheet) - { - s16 tileOffset = sprite->anims[sprite->animNum][sprite->animCmdIndex].frame.imageValue; - if (tileOffset < 0) - tileOffset = 0; - sprite->oam.tileNum = sprite->sheetTileStart + tileOffset; - } -} - -void ResetAffineAnimData(void) -{ - u8 i; - - gAffineAnimsDisabled = FALSE; - gOamMatrixAllocBitmap = 0; - - ResetOamMatrices(); - - for (i = 0; i < OAM_MATRIX_COUNT; i++) - AffineAnimStateReset(i); -} - -u8 AllocOamMatrix(void) -{ - u8 i = 0; - u32 bit = 1; - u32 bitmap = gOamMatrixAllocBitmap; - - while (i < OAM_MATRIX_COUNT) - { - if (!(bitmap & bit)) - { - gOamMatrixAllocBitmap |= bit; - return i; - } - - i++; - bit <<= 1; - } - - return 0xFF; -} - -void FreeOamMatrix(u8 matrixNum) -{ - u8 i = 0; - u32 bit = 1; - - while (i < matrixNum) - { - i++; - bit <<= 1; - } - - gOamMatrixAllocBitmap &= ~bit; - SetOamMatrix(matrixNum, 0x100, 0, 0, 0x100); -} - -void InitSpriteAffineAnim(struct Sprite *sprite) -{ - u8 matrixNum = AllocOamMatrix(); - if (matrixNum != 0xFF) - { - CalcCenterToCornerVec(sprite, sprite->oam.shape, sprite->oam.size, sprite->oam.affineMode); - sprite->oam.matrixNum = matrixNum; - sprite->affineAnimBeginning = TRUE; - AffineAnimStateReset(matrixNum); - } -} - -void SetOamMatrixRotationScaling(u8 matrixNum, s16 xScale, s16 yScale, u16 rotation) -{ - struct ObjAffineSrcData srcData; - struct OamMatrix matrix; - srcData.xScale = ConvertScaleParam(xScale); - srcData.yScale = ConvertScaleParam(yScale); - srcData.rotation = rotation; - ObjAffineSet(&srcData, &matrix, 1, 2); - CopyOamMatrix(matrixNum, &matrix); -} - -u16 LoadSpriteSheet(const struct SpriteSheet *sheet) -{ - s16 tileStart = AllocSpriteTiles(sheet->size / TILE_SIZE_4BPP); - - if (tileStart < 0) - { - return 0; - } - else - { - AllocSpriteTileRange(sheet->tag, (u16)tileStart, sheet->size / TILE_SIZE_4BPP); - CpuCopy16(sheet->data, (u8 *)OBJ_VRAM0 + TILE_SIZE_4BPP * tileStart, sheet->size); - return (u16)tileStart; - } -} - -void LoadSpriteSheets(const struct SpriteSheet *sheets) -{ - u8 i; - for (i = 0; sheets[i].data != NULL; i++) - LoadSpriteSheet(&sheets[i]); -} - -void FreeSpriteTilesByTag(u16 tag) -{ - u8 index = IndexOfSpriteTileTag(tag); - if (index != 0xFF) - { - u16 i; - u16 *rangeStarts; - u16 *rangeCounts; - u16 start; - u16 count; - rangeStarts = sSpriteTileRanges; - start = rangeStarts[index * 2]; - rangeCounts = sSpriteTileRanges + 1; - count = rangeCounts[index * 2]; - - for (i = start; i < start + count; i++) - FREE_SPRITE_TILE(i); - - sSpriteTileRangeTags[index] = 0xFFFF; - } -} - -void FreeSpriteTileRanges(void) -{ - u8 i; - - for (i = 0; i < MAX_SPRITES; i++) - { - sSpriteTileRangeTags[i] = 0xFFFF; - SET_SPRITE_TILE_RANGE(i, 0, 0); - } -} - -u16 GetSpriteTileStartByTag(u16 tag) -{ - u8 index = IndexOfSpriteTileTag(tag); - if (index == 0xFF) - return 0xFFFF; - return sSpriteTileRanges[index * 2]; -} - -u8 IndexOfSpriteTileTag(u16 tag) -{ - u8 i; - - for (i = 0; i < MAX_SPRITES; i++) - if (sSpriteTileRangeTags[i] == tag) - return i; - - return 0xFF; -} - -u16 GetSpriteTileTagByTileStart(u16 start) -{ - u8 i; - - for (i = 0; i < MAX_SPRITES; i++) - { - if (sSpriteTileRangeTags[i] != 0xFFFF && sSpriteTileRanges[i * 2] == start) - return sSpriteTileRangeTags[i]; - } - - return 0xFFFF; -} - -void AllocSpriteTileRange(u16 tag, u16 start, u16 count) -{ - u8 freeIndex = IndexOfSpriteTileTag(0xFFFF); - sSpriteTileRangeTags[freeIndex] = tag; - SET_SPRITE_TILE_RANGE(freeIndex, start, count); -} - -void FreeAllSpritePalettes(void) -{ - u8 i; - gReservedSpritePaletteCount = 0; - for (i = 0; i < 16; i++) - sSpritePaletteTags[i] = 0xFFFF; -} - -u8 LoadSpritePalette(const struct SpritePalette *palette) -{ - u8 index = IndexOfSpritePaletteTag(palette->tag); - - if (index != 0xFF) - return index; - - index = IndexOfSpritePaletteTag(0xFFFF); - - if (index == 0xFF) - { - return 0xFF; - } - else - { - sSpritePaletteTags[index] = palette->tag; - DoLoadSpritePalette(palette->data, index * 16); - return index; - } -} - -void LoadSpritePalettes(const struct SpritePalette *palettes) -{ - u8 i; - for (i = 0; palettes[i].data != NULL; i++) - if (LoadSpritePalette(&palettes[i]) == 0xFF) - break; -} - -void DoLoadSpritePalette(const u16 *src, u16 paletteOffset) -{ - LoadPalette(src, paletteOffset + 0x100, 32); -} - -u8 AllocSpritePalette(u16 tag) -{ - u8 index = IndexOfSpritePaletteTag(0xFFFF); - if (index == 0xFF) - { - return 0xFF; - } - else - { - sSpritePaletteTags[index] = tag; - return index; - } -} - -u8 IndexOfSpritePaletteTag(u16 tag) -{ - u8 i; - for (i = gReservedSpritePaletteCount; i < 16; i++) - if (sSpritePaletteTags[i] == tag) - return i; - - return 0xFF; -} - -u16 GetSpritePaletteTagByPaletteNum(u8 paletteNum) -{ - return sSpritePaletteTags[paletteNum]; -} - -void FreeSpritePaletteByTag(u16 tag) -{ - u8 index = IndexOfSpritePaletteTag(tag); - if (index != 0xFF) - sSpritePaletteTags[index] = 0xFFFF; -} - -void SetSubspriteTables(struct Sprite *sprite, const struct SubspriteTable *subspriteTables) -{ - sprite->subspriteTables = subspriteTables; - sprite->subspriteTableNum = 0; - sprite->subspriteMode = SUBSPRITES_ON; -} - -bool8 AddSpriteToOamBuffer(struct Sprite *sprite, u8 *oamIndex) -{ - if (*oamIndex >= gOamLimit) - return 1; - - if (!sprite->subspriteTables || sprite->subspriteMode == SUBSPRITES_OFF) - { - gMain.oamBuffer[*oamIndex] = sprite->oam; - (*oamIndex)++; - return 0; - } - else - { - return AddSubspritesToOamBuffer(sprite, &gMain.oamBuffer[*oamIndex], oamIndex); - } -} - -bool8 AddSubspritesToOamBuffer(struct Sprite *sprite, struct OamData *destOam, u8 *oamIndex) -{ - const struct SubspriteTable *subspriteTable; - struct OamData *oam; - - if (*oamIndex >= gOamLimit) - return 1; - - subspriteTable = &sprite->subspriteTables[sprite->subspriteTableNum]; - oam = &sprite->oam; - - if (!subspriteTable || !subspriteTable->subsprites) - { - *destOam = *oam; - (*oamIndex)++; - return 0; - } - else - { - u16 tileNum; - u16 baseX; - u16 baseY; - u8 subspriteCount; - u8 hFlip; - u8 vFlip; - u8 i; - - tileNum = oam->tileNum; - subspriteCount = subspriteTable->subspriteCount; - hFlip = ((s32)oam->matrixNum >> 3) & 1; - vFlip = ((s32)oam->matrixNum >> 4) & 1; - baseX = oam->x - sprite->centerToCornerVecX; - baseY = oam->y - sprite->centerToCornerVecY; - - for (i = 0; i < subspriteCount; i++, (*oamIndex)++) - { - u16 x; - u16 y; - - if (*oamIndex >= gOamLimit) - return 1; - - x = subspriteTable->subsprites[i].x; - y = subspriteTable->subsprites[i].y; - - if (hFlip) - { - s8 width = sOamDimensions[subspriteTable->subsprites[i].shape][subspriteTable->subsprites[i].size].width; - s16 right = x; - right += width; - x = right; - x = ~x + 1; - } - - if (vFlip) - { - s8 height = sOamDimensions[subspriteTable->subsprites[i].shape][subspriteTable->subsprites[i].size].height; - s16 bottom = y; - bottom += height; - y = bottom; - y = ~y + 1; - } - - destOam[i] = *oam; - destOam[i].shape = subspriteTable->subsprites[i].shape; - destOam[i].size = subspriteTable->subsprites[i].size; - destOam[i].x = (s16)baseX + (s16)x; - destOam[i].y = baseY + y; - destOam[i].tileNum = tileNum + subspriteTable->subsprites[i].tileOffset; - - if (sprite->subspriteMode != SUBSPRITES_IGNORE_PRIORITY) - destOam[i].priority = subspriteTable->subsprites[i].priority; - } - } - - return 0; -} diff --git a/src/string_util.c b/src/string_util.c deleted file mode 100644 index 39d235ab8..000000000 --- a/src/string_util.c +++ /dev/null @@ -1,784 +0,0 @@ -#include "global.h" -#include "string_util.h" -#include "text.h" - -EWRAM_DATA u8 gStringVar1[0x100] = {0}; -EWRAM_DATA u8 gStringVar2[0x100] = {0}; -EWRAM_DATA u8 gStringVar3[0x100] = {0}; -EWRAM_DATA u8 gStringVar4[0x3E8] = {0}; -EWRAM_DATA static u8 sUnknownStringVar[16] = {0}; - -static const u8 sDigits[] = __("0123456789ABCDEF"); - -static const s32 sPowersOfTen[] = -{ - 1, - 10, - 100, - 1000, - 10000, - 100000, - 1000000, - 10000000, - 100000000, - 1000000000, -}; - -extern const u8 gExpandedPlaceholder_Empty[]; -extern const u8 gExpandedPlaceholder_Kun[]; -extern const u8 gExpandedPlaceholder_Chan[]; -extern const u8 gExpandedPlaceholder_Sapphire[]; -extern const u8 gExpandedPlaceholder_Ruby[]; -extern const u8 gExpandedPlaceholder_Emerald[]; -extern const u8 gExpandedPlaceholder_Aqua[]; -extern const u8 gExpandedPlaceholder_Magma[]; -extern const u8 gExpandedPlaceholder_Archie[]; -extern const u8 gExpandedPlaceholder_Maxie[]; -extern const u8 gExpandedPlaceholder_Kyogre[]; -extern const u8 gExpandedPlaceholder_Groudon[]; -extern const u8 gExpandedPlaceholder_Brendan[]; -extern const u8 gExpandedPlaceholder_May[]; - -u8 *StringCopy10(u8 *dest, const u8 *src) -{ - u8 i; - u32 limit = 10; - - for (i = 0; i < limit; i++) - { - dest[i] = src[i]; - - if (dest[i] == EOS) - return &dest[i]; - } - - dest[i] = EOS; - return &dest[i]; -} - -u8 *StringGetEnd10(u8 *str) -{ - u8 i; - u32 limit = 10; - - for (i = 0; i < limit; i++) - if (str[i] == EOS) - return &str[i]; - - str[i] = EOS; - return &str[i]; -} - -u8 *StringCopy7(u8 *dest, const u8 *src) -{ - s32 i; - s32 limit = 7; - - for (i = 0; i < limit; i++) - { - dest[i] = src[i]; - - if (dest[i] == EOS) - return &dest[i]; - } - - dest[i] = EOS; - return &dest[i]; -} - -u8 *StringCopy(u8 *dest, const u8 *src) -{ - while (*src != EOS) - { - *dest = *src; - dest++; - src++; - } - - *dest = EOS; - return dest; -} - -u8 *StringAppend(u8 *dest, const u8 *src) -{ - while (*dest != EOS) - dest++; - - return StringCopy(dest, src); -} - -u8 *StringCopyN(u8 *dest, const u8 *src, u8 n) -{ - u16 i; - - for (i = 0; i < n; i++) - dest[i] = src[i]; - - return &dest[n]; -} - -u8 *StringAppendN(u8 *dest, const u8 *src, u8 n) -{ - while (*dest != EOS) - dest++; - - return StringCopyN(dest, src, n); -} - -u16 StringLength(const u8 *str) -{ - u16 length = 0; - - while (str[length] != EOS) - length++; - - return length; -} - -s32 StringCompare(const u8 *str1, const u8 *str2) -{ - while (*str1 == *str2) - { - if (*str1 == EOS) - return 0; - str1++; - str2++; - } - - return *str1 - *str2; -} - -s32 StringCompareN(const u8 *str1, const u8 *str2, u32 n) -{ - while (*str1 == *str2) - { - if (*str1 == EOS) - return 0; - str1++; - str2++; - if (--n == 0) - return 0; - } - - return *str1 - *str2; -} - -bool8 IsStringLengthAtLeast(const u8 *str, s32 n) -{ - u8 i; - - for (i = 0; i < n; i++) - if (str[i] && str[i] != EOS) - return TRUE; - - return FALSE; -} - -u8 *ConvertIntToDecimalStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n) -{ - enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state; - s32 powerOfTen; - s32 largestPowerOfTen = sPowersOfTen[n - 1]; - - state = WAITING_FOR_NONZERO_DIGIT; - - if (mode == STR_CONV_MODE_RIGHT_ALIGN) - state = WRITING_SPACES; - - if (mode == STR_CONV_MODE_LEADING_ZEROS) - state = WRITING_DIGITS; - - for (powerOfTen = largestPowerOfTen; powerOfTen > 0; powerOfTen /= 10) - { - u8 c; - u16 digit = value / powerOfTen; - s32 temp = value - (powerOfTen * digit); - - if (state == WRITING_DIGITS) - { - u8 *out = dest++; - - if (digit <= 9) - c = sDigits[digit]; - else - c = CHAR_QUESTION_MARK; - - *out = c; - } - else if (digit != 0 || powerOfTen == 1) - { - u8 *out; - state = WRITING_DIGITS; - out = dest++; - - if (digit <= 9) - c = sDigits[digit]; - else - c = CHAR_QUESTION_MARK; - - *out = c; - } - else if (state == WRITING_SPACES) - { - *dest++ = 0x77; - } - - value = temp; - } - - *dest = EOS; - return dest; -} - -u8 *ConvertUIntToDecimalStringN(u8 *dest, u32 value, enum StringConvertMode mode, u8 n) -{ - enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state; - s32 powerOfTen; - s32 largestPowerOfTen = sPowersOfTen[n - 1]; - - state = WAITING_FOR_NONZERO_DIGIT; - - if (mode == STR_CONV_MODE_RIGHT_ALIGN) - state = WRITING_SPACES; - - if (mode == STR_CONV_MODE_LEADING_ZEROS) - state = WRITING_DIGITS; - - for (powerOfTen = largestPowerOfTen; powerOfTen > 0; powerOfTen /= 10) - { - u8 c; - u16 digit = value / powerOfTen; - u32 temp = value - (powerOfTen * digit); - - if (state == WRITING_DIGITS) - { - u8 *out = dest++; - - if (digit <= 9) - c = sDigits[digit]; - else - c = CHAR_QUESTION_MARK; - - *out = c; - } - else if (digit != 0 || powerOfTen == 1) - { - u8 *out; - state = WRITING_DIGITS; - out = dest++; - - if (digit <= 9) - c = sDigits[digit]; - else - c = CHAR_QUESTION_MARK; - - *out = c; - } - else if (state == WRITING_SPACES) - { - *dest++ = 0x77; - } - - value = temp; - } - - *dest = EOS; - return dest; -} - -u8 *ConvertIntToHexStringN(u8 *dest, s32 value, enum StringConvertMode mode, u8 n) -{ - enum { WAITING_FOR_NONZERO_DIGIT, WRITING_DIGITS, WRITING_SPACES } state; - u8 i; - s32 powerOfSixteen; - s32 largestPowerOfSixteen = 1; - - for (i = 1; i < n; i++) - largestPowerOfSixteen *= 16; - - state = WAITING_FOR_NONZERO_DIGIT; - - if (mode == STR_CONV_MODE_RIGHT_ALIGN) - state = WRITING_SPACES; - - if (mode == STR_CONV_MODE_LEADING_ZEROS) - state = WRITING_DIGITS; - - for (powerOfSixteen = largestPowerOfSixteen; powerOfSixteen > 0; powerOfSixteen /= 16) - { - u8 c; - u32 digit = value / powerOfSixteen; - s32 temp = value % powerOfSixteen; - - if (state == WRITING_DIGITS) - { - char *out = dest++; - - if (digit <= 0xF) - c = sDigits[digit]; - else - c = CHAR_QUESTION_MARK; - - *out = c; - } - else if (digit != 0 || powerOfSixteen == 1) - { - char *out; - state = WRITING_DIGITS; - out = dest++; - - if (digit <= 0xF) - c = sDigits[digit]; - else - c = CHAR_QUESTION_MARK; - - *out = c; - } - else if (state == WRITING_SPACES) - { - *dest++ = 0x77; - } - - value = temp; - } - - *dest = EOS; - return dest; -} - -u8 *StringExpandPlaceholders(u8 *dest, const u8 *src) -{ - for (;;) - { - u8 c = *src++; - u8 placeholderId; - const u8 *expandedString; - - switch (c) - { - case PLACEHOLDER_BEGIN: - placeholderId = *src++; - expandedString = GetExpandedPlaceholder(placeholderId); - dest = StringExpandPlaceholders(dest, expandedString); - break; - case EXT_CTRL_CODE_BEGIN: - *dest++ = c; - c = *src++; - *dest++ = c; - - switch (c) - { - case 0x07: - case 0x09: - case 0x0F: - case 0x15: - case 0x16: - case 0x17: - case 0x18: - break; - case 0x04: - *dest++ = *src++; - case 0x0B: - *dest++ = *src++; - default: - *dest++ = *src++; - } - break; - case EOS: - *dest = EOS; - return dest; - case CHAR_PROMPT_SCROLL: - case CHAR_PROMPT_CLEAR: - case CHAR_NEWLINE: - default: - *dest++ = c; - } - } -} - -u8 *StringBraille(u8 *dest, const u8 *src) -{ - u8 setBrailleFont[] = { EXT_CTRL_CODE_BEGIN, 0x06, 0x06, EOS }; - u8 gotoLine2[] = { CHAR_NEWLINE, EXT_CTRL_CODE_BEGIN, 0x0E, 0x02, EOS }; - - dest = StringCopy(dest, setBrailleFont); - - for (;;) - { - u8 c = *src++; - - switch (c) - { - case EOS: - *dest = c; - return dest; - case CHAR_NEWLINE: - dest = StringCopy(dest, gotoLine2); - break; - default: - *dest++ = c; - *dest++ = c + 0x40; - break; - } - } -} - -static const u8 *ExpandPlaceholder_UnknownStringVar(void) -{ - return sUnknownStringVar; -} - -static const u8 *ExpandPlaceholder_PlayerName(void) -{ - return gSaveBlock2Ptr->playerName; -} - -static const u8 *ExpandPlaceholder_StringVar1(void) -{ - return gStringVar1; -} - -static const u8 *ExpandPlaceholder_StringVar2(void) -{ - return gStringVar2; -} - -static const u8 *ExpandPlaceholder_StringVar3(void) -{ - return gStringVar3; -} - -static const u8 *ExpandPlaceholder_KunChan(void) -{ - if (gSaveBlock2Ptr->playerGender == MALE) - return gExpandedPlaceholder_Kun; - else - return gExpandedPlaceholder_Chan; -} - -static const u8 *ExpandPlaceholder_RivalName(void) -{ - if (gSaveBlock2Ptr->playerGender == MALE) - return gExpandedPlaceholder_May; - else - return gExpandedPlaceholder_Brendan; -} - -static const u8 *ExpandPlaceholder_Version(void) -{ - return gExpandedPlaceholder_Emerald; -} - -static const u8 *ExpandPlaceholder_Aqua(void) -{ - return gExpandedPlaceholder_Aqua; -} - -static const u8 *ExpandPlaceholder_Magma(void) -{ - return gExpandedPlaceholder_Magma; -} - -static const u8 *ExpandPlaceholder_Archie(void) -{ - return gExpandedPlaceholder_Archie; -} - -static const u8 *ExpandPlaceholder_Maxie(void) -{ - return gExpandedPlaceholder_Maxie; -} - -static const u8 *ExpandPlaceholder_Kyogre(void) -{ - return gExpandedPlaceholder_Kyogre; -} - -static const u8 *ExpandPlaceholder_Groudon(void) -{ - return gExpandedPlaceholder_Groudon; -} - -const u8 *GetExpandedPlaceholder(u32 id) -{ - typedef const u8 *(*ExpandPlaceholderFunc)(void); - - static const ExpandPlaceholderFunc funcs[] = - { - ExpandPlaceholder_UnknownStringVar, - ExpandPlaceholder_PlayerName, - ExpandPlaceholder_StringVar1, - ExpandPlaceholder_StringVar2, - ExpandPlaceholder_StringVar3, - ExpandPlaceholder_KunChan, - ExpandPlaceholder_RivalName, - ExpandPlaceholder_Version, - ExpandPlaceholder_Aqua, - ExpandPlaceholder_Magma, - ExpandPlaceholder_Archie, - ExpandPlaceholder_Maxie, - ExpandPlaceholder_Kyogre, - ExpandPlaceholder_Groudon, - }; - - if (id >= ARRAY_COUNT(funcs)) - return gExpandedPlaceholder_Empty; - else - return funcs[id](); -} - -u8 *StringFill(u8 *dest, u8 c, u16 n) -{ - u16 i; - - for (i = 0; i < n; i++) - *dest++ = c; - - *dest = EOS; - return dest; -} - -u8 *StringCopyPadded(u8 *dest, const u8 *src, u8 c, u16 n) -{ - while (*src != EOS) - { - *dest++ = *src++; - - if (n) - n--; - } - - n--; - - while (n != (u16)-1) - { - *dest++ = c; - n--; - } - - *dest = EOS; - return dest; -} - -u8 *StringFillWithTerminator(u8 *dest, u16 n) -{ - return StringFill(dest, EOS, n); -} - -u8 *StringCopyN_Multibyte(u8 *dest, u8 *src, u32 n) -{ - u32 i; - - for (i = n - 1; i != (u32)-1; i--) - { - if (*src == EOS) - { - break; - } - else - { - *dest++ = *src++; - if (*(src - 1) == CHAR_SPECIAL_F9) - *dest++ = *src++; - } - } - - *dest = EOS; - return dest; -} - -u32 StringLength_Multibyte(const u8 *str) -{ - u32 length = 0; - - while (*str != EOS) - { - if (*str == CHAR_SPECIAL_F9) - str++; - str++; - length++; - } - - return length; -} - -u8 *WriteColorChangeControlCode(u8 *dest, u32 colorType, u8 color) -{ - *dest = EXT_CTRL_CODE_BEGIN; - dest++; - - switch (colorType) - { - case 0: - *dest = 1; - dest++; - break; - case 1: - *dest = 3; - dest++; - break; - case 2: - *dest = 2; - dest++; - break; - } - - *dest = color; - dest++; - *dest = EOS; - return dest; -} - -bool32 IsStringJapanese(u8 *str) -{ - while (*str != EOS) - { - if (*str <= 0xA0) - if (*str != CHAR_SPACE) - return TRUE; - str++; - } - - return FALSE; -} - -bool32 sub_800924C(u8 *str, s32 n) -{ - s32 i; - - for (i = 0; *str != EOS && i < n; i++) - { - if (*str <= 0xA0) - if (*str != CHAR_SPACE) - return TRUE; - str++; - } - - return FALSE; -} - -u8 GetExtCtrlCodeLength(u8 code) -{ - static const u8 lengths[] = - { - 1, - 2, - 2, - 2, - 4, - 2, - 2, - 1, - 2, - 1, - 1, - 3, - 2, - 2, - 2, - 1, - 3, - 2, - 2, - 2, - 2, - 1, - 1, - 1, - 1, - }; - - u8 length = 0; - if (code < ARRAY_COUNT(lengths)) - length = lengths[code]; - return length; -} - -static const u8 *SkipExtCtrlCode(const u8 *s) -{ - while (*s == EXT_CTRL_CODE_BEGIN) - { - s++; - s += GetExtCtrlCodeLength(*s); - } - - return s; -} - -s32 StringCompareWithoutExtCtrlCodes(const u8 *str1, const u8 *str2) -{ - s32 retVal = 0; - - while (1) - { - str1 = SkipExtCtrlCode(str1); - str2 = SkipExtCtrlCode(str2); - - if (*str1 > *str2) - break; - - if (*str1 < *str2) - { - retVal = -1; - if (*str2 == EOS) - retVal = 1; - } - - if (*str1 == EOS) - return retVal; - - str1++; - str2++; - } - - retVal = 1; - - if (*str1 == EOS) - retVal = -1; - - return retVal; -} - -void ConvertInternationalString(u8 *s, u8 language) -{ - if (language == LANGUAGE_JAPANESE) - { - u8 i; - - StripExtCtrlCodes(s); - i = StringLength(s); - s[i++] = EXT_CTRL_CODE_BEGIN; - s[i++] = 22; - s[i++] = EOS; - - i--; - - while (i != (u8)-1) - { - s[i + 2] = s[i]; - i--; - } - - s[0] = EXT_CTRL_CODE_BEGIN; - s[1] = 21; - } -} - -void StripExtCtrlCodes(u8 *str) -{ - u16 srcIndex = 0; - u16 destIndex = 0; - while (str[srcIndex] != EOS) - { - if (str[srcIndex] == EXT_CTRL_CODE_BEGIN) - { - srcIndex++; - srcIndex += GetExtCtrlCodeLength(str[srcIndex]); - } - else - { - str[destIndex++] = str[srcIndex++]; - } - } - str[destIndex] = EOS; -} diff --git a/src/text.c b/src/text.c deleted file mode 100644 index 7e4fa7104..000000000 --- a/src/text.c +++ /dev/null @@ -1,2540 +0,0 @@ -#include "global.h" -#include "battle.h" -#include "main.h" -#include "m4a.h" -#include "palette.h" -#include "sound.h" -#include "constants/songs.h" -#include "string_util.h" -#include "window.h" -#include "text.h" -#include "blit.h" -#include "menu.h" -#include "dynamic_placeholder_text_util.h" - -EWRAM_DATA struct TextPrinter gTempTextPrinter = {0}; -EWRAM_DATA struct TextPrinter gTextPrinters[NUM_TEXT_PRINTERS] = {0}; - -static u16 gFontHalfRowLookupTable[0x51]; -static u16 gLastTextBgColor; -static u16 gLastTextFgColor; -static u16 gLastTextShadowColor; - -const struct FontInfo *gFonts; -u8 gUnknown_03002F84; -struct Struct_03002F90 gUnknown_03002F90; -TextFlags gTextFlags; - -const u8 gFontHalfRowOffsets[] = -{ - 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00, - 0x09, 0x0A, 0x0B, 0x09, 0x0C, 0x0D, 0x0E, 0x0C, 0x0F, 0x10, 0x11, 0x0F, 0x09, 0x0A, 0x0B, 0x09, - 0x12, 0x13, 0x14, 0x12, 0x15, 0x16, 0x17, 0x15, 0x18, 0x19, 0x1A, 0x18, 0x12, 0x13, 0x14, 0x12, - 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00, - 0x1B, 0x1C, 0x1D, 0x1B, 0x1E, 0x1F, 0x20, 0x1E, 0x21, 0x22, 0x23, 0x21, 0x1B, 0x1C, 0x1D, 0x1B, - 0x24, 0x25, 0x26, 0x24, 0x27, 0x28, 0x29, 0x27, 0x2A, 0x2B, 0x2C, 0x2A, 0x24, 0x25, 0x26, 0x24, - 0x2D, 0x2E, 0x2F, 0x2D, 0x30, 0x31, 0x32, 0x30, 0x33, 0x34, 0x35, 0x33, 0x2D, 0x2E, 0x2F, 0x2D, - 0x1B, 0x1C, 0x1D, 0x1B, 0x1E, 0x1F, 0x20, 0x1E, 0x21, 0x22, 0x23, 0x21, 0x1B, 0x1C, 0x1D, 0x1B, - 0x36, 0x37, 0x38, 0x36, 0x39, 0x3A, 0x3B, 0x39, 0x3C, 0x3D, 0x3E, 0x3C, 0x36, 0x37, 0x38, 0x36, - 0x3F, 0x40, 0x41, 0x3F, 0x42, 0x43, 0x44, 0x42, 0x45, 0x46, 0x47, 0x45, 0x3F, 0x40, 0x41, 0x3F, - 0x48, 0x49, 0x4A, 0x48, 0x4B, 0x4C, 0x4D, 0x4B, 0x4E, 0x4F, 0x50, 0x4E, 0x48, 0x49, 0x4A, 0x48, - 0x36, 0x37, 0x38, 0x36, 0x39, 0x3A, 0x3B, 0x39, 0x3C, 0x3D, 0x3E, 0x3C, 0x36, 0x37, 0x38, 0x36, - 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00, - 0x09, 0x0A, 0x0B, 0x09, 0x0C, 0x0D, 0x0E, 0x0C, 0x0F, 0x10, 0x11, 0x0F, 0x09, 0x0A, 0x0B, 0x09, - 0x12, 0x13, 0x14, 0x12, 0x15, 0x16, 0x17, 0x15, 0x18, 0x19, 0x1A, 0x18, 0x12, 0x13, 0x14, 0x12, - 0x00, 0x01, 0x02, 0x00, 0x03, 0x04, 0x05, 0x03, 0x06, 0x07, 0x08, 0x06, 0x00, 0x01, 0x02, 0x00 -}; - -const u8 gDownArrowTiles[] = INCBIN_U8("data/graphics/fonts/down_arrow.4bpp"); -const u8 gDarkDownArrowTiles[] = INCBIN_U8("data/graphics/fonts/down_arrow_RS.4bpp"); -const u8 gUnusedFRLGBlankedDownArrow[] = INCBIN_U8("data/graphics/fonts/unused_frlg_blanked_down_arrow.4bpp"); -const u8 gUnusedFRLGDownArrow[] = INCBIN_U8("data/graphics/fonts/unused_frlg_down_arrow.4bpp"); -const u8 gDownArrowYCoords[] = { 0x0, 0x1, 0x2, 0x1 }; -const u8 gWindowVerticalScrollSpeeds[] = { 0x1, 0x2, 0x4, 0x0 }; - -const struct GlyphWidthFunc gGlyphWidthFuncs[] = -{ - { 0x0, GetGlyphWidthFont0 }, - { 0x1, GetGlyphWidthFont1 }, - { 0x2, GetGlyphWidthFont2 }, - { 0x3, GetGlyphWidthFont2 }, - { 0x4, GetGlyphWidthFont2 }, - { 0x5, GetGlyphWidthFont2 }, - { 0x6, GetGlyphWidthFont6 }, - { 0x7, GetGlyphWidthFont7 }, - { 0x8, GetGlyphWidthFont8 } -}; - -const struct KeypadIcon gKeypadIcons[] = -{ - { 0x0, 0x8, 0xC }, - { 0x1, 0x8, 0xC }, - { 0x2, 0x10, 0xC }, - { 0x4, 0x10, 0xC }, - { 0x6, 0x18, 0xC }, - { 0x9, 0x18, 0xC }, - { 0xC, 0x8, 0xC }, - { 0xD, 0x8, 0xC }, - { 0xE, 0x8, 0xC }, - { 0xF, 0x8, 0xC }, - { 0x20, 0x8, 0xC }, - { 0x21, 0x8, 0xC }, - { 0x22, 0x8, 0xC } -}; - -const u8 gKeypadIconTiles[] = INCBIN_U8("data/graphics/fonts/keypad_icons.4bpp"); - -const struct FontInfo gFontInfos[] = -{ - { Font0Func, 0x5, 0xC, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font1Func, 0x6, 0x10, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font2Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font3Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font4Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font5Func, 0x6, 0xE, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font6Func, 0x8, 0x10, 0x0, 0x8, 0x0, 0x2, 0x1, 0x3 }, - { Font7Func, 0x5, 0x10, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { Font8Func, 0x5, 0x8, 0x0, 0x0, 0x0, 0x2, 0x1, 0x3 }, - { NULL, 0x8, 0x8, 0x0, 0x0, 0x0, 0x1, 0x2, 0xF } -}; - -const u8 gMenuCursorDimensions[][2] = -{ - { 0x8, 0xC }, - { 0x8, 0xF }, - { 0x8, 0xE }, - { 0x8, 0xE }, - { 0x8, 0xE }, - { 0x8, 0xE }, - { 0x8, 0x10 }, - { 0x8, 0xF }, - { 0x8, 0x8 }, - { 0x0, 0x0 } -}; - -const u16 gFont9JapaneseGlyphs[] = INCBIN_U16("data/graphics/fonts/font9.hwjpnfont"); - -extern const u16 gFont8LatinGlyphs[]; -extern const u8 gFont8LatinGlyphWidths[]; -extern const u16 gFont0LatinGlyphs[]; -extern const u8 gFont0LatinGlyphWidths[]; -extern const u16 gFont7LatinGlyphs[]; -extern const u8 gFont7LatinGlyphWidths[]; -extern const u16 gFont2LatinGlyphs[]; -extern const u8 gFont2LatinGlyphWidths[]; -extern const u16 gFont1LatinGlyphs[]; -extern const u8 gFont1LatinGlyphWidths[]; -extern const u16 gFont0JapaneseGlyphs[]; -extern const u16 gFont1JapaneseGlyphs[]; -extern const u16 gFont2JapaneseGlyphs[]; -extern const u8 gFont2JapaneseGlyphWidths[]; - -void SetFontsPointer(const struct FontInfo *fonts) -{ - gFonts = fonts; -} - -void DeactivateAllTextPrinters(void) -{ - int printer; - for (printer = 0; printer < NUM_TEXT_PRINTERS; ++printer) - gTextPrinters[printer].active = 0; -} - -u16 AddTextPrinterParameterized(u8 windowId, u8 fontId, const u8 *str, u8 x, u8 y, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)) -{ - struct TextPrinterTemplate printerTemplate; - - printerTemplate.currentChar = str; - printerTemplate.windowId = windowId; - printerTemplate.fontId = fontId; - printerTemplate.x = x; - printerTemplate.y = y; - printerTemplate.currentX = x; - printerTemplate.currentY = y; - printerTemplate.letterSpacing = gFonts[fontId].letterSpacing; - printerTemplate.lineSpacing = gFonts[fontId].lineSpacing; - printerTemplate.unk = gFonts[fontId].unk; - printerTemplate.fgColor = gFonts[fontId].fgColor; - printerTemplate.bgColor = gFonts[fontId].bgColor; - printerTemplate.shadowColor = gFonts[fontId].shadowColor; - return AddTextPrinter(&printerTemplate, speed, callback); -} - -bool16 AddTextPrinter(struct TextPrinterTemplate *printerTemplate, u8 speed, void (*callback)(struct TextPrinterTemplate *, u16)) -{ - int i; - u16 j; - u8 *ptr; - - if (!gFonts) - return FALSE; - - gTempTextPrinter.active = 1; - gTempTextPrinter.state = 0; - gTempTextPrinter.textSpeed = speed; - gTempTextPrinter.delayCounter = 0; - gTempTextPrinter.scrollDistance = 0; - - for (i = 0; i < 7; i++) - { - gTempTextPrinter.subUnion.fields[i] = 0; - } - - gTempTextPrinter.printerTemplate = *printerTemplate; - gTempTextPrinter.callback = callback; - gTempTextPrinter.minLetterSpacing = 0; - gTempTextPrinter.japanese = 0; - - GenerateFontHalfRowLookupTable(printerTemplate->fgColor, printerTemplate->bgColor, printerTemplate->shadowColor); - if (speed != TEXT_SPEED_FF && speed != 0x0) - { - --gTempTextPrinter.textSpeed; - gTextPrinters[printerTemplate->windowId] = gTempTextPrinter; - } - else - { - gTempTextPrinter.textSpeed = 0; - for (j = 0; j < 0x400; ++j) - { - if (RenderFont(&gTempTextPrinter) == 1) - break; - } - - if (speed != TEXT_SPEED_FF) - CopyWindowToVram(gTempTextPrinter.printerTemplate.windowId, 2); - gTextPrinters[printerTemplate->windowId].active = 0; - } - gUnknown_03002F84 = 0; - return TRUE; -} - -void RunTextPrinters(void) -{ - int i; - - if (gUnknown_03002F84 == 0) - { - for (i = 0; i < 0x20; ++i) - { - if (gTextPrinters[i].active) - { - u16 temp = RenderFont(&gTextPrinters[i]); - switch (temp) - { - case 0: - CopyWindowToVram(gTextPrinters[i].printerTemplate.windowId, 2); - case 3: - if (gTextPrinters[i].callback != 0) - gTextPrinters[i].callback(&gTextPrinters[i].printerTemplate, temp); - break; - case 1: - gTextPrinters[i].active = 0; - break; - } - } - } - } -} - -bool16 IsTextPrinterActive(u8 id) -{ - return gTextPrinters[id].active; -} - -u32 RenderFont(struct TextPrinter *textPrinter) -{ - u32 ret; - while (TRUE) - { - ret = gFonts[textPrinter->printerTemplate.fontId].fontFunction(textPrinter); - if (ret != 2) - return ret; - } -} - -void GenerateFontHalfRowLookupTable(u8 fgColor, u8 bgColor, u8 shadowColor) -{ - u32 fg12, bg12, shadow12; - u32 temp; - - u16 *current = gFontHalfRowLookupTable; - - gLastTextBgColor = bgColor; - gLastTextFgColor = fgColor; - gLastTextShadowColor = shadowColor; - - bg12 = bgColor << 12; - fg12 = fgColor << 12; - shadow12 = shadowColor << 12; - - temp = (bgColor << 8) | (bgColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (bgColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (bgColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (fgColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (fgColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (fgColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (shadowColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (shadowColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (shadowColor << 4) | bgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (bgColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (bgColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (bgColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (fgColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (fgColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (fgColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (shadowColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (shadowColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (shadowColor << 4) | fgColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (bgColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (bgColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (bgColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (fgColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (fgColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (fgColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (bgColor << 8) | (shadowColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (fgColor << 8) | (shadowColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; - - temp = (shadowColor << 8) | (shadowColor << 4) | shadowColor; - *(current++) = (bg12) | temp; - *(current++) = (fg12) | temp; - *(current++) = (shadow12) | temp; -} - -void SaveTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor) -{ - *bgColor = gLastTextBgColor; - *fgColor = gLastTextFgColor; - *shadowColor = gLastTextShadowColor; -} - -void RestoreTextColors(u8 *fgColor, u8 *bgColor, u8 *shadowColor) -{ - GenerateFontHalfRowLookupTable(*fgColor, *bgColor, *shadowColor); -} - -void DecompressGlyphTile(const void *src_, void *dest_) -{ - u32 temp; - const u16 *src = src_; - u32 *dest = dest_; - - temp = *(src++); - *(dest)++ = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); - - temp = *(src++); - *(dest++) = ((gFontHalfRowLookupTable[gFontHalfRowOffsets[temp & 0xFF]]) << 16) | (gFontHalfRowLookupTable[gFontHalfRowOffsets[temp >> 8]]); -} - -u8 GetLastTextColor(u8 colorType) -{ - switch (colorType) - { - case 0: - return gLastTextFgColor; - case 2: - return gLastTextBgColor; - case 1: - return gLastTextShadowColor; - default: - return 0; - } -} - -#ifdef NONMATCHING - -#define GLYPH_COPY(fromY_, toY_, fromX_, toX_, unk) \ -{ \ - u32 i, j, *ptr, toY, fromX, toX, r5, toOrr, bits; \ - u8 *dst; \ - \ - j = fromX_; \ - i = fromY_; \ - ptr = unk; \ - toX = toX_; \ - toY = toY_; \ - fromX = fromX_; \ - \ - for (; i < toY; i++) \ - { \ - r5 = *(ptr++); \ - for (j = fromX; j < toX; j++) \ - { \ - toOrr = r5 & 0xF; \ - if (toOrr) \ - { \ - dst = windowTiles + ((j / 8) * 32) + ((j & 7) / 2) + ((i / 8) * widthOffset) + ((i & 7) * 4); \ - bits = ((j & 1) << 2); \ - *dst = ((toOrr << bits) | (*dst & (0xF0 >> bits))); \ - } \ - r5 >>= 4; \ - } \ - } \ -} - -void CopyGlyphToWindow(struct TextPrinter *textPrinter) -{ - struct Window *win; - struct WindowTemplate *winTempl; - struct Struct_03002F90 *unkStruct; - u32 currX, widthOffset, currY; - s32 r4, r0; - u8 *windowTiles; - - win = &gWindows[textPrinter->printerTemplate.windowId]; - winTempl = &win->window; - - r4 = (winTempl->width * 8) - textPrinter->printerTemplate.currentX; - if (r4 > gUnknown_03002F90.unk80) - r4 = gUnknown_03002F90.unk80; - - r0 = (winTempl->height * 8) - textPrinter->printerTemplate.currentY; - if (r0 > gUnknown_03002F90.unk81) - r0 = gUnknown_03002F90.unk81; - - currX = textPrinter->printerTemplate.currentX; - currY = textPrinter->printerTemplate.currentY; - unkStruct = &gUnknown_03002F90; - windowTiles = win->tileData; - widthOffset = winTempl->width * 32; - - if (r4 <= 8) - { - if (r0 <= 8) - { - GLYPH_COPY(currY, currY + r0, currX, currX + r4, unkStruct->unk0); - } - else - { - u32 temp; - GLYPH_COPY(currY, currY + 8, currX, currX + r4, unkStruct->unk0); - - temp = currY + 8; - GLYPH_COPY(temp, (temp - 8) + r0, currX, currX + r4, unkStruct->unk40); - } - } - else - { - if (r0 <= 8) - { - u32 temp; - GLYPH_COPY(currY, currY + r0, currX, currX + 8, unkStruct->unk0); - - temp = currX + 8; - GLYPH_COPY(currY, currY + r0, temp, (temp - 8) + r4, unkStruct->unk20); - } - else - { - u32 temp; - GLYPH_COPY(currY, currY + 8, currX, currX + 8, unkStruct->unk0); - - temp = currX + 8; - GLYPH_COPY(currY, currY + 8, temp, temp - 8 + r4, unkStruct->unk20); - - temp = currY + 8; - GLYPH_COPY(temp, temp - 8 + r0, currX, currX + 8, unkStruct->unk40); - { - u32 tempX, tempY; - tempX = currX + 8; - tempY = currY + 8; - GLYPH_COPY(tempY, tempY - 8 + r0, tempX, tempX - 8 + r4, unkStruct->unk60); - } - } - } -} -#else -NAKED -void CopyGlyphToWindow(struct TextPrinter *x) -{ - asm("push {r4-r7,lr}\n\ - mov r7, r10\n\ - mov r6, r9\n\ - mov r5, r8\n\ - push {r5-r7}\n\ - sub sp, #0x8C\n\ - add r3, r0, #0\n\ - ldrb r1, [r3, #0x4]\n\ - lsl r0, r1, #1\n\ - add r0, r1\n\ - lsl r0, #2\n\ - ldr r1, =gWindows\n\ - add r1, r0, r1\n\ - add r2, r1, #0\n\ - ldrb r7, [r1, #0x3]\n\ - lsl r0, r7, #3\n\ - ldrb r6, [r3, #0x8]\n\ - sub r4, r0, r6\n\ - ldr r5, =gUnknown_03002F90\n\ - add r0, r5, #0\n\ - add r0, #0x80\n\ - ldrb r0, [r0]\n\ - cmp r4, r0\n\ - ble _08004DD2\n\ - add r4, r0, #0\n\ -_08004DD2:\n\ - ldrb r0, [r1, #0x4]\n\ - lsl r0, #3\n\ - ldrb r3, [r3, #0x9]\n\ - sub r0, r3\n\ - add r1, r5, #0\n\ - add r1, #0x81\n\ - ldrb r1, [r1]\n\ - cmp r0, r1\n\ - ble _08004DE6\n\ - add r0, r1, #0\n\ -_08004DE6:\n\ - str r6, [sp]\n\ - mov r8, r3\n\ - add r3, r5, #0\n\ - ldr r2, [r2, #0x8]\n\ - mov r9, r2\n\ - lsl r1, r7, #5\n\ - str r1, [sp, #0x4]\n\ - cmp r4, #0x8\n\ - ble _08004DFA\n\ - b _08004F94\n\ -_08004DFA:\n\ - cmp r0, #0x8\n\ - bgt _08004E84\n\ - mov r1, r8\n\ - str r3, [sp, #0x8]\n\ - add r2, r6, #0\n\ - add r2, r4\n\ - mov r8, r2\n\ - add r0, r1, r0\n\ - str r0, [sp, #0xC]\n\ - str r6, [sp, #0x10]\n\ - cmp r1, r0\n\ - bcc _08004E14\n\ - b _080052AA\n\ -_08004E14:\n\ - ldr r3, [sp, #0x8]\n\ - ldm r3!, {r5}\n\ - str r3, [sp, #0x8]\n\ - ldr r4, [sp, #0x10]\n\ - add r0, r1, #0x1\n\ - mov r10, r0\n\ - cmp r4, r8\n\ - bcs _08004E72\n\ - mov r2, #0x7\n\ - mov r12, r2\n\ - lsr r0, r1, #3\n\ - ldr r2, [sp, #0x4]\n\ - add r3, r0, #0\n\ - mul r3, r2\n\ - add r7, r3, #0\n\ - mov r3, r12\n\ - and r1, r3\n\ - lsl r6, r1, #2\n\ -_08004E38:\n\ - add r3, r5, #0\n\ - mov r0, #0xF\n\ - and r3, r0\n\ - cmp r3, #0\n\ - beq _08004E6A\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r12\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_08004E6A:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r8\n\ - bcc _08004E38\n\ -_08004E72:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0xC]\n\ - cmp r1, r2\n\ - bcc _08004E14\n\ - b _080052AA\n\ - .pool\n\ -_08004E84:\n\ - mov r1, r8\n\ - str r3, [sp, #0x14]\n\ - ldr r3, [sp]\n\ - add r3, r4\n\ - mov r12, r3\n\ - add r2, r1, #0\n\ - add r2, #0x8\n\ - str r2, [sp, #0x18]\n\ - ldr r3, [sp]\n\ - str r3, [sp, #0x1C]\n\ - mov r2, r12\n\ - str r2, [sp, #0x74]\n\ - ldr r3, [sp, #0x18]\n\ - str r3, [sp, #0x88]\n\ - sub r0, #0x8\n\ - str r0, [sp, #0x80]\n\ - cmp r1, r3\n\ - bcs _08004F0E\n\ -_08004EA8:\n\ - ldr r0, [sp, #0x14]\n\ - ldm r0!, {r5}\n\ - str r0, [sp, #0x14]\n\ - ldr r4, [sp, #0x1C]\n\ - add r2, r1, #0x1\n\ - mov r8, r2\n\ - cmp r4, r12\n\ - bcs _08004F06\n\ - mov r3, #0x7\n\ - mov r10, r3\n\ - lsr r0, r1, #3\n\ - ldr r3, [sp, #0x4]\n\ - add r2, r0, #0\n\ - mul r2, r3\n\ - add r7, r2, #0\n\ - mov r0, r10\n\ - and r1, r0\n\ - lsl r6, r1, #2\n\ -_08004ECC:\n\ - add r3, r5, #0\n\ - mov r1, #0xF\n\ - and r3, r1\n\ - cmp r3, #0\n\ - beq _08004EFE\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r10\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_08004EFE:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r12\n\ - bcc _08004ECC\n\ -_08004F06:\n\ - mov r1, r8\n\ - ldr r2, [sp, #0x18]\n\ - cmp r1, r2\n\ - bcc _08004EA8\n\ -_08004F0E:\n\ - ldr r1, [sp, #0x88]\n\ - ldr r3, =gUnknown_03002F90 + 0x40\n\ - str r3, [sp, #0x20]\n\ - ldr r0, [sp, #0x74]\n\ - mov r8, r0\n\ - ldr r2, [sp, #0x80]\n\ - add r2, r1, r2\n\ - str r2, [sp, #0x24]\n\ - ldr r3, [sp]\n\ - str r3, [sp, #0x28]\n\ - cmp r1, r2\n\ - bcc _08004F28\n\ - b _080052AA\n\ -_08004F28:\n\ - ldr r0, [sp, #0x20]\n\ - ldm r0!, {r5}\n\ - str r0, [sp, #0x20]\n\ - ldr r4, [sp, #0x28]\n\ - add r2, r1, #0x1\n\ - mov r10, r2\n\ - cmp r4, r8\n\ - bcs _08004F86\n\ - mov r3, #0x7\n\ - mov r12, r3\n\ - lsr r0, r1, #3\n\ - ldr r3, [sp, #0x4]\n\ - add r2, r0, #0\n\ - mul r2, r3\n\ - add r7, r2, #0\n\ - mov r0, r12\n\ - and r1, r0\n\ - lsl r6, r1, #2\n\ -_08004F4C:\n\ - add r3, r5, #0\n\ - mov r1, #0xF\n\ - and r3, r1\n\ - cmp r3, #0\n\ - beq _08004F7E\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r12\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_08004F7E:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r8\n\ - bcc _08004F4C\n\ -_08004F86:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x24]\n\ - cmp r1, r2\n\ - bcc _08004F28\n\ - b _080052AA\n\ - .pool\n\ -_08004F94:\n\ - cmp r0, #0x8\n\ - ble _08004F9A\n\ - b _080050A4\n\ -_08004F9A:\n\ - mov r1, r8\n\ - str r3, [sp, #0x2C]\n\ - ldr r3, [sp]\n\ - add r3, #0x8\n\ - mov r12, r3\n\ - add r0, r8\n\ - str r0, [sp, #0x30]\n\ - ldr r0, [sp]\n\ - str r0, [sp, #0x34]\n\ - ldr r2, [sp, #0x30]\n\ - str r2, [sp, #0x78]\n\ - str r3, [sp, #0x84]\n\ - sub r4, #0x8\n\ - str r4, [sp, #0x7C]\n\ - cmp r8, r2\n\ - bcs _0800501C\n\ -_08004FBA:\n\ - ldr r0, [sp, #0x2C]\n\ - ldm r0!, {r5}\n\ - str r0, [sp, #0x2C]\n\ - ldr r4, [sp, #0x34]\n\ - add r2, r1, #0x1\n\ - mov r10, r2\n\ - cmp r4, r12\n\ - bcs _08005014\n\ - lsr r0, r1, #3\n\ - ldr r2, [sp, #0x4]\n\ - add r3, r0, #0\n\ - mul r3, r2\n\ - add r7, r3, #0\n\ - mov r3, #0x7\n\ - and r1, r3\n\ - lsl r6, r1, #2\n\ -_08004FDA:\n\ - add r3, r5, #0\n\ - mov r0, #0xF\n\ - and r3, r0\n\ - cmp r3, #0\n\ - beq _0800500C\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, #0x7\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_0800500C:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r12\n\ - bcc _08004FDA\n\ -_08005014:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x30]\n\ - cmp r1, r2\n\ - bcc _08004FBA\n\ -_0800501C:\n\ - mov r1, r8\n\ - ldr r3, =gUnknown_03002F90 + 0x20\n\ - str r3, [sp, #0x38]\n\ - ldr r0, [sp, #0x84]\n\ - ldr r2, [sp, #0x7C]\n\ - add r0, r2\n\ - mov r8, r0\n\ - ldr r3, [sp, #0x78]\n\ - str r3, [sp, #0x3C]\n\ - ldr r0, [sp, #0x84]\n\ - str r0, [sp, #0x40]\n\ - cmp r1, r3\n\ - bcc _08005038\n\ - b _080052AA\n\ -_08005038:\n\ - ldr r2, [sp, #0x38]\n\ - ldm r2!, {r5}\n\ - str r2, [sp, #0x38]\n\ - ldr r4, [sp, #0x40]\n\ - add r3, r1, #0x1\n\ - mov r10, r3\n\ - cmp r4, r8\n\ - bcs _08005096\n\ - mov r0, #0x7\n\ - mov r12, r0\n\ - lsr r0, r1, #3\n\ - ldr r3, [sp, #0x4]\n\ - add r2, r0, #0\n\ - mul r2, r3\n\ - add r7, r2, #0\n\ - mov r0, r12\n\ - and r1, r0\n\ - lsl r6, r1, #2\n\ -_0800505C:\n\ - add r3, r5, #0\n\ - mov r1, #0xF\n\ - and r3, r1\n\ - cmp r3, #0\n\ - beq _0800508E\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r12\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_0800508E:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r8\n\ - bcc _0800505C\n\ -_08005096:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x3C]\n\ - cmp r1, r2\n\ - bcc _08005038\n\ - b _080052AA\n\ - .pool\n\ -_080050A4:\n\ - mov r1, r8\n\ - str r5, [sp, #0x44]\n\ - ldr r3, [sp]\n\ - add r3, #0x8\n\ - mov r12, r3\n\ - mov r2, r8\n\ - add r2, #0x8\n\ - str r2, [sp, #0x48]\n\ - ldr r3, [sp]\n\ - str r3, [sp, #0x4C]\n\ - str r2, [sp, #0x88]\n\ - sub r0, #0x8\n\ - str r0, [sp, #0x80]\n\ - mov r0, r12\n\ - str r0, [sp, #0x84]\n\ - sub r4, #0x8\n\ - str r4, [sp, #0x7C]\n\ - cmp r8, r2\n\ - bcs _0800512C\n\ -_080050CA:\n\ - ldr r2, [sp, #0x44]\n\ - ldm r2!, {r5}\n\ - str r2, [sp, #0x44]\n\ - ldr r4, [sp, #0x4C]\n\ - add r3, r1, #0x1\n\ - mov r10, r3\n\ - cmp r4, r12\n\ - bcs _08005124\n\ - lsr r0, r1, #3\n\ - ldr r3, [sp, #0x4]\n\ - add r2, r0, #0\n\ - mul r2, r3\n\ - add r7, r2, #0\n\ - mov r0, #0x7\n\ - and r1, r0\n\ - lsl r6, r1, #2\n\ -_080050EA:\n\ - add r3, r5, #0\n\ - mov r1, #0xF\n\ - and r3, r1\n\ - cmp r3, #0\n\ - beq _0800511C\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, #0x7\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_0800511C:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r12\n\ - bcc _080050EA\n\ -_08005124:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x48]\n\ - cmp r1, r2\n\ - bcc _080050CA\n\ -_0800512C:\n\ - mov r1, r8\n\ - ldr r3, =gUnknown_03002F90 + 0x20\n\ - str r3, [sp, #0x50]\n\ - ldr r0, [sp, #0x84]\n\ - ldr r2, [sp, #0x7C]\n\ - add r0, r2\n\ - mov r8, r0\n\ - ldr r3, [sp, #0x88]\n\ - str r3, [sp, #0x54]\n\ - ldr r0, [sp, #0x84]\n\ - str r0, [sp, #0x58]\n\ - cmp r1, r3\n\ - bcs _080051AC\n\ -_08005146:\n\ - ldr r2, [sp, #0x50]\n\ - ldm r2!, {r5}\n\ - str r2, [sp, #0x50]\n\ - ldr r4, [sp, #0x58]\n\ - add r3, r1, #0x1\n\ - mov r10, r3\n\ - cmp r4, r8\n\ - bcs _080051A4\n\ - mov r0, #0x7\n\ - mov r12, r0\n\ - lsr r0, r1, #3\n\ - ldr r3, [sp, #0x4]\n\ - add r2, r0, #0\n\ - mul r2, r3\n\ - add r7, r2, #0\n\ - mov r0, r12\n\ - and r1, r0\n\ - lsl r6, r1, #2\n\ -_0800516A:\n\ - add r3, r5, #0\n\ - mov r1, #0xF\n\ - and r3, r1\n\ - cmp r3, #0\n\ - beq _0800519C\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r12\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_0800519C:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r8\n\ - bcc _0800516A\n\ -_080051A4:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x54]\n\ - cmp r1, r2\n\ - bcc _08005146\n\ -_080051AC:\n\ - ldr r1, [sp, #0x88]\n\ - ldr r3, =gUnknown_03002F90 + 0x40\n\ - str r3, [sp, #0x5C]\n\ - ldr r0, [sp, #0x84]\n\ - mov r8, r0\n\ - ldr r2, [sp, #0x80]\n\ - add r2, r1, r2\n\ - str r2, [sp, #0x60]\n\ - ldr r3, [sp]\n\ - str r3, [sp, #0x64]\n\ - cmp r1, r2\n\ - bcs _0800522A\n\ -_080051C4:\n\ - ldr r0, [sp, #0x5C]\n\ - ldm r0!, {r5}\n\ - str r0, [sp, #0x5C]\n\ - ldr r4, [sp, #0x64]\n\ - add r2, r1, #0x1\n\ - mov r10, r2\n\ - cmp r4, r8\n\ - bcs _08005222\n\ - mov r3, #0x7\n\ - mov r12, r3\n\ - lsr r0, r1, #3\n\ - ldr r3, [sp, #0x4]\n\ - add r2, r0, #0\n\ - mul r2, r3\n\ - add r7, r2, #0\n\ - mov r0, r12\n\ - and r1, r0\n\ - lsl r6, r1, #2\n\ -_080051E8:\n\ - add r3, r5, #0\n\ - mov r1, #0xF\n\ - and r3, r1\n\ - cmp r3, #0\n\ - beq _0800521A\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r12\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_0800521A:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r8\n\ - bcc _080051E8\n\ -_08005222:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x60]\n\ - cmp r1, r2\n\ - bcc _080051C4\n\ -_0800522A:\n\ - ldr r4, [sp, #0x84]\n\ - ldr r1, [sp, #0x88]\n\ - ldr r3, =gUnknown_03002F90 + 0x60\n\ - str r3, [sp, #0x68]\n\ - ldr r0, [sp, #0x7C]\n\ - add r0, r4\n\ - mov r8, r0\n\ - ldr r2, [sp, #0x80]\n\ - add r2, r1, r2\n\ - str r2, [sp, #0x6C]\n\ - str r4, [sp, #0x70]\n\ - cmp r1, r2\n\ - bcs _080052AA\n\ -_08005244:\n\ - ldr r3, [sp, #0x68]\n\ - ldm r3!, {r5}\n\ - str r3, [sp, #0x68]\n\ - ldr r4, [sp, #0x70]\n\ - add r0, r1, #0x1\n\ - mov r10, r0\n\ - cmp r4, r8\n\ - bcs _080052A2\n\ - mov r2, #0x7\n\ - mov r12, r2\n\ - lsr r0, r1, #3\n\ - ldr r2, [sp, #0x4]\n\ - add r3, r0, #0\n\ - mul r3, r2\n\ - add r7, r3, #0\n\ - mov r3, r12\n\ - and r1, r3\n\ - lsl r6, r1, #2\n\ -_08005268:\n\ - add r3, r5, #0\n\ - mov r0, #0xF\n\ - and r3, r0\n\ - cmp r3, #0\n\ - beq _0800529A\n\ - lsr r2, r4, #3\n\ - lsl r2, #5\n\ - add r2, r9\n\ - add r0, r4, #0\n\ - mov r1, r12\n\ - and r0, r1\n\ - lsr r0, #1\n\ - add r2, r0\n\ - add r2, r7\n\ - add r2, r6\n\ - mov r1, #0x1\n\ - and r1, r4\n\ - lsl r1, #2\n\ - lsl r3, r1\n\ - mov r0, #0xF0\n\ - asr r0, r1\n\ - ldrb r1, [r2]\n\ - and r0, r1\n\ - orr r3, r0\n\ - strb r3, [r2]\n\ -_0800529A:\n\ - lsr r5, #4\n\ - add r4, #0x1\n\ - cmp r4, r8\n\ - bcc _08005268\n\ -_080052A2:\n\ - mov r1, r10\n\ - ldr r2, [sp, #0x6C]\n\ - cmp r1, r2\n\ - bcc _08005244\n\ -_080052AA:\n\ - add sp, #0x8C\n\ - pop {r3-r5}\n\ - mov r8, r3\n\ - mov r9, r4\n\ - mov r10, r5\n\ - pop {r4-r7}\n\ - pop {r0}\n\ - bx r0\n\ - .pool"); -} -#endif // NONMATCHING - -void ClearTextSpan(struct TextPrinter *textPrinter, u32 width) -{ - struct Window *window; - struct Bitmap pixels_data; - struct Struct_03002F90 *gUnk; - u8* glyphHeight; - - if (gLastTextBgColor != 0) - { - window = &gWindows[textPrinter->printerTemplate.windowId]; - pixels_data.pixels = window->tileData; - pixels_data.width = window->window.width << 3; - pixels_data.height = window->window.height << 3; - - gUnk = &gUnknown_03002F90; - glyphHeight = &gUnk->unk81; - - FillBitmapRect4Bit( - &pixels_data, - textPrinter->printerTemplate.currentX, - textPrinter->printerTemplate.currentY, - width, - *glyphHeight, - gLastTextBgColor); - } -} - -u16 Font0Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 0; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font1Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 1; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font2Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 2; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font3Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 3; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font4Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 4; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font5Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 5; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font7Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 7; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -u16 Font8Func(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->hasGlyphIdBeenSet == FALSE) - { - textPrinter->subUnion.sub.glyphId = 8; - subStruct->hasGlyphIdBeenSet = TRUE; - } - return RenderText(textPrinter); -} - -void TextPrinterInitDownArrowCounters(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (gTextFlags.autoScroll == 1) - subStruct->autoScrollDelay = 0; - else - { - subStruct->downArrowYPosIdx = 0; - subStruct->downArrowDelay = 0; - } -} - -void TextPrinterDrawDownArrow(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - const u8 *arrowTiles; - - if (gTextFlags.autoScroll == 0) - { - if (subStruct->downArrowDelay != 0) - { - subStruct->downArrowDelay = ((*(u32*)&textPrinter->subUnion.sub) << 19 >> 27) - 1; // convoluted way of getting downArrowDelay, necessary to match - } - else - { - FillWindowPixelRect( - textPrinter->printerTemplate.windowId, - textPrinter->printerTemplate.bgColor << 4 | textPrinter->printerTemplate.bgColor, - textPrinter->printerTemplate.currentX, - textPrinter->printerTemplate.currentY, - 0x8, - 0x10); - - switch (gTextFlags.useAlternateDownArrow) - { - case 0: - default: - arrowTiles = gDownArrowTiles; - break; - case 1: - arrowTiles = gDarkDownArrowTiles; - break; - } - - BlitBitmapRectToWindow( - textPrinter->printerTemplate.windowId, - arrowTiles, - 0, - gDownArrowYCoords[*(u32*)subStruct << 17 >> 30], // subStruct->downArrowYPosIdx but again, stupidly retrieved - 0x8, - 0x10, - textPrinter->printerTemplate.currentX, - textPrinter->printerTemplate.currentY, - 0x8, - 0x10); - CopyWindowToVram(textPrinter->printerTemplate.windowId, 0x2); - - subStruct->downArrowDelay = 0x8; - subStruct->downArrowYPosIdx = (*(u32*)subStruct << 17 >> 30) + 1; - } - } -} - -void TextPrinterClearDownArrow(struct TextPrinter *textPrinter) -{ - FillWindowPixelRect( - textPrinter->printerTemplate.windowId, - textPrinter->printerTemplate.bgColor << 4 | textPrinter->printerTemplate.bgColor, - textPrinter->printerTemplate.currentX, - textPrinter->printerTemplate.currentY, - 0x8, - 0x10); - CopyWindowToVram(textPrinter->printerTemplate.windowId, 0x2); -} - -bool8 TextPrinterWaitAutoMode(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - - if (subStruct->autoScrollDelay == 49) - { - return TRUE; - } - else - { - ++subStruct->autoScrollDelay; - return FALSE; - } -} - -bool16 TextPrinterWaitWithDownArrow(struct TextPrinter *textPrinter) -{ - bool8 result = FALSE; - if (gTextFlags.autoScroll != 0) - { - result = TextPrinterWaitAutoMode(textPrinter); - } - else - { - TextPrinterDrawDownArrow(textPrinter); - if (gMain.newKeys & (A_BUTTON | B_BUTTON)) - { - result = TRUE; - PlaySE(SE_SELECT); - } - } - return result; -} - -bool16 TextPrinterWait(struct TextPrinter *textPrinter) -{ - bool16 result = FALSE; - if (gTextFlags.autoScroll != 0) - { - result = TextPrinterWaitAutoMode(textPrinter); - } - else - { - if (gMain.newKeys & (A_BUTTON | B_BUTTON)) - { - result = TRUE; - PlaySE(SE_SELECT); - } - } - return result; -} - -void DrawDownArrow(u8 windowId, u16 x, u16 y, u8 bgColor, bool8 drawArrow, u8 *counter, u8 *yCoordIndex) -{ - const u8 *arrowTiles; - - if (*counter != 0) - { - --*counter; - } - else - { - FillWindowPixelRect(windowId, (bgColor << 4) | bgColor, x, y, 0x8, 0x10); - if (drawArrow == 0) - { - switch (gTextFlags.useAlternateDownArrow) - { - case 0: - default: - arrowTiles = gDownArrowTiles; - break; - case 1: - arrowTiles = gDarkDownArrowTiles; - break; - } - - BlitBitmapRectToWindow( - windowId, - arrowTiles, - 0, - gDownArrowYCoords[*yCoordIndex & 3], - 0x8, - 0x10, - x, - y - 2, - 0x8, - 0x10); - CopyWindowToVram(windowId, 0x2); - *counter = 8; - ++*yCoordIndex; - } - } -} - -u16 RenderText(struct TextPrinter *textPrinter) -{ - struct TextPrinterSubStruct *subStruct = &textPrinter->subUnion.sub; - u16 currChar; - s32 width; - s32 widthHelper; - - switch (textPrinter->state) - { - case 0: - if ((gMain.heldKeys & (A_BUTTON | B_BUTTON)) && subStruct->hasPrintBeenSpedUp) - textPrinter->delayCounter = 0; - - if (textPrinter->delayCounter && textPrinter->textSpeed) - { - textPrinter->delayCounter--; - if (gTextFlags.canABSpeedUpPrint && (gMain.newKeys & (A_BUTTON | B_BUTTON))) - { - subStruct->hasPrintBeenSpedUp = TRUE; - textPrinter->delayCounter = 0; - } - return 3; - } - - if (!(gBattleTypeFlags & BATTLE_TYPE_RECORDED) && gTextFlags.autoScroll) - textPrinter->delayCounter = 3; - else - textPrinter->delayCounter = textPrinter->textSpeed; - - currChar = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - - switch (currChar) - { - case CHAR_NEWLINE: - textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; - textPrinter->printerTemplate.currentY += (gFonts[textPrinter->printerTemplate.fontId].maxLetterHeight + textPrinter->printerTemplate.lineSpacing); - return 2; - case PLACEHOLDER_BEGIN: - textPrinter->printerTemplate.currentChar++; - return 2; - case EXT_CTRL_CODE_BEGIN: - currChar = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - switch (currChar) - { - case 1: - textPrinter->printerTemplate.fgColor = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); - return 2; - case 2: - textPrinter->printerTemplate.bgColor = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); - return 2; - case 3: - textPrinter->printerTemplate.shadowColor = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); - return 2; - case 4: - textPrinter->printerTemplate.fgColor = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - textPrinter->printerTemplate.bgColor = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - textPrinter->printerTemplate.shadowColor = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - GenerateFontHalfRowLookupTable(textPrinter->printerTemplate.fgColor, textPrinter->printerTemplate.bgColor, textPrinter->printerTemplate.shadowColor); - return 2; - case 5: - textPrinter->printerTemplate.currentChar++; - return 2; - case 6: - subStruct->glyphId = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - return 2; - case EXT_CTRL_CODE_UNKNOWN_7: - return 2; - case 8: - textPrinter->delayCounter = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - textPrinter->state = 6; - return 2; - case 9: - textPrinter->state = 1; - if (gTextFlags.autoScroll) - subStruct->autoScrollDelay = 0; - return 3; - case 10: - textPrinter->state = 5; - return 3; - case 11: - currChar = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - currChar |= *textPrinter->printerTemplate.currentChar << 8; - textPrinter->printerTemplate.currentChar++; - PlayBGM(currChar); - return 2; - case 12: - currChar = *textPrinter->printerTemplate.currentChar | 0x100; - textPrinter->printerTemplate.currentChar++; - break; - case 16: - currChar = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - currChar |= (*textPrinter->printerTemplate.currentChar << 8); - textPrinter->printerTemplate.currentChar++; - PlaySE(currChar); - return 2; - case 13: - textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x + *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - return 2; - case 14: - textPrinter->printerTemplate.currentY = textPrinter->printerTemplate.y + *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - return 2; - case 15: - FillWindowPixelBuffer(textPrinter->printerTemplate.windowId, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); - textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; - textPrinter->printerTemplate.currentY = textPrinter->printerTemplate.y; - return 2; - case 23: - m4aMPlayStop(&gMPlayInfo_BGM); - return 2; - case 24: - m4aMPlayContinue(&gMPlayInfo_BGM); - return 2; - case EXT_CTRL_CODE_CLEAR: - width = *textPrinter->printerTemplate.currentChar; - textPrinter->printerTemplate.currentChar++; - if (width > 0) - { - ClearTextSpan(textPrinter, width); - textPrinter->printerTemplate.currentX += width; - return 0; - } - return 2; - case 18: - textPrinter->printerTemplate.currentX = *textPrinter->printerTemplate.currentChar + textPrinter->printerTemplate.x; - textPrinter->printerTemplate.currentChar++; - return 2; - case EXT_CTRL_CODE_CLEAR_TO: - { - widthHelper = *textPrinter->printerTemplate.currentChar; - widthHelper += textPrinter->printerTemplate.x; - textPrinter->printerTemplate.currentChar++; - width = widthHelper - textPrinter->printerTemplate.currentX; - if (width > 0) - { - ClearTextSpan(textPrinter, width); - textPrinter->printerTemplate.currentX += width; - return 0; - } - } - return 2; - case EXT_CTRL_CODE_MIN_LETTER_SPACING: - textPrinter->minLetterSpacing = *textPrinter->printerTemplate.currentChar++; - return 2; - case EXT_CTRL_CODE_JPN: - textPrinter->japanese = 1; - return 2; - case EXT_CTRL_CODE_ENG: - textPrinter->japanese = 0; - return 2; - } - break; - case CHAR_PROMPT_CLEAR: - textPrinter->state = 2; - TextPrinterInitDownArrowCounters(textPrinter); - return 3; - case CHAR_PROMPT_SCROLL: - textPrinter->state = 3; - TextPrinterInitDownArrowCounters(textPrinter); - return 3; - case CHAR_SPECIAL_F9: - currChar = *textPrinter->printerTemplate.currentChar | 0x100; - textPrinter->printerTemplate.currentChar++; - break; - case CHAR_SPECIAL_F8: - currChar = *textPrinter->printerTemplate.currentChar++; - gUnknown_03002F90.unk80 = DrawKeypadIcon(textPrinter->printerTemplate.windowId, currChar, textPrinter->printerTemplate.currentX, textPrinter->printerTemplate.currentY); - textPrinter->printerTemplate.currentX += gUnknown_03002F90.unk80 + textPrinter->printerTemplate.letterSpacing; - return 0; - case EOS: - return 1; - } - - switch (subStruct->glyphId) - { - case 0: - DecompressGlyphFont0(currChar, textPrinter->japanese); - break; - case 1: - DecompressGlyphFont1(currChar, textPrinter->japanese); - break; - case 2: - case 3: - case 4: - case 5: - DecompressGlyphFont2(currChar, textPrinter->japanese); - break; - case 7: - DecompressGlyphFont7(currChar, textPrinter->japanese); - break; - case 8: - DecompressGlyphFont8(currChar, textPrinter->japanese); - break; - case 6: - break; - } - - CopyGlyphToWindow(textPrinter); - - if (textPrinter->minLetterSpacing) - { - textPrinter->printerTemplate.currentX += gUnknown_03002F90.unk80; - width = textPrinter->minLetterSpacing - gUnknown_03002F90.unk80; - if (width > 0) - { - ClearTextSpan(textPrinter, width); - textPrinter->printerTemplate.currentX += width; - } - } - else - { - if (textPrinter->japanese) - textPrinter->printerTemplate.currentX += (gUnknown_03002F90.unk80 + textPrinter->printerTemplate.letterSpacing); - else - textPrinter->printerTemplate.currentX += gUnknown_03002F90.unk80; - } - return 0; - case 1: - if (TextPrinterWait(textPrinter)) - textPrinter->state = 0; - return 3; - case 2: - if (TextPrinterWaitWithDownArrow(textPrinter)) - { - FillWindowPixelBuffer(textPrinter->printerTemplate.windowId, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); - textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; - textPrinter->printerTemplate.currentY = textPrinter->printerTemplate.y; - textPrinter->state = 0; - } - return 3; - case 3: - if (TextPrinterWaitWithDownArrow(textPrinter)) - { - TextPrinterClearDownArrow(textPrinter); - textPrinter->scrollDistance = gFonts[textPrinter->printerTemplate.fontId].maxLetterHeight + textPrinter->printerTemplate.lineSpacing; - textPrinter->printerTemplate.currentX = textPrinter->printerTemplate.x; - textPrinter->state = 4; - } - return 3; - case 4: - if (textPrinter->scrollDistance) - { - int scrollSpeed = GetPlayerTextSpeed(); - int speed = gWindowVerticalScrollSpeeds[scrollSpeed]; - if (textPrinter->scrollDistance < speed) - { - ScrollWindow(textPrinter->printerTemplate.windowId, 0, textPrinter->scrollDistance, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); - textPrinter->scrollDistance = 0; - } - else - { - ScrollWindow(textPrinter->printerTemplate.windowId, 0, speed, PIXEL_FILL(textPrinter->printerTemplate.bgColor)); - textPrinter->scrollDistance -= speed; - } - CopyWindowToVram(textPrinter->printerTemplate.windowId, 2); - } - else - { - textPrinter->state = 0; - } - return 3; - case 5: - if (!IsSEPlaying()) - textPrinter->state = 0; - return 3; - case 6: - if (textPrinter->delayCounter != 0) - textPrinter->delayCounter--; - else - textPrinter->state = 0; - return 3; - } - - return 1; -} - -u32 GetStringWidthFixedWidthFont(const u8 *str, u8 fontId, u8 letterSpacing) -{ - int i; - u8 width; - int temp; - int temp2; - u8 line; - int strPos; - u8 lineWidths[8]; - const u8 *strLocal; - - for (i = 0; i < 8; i++) - { - lineWidths[i] = 0; - } - - width = 0; - line = 0; - strLocal = str; - strPos = 0; - - do - { - temp = strLocal[strPos++]; - switch (temp) - { - case CHAR_NEWLINE: - case EOS: - lineWidths[line] = width; - width = 0; - line++; - break; - case EXT_CTRL_CODE_BEGIN: - temp2 = strLocal[strPos++]; - switch (temp2) - { - case 0x4: - ++strPos; - case 0xB: - case 0x10: - ++strPos; - case 0x1: - case 0x2: - case 0x3: - case 0x5: - case 0x6: - case 0x8: - case 0xC: - case 0xD: - case 0xE: - case 0x11: - case 0x12: - case 0x13: - case 0x14: - ++strPos; - break; - case EXT_CTRL_CODE_UNKNOWN_7: - case 0x9: - case 0xA: - case 0xF: - case EXT_CTRL_CODE_JPN: - case EXT_CTRL_CODE_ENG: - default: - break; - } - break; - case CHAR_SPECIAL_F7: - case PLACEHOLDER_BEGIN: - ++strPos; - break; - case CHAR_PROMPT_SCROLL: - case CHAR_PROMPT_CLEAR: - break; - case CHAR_SPECIAL_F8: - case CHAR_SPECIAL_F9: - ++strPos; - default: - ++width; - break; - } - } while (temp != EOS); - - for (width = 0, strPos = 0; strPos < 8; ++strPos) - { - if (width < lineWidths[strPos]) - width = lineWidths[strPos]; - } - - return (u8)(GetFontAttribute(fontId, FONTATTR_MAX_LETTER_WIDTH) + letterSpacing) * width; -} - -u32 (*GetFontWidthFunc(u8 glyphId))(u16, bool32) -{ - u32 i; - - for (i = 0; i < 9; ++i) - { - if (glyphId == gGlyphWidthFuncs[i].fontId) - return gGlyphWidthFuncs[i].func; - } - - return NULL; -} - -s32 GetStringWidth(u8 fontId, const u8 *str, s16 letterSpacing) -{ - bool8 isJapanese; - int minGlyphWidth; - u32 (*func)(u16 glyphId, bool32 isJapanese); - s32 result; - int localLetterSpacing; - u32 lineWidth; - const u8 *bufferPointer; - int glyphWidth; - s32 width; - - isJapanese = 0; - minGlyphWidth = 0; - - func = GetFontWidthFunc(fontId); - if (func == NULL) - return 0; - - if (letterSpacing == -1) - localLetterSpacing = GetFontAttribute(fontId, FONTATTR_LETTER_SPACING); - else - localLetterSpacing = letterSpacing; - - width = 0; - lineWidth = 0; - bufferPointer = 0; - - while (*str != EOS) - { - switch (*str) - { - case CHAR_NEWLINE: - if (lineWidth > width) - width = lineWidth; - lineWidth = 0; - break; - case PLACEHOLDER_BEGIN: - switch (*++str) - { - case 0x2: - bufferPointer = gStringVar1; - break; - case 0x3: - bufferPointer = gStringVar2; - break; - case 0x4: - bufferPointer = gStringVar3; - break; - default: - return 0; - } - case CHAR_SPECIAL_F7: - if (bufferPointer == NULL) - bufferPointer = DynamicPlaceholderTextUtil_GetPlaceholderPtr(*++str); - while (*bufferPointer != EOS) - { - glyphWidth = func(*bufferPointer++, isJapanese); - if (minGlyphWidth > 0) - { - if (glyphWidth < minGlyphWidth) - glyphWidth = minGlyphWidth; - lineWidth += glyphWidth; - } - else - { - lineWidth += glyphWidth; - if (isJapanese && str[1] != EOS) - lineWidth += localLetterSpacing; - } - } - bufferPointer = 0; - break; - case EXT_CTRL_CODE_BEGIN: - switch (*++str) - { - case 0x4: - ++str; - case 0xB: - case 0x10: - ++str; - case 0x1: - case 0x2: - case 0x3: - case 0x5: - case 0x8: - case 0xC: - case 0xD: - case 0xE: - ++str; - break; - case 0x6: - func = GetFontWidthFunc(*++str); - if (func == NULL) - return 0; - if (letterSpacing == -1) - localLetterSpacing = GetFontAttribute(*str, FONTATTR_LETTER_SPACING); - break; - case 0x11: - glyphWidth = *++str; - lineWidth += glyphWidth; - break; - case 0x12: - lineWidth = *++str; - break; - case 0x13: - if (*++str > lineWidth) - lineWidth = *str; - break; - case 0x14: - minGlyphWidth = *++str; - break; - case EXT_CTRL_CODE_JPN: - isJapanese = 1; - break; - case EXT_CTRL_CODE_ENG: - isJapanese = 0; - break; - case EXT_CTRL_CODE_UNKNOWN_7: - case 0x9: - case 0xA: - case 0xF: - default: - break; - } - break; - case CHAR_SPECIAL_F8: - case CHAR_SPECIAL_F9: - if (*str == CHAR_SPECIAL_F9) - glyphWidth = func(*++str | 0x100, isJapanese); - else - glyphWidth = GetKeypadIconWidth(*++str); - - if (minGlyphWidth > 0) - { - if (glyphWidth < minGlyphWidth) - glyphWidth = minGlyphWidth; - lineWidth += glyphWidth; - } - else - { - lineWidth += glyphWidth; - if (isJapanese && str[1] != EOS) - lineWidth += localLetterSpacing; - } - break; - case CHAR_PROMPT_SCROLL: - case CHAR_PROMPT_CLEAR: - break; - default: - glyphWidth = func(*str, isJapanese); - if (minGlyphWidth > 0) - { - if (glyphWidth < minGlyphWidth) - glyphWidth = minGlyphWidth; - lineWidth += glyphWidth; - } - else - { - lineWidth += glyphWidth; - if (isJapanese && str[1] != EOS) - lineWidth += localLetterSpacing; - } - break; - } - ++str; - } - - if (lineWidth > width) - return lineWidth; - else - return width; -} - -u8 RenderTextFont9(u8 *pixels, u8 fontId, u8 *str) -{ - u8 shadowColor; - u8 *strLocal; - int strPos; - int temp; - int temp2; - u8 colorBackup[3]; - u8 fgColor; - u8 bgColor; - - SaveTextColors(&colorBackup[0], &colorBackup[1], &colorBackup[2]); - - fgColor = 1; - bgColor = 0; - shadowColor = 3; - - GenerateFontHalfRowLookupTable(1, 0, 3); - strLocal = str; - strPos = 0; - - do - { - temp = strLocal[strPos++]; - switch (temp) - { - case EXT_CTRL_CODE_BEGIN: - temp2 = strLocal[strPos++]; - switch (temp2) - { - case 0x4: - fgColor = strLocal[strPos++]; - bgColor = strLocal[strPos++]; - shadowColor = strLocal[strPos++]; - GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); - continue; - case 0x1: - fgColor = strLocal[strPos++]; - GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); - continue; - case 0x2: - bgColor = strLocal[strPos++]; - GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); - continue; - case 0x3: - shadowColor = strLocal[strPos++]; - GenerateFontHalfRowLookupTable(fgColor, bgColor, shadowColor); - continue; - case 0x6: - fontId = strLocal[strPos++]; - break; - case 0xB: - case 0x10: - ++strPos; - case 0x5: - case 0x8: - case 0xC: - case 0xD: - case 0xE: - case 0x11: - case 0x12: - case 0x13: - case 0x14: - ++strPos; - break; - case EXT_CTRL_CODE_UNKNOWN_7: - case 0x9: - case 0xA: - case 0xF: - case EXT_CTRL_CODE_JPN: - case EXT_CTRL_CODE_ENG: - default: - continue; - } - break; - case CHAR_SPECIAL_F7: - case CHAR_SPECIAL_F8: - case CHAR_SPECIAL_F9: - case PLACEHOLDER_BEGIN: - ++strPos; - break; - case CHAR_PROMPT_SCROLL: - case CHAR_PROMPT_CLEAR: - case CHAR_NEWLINE: - case EOS: - break; - default: - switch (fontId) - { - case 9: - DecompressGlyphFont9(temp); - break; - case 1: - default: - DecompressGlyphFont1(temp, 1); - break; - } - CpuCopy32(gUnknown_03002F90.unk0, pixels, 0x20); - CpuCopy32(gUnknown_03002F90.unk40, pixels + 0x20, 0x20); - pixels += 0x40; - break; - } - } - while (temp != EOS); - - RestoreTextColors(&colorBackup[0], &colorBackup[1], &colorBackup[2]); - return 1; -} - -u8 DrawKeypadIcon(u8 windowId, u8 keypadIconId, u16 x, u16 y) -{ - BlitBitmapRectToWindow( - windowId, - gKeypadIconTiles + (gKeypadIcons[keypadIconId].tileOffset * 0x20), - 0, - 0, - 0x80, - 0x80, - x, - y, - gKeypadIcons[keypadIconId].width, - gKeypadIcons[keypadIconId].height); - return gKeypadIcons[keypadIconId].width; -} - -u8 GetKeypadIconTileOffset(u8 keypadIconId) -{ - return gKeypadIcons[keypadIconId].tileOffset; -} - -u8 GetKeypadIconWidth(u8 keypadIconId) -{ - return gKeypadIcons[keypadIconId].width; -} - -u8 GetKeypadIconHeight(u8 keypadIconId) -{ - return gKeypadIcons[keypadIconId].height; -} - -void SetDefaultFontsPointer(void) -{ - SetFontsPointer(&gFontInfos[0]); -} - -u8 GetFontAttribute(u8 fontId, u8 attributeId) -{ - int result = 0; - switch (attributeId) - { - case FONTATTR_MAX_LETTER_WIDTH: - result = gFontInfos[fontId].maxLetterWidth; - break; - case FONTATTR_MAX_LETTER_HEIGHT: - result = gFontInfos[fontId].maxLetterHeight; - break; - case FONTATTR_LETTER_SPACING: - result = gFontInfos[fontId].letterSpacing; - break; - case FONTATTR_LINE_SPACING: - result = gFontInfos[fontId].lineSpacing; - break; - case FONTATTR_UNKNOWN: - result = gFontInfos[fontId].unk; - break; - case FONTATTR_COLOR_FOREGROUND: - result = gFontInfos[fontId].fgColor; - break; - case FONTATTR_COLOR_BACKGROUND: - result = gFontInfos[fontId].bgColor; - break; - case FONTATTR_COLOR_SHADOW: - result = gFontInfos[fontId].shadowColor; - break; - } - return result; -} - -u8 GetMenuCursorDimensionByFont(u8 fontId, u8 whichDimension) -{ - return gMenuCursorDimensions[fontId][whichDimension]; -} - -void DecompressGlyphFont0(u16 glyphId, bool32 isJapanese) -{ - const u16* glyphs; - - if (isJapanese == 1) - { - glyphs = gFont0JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & 0xF)); - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 - gUnknown_03002F90.unk80 = 8; // gGlyphWidth - gUnknown_03002F90.unk81 = 12; // gGlyphHeight - } - else - { - glyphs = gFont0LatinGlyphs + (0x20 * glyphId); - gUnknown_03002F90.unk80 = gFont0LatinGlyphWidths[glyphId]; - - if (gUnknown_03002F90.unk80 <= 8) - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - } - else - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); - } - - gUnknown_03002F90.unk81 = 13; - } -} - -u32 GetGlyphWidthFont0(u16 glyphId, bool32 isJapanese) -{ - if (isJapanese == TRUE) - return 8; - else - return gFont0LatinGlyphWidths[glyphId]; -} - -void DecompressGlyphFont7(u16 glyphId, bool32 isJapanese) -{ - const u16* glyphs; - - if (isJapanese == TRUE) - { - int eff; - glyphs = gFont1JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & (eff = 0xF))); // shh, no questions, only matching now - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 - gUnknown_03002F90.unk80 = 8; // gGlyphWidth - gUnknown_03002F90.unk81 = 15; // gGlyphHeight - } - else - { - glyphs = gFont7LatinGlyphs + (0x20 * glyphId); - gUnknown_03002F90.unk80 = gFont7LatinGlyphWidths[glyphId]; - - if (gUnknown_03002F90.unk80 <= 8) - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - } - else - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); - } - - gUnknown_03002F90.unk81 = 15; - } -} - -u32 GetGlyphWidthFont7(u16 glyphId, bool32 isJapanese) -{ - if (isJapanese == TRUE) - return 8; - else - return gFont7LatinGlyphWidths[glyphId]; -} - -void DecompressGlyphFont8(u16 glyphId, bool32 isJapanese) -{ - const u16* glyphs; - - if (isJapanese == TRUE) - { - glyphs = gFont0JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & 0xF)); - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 - gUnknown_03002F90.unk80 = 8; // gGlyphWidth - gUnknown_03002F90.unk81 = 12; // gGlyphHeight - } - else - { - glyphs = gFont8LatinGlyphs + (0x20 * glyphId); - gUnknown_03002F90.unk80 = gFont8LatinGlyphWidths[glyphId]; - - if (gUnknown_03002F90.unk80 <= 8) - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - } - else - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); - } - - gUnknown_03002F90.unk81 = 12; - } -} - -u32 GetGlyphWidthFont8(u16 glyphId, bool32 isJapanese) -{ - if (isJapanese == TRUE) - return 8; - else - return gFont8LatinGlyphWidths[glyphId]; -} - -void DecompressGlyphFont2(u16 glyphId, bool32 isJapanese) -{ - const u16* glyphs; - - if (isJapanese == TRUE) - { - glyphs = gFont2JapaneseGlyphs + (0x100 * (glyphId >> 0x3)) + (0x10 * (glyphId & 0x7)); - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); // gUnknown_03002F90 + 0x40 - DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x20 - DecompressGlyphTile(glyphs + 0x88, gUnknown_03002F90.unk60); // gUnknown_03002F90 + 0x60 - gUnknown_03002F90.unk80 = gFont2JapaneseGlyphWidths[glyphId]; // gGlyphWidth - gUnknown_03002F90.unk81 = 14; // gGlyphHeight - } - else - { - glyphs = gFont2LatinGlyphs + (0x20 * glyphId); - gUnknown_03002F90.unk80 = gFont2LatinGlyphWidths[glyphId]; - - if (gUnknown_03002F90.unk80 <= 8) - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - } - else - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); - } - - gUnknown_03002F90.unk81 = 14; - } -} - -u32 GetGlyphWidthFont2(u16 glyphId, bool32 isJapanese) -{ - if (isJapanese == TRUE) - return gFont2JapaneseGlyphWidths[glyphId]; - else - return gFont2LatinGlyphWidths[glyphId]; -} - -void DecompressGlyphFont1(u16 glyphId, bool32 isJapanese) -{ - const u16* glyphs; - - if (isJapanese == TRUE) - { - int eff; - glyphs = gFont1JapaneseGlyphs + (0x100 * (glyphId >> 0x4)) + (0x8 * (glyphId & (eff = 0xF))); // shh, no questions, only matching now - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); // gUnknown_03002F90 + 0x40 - gUnknown_03002F90.unk80 = 8; // gGlyphWidth - gUnknown_03002F90.unk81 = 15; // gGlyphHeight - } - else - { - glyphs = gFont1LatinGlyphs + (0x20 * glyphId); - gUnknown_03002F90.unk80 = gFont1LatinGlyphWidths[glyphId]; - - if (gUnknown_03002F90.unk80 <= 8) - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - } - else - { - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x8, gUnknown_03002F90.unk20); - DecompressGlyphTile(glyphs + 0x10, gUnknown_03002F90.unk40); - DecompressGlyphTile(glyphs + 0x18, gUnknown_03002F90.unk60); - } - - gUnknown_03002F90.unk81 = 15; - } -} - -u32 GetGlyphWidthFont1(u16 glyphId, bool32 isJapanese) -{ - if (isJapanese == TRUE) - return 8; - else - return gFont1LatinGlyphWidths[glyphId]; -} - -void DecompressGlyphFont9(u16 glyphId) -{ - const u16* glyphs; - - glyphs = gFont9JapaneseGlyphs + (0x100 * (glyphId >> 4)) + (0x8 * (glyphId & 0xF)); - DecompressGlyphTile(glyphs, gUnknown_03002F90.unk0); - DecompressGlyphTile(glyphs + 0x80, gUnknown_03002F90.unk40); - gUnknown_03002F90.unk80 = 8; - gUnknown_03002F90.unk81 = 12; -} diff --git a/src/trade.c b/src/trade.c index d95e4ebf5..7e585e280 100644 --- a/src/trade.c +++ b/src/trade.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle_anim.h" #include "battle_interface.h" #include "bg.h" diff --git a/src/trainer_card.c b/src/trainer_card.c index 9c4816c42..f7685b8cc 100755 --- a/src/trainer_card.c +++ b/src/trainer_card.c @@ -4,7 +4,7 @@ #include "task.h" #include "main.h" #include "window.h" -#include "alloc.h" +#include "malloc.h" #include "link.h" #include "bg.h" #include "sound.h" diff --git a/src/trainer_hill.c b/src/trainer_hill.c index 2a7e003d9..995dc164a 100644 --- a/src/trainer_hill.c +++ b/src/trainer_hill.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "battle_tower.h" #include "battle_setup.h" diff --git a/src/trainer_pokemon_sprites.c b/src/trainer_pokemon_sprites.c index 047783a85..f360ec84c 100644 --- a/src/trainer_pokemon_sprites.c +++ b/src/trainer_pokemon_sprites.c @@ -1,7 +1,7 @@ #include "global.h" #include "sprite.h" #include "window.h" -#include "alloc.h" +#include "malloc.h" #include "constants/species.h" #include "palette.h" #include "decompress.h" diff --git a/src/tv.c b/src/tv.c index ef826a4af..3e9390e61 100644 --- a/src/tv.c +++ b/src/tv.c @@ -31,7 +31,7 @@ #include "text.h" #include "script_menu.h" #include "naming_screen.h" -#include "alloc.h" +#include "malloc.h" #include "region_map.h" #include "constants/region_map_sections.h" #include "decoration.h" diff --git a/src/union_room.c b/src/union_room.c index 01288c8cc..95278a08f 100644 --- a/src/union_room.c +++ b/src/union_room.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "battle.h" #include "berry_crush.h" #include "bg.h" diff --git a/src/union_room_battle.c b/src/union_room_battle.c index 79d12291d..b49406eac 100644 --- a/src/union_room_battle.c +++ b/src/union_room_battle.c @@ -6,7 +6,7 @@ #include "bg.h" #include "palette.h" #include "gpu_regs.h" -#include "alloc.h" +#include "malloc.h" #include "menu.h" #include "window.h" #include "text_window.h" diff --git a/src/union_room_chat.c b/src/union_room_chat.c index 4afeb99cf..8a3ab758e 100755 --- a/src/union_room_chat.c +++ b/src/union_room_chat.c @@ -1,5 +1,5 @@ #include "global.h" -#include "alloc.h" +#include "malloc.h" #include "bg.h" #include "decompress.h" #include "dma3.h" diff --git a/src/unk_pokedex_area_screen_helper.c b/src/unk_pokedex_area_screen_helper.c index 0041d628c..9b2401cb2 100644 --- a/src/unk_pokedex_area_screen_helper.c +++ b/src/unk_pokedex_area_screen_helper.c @@ -2,7 +2,7 @@ #include "main.h" #include "menu.h" #include "bg.h" -#include "alloc.h" +#include "malloc.h" #include "palette.h" #include "unk_pokedex_area_screen_helper.h" diff --git a/src/use_pokeblock.c b/src/use_pokeblock.c index 655f38def..4d7605c50 100644 --- a/src/use_pokeblock.c +++ b/src/use_pokeblock.c @@ -1,7 +1,7 @@ #include "global.h" #include "main.h" #include "pokeblock.h" -#include "alloc.h" +#include "malloc.h" #include "decompress.h" #include "graphics.h" #include "palette.h" diff --git a/src/window.c b/src/window.c deleted file mode 100644 index fbce57743..000000000 --- a/src/window.c +++ /dev/null @@ -1,721 +0,0 @@ -#include "global.h" -#include "window.h" -#include "alloc.h" -#include "bg.h" -#include "blit.h" - -u32 filler_03002F58; -u32 filler_03002F5C; -// This global is set to 0 and never changed. -u8 gTransparentTileNumber; -u32 filler_03002F64; -void *gUnknown_03002F70[4]; -extern u32 gUnneededFireRedVariable; - -#define WINDOWS_MAX 32 - -EWRAM_DATA struct Window gWindows[WINDOWS_MAX] = {0}; -EWRAM_DATA static struct Window* sWindowPtr = NULL; -EWRAM_DATA static u16 sWindowSize = 0; - -static u8 GetNumActiveWindowsOnBg(u8 bgId); -static u8 GetNumActiveWindowsOnBg8Bit(u8 bgId); - -static const struct WindowTemplate sDummyWindowTemplate = DUMMY_WIN_TEMPLATE; - -static void nullsub_8(void) -{ - -} - -bool16 InitWindows(const struct WindowTemplate *templates) -{ - int i; - void *bgTilemapBuffer; - int j; - u8 bgLayer; - u16 attrib; - u8* allocatedTilemapBuffer; - int allocatedBaseBlock; - - for (i = 0; i < 0x4; ++i) - { - bgTilemapBuffer = GetBgTilemapBuffer(i); - if (bgTilemapBuffer != NULL) - gUnknown_03002F70[i] = nullsub_8; - else - gUnknown_03002F70[i] = bgTilemapBuffer; - } - - for (i = 0; i < 0x20; ++i) - { - gWindows[i].window = sDummyWindowTemplate; - gWindows[i].tileData = NULL; - } - - for (i = 0, allocatedBaseBlock = 0, bgLayer = templates[i].bg; bgLayer != 0xFF && i < 0x20; ++i, bgLayer = templates[i].bg) - { - if (gUnneededFireRedVariable == 1) - { - allocatedBaseBlock = DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, 0, templates[i].width * templates[i].height, 0); - if (allocatedBaseBlock == -1) - return FALSE; - } - - if (gUnknown_03002F70[bgLayer] == NULL) - { - attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC); - - if (attrib != 0xFFFF) - { - allocatedTilemapBuffer = AllocZeroed(attrib); - - if (allocatedTilemapBuffer == NULL) - { - FreeAllWindowBuffers(); - return FALSE; - } - - for (j = 0; j < attrib; ++j) - allocatedTilemapBuffer[j] = 0; - - gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; - SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer); - } - } - - allocatedTilemapBuffer = AllocZeroed((u16)(0x20 * (templates[i].width * templates[i].height))); - - if (allocatedTilemapBuffer == NULL) - { - if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gUnknown_03002F70[bgLayer] != nullsub_8)) - { - Free(gUnknown_03002F70[bgLayer]); - gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; - } - - return FALSE; - } - - gWindows[i].tileData = allocatedTilemapBuffer; - gWindows[i].window = templates[i]; - - if (gUnneededFireRedVariable == 1) - { - gWindows[i].window.baseBlock = allocatedBaseBlock; - DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, allocatedBaseBlock, templates[i].width * templates[i].height, 1); - } - } - - gTransparentTileNumber = 0; - return TRUE; -} - -u16 AddWindow(const struct WindowTemplate *template) -{ - u16 win; - u8 bgLayer; - int allocatedBaseBlock; - u16 attrib; - u8 *allocatedTilemapBuffer; - int i; - - for (win = 0; win < WINDOWS_MAX; ++win) - { - if ((bgLayer = gWindows[win].window.bg) == 0xFF) - break; - } - - if (win == WINDOWS_MAX) - return 0xFF; - - bgLayer = template->bg; - allocatedBaseBlock = 0; - - if (gUnneededFireRedVariable == 1) - { - allocatedBaseBlock = DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, 0, template->width * template->height, 0); - - if (allocatedBaseBlock == -1) - return 0xFF; - } - - if (gUnknown_03002F70[bgLayer] == NULL) - { - attrib = GetBgAttribute(bgLayer, BG_ATTR_METRIC); - - if (attrib != 0xFFFF) - { - allocatedTilemapBuffer = AllocZeroed(attrib); - - if (allocatedTilemapBuffer == NULL) - return 0xFF; - - for (i = 0; i < attrib; ++i) - allocatedTilemapBuffer[i] = 0; - - gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; - SetBgTilemapBuffer(bgLayer, allocatedTilemapBuffer); - } - } - - allocatedTilemapBuffer = AllocZeroed((u16)(0x20 * (template->width * template->height))); - - if (allocatedTilemapBuffer == NULL) - { - if ((GetNumActiveWindowsOnBg(bgLayer) == 0) && (gUnknown_03002F70[bgLayer] != nullsub_8)) - { - Free(gUnknown_03002F70[bgLayer]); - gUnknown_03002F70[bgLayer] = allocatedTilemapBuffer; - } - return 0xFF; - } - - gWindows[win].tileData = allocatedTilemapBuffer; - gWindows[win].window = *template; - - if (gUnneededFireRedVariable == 1) - { - gWindows[win].window.baseBlock = allocatedBaseBlock; - DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, allocatedBaseBlock, gWindows[win].window.width * gWindows[win].window.height, 1); - } - - return win; -} - -int AddWindowWithoutTileMap(const struct WindowTemplate *template) -{ - u16 win; - u8 bgLayer; - int allocatedBaseBlock; - - for (win = 0; win < WINDOWS_MAX; ++win) - { - if (gWindows[win].window.bg == 0xFF) - break; - } - - if (win == WINDOWS_MAX) - return 0xFF; - - bgLayer = template->bg; - allocatedBaseBlock = 0; - - if (gUnneededFireRedVariable == 1) - { - allocatedBaseBlock = DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, 0, template->width * template->height, 0); - - if (allocatedBaseBlock == -1) - return 0xFF; - } - - gWindows[win].window = *template; - - if (gUnneededFireRedVariable == 1) - { - gWindows[win].window.baseBlock = allocatedBaseBlock; - DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, allocatedBaseBlock, gWindows[win].window.width * gWindows[win].window.height, 1); - } - - return win; -} - -void RemoveWindow(u8 windowId) -{ - u8 bgLayer = gWindows[windowId].window.bg; - - if (gUnneededFireRedVariable == 1) - { - DummiedOutFireRedLeafGreenTileAllocFunc(bgLayer, gWindows[windowId].window.baseBlock, gWindows[windowId].window.width * gWindows[windowId].window.height, 2); - } - - gWindows[windowId].window = sDummyWindowTemplate; - - if (GetNumActiveWindowsOnBg(bgLayer) == 0) - { - if (gUnknown_03002F70[bgLayer] != nullsub_8) - { - Free(gUnknown_03002F70[bgLayer]); - gUnknown_03002F70[bgLayer] = 0; - } - } - - if (gWindows[windowId].tileData != NULL) - { - Free(gWindows[windowId].tileData); - gWindows[windowId].tileData = NULL; - } -} - -void FreeAllWindowBuffers(void) -{ - int i; - - for (i = 0; i < 4; ++i) - { - if (gUnknown_03002F70[i] != NULL && gUnknown_03002F70[i] != nullsub_8) - { - Free(gUnknown_03002F70[i]); - gUnknown_03002F70[i] = NULL; - } - } - - for (i = 0; i < 0x20; ++i) - { - if (gWindows[i].tileData != NULL) - { - Free(gWindows[i].tileData); - gWindows[i].tileData = NULL; - } - } -} - -void CopyWindowToVram(u8 windowId, u8 mode) -{ - struct Window windowLocal = gWindows[windowId]; - u16 windowSize = 32 * (windowLocal.window.width * windowLocal.window.height); - - switch (mode) - { - case 1: - CopyBgTilemapBufferToVram(windowLocal.window.bg); - break; - case 2: - LoadBgTiles(windowLocal.window.bg, windowLocal.tileData, windowSize, windowLocal.window.baseBlock); - break; - case 3: - LoadBgTiles(windowLocal.window.bg, windowLocal.tileData, windowSize, windowLocal.window.baseBlock); - CopyBgTilemapBufferToVram(windowLocal.window.bg); - break; - } -} - -void CopyWindowRectToVram(u32 windowId, u32 mode, u32 x, u32 y, u32 w, u32 h) -{ - struct Window windowLocal; - int rectSize; - int rectPos; - - if (w != 0 && h != 0) - { - windowLocal = gWindows[windowId]; - - rectSize = ((h - 1) * windowLocal.window.width); - rectSize += (windowLocal.window.width - x); - rectSize -= (windowLocal.window.width - (x + w)); - rectSize *= 32; - - rectPos = (y * windowLocal.window.width) + x; - - switch (mode) - { - case 1: - CopyBgTilemapBufferToVram(windowLocal.window.bg); - break; - case 2: - LoadBgTiles(windowLocal.window.bg, windowLocal.tileData + (rectPos * 32), rectSize, windowLocal.window.baseBlock + rectPos); - break; - case 3: - LoadBgTiles(windowLocal.window.bg, windowLocal.tileData + (rectPos * 32), rectSize, windowLocal.window.baseBlock + rectPos); - CopyBgTilemapBufferToVram(windowLocal.window.bg); - break; - } - } -} - -void PutWindowTilemap(u8 windowId) -{ - struct Window windowLocal = gWindows[windowId]; - - WriteSequenceToBgTilemapBuffer( - windowLocal.window.bg, - GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE) + windowLocal.window.baseBlock, - windowLocal.window.tilemapLeft, - windowLocal.window.tilemapTop, - windowLocal.window.width, - windowLocal.window.height, - windowLocal.window.paletteNum, - 1); -} - -void PutWindowRectTilemapOverridePalette(u8 windowId, u8 x, u8 y, u8 width, u8 height, u8 palette) -{ - struct Window windowLocal = gWindows[windowId]; - u16 currentRow = windowLocal.window.baseBlock + (y * windowLocal.window.width) + x + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE); - int i; - - for (i = 0; i < height; ++i) - { - WriteSequenceToBgTilemapBuffer( - windowLocal.window.bg, - currentRow, - windowLocal.window.tilemapLeft + x, - windowLocal.window.tilemapTop + y + i, - width, - 1, - palette, - 1); - - currentRow += windowLocal.window.width; - } -} - -// Fills a window with transparent tiles. -void ClearWindowTilemap(u8 windowId) -{ - struct Window windowLocal = gWindows[windowId]; - - FillBgTilemapBufferRect( - windowLocal.window.bg, - gTransparentTileNumber, - windowLocal.window.tilemapLeft, - windowLocal.window.tilemapTop, - windowLocal.window.width, - windowLocal.window.height, - windowLocal.window.paletteNum); -} - -void PutWindowRectTilemap(u8 windowId, u8 x, u8 y, u8 width, u8 height) -{ - struct Window windowLocal = gWindows[windowId]; - u16 currentRow = windowLocal.window.baseBlock + (y * windowLocal.window.width) + x + GetBgAttribute(windowLocal.window.bg, BG_ATTR_BASETILE); - int i; - - for (i = 0; i < height; ++i) - { - WriteSequenceToBgTilemapBuffer( - windowLocal.window.bg, - currentRow, - windowLocal.window.tilemapLeft + x, - windowLocal.window.tilemapTop + y + i, - width, - 1, - windowLocal.window.paletteNum, - 1); - - currentRow += windowLocal.window.width; - } -} - -void BlitBitmapToWindow(u8 windowId, const u8 *pixels, u16 x, u16 y, u16 width, u16 height) -{ - BlitBitmapRectToWindow(windowId, pixels, 0, 0, width, height, x, y, width, height); -} - -void BlitBitmapRectToWindow(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight) -{ - struct Bitmap sourceRect; - struct Bitmap destRect; - - sourceRect.pixels = (u8*)pixels; - sourceRect.width = srcWidth; - sourceRect.height = srcHeight; - - destRect.pixels = gWindows[windowId].tileData; - destRect.width = 8 * gWindows[windowId].window.width; - destRect.height = 8 * gWindows[windowId].window.height; - - BlitBitmapRect4Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, 0); -} - -static void BlitBitmapRectToWindowWithColorKey(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 colorKey) -{ - struct Bitmap sourceRect; - struct Bitmap destRect; - - sourceRect.pixels = (u8*)pixels; - sourceRect.width = srcWidth; - sourceRect.height = srcHeight; - - destRect.pixels = gWindows[windowId].tileData; - destRect.width = 8 * gWindows[windowId].window.width; - destRect.height = 8 * gWindows[windowId].window.height; - - BlitBitmapRect4Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, colorKey); -} - -void FillWindowPixelRect(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height) -{ - struct Bitmap pixelRect; - - pixelRect.pixels = gWindows[windowId].tileData; - pixelRect.width = 8 * gWindows[windowId].window.width; - pixelRect.height = 8 * gWindows[windowId].window.height; - - FillBitmapRect4Bit(&pixelRect, x, y, width, height, fillValue); -} - -void CopyToWindowPixelBuffer(u8 windowId, const void *src, u16 size, u16 tileOffset) -{ - if (size != 0) - CpuCopy16(src, gWindows[windowId].tileData + (0x20 * tileOffset), size); - else - LZ77UnCompWram(src, gWindows[windowId].tileData + (0x20 * tileOffset)); -} - -// Sets all pixels within the window to the fillValue color. -void FillWindowPixelBuffer(u8 windowId, u8 fillValue) -{ - int fillSize = gWindows[windowId].window.width * gWindows[windowId].window.height; - CpuFastFill8(fillValue, gWindows[windowId].tileData, 0x20 * fillSize); -} - -#define MOVE_TILES_DOWN(a) \ -{ \ - destOffset = i + (a); \ - srcOffset = i + (((width * (distanceLoop & ~7)) | (distanceLoop & 7)) * 4); \ - if (srcOffset < size) \ - *(u32*)(tileData + destOffset) = *(u32*)(tileData + srcOffset); \ - else \ - *(u32*)(tileData + destOffset) = fillValue32; \ - distanceLoop++; \ -} - -#define MOVE_TILES_UP(a) \ -{ \ - destOffset = i + (a); \ - srcOffset = i + (((width * (distanceLoop & ~7)) | (distanceLoop & 7)) * 4); \ - if (srcOffset < size) \ - *(u32*)(tileData - destOffset) = *(u32*)(tileData - srcOffset); \ - else \ - *(u32*)(tileData - destOffset) = fillValue32; \ - distanceLoop++; \ -} - -void ScrollWindow(u8 windowId, u8 direction, u8 distance, u8 fillValue) -{ - struct WindowTemplate window = gWindows[windowId].window; - u8 *tileData = gWindows[windowId].tileData; - u32 fillValue32 = (fillValue << 24) | (fillValue << 16) | (fillValue << 8) | fillValue; - s32 size = window.height * window.width * 32; - u32 width = window.width; - s32 i; - s32 srcOffset, destOffset; - u32 distanceLoop; - - switch (direction) - { - case 0: - for (i = 0; i < size; i += 32) - { - distanceLoop = distance; - MOVE_TILES_DOWN(0) - MOVE_TILES_DOWN(4) - MOVE_TILES_DOWN(8) - MOVE_TILES_DOWN(12) - MOVE_TILES_DOWN(16) - MOVE_TILES_DOWN(20) - MOVE_TILES_DOWN(24) - MOVE_TILES_DOWN(28) - } - break; - case 1: - tileData += size - 4; - for (i = 0; i < size; i += 32) - { - distanceLoop = distance; - MOVE_TILES_UP(0) - MOVE_TILES_UP(4) - MOVE_TILES_UP(8) - MOVE_TILES_UP(12) - MOVE_TILES_UP(16) - MOVE_TILES_UP(20) - MOVE_TILES_UP(24) - MOVE_TILES_UP(28) - } - break; - case 2: - break; - } -} - -void CallWindowFunction(u8 windowId, void ( *func)(u8, u8, u8, u8, u8, u8)) -{ - struct WindowTemplate window = gWindows[windowId].window; - func(window.bg, window.tilemapLeft, window.tilemapTop, window.width, window.height, window.paletteNum); -} - -bool8 SetWindowAttribute(u8 windowId, u8 attributeId, u32 value) -{ - switch (attributeId) - { - case WINDOW_TILEMAP_LEFT: - gWindows[windowId].window.tilemapLeft = value; - return FALSE; - case WINDOW_TILEMAP_TOP: - gWindows[windowId].window.tilemapTop = value; - return FALSE; - case WINDOW_PALETTE_NUM: - gWindows[windowId].window.paletteNum = value; - return FALSE; - case WINDOW_BASE_BLOCK: - gWindows[windowId].window.baseBlock = value; - return FALSE; - case WINDOW_TILE_DATA: - gWindows[windowId].tileData = (u8*)(value); - return TRUE; - case WINDOW_BG: - case WINDOW_WIDTH: - case WINDOW_HEIGHT: - default: - return TRUE; - } -} - -u32 GetWindowAttribute(u8 windowId, u8 attributeId) -{ - switch (attributeId) - { - case WINDOW_BG: - return gWindows[windowId].window.bg; - case WINDOW_TILEMAP_LEFT: - return gWindows[windowId].window.tilemapLeft; - case WINDOW_TILEMAP_TOP: - return gWindows[windowId].window.tilemapTop; - case WINDOW_WIDTH: - return gWindows[windowId].window.width; - case WINDOW_HEIGHT: - return gWindows[windowId].window.height; - case WINDOW_PALETTE_NUM: - return gWindows[windowId].window.paletteNum; - case WINDOW_BASE_BLOCK: - return gWindows[windowId].window.baseBlock; - case WINDOW_TILE_DATA: - return (u32)(gWindows[windowId].tileData); - default: - return 0; - } -} - -static u8 GetNumActiveWindowsOnBg(u8 bgId) -{ - u8 windowsNum = 0; - s32 i; - for (i = 0; i < WINDOWS_MAX; i++) - { - if (gWindows[i].window.bg == bgId) - windowsNum++; - } - return windowsNum; -} - -static void nullsub_9(void) -{ - -} - -u16 AddWindow8Bit(const struct WindowTemplate *template) -{ - u16 windowId; - u8* memAddress; - u8 bgLayer; - - for (windowId = 0; windowId < 32; windowId++) - { - if (gWindows[windowId].window.bg == 0xFF) - break; - } - if (windowId == WINDOWS_MAX) - return 0xFF; - bgLayer = template->bg; - if (gUnknown_03002F70[bgLayer] == 0) - { - u16 attribute = GetBgAttribute(bgLayer, BG_ATTR_METRIC); - if (attribute != 0xFFFF) - { - s32 i; - memAddress = Alloc(attribute); - if (memAddress == NULL) - return 0xFF; - for (i = 0; i < attribute; i++) // if we're going to zero out the memory anyway, why not call AllocZeroed? - memAddress[i] = 0; - gUnknown_03002F70[bgLayer] = memAddress; - SetBgTilemapBuffer(bgLayer, memAddress); - } - } - memAddress = Alloc((u16)(0x40 * (template->width * template->height))); - if (memAddress == NULL) - { - if (GetNumActiveWindowsOnBg8Bit(bgLayer) == 0 && gUnknown_03002F70[bgLayer] != nullsub_9) - { - Free(gUnknown_03002F70[bgLayer]); - gUnknown_03002F70[bgLayer] = NULL; - } - return 0xFF; - } - else - { - gWindows[windowId].tileData = memAddress; - gWindows[windowId].window = *template; - return windowId; - } -} - -void FillWindowPixelBuffer8Bit(u8 windowId, u8 fillValue) -{ - s32 i; - s32 size; - - size = (u16)(0x40 * (gWindows[windowId].window.width * gWindows[windowId].window.height)); - for (i = 0; i < size; i++) - gWindows[windowId].tileData[i] = fillValue; -} - -void FillWindowPixelRect8Bit(u8 windowId, u8 fillValue, u16 x, u16 y, u16 width, u16 height) -{ - struct Bitmap pixelRect; - - pixelRect.pixels = gWindows[windowId].tileData; - pixelRect.width = 8 * gWindows[windowId].window.width; - pixelRect.height = 8 * gWindows[windowId].window.height; - - FillBitmapRect8Bit(&pixelRect, x, y, width, height, fillValue); -} - -void BlitBitmapRectToWindow4BitTo8Bit(u8 windowId, const u8 *pixels, u16 srcX, u16 srcY, u16 srcWidth, int srcHeight, u16 destX, u16 destY, u16 rectWidth, u16 rectHeight, u8 paletteNum) -{ - struct Bitmap sourceRect; - struct Bitmap destRect; - - sourceRect.pixels = (u8*) pixels; - sourceRect.width = srcWidth; - sourceRect.height = srcHeight; - - destRect.pixels = gWindows[windowId].tileData; - destRect.width = 8 * gWindows[windowId].window.width; - destRect.height = 8 * gWindows[windowId].window.height; - - BlitBitmapRect4BitTo8Bit(&sourceRect, &destRect, srcX, srcY, destX, destY, rectWidth, rectHeight, 0, paletteNum); -} - -void CopyWindowToVram8Bit(u8 windowId, u8 mode) -{ - sWindowPtr = &gWindows[windowId]; - sWindowSize = 0x40 * (sWindowPtr->window.width * sWindowPtr->window.height); - - switch (mode) - { - case 1: - CopyBgTilemapBufferToVram(sWindowPtr->window.bg); - break; - case 2: - LoadBgTiles(sWindowPtr->window.bg, sWindowPtr->tileData, sWindowSize, sWindowPtr->window.baseBlock); - break; - case 3: - LoadBgTiles(sWindowPtr->window.bg, sWindowPtr->tileData, sWindowSize, sWindowPtr->window.baseBlock); - CopyBgTilemapBufferToVram(sWindowPtr->window.bg); - break; - } -} - -static u8 GetNumActiveWindowsOnBg8Bit(u8 bgId) -{ - u8 windowsNum = 0; - s32 i; - for (i = 0; i < WINDOWS_MAX; i++) - { - if (gWindows[i].window.bg == bgId) - windowsNum++; - } - return windowsNum; -} diff --git a/sym_bss.txt b/sym_bss.txt index 29b30f2db..c2a60404e 100644 --- a/sym_bss.txt +++ b/sym_bss.txt @@ -1,10 +1,10 @@ .include "src/main.o" - .include "src/alloc.o" - .include "src/dma3_manager.o" - .include "src/gpu_regs.o" - .include "src/bg.o" - .include "src/text.o" - .include "src/sprite.o" + .include "gflib/malloc.o" + .include "gflib/dma3_manager.o" + .include "gflib/gpu_regs.o" + .include "gflib/bg.o" + .include "gflib/text.o" + .include "gflib/sprite.o" .include "src/link.o" .include "src/link_rfu.o" .include "src/union_room.o" diff --git a/sym_common.txt b/sym_common.txt index f277ed0f2..7c306c61b 100644 --- a/sym_common.txt +++ b/sym_common.txt @@ -1,9 +1,37 @@ .space 0x8 .include "main.o" - .include "bg.o" - .include "window.o" - .include "text.o" - .include "sprite.o" + @ ../gflib/bg.o + .align 2 +gUnneededFireRedVariable: + .space 4 + @ ../gflib/window.o + .align 4 +gTransparentTileNumber: + .space 1 + .align 4 +gUnknown_03002F70: + .space 16 + @ ../gflib/text.o + .align 4 +gFonts: + .space 4 + .align 2 +gUnknown_03002F84: + .space 1 + .align 4 +gUnknown_03002F90: + .space 132 + .align 2 +gTextFlags: + .space 4 + @ ../gflib/sprite.o + .align 2 +gOamMatrixAllocBitmap: + .space 4 + .align 2 +gReservedSpritePaletteCount: + .space 1 + .align 4 .include "link.o" .include "link_rfu.o" .include "rtc.o" diff --git a/sym_ewram.txt b/sym_ewram.txt index 3de0c45cb..bdbb85d37 100644 --- a/sym_ewram.txt +++ b/sym_ewram.txt @@ -1,9 +1,9 @@ .include "src/decompress.o" .include "src/main.o" - .include "src/window.o" - .include "src/text.o" - .include "src/sprite.o" - .include "src/string_util.o" + .include "gflib/window.o" + .include "gflib/text.o" + .include "gflib/sprite.o" + .include "gflib/string_util.o" .include "src/link.o" .include "src/link_rfu.o" .include "src/union_room.o" -- cgit v1.2.3 From 888cbd634c746307a3992218a384139bfd5534d5 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 8 Sep 2019 21:26:46 -0400 Subject: libagbsyscall as an actual lib --- Makefile | 15 +- berry_fix/Makefile | 28 ++- berry_fix/payload/Makefile | 26 ++- ld_script.txt | 15 ++ libagbsyscall/Makefile | 98 ++++++++++ libagbsyscall/libagbsyscall.s | 432 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 604 insertions(+), 10 deletions(-) create mode 100644 libagbsyscall/Makefile create mode 100644 libagbsyscall/libagbsyscall.s diff --git a/Makefile b/Makefile index 451a28740..6869e5df1 100644 --- a/Makefile +++ b/Makefile @@ -82,7 +82,7 @@ endif LDFLAGS = -Map ../../$(MAP) -LIB := $(LIBPATH) -lgcc -lc +LIB := $(LIBPATH) -lgcc -lc -L../../libagbsyscall -lagbsyscall SHA1 := $(shell { command -v sha1sum || command -v shasum; } 2>/dev/null) -c GFX := tools/gbagfx/gbagfx$(EXE) @@ -111,7 +111,7 @@ MAKEFLAGS += --no-print-directory # Secondary expansion is required for dependency variables in object rules. .SECONDEXPANSION: -.PHONY: all rom clean compare tidy tools mostlyclean clean-tools $(TOOLDIRS) berry_fix +.PHONY: all rom clean compare tidy tools mostlyclean clean-tools $(TOOLDIRS) berry_fix libagbsyscall infoshell = $(foreach line, $(shell $1 | sed "s/ /__SPACE__/g"), $(info $(subst __SPACE__, ,$(line)))) @@ -161,7 +161,7 @@ tools: $(TOOLDIRS) $(TOOLDIRS): @$(MAKE) -C $@ CC=$(HOSTCC) CXX=$(HOSTCXX) -rom: berry_fix $(ROM) +rom: $(ROM) ifeq ($(COMPARE),1) @$(SHA1) rom.sha1 endif @@ -182,7 +182,9 @@ mostlyclean: tidy rm -f $(DATA_ASM_SUBDIR)/maps/connections.inc $(DATA_ASM_SUBDIR)/maps/events.inc $(DATA_ASM_SUBDIR)/maps/groups.inc $(DATA_ASM_SUBDIR)/maps/headers.inc find $(DATA_ASM_SUBDIR)/maps \( -iname 'connections.inc' -o -iname 'events.inc' -o -iname 'header.inc' \) -exec rm {} + rm -f $(AUTO_GEN_TARGETS) + rm -f $(OBJ_DIR)/libagbsyscall.a @$(MAKE) clean -C berry_fix + @$(MAKE) clean -C libagbsyscall tidy: rm -f $(ROM) $(ELF) $(MAP) @@ -310,7 +312,7 @@ endif $(OBJ_DIR)/ld_script.ld: $(LD_SCRIPT) $(LD_SCRIPT_DEPS) cd $(OBJ_DIR) && sed "s#tools/#../../tools/#g" ../../$(LD_SCRIPT) > ld_script.ld -$(ELF): $(OBJ_DIR)/ld_script.ld $(OBJS) +$(ELF): $(OBJ_DIR)/ld_script.ld $(OBJS) libagbsyscall cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ld_script.ld -o ../../$@ $(OBJS_REL) $(LIB) $(FIX) $@ -t"$(TITLE)" -c$(GAME_CODE) -m$(MAKER_CODE) -r$(REVISION) --silent @@ -323,4 +325,7 @@ modern: ; @$(MAKE) MODERN=1 berry_fix/berry_fix.gba: berry_fix berry_fix: - @$(MAKE) -C berry_fix COMPARE=$(COMPARE) + @$(MAKE) -C berry_fix COMPARE=$(COMPARE) TOOLCHAIN=$(TOOLCHAIN) + +libagbsyscall: + @$(MAKE) -C libagbsyscall TOOLCHAIN=$(TOOLCHAIN) diff --git a/berry_fix/Makefile b/berry_fix/Makefile index 2bc225eba..0ead3804f 100644 --- a/berry_fix/Makefile +++ b/berry_fix/Makefile @@ -1,7 +1,29 @@ -include $(DEVKITARM)/base_tools +TOOLCHAIN := $(DEVKITARM) +COMPARE ?= 0 + +ifeq ($(CC),) +HOSTCC := gcc +else +HOSTCC := $(CC) +endif + +ifeq ($(CXX),) +HOSTCXX := g++ +else +HOSTCXX := $(CXX) +endif + +ifneq (,$(wildcard $(TOOLCHAIN)/base_tools)) +include $(TOOLCHAIN)/base_tools +else +export PATH := $(TOOLCHAIN)/bin:$(PATH) +PREFIX := arm-none-eabi- +OBJCOPY := $(PREFIX)objcopy +export CC := $(PREFIX)gcc +export AS := $(PREFIX)as +endif export CPP := $(PREFIX)cpp export LD := $(PREFIX)ld -COMPARE ?= 0 ifeq ($(OS),Windows_NT) EXE := .exe @@ -144,7 +166,7 @@ $(DATA_ASM_BUILDDIR)/%.o: data_dep = $(shell $(SCANINC) $(DATA_ASM_SUBDIR)/$*.s) endif payload: - @$(MAKE) -C payload COMPARE=$(COMPARE) + @$(MAKE) -C payload COMPARE=$(COMPARE) TOOLCHAIN=$(TOOLCHAIN) payload/payload.gba: payload diff --git a/berry_fix/payload/Makefile b/berry_fix/payload/Makefile index 63e326724..a121fda93 100644 --- a/berry_fix/payload/Makefile +++ b/berry_fix/payload/Makefile @@ -1,7 +1,29 @@ -include $(DEVKITARM)/base_tools +TOOLCHAIN := $(DEVKITARM) +COMPARE ?= 0 + +ifeq ($(CC),) +HOSTCC := gcc +else +HOSTCC := $(CC) +endif + +ifeq ($(CXX),) +HOSTCXX := g++ +else +HOSTCXX := $(CXX) +endif + +ifneq (,$(wildcard $(TOOLCHAIN)/base_tools)) +include $(TOOLCHAIN)/base_tools +else +export PATH := $(TOOLCHAIN)/bin:$(PATH) +PREFIX := arm-none-eabi- +OBJCOPY := $(PREFIX)objcopy +export CC := $(PREFIX)gcc +export AS := $(PREFIX)as +endif export CPP := $(PREFIX)cpp export LD := $(PREFIX)ld -COMPARE ?= 0 ifeq ($(OS),Windows_NT) EXE := .exe diff --git a/ld_script.txt b/ld_script.txt index cb3a7fa16..9caa100cc 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -361,6 +361,21 @@ SECTIONS { src/librfu_rfu.o(.text); asm/librfu.o(.text); src/libisagbprn.o(.text); + *libagbsyscall.a:ArcTan2.o(.text); + *libagbsyscall.a:BgAffineSet.o(.text); + *libagbsyscall.a:CpuFastSet.o(.text); + *libagbsyscall.a:CpuSet.o(.text); + *libagbsyscall.a:Div.o(.text); + *libagbsyscall.a:LZ77UnCompVram.o(.text); + *libagbsyscall.a:LZ77UnCompWram.o(.text); + *libagbsyscall.a:MultiBoot.o(.text); + *libagbsyscall.a:ObjAffineSet.o(.text); + *libagbsyscall.a:RLUnCompVram.o(.text); + *libagbsyscall.a:RLUnCompWram.o(.text); + *libagbsyscall.a:RegisterRamReset.o(.text); + *libagbsyscall.a:SoftReset.o(.text); + *libagbsyscall.a:Sqrt.o(.text); + *libagbsyscall.a:VBlankIntrWait.o(.text); src/libagbsyscall.o(.text); *libgcc.a:_call_via_rX.o(.text); *libgcc.a:_divdi3.o(.text); diff --git a/libagbsyscall/Makefile b/libagbsyscall/Makefile new file mode 100644 index 000000000..911cdb237 --- /dev/null +++ b/libagbsyscall/Makefile @@ -0,0 +1,98 @@ +TOOLCHAIN := $(DEVKITARM) +COMPARE ?= 0 + +ifeq ($(CC),) +HOSTCC := gcc +else +HOSTCC := $(CC) +endif + +ifeq ($(CXX),) +HOSTCXX := g++ +else +HOSTCXX := $(CXX) +endif + +ifneq (,$(wildcard $(TOOLCHAIN)/base_tools)) +include $(TOOLCHAIN)/base_tools +else +export PATH := $(TOOLCHAIN)/bin:$(PATH) +PREFIX := arm-none-eabi- +OBJCOPY := $(PREFIX)objcopy +export CC := $(PREFIX)gcc +export AS := $(PREFIX)as +endif +export CPP := $(PREFIX)cpp +export LD := $(PREFIX)ld + +ifeq ($(OS),Windows_NT) +EXE := .exe +else +EXE := +endif + +ASFLAGS := -mcpu=arm7tdmi +ARFLAGS := rc + +SYSCALLS := IntrWait \ + RegisterRamReset \ + Sqrt \ + MusicPlayerOpen \ + SoundBiasReset \ + SoundDriverVSyncOn \ + Mod \ + VBlankIntrWait \ + MusicPlayerStart \ + SoundDriverVSyncOff \ + HuffUnComp \ + SoftResetExram \ + MusicPlayerFadeOut \ + LZ77UnCompWram \ + SoundDriverMain \ + SoundBiasChange \ + LZ77UnCompVram \ + ArcTan2 \ + MusicPlayerStop \ + DivArm \ + ModArm \ + SoundDriverVSync \ + SoundDriverInit \ + BgAffineSet \ + Diff8bitUnFilterWram \ + MultiBoot \ + MidiKey2Freq \ + Div \ + Diff8bitUnFilterVram \ + ArcTan \ + ObjAffineSet \ + SoftResetRom \ + SoundDriverMode \ + RLUnCompWram \ + BitUnPack \ + SoundChannelClear \ + CpuFastSet \ + CpuSet \ + Diff16bitUnFilter \ + SoundBiasSet \ + MusicPlayerContinue \ + SoftReset \ + RLUnCompVram + +ASM_SRCS := libagbsyscall.s +ASM_OBJS := $(foreach syscall, $(SYSCALLS), $(syscall).o) + +LIB := libagbsyscall.a + +.PHONY: all clean + +all: $(LIB) + @: + +clean: + rm -f $(LIB) $(ASM_OBJS) + +$(LIB): $(ASM_OBJS) + $(AR) $(ARFLAGS) $@ $^ + +$(ASM_OBJS): libagbsyscall.s + $(AS) $(ASFLAGS) --defsym L_$(*F)=1 -o $@ $< diff --git a/libagbsyscall/libagbsyscall.s b/libagbsyscall/libagbsyscall.s new file mode 100644 index 000000000..893235e32 --- /dev/null +++ b/libagbsyscall/libagbsyscall.s @@ -0,0 +1,432 @@ + .include "../constants/gba_constants.inc" + .include "../asm/macros/function.inc" + + .text + + .set SOFT_RESET_DIRECT_BUF, 0x03007FFA + .set RESET_EX_WRAM_FLAG, 0x1 + + .ifdef NO_GRANULAR_AGBSYSCALL + .set L_IntrWait, 1 + .set L_RegisterRamReset, 1 + .set L_Sqrt, 1 + .set L_MusicPlayerOpen, 1 + .set L_SoundBiasReset, 1 + .set L_SoundDriverVSyncOn, 1 + .set L_Mod, 1 + .set L_VBlankIntrWait, 1 + .set L_MusicPlayerStart, 1 + .set L_SoundDriverVSyncOff, 1 + .set L_HuffUnComp, 1 + .set L_SoftResetExram, 1 + .set L_MusicPlayerFadeOut, 1 + .set L_LZ77UnCompWram, 1 + .set L_SoundDriverMain, 1 + .set L_SoundBiasChange, 1 + .set L_LZ77UnCompVram, 1 + .set L_ArcTan2, 1 + .set L_MusicPlayerStop, 1 + .set L_DivArm, 1 + .set L_ModArm, 1 + .set L_SoundDriverVSync, 1 + .set L_SoundDriverInit, 1 + .set L_BgAffineSet, 1 + .set L_Diff8bitUnFilterWram, 1 + .set L_MultiBoot, 1 + .set L_MidiKey2Freq, 1 + .set L_Div, 1 + .set L_Diff8bitUnFilterVram, 1 + .set L_ArcTan, 1 + .set L_ObjAffineSet, 1 + .set L_SoftResetRom, 1 + .set L_SoundDriverMode, 1 + .set L_RLUnCompWram, 1 + .set L_BitUnPack, 1 + .set L_SoundChannelClear, 1 + .set L_CpuFastSet, 1 + .set L_CpuSet, 1 + .set L_Diff16bitUnFilter, 1 + .set L_SoundBiasSet, 1 + .set L_MusicPlayerContinue, 1 + .set L_SoftReset, 1 + .set L_RLUnCompVram, 1 + .endif + + .ifdef L_IntrWait + thumb_func_start IntrWait +IntrWait: + mov r2, #0 + swi 4 + bx lr + thumb_func_end IntrWait + .endif + + .ifdef L_RegisterRamReset + thumb_func_start RegisterRamReset +RegisterRamReset: + swi 1 + bx lr + thumb_func_end RegisterRamReset + .endif + + .ifdef L_Sqrt + thumb_func_start Sqrt +Sqrt: + swi 8 + bx lr + thumb_func_end Sqrt + .endif + + .ifdef L_MusicPlayerOpen + thumb_func_start MusicPlayerOpen +MusicPlayerOpen: + swi 32 + bx lr + thumb_func_end MusicPlayerOpen + .endif + + .ifdef L_SoundBiasReset + thumb_func_start SoundBiasReset +SoundBiasReset: + mov r0, #0 + swi 25 + bx lr + thumb_func_end SoundBiasReset + .endif + + .ifdef L_SoundDriverVSyncOn + thumb_func_start SoundDriverVSyncOn +SoundDriverVSyncOn: + swi 41 + bx lr + thumb_func_end SoundDriverVSyncOn + .endif + + .ifdef L_Mod + thumb_func_start Mod +Mod: + swi 6 + mov r0, r1 + bx lr + thumb_func_end Mod + .endif + + .ifdef L_VBlankIntrWait + thumb_func_start VBlankIntrWait +VBlankIntrWait: + mov r2, #0 + swi 5 + bx lr + thumb_func_end VBlankIntrWait + .endif + + .ifdef L_MusicPlayerStart + thumb_func_start MusicPlayerStart +MusicPlayerStart: + swi 33 + bx lr + thumb_func_end MusicPlayerStart + .endif + + .ifdef L_SoundDriverVSyncOff + thumb_func_start SoundDriverVSyncOff +SoundDriverVSyncOff: + swi 40 + bx lr + thumb_func_end SoundDriverVSyncOff + .endif + + .ifdef L_HuffUnComp + thumb_func_start HuffUnComp +HuffUnComp: + swi 19 + bx lr + thumb_func_end HuffUnComp + .endif + + .ifdef L_SoftResetExram + thumb_func_start SoftResetExram +SoftResetExram: + ldr r3, =REG_IME + mov r2, #0 + strb r2, [r3, #0] + ldr r3, =SOFT_RESET_DIRECT_BUF + mov r2, #1 + strb r2, [r3, #0] + sub r3, #SOFT_RESET_DIRECT_BUF - 0x3007f00 + mov sp, r3 + mov r2, #RESET_EX_WRAM_FLAG + bic r0, r2 + swi 1 + swi 0 + .pool + thumb_func_end SoftResetExram + .endif + + .ifdef L_MusicPlayerFadeOut + thumb_func_start MusicPlayerFadeOut +MusicPlayerFadeOut: + swi 36 + bx lr + thumb_func_end MusicPlayerFadeOut + .endif + + .ifdef L_LZ77UnCompWram + thumb_func_start LZ77UnCompWram +LZ77UnCompWram: + swi 17 + bx lr + thumb_func_end LZ77UnCompWram + .endif + + .ifdef L_SoundDriverMain + thumb_func_start SoundDriverMain +SoundDriverMain: + swi 28 + bx lr + thumb_func_end SoundDriverMain + .endif + + .ifdef L_SoundBiasChange + thumb_func_start SoundBiasChange +SoundBiasChange: + swi 25 + bx lr + thumb_func_end SoundBiasChange + .endif + + .ifdef L_LZ77UnCompVram + thumb_func_start LZ77UnCompVram +LZ77UnCompVram: + swi 18 + bx lr + thumb_func_end LZ77UnCompVram + .endif + + .ifdef L_ArcTan2 + thumb_func_start ArcTan2 +ArcTan2: + swi 10 + bx lr + thumb_func_end ArcTan2 + .endif + + .ifdef L_MusicPlayerStop + thumb_func_start MusicPlayerStop +MusicPlayerStop: + swi 34 + bx lr + thumb_func_end MusicPlayerStop + .endif + + .ifdef L_DivArm + thumb_func_start DivArm +DivArm: + swi 7 + bx lr + thumb_func_end DivArm + .endif + + .ifdef L_ModArm + thumb_func_start ModArm +ModArm: + swi 7 + mov r0, r1 + bx lr + thumb_func_end ModArm + .endif + + .ifdef L_SoundDriverVSync + thumb_func_start SoundDriverVSync +SoundDriverVSync: + swi 29 + bx lr + thumb_func_end SoundDriverVSync + .endif + + .ifdef L_SoundDriverInit + thumb_func_start SoundDriverInit +SoundDriverInit: + swi 26 + bx lr + thumb_func_end SoundDriverInit + .endif + + .ifdef L_BgAffineSet + thumb_func_start BgAffineSet +BgAffineSet: + swi 14 + bx lr + thumb_func_end BgAffineSet + .endif + + .ifdef L_Diff8bitUnFilterWram + thumb_func_start Diff8bitUnFilterWram +Diff8bitUnFilterWram: + swi 22 + bx lr + thumb_func_end Diff8bitUnFilterWram + .endif + + .ifdef L_MultiBoot + thumb_func_start MultiBoot +MultiBoot: + mov r1, #1 + swi 37 + bx lr + thumb_func_end MultiBoot + .endif + + .ifdef L_MidiKey2Freq + thumb_func_start MidiKey2Freq +MidiKey2Freq: + swi 31 + bx lr + thumb_func_end MidiKey2Freq + .endif + + .ifdef L_Div + thumb_func_start Div +Div: + swi 6 + bx lr + thumb_func_end Div + .endif + + .ifdef L_Diff8bitUnFilterVram + thumb_func_start Diff8bitUnFilterVram +Diff8bitUnFilterVram: + swi 23 + bx lr + thumb_func_end Diff8bitUnFilterVram + .endif + + .ifdef L_ArcTan + thumb_func_start ArcTan +ArcTan: + swi 9 + bx lr + thumb_func_end ArcTan + .endif + + .ifdef L_ObjAffineSet + thumb_func_start ObjAffineSet +ObjAffineSet: + swi 15 + bx lr + thumb_func_end ObjAffineSet + .endif + + .ifdef L_SoftResetRom + thumb_func_start SoftResetRom +SoftResetRom: + ldr r3, =REG_IME + mov r2, #0 + strb r2, [r3, #0] + ldr r3, =SOFT_RESET_DIRECT_BUF + mov r2, #0 + strb r2, [r3, #0] + sub r3, #SOFT_RESET_DIRECT_BUF - 0x3007f00 + mov sp, r3 + swi 1 + swi 0 + .pool + thumb_func_end SoftResetRom + .endif + + .ifdef L_SoundDriverMode + thumb_func_start SoundDriverMode +SoundDriverMode: + swi 27 + bx lr + thumb_func_end SoundDriverMode + .endif + + .ifdef L_RLUnCompWram + thumb_func_start RLUnCompWram +RLUnCompWram: + swi 20 + bx lr + thumb_func_end RLUnCompWram + .endif + + .ifdef L_BitUnPack + thumb_func_start BitUnPack +BitUnPack: + swi 16 + bx lr + thumb_func_end BitUnPack + .endif + + .ifdef L_SoundChannelClear + thumb_func_start SoundChannelClear +SoundChannelClear: + swi 30 + bx lr + thumb_func_end SoundChannelClear + .endif + + .ifdef L_CpuFastSet + thumb_func_start CpuFastSet +CpuFastSet: + swi 12 + bx lr + thumb_func_end CpuFastSet + .endif + + .ifdef L_CpuSet + thumb_func_start CpuSet +CpuSet: + swi 11 + bx lr + thumb_func_end CpuSet + .endif + + .ifdef L_Diff16bitUnFilter + thumb_func_start Diff16bitUnFilter +Diff16bitUnFilter: + swi 24 + bx lr + thumb_func_end Diff16bitUnFilter + .endif + + .ifdef L_SoundBiasSet + thumb_func_start SoundBiasSet +SoundBiasSet: + mov r0, #1 + swi 25 + bx lr + thumb_func_end SoundBiasSet + .endif + + .ifdef L_MusicPlayerContinue + thumb_func_start MusicPlayerContinue +MusicPlayerContinue: + swi 35 + bx lr + thumb_func_end MusicPlayerContinue + .endif + + .ifdef L_SoftReset + thumb_func_start SoftReset +SoftReset: + ldr r3, =REG_IME + mov r2, #0 + strb r2, [r3, #0] + ldr r1, =0x3007f00 + mov sp, r1 + swi 1 + swi 0 + .pool + thumb_func_end SoftReset + .endif + + .ifdef L_RLUnCompVram + thumb_func_start RLUnCompVram +RLUnCompVram: + swi 21 + bx lr + thumb_func_end RLUnCompVram + .endif + + .align 2, 0 @ Don't pad with nop -- cgit v1.2.3 From 27aae9da9a570b6787171e33fa29c43c507187c1 Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Sun, 8 Sep 2019 21:41:40 -0400 Subject: Fix modern --- Makefile | 2 +- ld_script.txt | 1 - ld_script_modern.txt | 7 +++- src/libagbsyscall.s | 116 --------------------------------------------------- 4 files changed, 7 insertions(+), 119 deletions(-) delete mode 100644 src/libagbsyscall.s diff --git a/Makefile b/Makefile index 6869e5df1..ca0f76cfc 100644 --- a/Makefile +++ b/Makefile @@ -312,7 +312,7 @@ endif $(OBJ_DIR)/ld_script.ld: $(LD_SCRIPT) $(LD_SCRIPT_DEPS) cd $(OBJ_DIR) && sed "s#tools/#../../tools/#g" ../../$(LD_SCRIPT) > ld_script.ld -$(ELF): $(OBJ_DIR)/ld_script.ld $(OBJS) libagbsyscall +$(ELF): $(OBJ_DIR)/ld_script.ld $(OBJS) berry_fix libagbsyscall cd $(OBJ_DIR) && $(LD) $(LDFLAGS) -T ld_script.ld -o ../../$@ $(OBJS_REL) $(LIB) $(FIX) $@ -t"$(TITLE)" -c$(GAME_CODE) -m$(MAKER_CODE) -r$(REVISION) --silent diff --git a/ld_script.txt b/ld_script.txt index 9caa100cc..d37cd595f 100644 --- a/ld_script.txt +++ b/ld_script.txt @@ -376,7 +376,6 @@ SECTIONS { *libagbsyscall.a:SoftReset.o(.text); *libagbsyscall.a:Sqrt.o(.text); *libagbsyscall.a:VBlankIntrWait.o(.text); - src/libagbsyscall.o(.text); *libgcc.a:_call_via_rX.o(.text); *libgcc.a:_divdi3.o(.text); *libgcc.a:_divsi3.o(.text); diff --git a/ld_script_modern.txt b/ld_script_modern.txt index 5157e81dc..bf575857e 100644 --- a/ld_script_modern.txt +++ b/ld_script_modern.txt @@ -14,6 +14,7 @@ SECTIONS { . = 0x1C000; src/*.o(ewram_data); + gflib/*.o(ewram_data); . = 0x40000; } @@ -25,12 +26,14 @@ SECTIONS { { /* .bss starts at 0x3000000 */ src/*.o(.bss); + gflib/*.o(.bss); /* .bss.code starts at 0x3001AA8 */ src/m4a.o(.bss.code); /* COMMON starts at 0x30022A8 */ src/*.o(COMMON); + gflib/*.o(COMMON); *libc.a:sbrkr.o(COMMON); end = .; . = 0x8000; @@ -43,6 +46,7 @@ SECTIONS { { src/crt0.o(.text); src/*.o(.text); + gflib/*.o(.text); asm/*.o(.text); } =0 @@ -67,7 +71,7 @@ SECTIONS { asm/librfu_intr.o(.text); src/librfu_rfu.o(.text); asm/librfu.o(.text); - src/libagbsyscall.o(.text); + *libagbsyscall.a:*.o(.text*); *libgcc.a:*.o(.text*); *libc.a:*.o(.text*); src/libisagbprn.o(.text); @@ -77,6 +81,7 @@ SECTIONS { ALIGN(4) { src/*.o(.rodata*); + gflib/*.o(.rodata*); data/*.o(.rodata*); } =0 diff --git a/src/libagbsyscall.s b/src/libagbsyscall.s deleted file mode 100644 index d5639f689..000000000 --- a/src/libagbsyscall.s +++ /dev/null @@ -1,116 +0,0 @@ - .include "constants/gba_constants.inc" - .include "asm/macros.inc" - - .syntax unified - - .text - - thumb_func_start ArcTan2 -ArcTan2: @ 82E7078 - swi 0xA - bx lr - thumb_func_end ArcTan2 - - thumb_func_start BgAffineSet -@ void BgAffineSet(BgAffineSet_src_data *src, BgAffineSet_dest_data *dest, int entry_count) -BgAffineSet: @ 82E707C - swi 0xE - bx lr - thumb_func_end BgAffineSet - - thumb_func_start CpuFastSet -@ void CpuFastSet(void *src, void *dest, unsigned int mode) -CpuFastSet: @ 82E7080 - swi 0xC - bx lr - thumb_func_end CpuFastSet - - thumb_func_start CpuSet -@ void CpuSet(void *src, void *dest, unsigned int mode) -CpuSet: @ 82E7084 - swi 0xB - bx lr - thumb_func_end CpuSet - - thumb_func_start Div -Div: @ 82E7088 - swi 0x6 - bx lr - thumb_func_end Div - - thumb_func_start LZ77UnCompVram -@ void LZ77UnCompVram(void *src, void *dest) -LZ77UnCompVram: @ 82E708C - swi 0x12 - bx lr - thumb_func_end LZ77UnCompVram - - thumb_func_start LZ77UnCompWram -@ void LZ77UnCompWram(void *src, void *dest) -LZ77UnCompWram: @ 82E7090 - swi 0x11 - bx lr - thumb_func_end LZ77UnCompWram - - thumb_func_start MultiBoot -@ s32 MultiBoot(struct MultiBootParam *mp) -MultiBoot: @ 82E7094 - movs r1, 0x1 - swi 0x25 - bx lr - thumb_func_end MultiBoot - - thumb_func_start ObjAffineSet -ObjAffineSet: @ 82E709C - swi 0xF - bx lr - thumb_func_end ObjAffineSet - - thumb_func_start RLUnCompVram -RLUnCompVram: @ 82E70A0 - swi 0x15 - bx lr - thumb_func_end RLUnCompVram - - thumb_func_start RLUnCompWram -RLUnCompWram: @ 82E70A4 - swi 0x14 - bx lr - thumb_func_end RLUnCompWram - - thumb_func_start RegisterRamReset -@ void RegisterRamReset(int ResetFlags) -RegisterRamReset: @ 82E70A8 - swi 0x1 - bx lr - thumb_func_end RegisterRamReset - - thumb_func_start SoftReset -@ void SoftReset() -SoftReset: @ 82E70AC - ldr r3, =REG_IME - movs r2, 0 - strb r2, [r3] - ldr r1, =0x3007F00 - mov sp, r1 - swi 0x1 - swi 0 - .pool - thumb_func_end SoftReset - - thumb_func_start Sqrt -@ s16 Sqrt(int) -Sqrt: @ 82E70C4 - swi 0x8 - bx lr - thumb_func_end Sqrt - - thumb_func_start VBlankIntrWait -@ void VBlankIntrWait() -VBlankIntrWait: @ 82E70C8 - movs r2, 0 - swi 0x5 - bx lr - thumb_func_end VBlankIntrWait - - .align 2, 0 @ Don't pad with nop. -- cgit v1.2.3 From ab139bf2542971c085f05617aa4cedbe7ae859dd Mon Sep 17 00:00:00 2001 From: PikalaxALT Date: Fri, 27 Sep 2019 09:31:31 -0400 Subject: Clean up leftover Makefile clean instruction --- Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/Makefile b/Makefile index ca0f76cfc..293f86f17 100644 --- a/Makefile +++ b/Makefile @@ -182,7 +182,6 @@ mostlyclean: tidy rm -f $(DATA_ASM_SUBDIR)/maps/connections.inc $(DATA_ASM_SUBDIR)/maps/events.inc $(DATA_ASM_SUBDIR)/maps/groups.inc $(DATA_ASM_SUBDIR)/maps/headers.inc find $(DATA_ASM_SUBDIR)/maps \( -iname 'connections.inc' -o -iname 'events.inc' -o -iname 'header.inc' \) -exec rm {} + rm -f $(AUTO_GEN_TARGETS) - rm -f $(OBJ_DIR)/libagbsyscall.a @$(MAKE) clean -C berry_fix @$(MAKE) clean -C libagbsyscall -- cgit v1.2.3 From 53bc1bd0d196ff5975c76be474d033db778aa6e2 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 30 Sep 2019 00:09:29 -0400 Subject: Document text input strings --- data/text_input_strings.s | 92 ++++++++++++++++----------------- include/naming_screen.h | 16 +++--- include/strings.h | 93 ++++++++++++++++----------------- include/union_room_chat.h | 11 ++++ src/easy_chat.c | 21 ++++---- src/naming_screen.c | 116 ++++++++++++++++++++++------------------- src/union_room_chat.c | 128 +++++++++++++++++++++++++++++++--------------- 7 files changed, 273 insertions(+), 204 deletions(-) diff --git a/data/text_input_strings.s b/data/text_input_strings.s index 919b49416..bb4006a1f 100644 --- a/data/text_input_strings.s +++ b/data/text_input_strings.s @@ -5,142 +5,142 @@ .align 2 -gUnknown_862B810:: @ 862B810 +gText_EasyChatKeyboard_ABCDEFothers:: @ 862B810 .string "{CLEAR 11}A{CLEAR 6}B{CLEAR 6}C{CLEAR 26}D{CLEAR 6}E{CLEAR 6}F{CLEAR 26}others$" -gUnknown_862B832:: @ 862B832 +gText_EasyChatKeyboard_GHIJKL:: @ 862B832 .string "{CLEAR 11}G{CLEAR 6}H{CLEAR 6}I{CLEAR 26}J{CLEAR 6}K{CLEAR 6}L$" -gUnknown_862B84B:: @ 862B84B +gText_EasyChatKeyboard_MNOPQRS:: @ 862B84B .string "{CLEAR 11}M{CLEAR 6}N{CLEAR 6}O{CLEAR 26}P{CLEAR 6}Q{CLEAR 6}R{CLEAR 6}S{CLEAR 26} $" -gUnknown_862B86C:: @ 862B86C +gText_EasyChatKeyboard_TUVWXYZ:: @ 862B86C .string "{CLEAR 11}T{CLEAR 6}U{CLEAR 6}V{CLEAR 26}W{CLEAR 6}X{CLEAR 6}Y{CLEAR 6}Z{CLEAR 26} $" -gUnknown_0862B88D:: @ 862B88D +gText_NamingScreenKeyboard_abcdef:: @ 862B88D .string "{CLEAR 11}a{CLEAR 6}b{CLEAR 6}c{CLEAR 26}d{CLEAR 6}e{CLEAR 6}f{CLEAR 6} {CLEAR 30}.$" -gUnknown_0862B8AE:: @ 862B8AE +gText_NamingScreenKeyboard_ghijkl:: @ 862B8AE .string "{CLEAR 11}g{CLEAR 6}h{CLEAR 7}i{CLEAR 27}j{CLEAR 7}k{CLEAR 7}l{CLEAR 7} {CLEAR 30},$" -gUnknown_0862B8CF:: @ 862B8CF +gText_NamingScreenKeyboard_mnopqrs:: @ 862B8CF .string "{CLEAR 11}m{CLEAR 6}n{CLEAR 6}o{CLEAR 26}p{CLEAR 6}q{CLEAR 7}r{CLEAR 6}s{CLEAR 27} $" -gUnknown_0862B8F0:: @ 862B8F0 +gText_NamingScreenKeyboard_tuvwxyz:: @ 862B8F0 .string "{CLEAR 11}t{CLEAR 6}u{CLEAR 6}v{CLEAR 26}w{CLEAR 6}x{CLEAR 6}y{CLEAR 6}z{CLEAR 26} $" -gUnknown_0862B911:: @ 862B911 +gText_NamingScreenKeyboard_ABCDEF:: @ 862B911 .string "{CLEAR 11}A{CLEAR 6}B{CLEAR 6}C{CLEAR 26}D{CLEAR 6}E{CLEAR 6}F{CLEAR 6} {CLEAR 30}.$" -gUnknown_0862B932:: @ 862B932 +gText_NamingScreenKeyboard_GHIJKL:: @ 862B932 .string "{CLEAR 11}G{CLEAR 6}H{CLEAR 6}I{CLEAR 26}J{CLEAR 6}K{CLEAR 6}L{CLEAR 6} {CLEAR 30},$" -gUnknown_0862B953:: @ 862B953 +gText_NamingScreenKeyboard_MNOPQRS:: @ 862B953 .string "{CLEAR 11}M{CLEAR 6}N{CLEAR 6}O{CLEAR 26}P{CLEAR 6}Q{CLEAR 6}R{CLEAR 6}S{CLEAR 26} $" -gUnknown_0862B974:: @ 862B974 +gText_NamingScreenKeyboard_TUVWXYZ:: @ 862B974 .string "{CLEAR 11}T{CLEAR 6}U{CLEAR 6}V{CLEAR 26}W{CLEAR 6}X{CLEAR 6}Y{CLEAR 6}Z{CLEAR 26} $" -gUnknown_0862B995:: @ 862B995 +gText_NamingScreenKeyboard_01234:: @ 862B995 .string "{CLEAR 11}0{CLEAR 16}1{CLEAR 16}2{CLEAR 16}3{CLEAR 16}4{CLEAR 16} $" -gUnknown_0862B9AE:: @ 862B9AE +gText_NamingScreenKeyboard_56789:: @ 862B9AE .string "{CLEAR 11}5{CLEAR 16}6{CLEAR 16}7{CLEAR 16}8{CLEAR 16}9{CLEAR 16} $" -gUnknown_0862B9C7:: @ 862B9C7 +gText_NamingScreenKeyboard_Symbols1:: @ 862B9C7 .string "{CLEAR 12}!{CLEAR 17}?{CLEAR 16}♂{CLEAR 16}♀{CLEAR 16}/{CLEAR 17}-$" -gUnknown_0862B9E0:: @ 862B9E0 +gText_NamingScreenKeyboard_Symbols2:: @ 862B9E0 .string "{CLEAR 11}…{CLEAR 16}“{CLEAR 16}”{CLEAR 18}‘{CLEAR 19}'{CLEAR 18} $" -gUnknown_0862B9F9:: @ 862B9F9 +gText_UnionRoomChatKeyboard_ABCDE:: @ 862B9F9 .string "ABCDE$" -gUnknown_0862B9FF:: @ 862B9FF +gText_UnionRoomChatKeyboard_FGHIJ:: @ 862B9FF .string "FGHIJ$" -gUnknown_0862BA05:: @ 862BA05 +gText_UnionRoomChatKeyboard_KLMNO:: @ 862BA05 .string "KLMNO$" -gUnknown_0862BA0B:: @ 862BA0B +gText_UnionRoomChatKeyboard_PQRST:: @ 862BA0B .string "PQRST$" -gUnknown_0862BA11:: @ 862BA11 +gText_UnionRoomChatKeyboard_UVWXY:: @ 862BA11 .string "UVWXY$" -gUnknown_0862BA17:: @ 862BA17 +gText_UnionRoomChatKeyboard_Z:: @ 862BA17 .string "Z $" -gUnknown_0862BA1D:: @ 862BA1D +gText_UnionRoomChatKeyboard_01234Upper:: @ 862BA1D .string "01234$" -gUnknown_0862BA23:: @ 862BA23 +gText_UnionRoomChatKeyboard_56789Upper:: @ 862BA23 .string "56789$" -gUnknown_0862BA29:: @ 862BA29 +gText_UnionRoomChatKeyboard_PunctuationUpper:: @ 862BA29 .string ".,!? $" -gUnknown_0862BA2F:: @ 862BA2F +gText_UnionRoomChatKeyboard_SymbolsUpper:: @ 862BA2F .string "-/&… $" -gUnknown_0862BA35:: @ 862BA35 +gText_UnionRoomChatKeyboard_abcde:: @ 862BA35 .string "abcde$" -gUnknown_0862BA3B:: @ 862BA3B +gText_UnionRoomChatKeyboard_fghij:: @ 862BA3B .string "fghij$" -gUnknown_0862BA41:: @ 862BA41 +gText_UnionRoomChatKeyboard_klmno:: @ 862BA41 .string "klmno$" -gUnknown_0862BA47:: @ 862BA47 +gText_UnionRoomChatKeyboard_pqrst:: @ 862BA47 .string "pqrst$" -gUnknown_0862BA4D:: @ 862BA4D +gText_UnionRoomChatKeyboard_uvwxy:: @ 862BA4D .string "uvwxy$" -gUnknown_0862BA53:: @ 862BA53 +gText_UnionRoomChatKeyboard_z:: @ 862BA53 .string "z $" -gUnknown_0862BA59:: @ 862BA59 +gText_UnionRoomChatKeyboard_01234Lower:: @ 862BA59 .string "01234$" -gUnknown_0862BA5F:: @ 862BA5F +gText_UnionRoomChatKeyboard_56789Lower:: @ 862BA5F .string "56789$" -gUnknown_0862BA65:: @ 862BA65 +gText_UnionRoomChatKeyboard_PunctuationLower:: @ 862BA65 .string ".,!? $" -gUnknown_0862BA6B:: @ 862BA6B +gText_UnionRoomChatKeyboard_SymbolsLower:: @ 862BA6B .string "-/&… $" .string "$$$$$$$$" -gUnknown_0862BA79:: @ 862BA79 +gText_UnionRoomChatKeyboard_Emoji1:: @ 862BA79 .string "{EMOJI_MISCHIEVOUS}{EMOJI_HAPPY}{EMOJI_ANGRY}{EMOJI_SURPRISED}{EMOJI_BIGANGER}$" -gUnknown_0862BA84:: @ 862BA84 +gText_UnionRoomChatKeyboard_Emoji2:: @ 862BA84 .string "{EMOJI_BIGSMILE}{EMOJI_EVIL}{EMOJI_NEUTRAL}{EMOJI_TIRED}{EMOJI_SHOCKED}$" -gUnknown_0862BA8F:: @ 862BA8F +gText_UnionRoomChatKeyboard_Emoji3:: @ 862BA8F .string "{EMOJI_LEAF}{EMOJI_FIRE}{EMOJI_WATER}{EMOJI_BOLT}{EMOJI_BALL}$" -gUnknown_0862BA9A:: @ 862BA9A +gText_UnionRoomChatKeyboard_Emoji4:: @ 862BA9A .string "♂♀{EMOJI_LEFT_PAREN}{EMOJI_RIGHT_PAREN}{EMOJI_TILDE}$" -gUnknown_0862BAA3:: @ 862BAA3 +gText_UnionRoomChatKeyboard_Emoji5:: @ 862BAA3 .string "{EMOJI_LEFT_EYE}{EMOJI_RIGHT_EYE}{EMOJI_SMALLWHEEL}{EMOJI_SPHERE}{EMOJI_IRRITATED}$" -gUnknown_0862BAAE:: @ 862BAAE +gText_UnionRoomChatKeyboard_Emoji6:: @ 862BAAE .string "{EMOJI_AT}{EMOJI_BIGWHEEL}{EMOJI_TONGUE}{EMOJI_ACUTE}{EMOJI_GRAVE}$" -gUnknown_0862BAB9:: @ 862BAB9 +gText_UnionRoomChatKeyboard_Emoji7:: @ 862BAB9 .string "{EMOJI_RIGHT_FIST}{EMOJI_LEFT_FIST}{EMOJI_TRIANGLE_OUTLINE}{EMOJI_UNION}{EMOJI_GREATER_THAN}$" -gUnknown_0862BAC4:: @ 862BAC4 +gText_UnionRoomChatKeyboard_Emoji8:: @ 862BAC4 .string "{EMOJI_CIRCLE}{EMOJI_TRIANGLE}{EMOJI_SQUARE}{EMOJI_HEART}{EMOJI_MOON}$" -gUnknown_0862BACF:: @ 862BACF +gText_UnionRoomChatKeyboard_Emoji9:: @ 862BACF .string "{EMOJI_NOTE}{EMOJI_PLUS}{EMOJI_MINUS}{EMOJI_EQUALS}{EMOJI_PIPE}$" -gUnknown_0862BADA:: @ 862BADA +gText_UnionRoomChatKeyboard_Emoji10:: @ 862BADA .string "{EMOJI_HIGHBAR}{EMOJI_UNDERSCORE};: $" diff --git a/include/naming_screen.h b/include/naming_screen.h index 611f53ef2..7d32abcd3 100644 --- a/include/naming_screen.h +++ b/include/naming_screen.h @@ -9,6 +9,16 @@ #define KBEVENT_PRESSED_SELECT 8 #define KBEVENT_PRESSED_START 9 +#define KBROW_COUNT 4 + +enum +{ + KBPAGE_LETTERS_LOWER, + KBPAGE_LETTERS_UPPER, + KBPAGE_SYMBOLS, + KBPAGE_COUNT, +}; + enum { NAMING_SCREEN_PLAYER, @@ -57,12 +67,6 @@ struct NamingScreenData { /*0x1E3C*/ MainCallback returnCallback; }; -enum -{ - PAGE_OTHERS, - PAGE_UPPER, - PAGE_LOWER, -}; enum { diff --git a/include/strings.h b/include/strings.h index c1414269a..0b678f2f5 100644 --- a/include/strings.h +++ b/include/strings.h @@ -2512,52 +2512,53 @@ extern const u8 gEasyChatGroupName_Move2[]; extern const u8 gEasyChatGroupName_TrendySaying[]; extern const u8 gEasyChatGroupName_Pokemon2[]; -extern const u8 gUnknown_862B810[]; -extern const u8 gUnknown_862B832[]; -extern const u8 gUnknown_862B84B[]; -extern const u8 gUnknown_862B86C[]; -extern const u8 gUnknown_0862B88D[]; -extern const u8 gUnknown_0862B8AE[]; -extern const u8 gUnknown_0862B8CF[]; -extern const u8 gUnknown_0862B8F0[]; -extern const u8 gUnknown_0862B911[]; -extern const u8 gUnknown_0862B932[]; -extern const u8 gUnknown_0862B953[]; -extern const u8 gUnknown_0862B974[]; -extern const u8 gUnknown_0862B995[]; -extern const u8 gUnknown_0862B9AE[]; -extern const u8 gUnknown_0862B9C7[]; -extern const u8 gUnknown_0862B9E0[]; -extern const u8 gUnknown_0862B9F9[]; -extern const u8 gUnknown_0862B9FF[]; -extern const u8 gUnknown_0862BA05[]; -extern const u8 gUnknown_0862BA0B[]; -extern const u8 gUnknown_0862BA11[]; -extern const u8 gUnknown_0862BA17[]; -extern const u8 gUnknown_0862BA1D[]; -extern const u8 gUnknown_0862BA23[]; -extern const u8 gUnknown_0862BA29[]; -extern const u8 gUnknown_0862BA2F[]; -extern const u8 gUnknown_0862BA35[]; -extern const u8 gUnknown_0862BA3B[]; -extern const u8 gUnknown_0862BA41[]; -extern const u8 gUnknown_0862BA47[]; -extern const u8 gUnknown_0862BA4D[]; -extern const u8 gUnknown_0862BA53[]; -extern const u8 gUnknown_0862BA59[]; -extern const u8 gUnknown_0862BA5F[]; -extern const u8 gUnknown_0862BA65[]; -extern const u8 gUnknown_0862BA6B[]; -extern const u8 gUnknown_0862BA79[]; -extern const u8 gUnknown_0862BA84[]; -extern const u8 gUnknown_0862BA8F[]; -extern const u8 gUnknown_0862BA9A[]; -extern const u8 gUnknown_0862BAA3[]; -extern const u8 gUnknown_0862BAAE[]; -extern const u8 gUnknown_0862BAB9[]; -extern const u8 gUnknown_0862BAC4[]; -extern const u8 gUnknown_0862BACF[]; -extern const u8 gUnknown_0862BADA[]; +// Text Input Strings +extern const u8 gText_EasyChatKeyboard_ABCDEFothers[]; +extern const u8 gText_EasyChatKeyboard_GHIJKL[]; +extern const u8 gText_EasyChatKeyboard_MNOPQRS[]; +extern const u8 gText_EasyChatKeyboard_TUVWXYZ[]; +extern const u8 gText_NamingScreenKeyboard_abcdef[]; +extern const u8 gText_NamingScreenKeyboard_ghijkl[]; +extern const u8 gText_NamingScreenKeyboard_mnopqrs[]; +extern const u8 gText_NamingScreenKeyboard_tuvwxyz[]; +extern const u8 gText_NamingScreenKeyboard_ABCDEF[]; +extern const u8 gText_NamingScreenKeyboard_GHIJKL[]; +extern const u8 gText_NamingScreenKeyboard_MNOPQRS[]; +extern const u8 gText_NamingScreenKeyboard_TUVWXYZ[]; +extern const u8 gText_NamingScreenKeyboard_01234[]; +extern const u8 gText_NamingScreenKeyboard_56789[]; +extern const u8 gText_NamingScreenKeyboard_Symbols1[]; +extern const u8 gText_NamingScreenKeyboard_Symbols2[]; +extern const u8 gText_UnionRoomChatKeyboard_ABCDE[]; +extern const u8 gText_UnionRoomChatKeyboard_FGHIJ[]; +extern const u8 gText_UnionRoomChatKeyboard_KLMNO[]; +extern const u8 gText_UnionRoomChatKeyboard_PQRST[]; +extern const u8 gText_UnionRoomChatKeyboard_UVWXY[]; +extern const u8 gText_UnionRoomChatKeyboard_Z[]; +extern const u8 gText_UnionRoomChatKeyboard_01234Upper[]; +extern const u8 gText_UnionRoomChatKeyboard_56789Upper[]; +extern const u8 gText_UnionRoomChatKeyboard_PunctuationUpper[]; +extern const u8 gText_UnionRoomChatKeyboard_SymbolsUpper[]; +extern const u8 gText_UnionRoomChatKeyboard_abcde[]; +extern const u8 gText_UnionRoomChatKeyboard_fghij[]; +extern const u8 gText_UnionRoomChatKeyboard_klmno[]; +extern const u8 gText_UnionRoomChatKeyboard_pqrst[]; +extern const u8 gText_UnionRoomChatKeyboard_uvwxy[]; +extern const u8 gText_UnionRoomChatKeyboard_z[]; +extern const u8 gText_UnionRoomChatKeyboard_01234Lower[]; +extern const u8 gText_UnionRoomChatKeyboard_56789Lower[]; +extern const u8 gText_UnionRoomChatKeyboard_PunctuationLower[]; +extern const u8 gText_UnionRoomChatKeyboard_SymbolsLower[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji1[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji2[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji3[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji4[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji5[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji6[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji7[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji8[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji9[]; +extern const u8 gText_UnionRoomChatKeyboard_Emoji10[]; // E-Reader extern const u8 gUnknown_085EDFD6[]; diff --git a/include/union_room_chat.h b/include/union_room_chat.h index 17e482525..e7ca9d46e 100755 --- a/include/union_room_chat.h +++ b/include/union_room_chat.h @@ -1,6 +1,17 @@ #ifndef GUARD_UNION_ROOM_CHAT_H #define GUARD_UNION_ROOM_CHAT_H +enum +{ + UNION_ROOM_KB_PAGE_UPPER, + UNION_ROOM_KB_PAGE_LOWER, + UNION_ROOM_KB_PAGE_EMOJI, + UNION_ROOM_KB_PAGE_COUNT +}; + +#define UNION_ROOM_KB_ROW_COUNT 10 + + void sub_801DD98(void); void copy_strings_to_sav1(void); diff --git a/src/easy_chat.c b/src/easy_chat.c index ba54de783..a25616e5b 100644 --- a/src/easy_chat.c +++ b/src/easy_chat.c @@ -209,7 +209,7 @@ static void sub_811D830(void); static void sub_811D058(u8, u8, const u8 *, u8, u8, u8, u8, u8, u8); static void sub_811DD84(void); static void sub_811D6F4(void); -static void sub_811D758(void); +static void PrintEasyChatKeyboardText(void); static void sub_811D794(void); static const u8 *GetEasyChatWordGroupName(u8); static void sub_811D864(u8, u8); @@ -698,11 +698,12 @@ static const struct WindowTemplate sEasyChatYesNoWindowTemplate = { static const u8 sText_Clear17[] = _("{CLEAR 17}"); -static const u8 *const sUnknown_08597C90[] = { - gUnknown_862B810, - gUnknown_862B832, - gUnknown_862B84B, - gUnknown_862B86C, +static const u8 *const sEasyChatKeyboardText[] = +{ + gText_EasyChatKeyboard_ABCDEFothers, + gText_EasyChatKeyboard_GHIJKL, + gText_EasyChatKeyboard_MNOPQRS, + gText_EasyChatKeyboard_TUVWXYZ, }; static const struct SpriteSheet sEasyChatSpriteSheets[] = { @@ -3967,7 +3968,7 @@ static void sub_811D698(u32 arg0) sub_811D6F4(); break; case 1: - sub_811D758(); + PrintEasyChatKeyboardText(); break; case 2: sub_811D794(); @@ -4010,12 +4011,12 @@ static void sub_811D6F4(void) } } -static void sub_811D758(void) +static void PrintEasyChatKeyboardText(void) { u32 i; - for (i = 0; i < ARRAY_COUNT(sUnknown_08597C90); i++) - sub_811D028(2, 1, sUnknown_08597C90[i], 10, 97 + i * 16, 0xFF, NULL); + for (i = 0; i < ARRAY_COUNT(sEasyChatKeyboardText); i++) + sub_811D028(2, 1, sEasyChatKeyboardText[i], 10, 97 + i * 16, 0xFF, NULL); } static void sub_811D794(void) diff --git a/src/naming_screen.c b/src/naming_screen.c index 472489a26..c4dbe2bda 100644 --- a/src/naming_screen.c +++ b/src/naming_screen.c @@ -43,18 +43,6 @@ extern const u8 gText_YourName[]; extern const u8 gText_BoxName[]; extern const u8 gText_PkmnsNickname[]; extern const u8 gText_TellHimTheWords[]; -extern const u8 gUnknown_0862B88D[]; -extern const u8 gUnknown_0862B8AE[]; -extern const u8 gUnknown_0862B8CF[]; -extern const u8 gUnknown_0862B8F0[]; -extern const u8 gUnknown_0862B911[]; -extern const u8 gUnknown_0862B932[]; -extern const u8 gUnknown_0862B953[]; -extern const u8 gUnknown_0862B974[]; -extern const u8 gUnknown_0862B995[]; -extern const u8 gUnknown_0862B9AE[]; -extern const u8 gUnknown_0862B9C7[]; -extern const u8 gUnknown_0862B9E0[]; // start of .rodata @@ -171,7 +159,7 @@ static const struct SpriteTemplate gUnknown_0858C138; static const struct SpriteTemplate sSpriteTemplate_InputArrow; static const struct SpriteTemplate sSpriteTemplate_Underscore; static const struct SpriteTemplate gUnknown_0858C180; -static const u8* const gUnknown_0858C198[][4]; +static const u8* const sNamingScreenKeyboardText[KBPAGE_COUNT][KBROW_COUNT]; static const struct SpriteSheet gUnknown_0858C1C8[]; static const struct SpritePalette gUnknown_0858C230[]; @@ -428,35 +416,52 @@ static void sub_80E31B0(u8 taskId) } } -static const u8 gUnknown_0858BEBB[] = { 0, 2, 1 }; -static const u8 gUnknown_0858BEBE[] = { 1, 0, 2 }; -static const u8 gUnknown_0858BEC1[] = { 2, 1, 0 }; +static const u8 sPageOrderLowerFirst[] = +{ + KBPAGE_LETTERS_LOWER, + KBPAGE_SYMBOLS, + KBPAGE_LETTERS_UPPER +}; + +static const u8 sPageOrderUpperFirst[] = +{ + KBPAGE_LETTERS_UPPER, + KBPAGE_LETTERS_LOWER, + KBPAGE_SYMBOLS +}; + +static const u8 sPageOrderSymbolsFirst[] = +{ + KBPAGE_SYMBOLS, + KBPAGE_LETTERS_UPPER, + KBPAGE_LETTERS_LOWER +}; static u8 sub_80E3244(u8 a1) { - return gUnknown_0858BEBB[a1]; + return sPageOrderLowerFirst[a1]; } static u8 sub_80E3254(void) { - return gUnknown_0858BEBE[gNamingScreenData->currentPage]; + return sPageOrderUpperFirst[gNamingScreenData->currentPage]; } static u8 sub_80E3274(void) { - return gUnknown_0858BEC1[gNamingScreenData->currentPage]; + return sPageOrderSymbolsFirst[gNamingScreenData->currentPage]; } static bool8 MainState_BeginFadeIn(void) { sub_80E4CF8(3, gUnknown_08DD4544); - gNamingScreenData->currentPage = PAGE_UPPER; + gNamingScreenData->currentPage = KBPAGE_LETTERS_UPPER; sub_80E4CF8(2, gUnknown_08DD46E0); sub_80E4CF8(1, gUnknown_08DD4620); - sub_80E4DE4(gNamingScreenData->windows[1], 0); - sub_80E4DE4(gNamingScreenData->windows[0], 1); - nullsub_10(2, 0); - nullsub_10(1, 1); + sub_80E4DE4(gNamingScreenData->windows[1], KBPAGE_LETTERS_LOWER); + sub_80E4DE4(gNamingScreenData->windows[0], KBPAGE_LETTERS_UPPER); + nullsub_10(2, KBPAGE_LETTERS_LOWER); + nullsub_10(1, KBPAGE_LETTERS_UPPER); sub_80E4D10(); sub_80E4964(); sub_80E4EF0(); @@ -1660,7 +1665,7 @@ static void sub_80E4CF8(u8 bg, const void *src) CopyToBgTilemapBuffer(bg, src, 0, 0); } -static void nullsub_10(u8 a1, u8 a2) +static void nullsub_10(u8 a1, u8 page) { } @@ -1703,29 +1708,29 @@ static const struct TextColorThing sUnkColorStruct = } }; -static const u8 sFillValues[3] = +static const u8 sFillValues[KBPAGE_COUNT] = { - PIXEL_FILL(0xE), - PIXEL_FILL(0xD), - PIXEL_FILL(0xF) + [KBPAGE_LETTERS_LOWER] = PIXEL_FILL(0xE), + [KBPAGE_LETTERS_UPPER] = PIXEL_FILL(0xD), + [KBPAGE_SYMBOLS] = PIXEL_FILL(0xF) }; -static const u8 *const sUnkColors[3] = +static const u8 *const sUnkColors[KBPAGE_COUNT] = { - sUnkColorStruct.colors[1], - sUnkColorStruct.colors[0], - sUnkColorStruct.colors[2] + [KBPAGE_LETTERS_LOWER] = sUnkColorStruct.colors[1], + [KBPAGE_LETTERS_UPPER] = sUnkColorStruct.colors[0], + [KBPAGE_SYMBOLS] = sUnkColorStruct.colors[2] }; -static void sub_80E4DE4(u8 window, u8 a1) +static void sub_80E4DE4(u8 window, u8 page) { u8 i; - FillWindowPixelBuffer(window, sFillValues[a1]); + FillWindowPixelBuffer(window, sFillValues[page]); - for (i = 0; i < 4; i++) + for (i = 0; i < KBROW_COUNT; i++) { - AddTextPrinterParameterized3(window, 1, 0, i * 16 + 1, sUnkColors[a1], 0, gUnknown_0858C198[a1][i]); + AddTextPrinterParameterized3(window, 1, 0, i * 16 + 1, sUnkColors[page], 0, sNamingScreenKeyboardText[page][i]); } PutWindowTilemap(window); @@ -1857,7 +1862,7 @@ static const struct NamingScreenTemplate playerNamingScreenTemplate = .maxChars = 7, .iconFunction = 1, .addGenderIcon = 0, - .initialPage = PAGE_UPPER, + .initialPage = KBPAGE_LETTERS_UPPER, .unused = 35, .title = gText_YourName, }; @@ -1868,7 +1873,7 @@ static const struct NamingScreenTemplate pcBoxNamingTemplate = .maxChars = 8, .iconFunction = 2, .addGenderIcon = 0, - .initialPage = PAGE_UPPER, + .initialPage = KBPAGE_LETTERS_UPPER, .unused = 19, .title = gText_BoxName, }; @@ -1879,7 +1884,7 @@ static const struct NamingScreenTemplate monNamingScreenTemplate = .maxChars = 10, .iconFunction = 3, .addGenderIcon = 1, - .initialPage = PAGE_UPPER, + .initialPage = KBPAGE_LETTERS_UPPER, .unused = 35, .title = gText_PkmnsNickname, }; @@ -1890,7 +1895,7 @@ static const struct NamingScreenTemplate wandaWordsScreenTemplate = .maxChars = 15, .iconFunction = 4, .addGenderIcon = 0, - .initialPage = PAGE_UPPER, + .initialPage = KBPAGE_LETTERS_UPPER, .unused = 11, .title = gText_TellHimTheWords, }; @@ -2144,25 +2149,28 @@ static const struct SpriteTemplate gUnknown_0858C180 = .callback = SpriteCallbackDummy }; -static const u8* const gUnknown_0858C198[][4] = +static const u8* const sNamingScreenKeyboardText[KBPAGE_COUNT][KBROW_COUNT] = { + [KBPAGE_LETTERS_LOWER] = { - gUnknown_0862B88D, - gUnknown_0862B8AE, - gUnknown_0862B8CF, - gUnknown_0862B8F0 + gText_NamingScreenKeyboard_abcdef, + gText_NamingScreenKeyboard_ghijkl, + gText_NamingScreenKeyboard_mnopqrs, + gText_NamingScreenKeyboard_tuvwxyz }, + [KBPAGE_LETTERS_UPPER] = { - gUnknown_0862B911, - gUnknown_0862B932, - gUnknown_0862B953, - gUnknown_0862B974 + gText_NamingScreenKeyboard_ABCDEF, + gText_NamingScreenKeyboard_GHIJKL, + gText_NamingScreenKeyboard_MNOPQRS, + gText_NamingScreenKeyboard_TUVWXYZ }, + [KBPAGE_SYMBOLS] = { - gUnknown_0862B995, - gUnknown_0862B9AE, - gUnknown_0862B9C7, - gUnknown_0862B9E0 + gText_NamingScreenKeyboard_01234, + gText_NamingScreenKeyboard_56789, + gText_NamingScreenKeyboard_Symbols1, + gText_NamingScreenKeyboard_Symbols2 }, }; diff --git a/src/union_room_chat.c b/src/union_room_chat.c index 4afeb99cf..eaf24fc02 100755 --- a/src/union_room_chat.c +++ b/src/union_room_chat.c @@ -22,6 +22,7 @@ #include "task.h" #include "text.h" #include "text_window.h" +#include "union_room_chat.h" #include "window.h" #include "constants/rgb.h" #include "constants/songs.h" @@ -37,9 +38,9 @@ struct UnionRoomChat u8 unkD; u8 unkE; u8 unkF; - u8 unk10; + u8 currentPage; u8 unk11; - u8 unk12; + u8 currentRow; u8 unk13; u8 unk14; u8 unk15; @@ -50,7 +51,7 @@ struct UnionRoomChat u8 unk1A[0x1F]; u8 unk39[0x40]; u8 unk79[0x40]; - u8 unkB9[10][21]; + u8 unkB9[UNION_ROOM_KB_ROW_COUNT][21]; u8 filler18B[0x5]; u8 unk190[0x28]; u16 unk1B8; @@ -232,7 +233,13 @@ void (*const gUnknown_082F2A7C[])(void) = sub_801E978, }; -static const u8 sUnknown_082F2AA4[] = {9, 9, 9, 9}; +static const u8 sKeyboardPageMaxRow[] = +{ + [UNION_ROOM_KB_PAGE_UPPER] = 9, + [UNION_ROOM_KB_PAGE_LOWER] = 9, + [UNION_ROOM_KB_PAGE_EMOJI] = 9, + 9 +}; static const u8 gUnknown_082F2AA8[] = { CHAR_SPACE, 0x16, 0x17, 0x68, 0x19, 0x1A, 0x1B, 0x1C, @@ -269,10 +276,47 @@ static const u8 gUnknown_082F2AA8[] = { CHAR_SPACE, CHAR_SPACE, CHAR_SPACE, CHAR_SPACE, CHAR_SPACE, CHAR_SPACE, CHAR_SPACE, CHAR_SPACE }; -const u8 *const gUnknown_082F2BA8[][10] = { - {gUnknown_0862B9F9, gUnknown_0862B9FF, gUnknown_0862BA05, gUnknown_0862BA0B, gUnknown_0862BA11, gUnknown_0862BA17, gUnknown_0862BA1D, gUnknown_0862BA23, gUnknown_0862BA29, gUnknown_0862BA2F}, - {gUnknown_0862BA35, gUnknown_0862BA3B, gUnknown_0862BA41, gUnknown_0862BA47, gUnknown_0862BA4D, gUnknown_0862BA53, gUnknown_0862BA59, gUnknown_0862BA5F, gUnknown_0862BA65, gUnknown_0862BA6B}, - {gUnknown_0862BA79, gUnknown_0862BA84, gUnknown_0862BA8F, gUnknown_0862BA9A, gUnknown_0862BAA3, gUnknown_0862BAAE, gUnknown_0862BAB9, gUnknown_0862BAC4, gUnknown_0862BACF, gUnknown_0862BADA} +static const u8 *const sUnionRoomKeyboardText[UNION_ROOM_KB_PAGE_COUNT][UNION_ROOM_KB_ROW_COUNT] = +{ + [UNION_ROOM_KB_PAGE_UPPER] = + { + gText_UnionRoomChatKeyboard_ABCDE, + gText_UnionRoomChatKeyboard_FGHIJ, + gText_UnionRoomChatKeyboard_KLMNO, + gText_UnionRoomChatKeyboard_PQRST, + gText_UnionRoomChatKeyboard_UVWXY, + gText_UnionRoomChatKeyboard_Z, + gText_UnionRoomChatKeyboard_01234Upper, + gText_UnionRoomChatKeyboard_56789Upper, + gText_UnionRoomChatKeyboard_PunctuationUpper, + gText_UnionRoomChatKeyboard_SymbolsUpper + }, + [UNION_ROOM_KB_PAGE_LOWER] = + { + gText_UnionRoomChatKeyboard_abcde, + gText_UnionRoomChatKeyboard_fghij, + gText_UnionRoomChatKeyboard_klmno, + gText_UnionRoomChatKeyboard_pqrst, + gText_UnionRoomChatKeyboard_uvwxy, + gText_UnionRoomChatKeyboard_z, + gText_UnionRoomChatKeyboard_01234Lower, + gText_UnionRoomChatKeyboard_56789Lower, + gText_UnionRoomChatKeyboard_PunctuationLower, + gText_UnionRoomChatKeyboard_SymbolsLower + }, + [UNION_ROOM_KB_PAGE_EMOJI] = + { + gText_UnionRoomChatKeyboard_Emoji1, + gText_UnionRoomChatKeyboard_Emoji2, + gText_UnionRoomChatKeyboard_Emoji3, + gText_UnionRoomChatKeyboard_Emoji4, + gText_UnionRoomChatKeyboard_Emoji5, + gText_UnionRoomChatKeyboard_Emoji6, + gText_UnionRoomChatKeyboard_Emoji7, + gText_UnionRoomChatKeyboard_Emoji8, + gText_UnionRoomChatKeyboard_Emoji9, + gText_UnionRoomChatKeyboard_Emoji10 + } }; const u16 gUnknown_082F2C20[] = INCBIN_U16("graphics/interface/unk_palette1.gbapal"); @@ -559,9 +603,9 @@ static void sub_801DDD0(struct UnionRoomChat *unionRoomChat) unionRoomChat->unk4 = 0; unionRoomChat->unk6 = 0; - unionRoomChat->unk10 = 0; + unionRoomChat->currentPage = 0; unionRoomChat->unk11 = 0; - unionRoomChat->unk12 = 0; + unionRoomChat->currentRow = 0; unionRoomChat->unk14 = 0; unionRoomChat->unk15 = 0; unionRoomChat->unk16 = 0; @@ -571,7 +615,7 @@ static void sub_801DDD0(struct UnionRoomChat *unionRoomChat) unionRoomChat->unk17 = 0; unionRoomChat->unk18 = 0; sub_801EF1C(unionRoomChat->unk190); - for (i = 0; i < 10; i++) + for (i = 0; i < UNION_ROOM_KB_ROW_COUNT; i++) StringCopy(unionRoomChat->unkB9[i], gSaveBlock1Ptr->unk3C88[i]); } @@ -715,7 +759,7 @@ static void sub_801E030(void) } else if (gMain.newKeys & R_BUTTON) { - if (gUnknown_02022C84->unk10 != 3) + if (gUnknown_02022C84->currentPage != UNION_ROOM_KB_PAGE_COUNT) { sub_801ED94(); sub_801F5EC(8, 0); @@ -763,7 +807,7 @@ static void sub_801E120(void) default: sub_801F5EC(4, 0); var0 = 1; - if (gUnknown_02022C84->unk10 == input || input > 3) + if (gUnknown_02022C84->currentPage == input || input > UNION_ROOM_KB_PAGE_COUNT) var0 = 0; break; case MENU_NOTHING_CHOSEN: @@ -786,9 +830,9 @@ static void sub_801E120(void) } gUnknown_02022C84->unk11 = 0; - gUnknown_02022C84->unk12 = 0; + gUnknown_02022C84->currentRow = 0; sub_801F5EC(5, 1); - gUnknown_02022C84->unk10 = input; + gUnknown_02022C84->currentPage = input; gUnknown_02022C84->unk6 = 4; break; case 3: @@ -1260,15 +1304,15 @@ static bool32 sub_801EBE4(void) { if (gMain.newAndRepeatedKeys & DPAD_DOWN) { - if (gUnknown_02022C84->unk12 < sUnknown_082F2AA4[gUnknown_02022C84->unk10]) - gUnknown_02022C84->unk12++; + if (gUnknown_02022C84->currentRow < sKeyboardPageMaxRow[gUnknown_02022C84->currentPage]) + gUnknown_02022C84->currentRow++; else - gUnknown_02022C84->unk12 = 0; + gUnknown_02022C84->currentRow = 0; return TRUE; } - if (gUnknown_02022C84->unk10 != 3) + if (gUnknown_02022C84->currentPage != UNION_ROOM_KB_PAGE_COUNT) { if (gMain.newAndRepeatedKeys & DPAD_LEFT) { @@ -1296,10 +1340,10 @@ static bool32 sub_801EBE4(void) } else { - if (gUnknown_02022C84->unk12) - gUnknown_02022C84->unk12--; + if (gUnknown_02022C84->currentRow) + gUnknown_02022C84->currentRow--; else - gUnknown_02022C84->unk12 = sUnknown_082F2AA4[gUnknown_02022C84->unk10]; + gUnknown_02022C84->currentRow = sKeyboardPageMaxRow[gUnknown_02022C84->currentPage]; return TRUE; } @@ -1313,9 +1357,9 @@ static void sub_801EC94(void) u8 *str; u8 buffer[21]; - if (gUnknown_02022C84->unk10 != 3) + if (gUnknown_02022C84->currentPage != UNION_ROOM_KB_PAGE_COUNT) { - charsStr = gUnknown_082F2BA8[gUnknown_02022C84->unk10][gUnknown_02022C84->unk12]; + charsStr = sUnionRoomKeyboardText[gUnknown_02022C84->currentPage][gUnknown_02022C84->currentRow]; for (i = 0; i < gUnknown_02022C84->unk11; i++) { if (*charsStr == CHAR_SPECIAL_F9) @@ -1327,7 +1371,7 @@ static void sub_801EC94(void) } else { - u8 *tempStr = StringCopy(buffer, gUnknown_02022C84->unkB9[gUnknown_02022C84->unk12]); + u8 *tempStr = StringCopy(buffer, gUnknown_02022C84->unkB9[gUnknown_02022C84->currentRow]); tempStr[0] = CHAR_SPACE; tempStr[1] = EOS; charsStr = buffer; @@ -1395,7 +1439,7 @@ static bool32 sub_801EDC4(void) static void sub_801EDE0(void) { u8 *src = sub_801F114(); - StringCopy(gUnknown_02022C84->unkB9[gUnknown_02022C84->unk12], src); + StringCopy(gUnknown_02022C84->unkB9[gUnknown_02022C84->currentRow], src); gUnknown_02022C84->unk18 = 1; } @@ -1409,7 +1453,7 @@ static void sub_801EE10(void) static void sub_801EE2C(void) { int i; - for (i = 0; i < 10; i++) + for (i = 0; i < UNION_ROOM_KB_ROW_COUNT; i++) StringCopy(gSaveBlock1Ptr->unk3C88[i], gUnknown_02022C84->unkB9[i]); } @@ -1551,15 +1595,15 @@ static bool32 sub_801EFF8(u8 *arg0, u8 *arg1) return FALSE; } -static u8 sub_801F0B0(void) +static u8 GetCurrentKeyboardPage(void) { - return gUnknown_02022C84->unk10; + return gUnknown_02022C84->currentPage; } static void sub_801F0BC(u8 *arg0, u8 *arg1) { *arg0 = gUnknown_02022C84->unk11; - *arg1 = gUnknown_02022C84->unk12; + *arg1 = gUnknown_02022C84->currentRow; } static u8 *sub_801F0D0(void) @@ -2528,7 +2572,7 @@ static void sub_8020118(u16 x, u8 *str, u8 fillValue, u8 arg3, u8 arg4) static void sub_80201A4(void) { - u8 var0; + u8 page; int i; int var1; u16 left; @@ -2538,11 +2582,11 @@ static void sub_80201A4(void) u8 *str2; FillWindowPixelBuffer(2, PIXEL_FILL(15)); - var0 = sub_801F0B0(); + page = GetCurrentKeyboardPage(); sp[0] = 0; sp[1] = 14; sp[2] = 13; - if (var0 != 3) + if (page != UNION_ROOM_KB_PAGE_COUNT) { str = &sp[4]; str[0] = EXT_CTRL_CODE_BEGIN; @@ -2550,15 +2594,15 @@ static void sub_80201A4(void) var1 = 8; str[2] = var1; left = var1; - if (var0 == 2) + if (page == UNION_ROOM_KB_PAGE_EMOJI) left = 6; - for (i = 0, top = 0; i < 10; i++, top += 12) + for (i = 0, top = 0; i < UNION_ROOM_KB_ROW_COUNT; i++, top += 12) { - if (!gUnknown_082F2BA8[var0][i]) + if (!sUnionRoomKeyboardText[page][i]) return; - StringCopy(&sp[7], gUnknown_082F2BA8[var0][i]); + StringCopy(&sp[7], sUnionRoomKeyboardText[page][i]); AddTextPrinterParameterized3(2, 0, left, top, sp, TEXT_SPEED_FF, &sp[4]); } } @@ -2631,7 +2675,7 @@ static void sub_80203B0(void) FillWindowPixelBuffer(3, PIXEL_FILL(1)); DrawTextBorderOuter(3, 1, 13); PrintTextArray(3, 2, 8, 1, 14, 5, gUnknown_082F2DC8); - sub_81983AC(3, 2, 0, 1, 14, 5, sub_801F0B0()); + sub_81983AC(3, 2, 0, 1, 14, 5, GetCurrentKeyboardPage()); PutWindowTilemap(3); } @@ -2820,7 +2864,7 @@ static void sub_802091C(bool32 invisible) static void sub_802093C(void) { u8 x, y; - u8 var2 = sub_801F0B0(); + u8 var2 = GetCurrentKeyboardPage(); sub_801F0BC(&x, &y); if (var2 != 3) { @@ -2845,7 +2889,7 @@ static void sub_80209AC(int arg0) static void sub_80209E0(void) { - if (sub_801F0B0() != 3) + if (GetCurrentKeyboardPage() != 3) StartSpriteAnim(gUnknown_02022C8C->unk0, 1); else StartSpriteAnim(gUnknown_02022C8C->unk0, 3); @@ -2860,7 +2904,7 @@ static bool32 sub_8020A1C(void) if (++gUnknown_02022C8C->unk14 > 3) { - if (sub_801F0B0() != 3) + if (GetCurrentKeyboardPage() != 3) StartSpriteAnim(gUnknown_02022C8C->unk0, 0); else StartSpriteAnim(gUnknown_02022C8C->unk0, 2); @@ -2914,7 +2958,7 @@ static void sub_8020B20(void) static void sub_8020B80(void) { - if (sub_801F0B0() == 3) + if (GetCurrentKeyboardPage() == UNION_ROOM_KB_PAGE_COUNT) { if (sub_801F0DC() != 0) { -- cgit v1.2.3 From a923ae78c3dbfd711dd0f1168a340abb0eb2dc8c Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 30 Sep 2019 00:13:08 -0400 Subject: Add missing page num usage --- src/union_room_chat.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/union_room_chat.c b/src/union_room_chat.c index eaf24fc02..c68a2c0a9 100755 --- a/src/union_room_chat.c +++ b/src/union_room_chat.c @@ -2864,9 +2864,9 @@ static void sub_802091C(bool32 invisible) static void sub_802093C(void) { u8 x, y; - u8 var2 = GetCurrentKeyboardPage(); + u8 page = GetCurrentKeyboardPage(); sub_801F0BC(&x, &y); - if (var2 != 3) + if (page != UNION_ROOM_KB_PAGE_COUNT) { StartSpriteAnim(gUnknown_02022C8C->unk0, 0); gUnknown_02022C8C->unk0->pos1.x = x * 8 + 10; @@ -2889,7 +2889,7 @@ static void sub_80209AC(int arg0) static void sub_80209E0(void) { - if (GetCurrentKeyboardPage() != 3) + if (GetCurrentKeyboardPage() != UNION_ROOM_KB_PAGE_COUNT) StartSpriteAnim(gUnknown_02022C8C->unk0, 1); else StartSpriteAnim(gUnknown_02022C8C->unk0, 3); @@ -2904,7 +2904,7 @@ static bool32 sub_8020A1C(void) if (++gUnknown_02022C8C->unk14 > 3) { - if (GetCurrentKeyboardPage() != 3) + if (GetCurrentKeyboardPage() != UNION_ROOM_KB_PAGE_COUNT) StartSpriteAnim(gUnknown_02022C8C->unk0, 0); else StartSpriteAnim(gUnknown_02022C8C->unk0, 2); -- cgit v1.2.3 From 9aed405851ce210973fc7b887ec306ebfa931fc9 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 30 Sep 2019 01:42:52 -0400 Subject: Document a few field specials --- data/event_scripts.s | 10 +-- .../scripts.inc | 2 +- .../BattleFrontier_BattlePikeLobby/scripts.inc | 4 +- .../scripts.inc | 2 +- data/maps/JaggedPass/scripts.inc | 20 ++--- data/maps/MagmaHideout_4F/scripts.inc | 20 ++--- data/maps/MirageTower_4F/scripts.inc | 10 +-- data/maps/NavelRock_Bottom/scripts.inc | 20 ++--- data/maps/NavelRock_Top/scripts.inc | 12 +-- data/maps/PetalburgWoods/scripts.inc | 4 +- data/maps/Route102/scripts.inc | 4 +- data/maps/Route104/scripts.inc | 12 +-- data/maps/Route116/scripts.inc | 8 +- data/maps/SeafloorCavern_Room9/scripts.inc | 20 ++--- data/maps/SkyPillar_Outside/scripts.inc | 20 ++--- data/maps/SkyPillar_Top/scripts.inc | 20 ++--- data/maps/SootopolisCity/scripts.inc | 100 ++++++++++----------- data/specials.inc | 10 +-- src/field_specials.c | 90 +++++++++++-------- 19 files changed, 203 insertions(+), 185 deletions(-) diff --git a/data/event_scripts.s b/data/event_scripts.s index 2b8678851..d22613f40 100644 --- a/data/event_scripts.s +++ b/data/event_scripts.s @@ -1753,11 +1753,11 @@ EventScript_27229B:: @ 827229B end EventScript_2722A7:: @ 82722A7 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate releaseall end diff --git a/data/maps/BattleFrontier_BattleArenaBattleRoom/scripts.inc b/data/maps/BattleFrontier_BattleArenaBattleRoom/scripts.inc index 205ee0e71..2823a4f30 100644 --- a/data/maps/BattleFrontier_BattleArenaBattleRoom/scripts.inc +++ b/data/maps/BattleFrontier_BattleArenaBattleRoom/scripts.inc @@ -6,7 +6,7 @@ BattleFrontier_BattleArenaBattleRoom_MapScripts:: @ 8257487 .byte 0 BattleFrontier_BattleArenaBattleRoom_MapScript1_25749C: @ 825749C - special sub_8139980 + special OffsetCameraForBattle end BattleFrontier_BattleArenaBattleRoom_MapScript1_2574A0: @ 82574A0 diff --git a/data/maps/BattleFrontier_BattlePikeLobby/scripts.inc b/data/maps/BattleFrontier_BattlePikeLobby/scripts.inc index 05cdfa8a3..c3f5e80de 100644 --- a/data/maps/BattleFrontier_BattlePikeLobby/scripts.inc +++ b/data/maps/BattleFrontier_BattlePikeLobby/scripts.inc @@ -329,8 +329,8 @@ BattleFrontier_BattlePikeCorridor_EventScript_25BB49:: @ 825BB49 BattleFrontier_BattlePikeLobby_EventScript_25BB49:: @ 825BB49 BattleFrontier_BattlePikeRandomRoom1_EventScript_25BB49:: @ 825BB49 BattleFrontier_BattlePikeThreePathRoom_EventScript_25BB49:: @ 825BB49 - playse 267 - special sub_813B880 + playse SE_CURTAIN + special CloseBattlePikeCurtain waitstate waitse return diff --git a/data/maps/BattleFrontier_BattleTowerCorridor2/scripts.inc b/data/maps/BattleFrontier_BattleTowerCorridor2/scripts.inc index 6e3e05959..097e732ab 100644 --- a/data/maps/BattleFrontier_BattleTowerCorridor2/scripts.inc +++ b/data/maps/BattleFrontier_BattleTowerCorridor2/scripts.inc @@ -36,7 +36,7 @@ BattleFrontier_BattleTowerCorridor2_EventScript_248D9F:: @ 8248D9F hideobjectat EVENT_OBJ_ID_PLAYER, MAP_BATTLE_FRONTIER_BATTLE_TOWER_CORRIDOR2 hideobjectat 1, MAP_BATTLE_FRONTIER_BATTLE_TOWER_CORRIDOR2 hideobjectat 4, MAP_BATTLE_FRONTIER_BATTLE_TOWER_CORRIDOR2 - special sub_8139980 + special OffsetCameraForBattle end BattleFrontier_BattleTowerCorridor2_MapScript2_248DB2: @ 8248DB2 diff --git a/data/maps/JaggedPass/scripts.inc b/data/maps/JaggedPass/scripts.inc index 1bb9e82a2..2a12ab5a2 100644 --- a/data/maps/JaggedPass/scripts.inc +++ b/data/maps/JaggedPass/scripts.inc @@ -42,19 +42,19 @@ JaggedPass_EventScript_2306A8:: @ 82306A8 JaggedPass_EventScript_2306BB:: @ 82306BB lockall - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate msgbox JaggedPass_Text_230DBA, MSGBOX_DEFAULT closemessage - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 16 - setvar VAR_0x8007, 3 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 16 @ num shakes + setvar VAR_0x8007, 3 @ shake delay + special ShakeCamera waitstate playse SE_KOUKA_M setmetatile 16, 17, METATILE_Lavaridge_CaveEntrance_Top, 1 diff --git a/data/maps/MagmaHideout_4F/scripts.inc b/data/maps/MagmaHideout_4F/scripts.inc index 7bbb4ac18..de6790a25 100644 --- a/data/maps/MagmaHideout_4F/scripts.inc +++ b/data/maps/MagmaHideout_4F/scripts.inc @@ -29,21 +29,21 @@ MagmaHideout_4F_EventScript_23A560:: @ 823A560 waitmovement 0 special sub_80B058C waitstate - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 1, MagmaHideout_4F_Movement_23A67D waitmovement 0 removeobject 1 delay 4 - setvar VAR_0x8004, 2 - setvar VAR_0x8005, 2 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 2 @ vertical pan + setvar VAR_0x8005, 2 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate delay 30 applymovement 6, MagmaHideout_4F_Movement_23A680 diff --git a/data/maps/MirageTower_4F/scripts.inc b/data/maps/MirageTower_4F/scripts.inc index 5208820df..5bb5308c3 100644 --- a/data/maps/MirageTower_4F/scripts.inc +++ b/data/maps/MirageTower_4F/scripts.inc @@ -44,11 +44,11 @@ MirageTower_4F_EventScript_23ADC0:: @ 823ADC0 end MirageTower_4F_EventScript_23ADCA:: @ 823ADCA - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 32 - setvar VAR_0x8007, 2 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 32 @ num shakes + setvar VAR_0x8007, 2 @ shake delay + special ShakeCamera waitstate special DoMirageTowerCeilingCrumble waitstate diff --git a/data/maps/NavelRock_Bottom/scripts.inc b/data/maps/NavelRock_Bottom/scripts.inc index c67c9897c..64b860fa7 100644 --- a/data/maps/NavelRock_Bottom/scripts.inc +++ b/data/maps/NavelRock_Bottom/scripts.inc @@ -34,18 +34,18 @@ NavelRock_Bottom_EventScript_2692A2:: @ 82692A2 waitse delay 20 playse SE_T_AME_E - setvar VAR_0x8004, 0 - setvar VAR_0x8005, 3 - setvar VAR_0x8006, 4 - setvar VAR_0x8007, 2 - special sub_8139560 + setvar VAR_0x8004, 0 @ vertical pan + setvar VAR_0x8005, 3 @ horizontal pan + setvar VAR_0x8006, 4 @ num shakes + setvar VAR_0x8007, 2 @ shake delay + special ShakeCamera delay 30 playse SE_T_AME_E - setvar VAR_0x8004, 0 - setvar VAR_0x8005, 3 - setvar VAR_0x8006, 4 - setvar VAR_0x8007, 2 - special sub_8139560 + setvar VAR_0x8004, 0 @ vertical pan + setvar VAR_0x8005, 3 @ horizontal pan + setvar VAR_0x8006, 4 @ num shakes + setvar VAR_0x8007, 2 @ shake delay + special ShakeCamera delay 30 delay 50 waitse diff --git a/data/maps/NavelRock_Top/scripts.inc b/data/maps/NavelRock_Top/scripts.inc index 577d2cdd3..a3179e8a6 100644 --- a/data/maps/NavelRock_Top/scripts.inc +++ b/data/maps/NavelRock_Top/scripts.inc @@ -35,9 +35,9 @@ NavelRock_Top_EventScript_26916F:: @ 826916F lockall setvar VAR_TEMP_1, 1 special SpawnCameraObject - setvar VAR_0x8004, 3 - setvar VAR_0x8005, 35 - special sub_813B80C + setvar VAR_0x8004, 3 @ num loops + setvar VAR_0x8005, 35 @ delay + special LoopWingFlapSE applymovement 1, NavelRock_Top_Movement_269243 applymovement EVENT_OBJ_ID_CAMERA, NavelRock_Top_Movement_269230 waitmovement 0 @@ -49,9 +49,9 @@ NavelRock_Top_EventScript_26916F:: @ 826916F delay 30 waitmoncry delay 60 - setvar VAR_0x8004, 3 - setvar VAR_0x8005, 35 - special sub_813B80C + setvar VAR_0x8004, 3 @ num loops + setvar VAR_0x8005, 35 @ delay + special LoopWingFlapSE applymovement EVENT_OBJ_ID_CAMERA, NavelRock_Top_Movement_269234 applymovement 1, NavelRock_Top_Movement_26923A waitmovement 0 diff --git a/data/maps/PetalburgWoods/scripts.inc b/data/maps/PetalburgWoods/scripts.inc index 423d4c432..da07716db 100644 --- a/data/maps/PetalburgWoods/scripts.inc +++ b/data/maps/PetalburgWoods/scripts.inc @@ -281,8 +281,8 @@ PetalburgWoods_EventScript_22E284:: @ 822E284 specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq PetalburgWoods_EventScript_22E31B - setvar VAR_0x8004, 621 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_JAMES_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq PetalburgWoods_EventScript_22E2EF msgbox PetalburgWoods_Text_22E889, MSGBOX_DEFAULT diff --git a/data/maps/Route102/scripts.inc b/data/maps/Route102/scripts.inc index ada20447a..58adf4456 100644 --- a/data/maps/Route102/scripts.inc +++ b/data/maps/Route102/scripts.inc @@ -22,8 +22,8 @@ Route102_EventScript_1EC105:: @ 81EC105 specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq Route102_EventScript_1EC19C - setvar VAR_0x8004, 318 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_CALVIN_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq Route102_EventScript_1EC170 msgbox Route102_Text_2945AC, MSGBOX_DEFAULT diff --git a/data/maps/Route104/scripts.inc b/data/maps/Route104/scripts.inc index 74a7e3ddf..0eaf5d487 100644 --- a/data/maps/Route104/scripts.inc +++ b/data/maps/Route104/scripts.inc @@ -870,8 +870,8 @@ Route104_EventScript_1ED38D:: @ 81ED38D specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq Route104_EventScript_1ED424 - setvar VAR_0x8004, 604 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_HALEY_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq Route104_EventScript_1ED3F8 msgbox Route104_Text_295689, MSGBOX_DEFAULT @@ -913,8 +913,8 @@ Route104_EventScript_1ED43B:: @ 81ED43B specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq Route104_EventScript_1ED4D2 - setvar VAR_0x8004, 136 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_WINSTON_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq Route104_EventScript_1ED4A6 msgbox Route104_Text_2958C1, MSGBOX_DEFAULT @@ -956,8 +956,8 @@ Route104_EventScript_1ED4E9:: @ 81ED4E9 specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq Route104_EventScript_1ED580 - setvar VAR_0x8004, 114 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_CINDY_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq Route104_EventScript_1ED554 msgbox Route104_Text_295AC3, MSGBOX_DEFAULT diff --git a/data/maps/Route116/scripts.inc b/data/maps/Route116/scripts.inc index 161e9be51..0dbe63728 100644 --- a/data/maps/Route116/scripts.inc +++ b/data/maps/Route116/scripts.inc @@ -258,8 +258,8 @@ Route116_EventScript_1F2EB7:: @ 81F2EB7 specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq Route116_EventScript_1F2F4E - setvar VAR_0x8004, 273 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_JERRY_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq Route116_EventScript_1F2F22 msgbox Route116_Text_29BD92, MSGBOX_DEFAULT @@ -311,8 +311,8 @@ Route116_EventScript_1F2F93:: @ 81F2F93 specialvar VAR_RESULT, ShouldTryRematchBattle compare VAR_RESULT, 1 goto_if_eq Route116_EventScript_1F302A - setvar VAR_0x8004, 280 - specialvar VAR_RESULT, sub_813B4E0 + setvar VAR_0x8004, TRAINER_KAREN_1 + specialvar VAR_RESULT, IsTrainerRegistered compare VAR_RESULT, 0 goto_if_eq Route116_EventScript_1F2FFE msgbox Route116_Text_29C010, MSGBOX_DEFAULT diff --git a/data/maps/SeafloorCavern_Room9/scripts.inc b/data/maps/SeafloorCavern_Room9/scripts.inc index 17b1515c0..49981d335 100644 --- a/data/maps/SeafloorCavern_Room9/scripts.inc +++ b/data/maps/SeafloorCavern_Room9/scripts.inc @@ -59,21 +59,21 @@ SeafloorCavern_Room9_EventScript_234DC9:: @ 8234DC9 waitmovement 0 special sub_80B058C waitstate - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 1, SeafloorCavern_Room9_Movement_23504B waitmovement 0 removeobject 1 delay 4 - setvar VAR_0x8004, 2 - setvar VAR_0x8005, 2 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 2 @ vertical pan + setvar VAR_0x8005, 2 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate delay 30 setvar VAR_0x8004, 2 diff --git a/data/maps/SkyPillar_Outside/scripts.inc b/data/maps/SkyPillar_Outside/scripts.inc index 95f50ec02..9262ca4c4 100644 --- a/data/maps/SkyPillar_Outside/scripts.inc +++ b/data/maps/SkyPillar_Outside/scripts.inc @@ -41,11 +41,11 @@ SkyPillar_Outside_EventScript_239304:: @ 8239304 msgbox SkyPillar_Outside_Text_239416, MSGBOX_DEFAULT closemessage delay 30 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 3 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 3 @ shake delay + special ShakeCamera waitstate delay 40 msgbox SkyPillar_Outside_Text_2394BC, MSGBOX_DEFAULT @@ -53,11 +53,11 @@ SkyPillar_Outside_EventScript_239304:: @ 8239304 applymovement EVENT_OBJ_ID_PLAYER, SkyPillar_Outside_Movement_2393EB applymovement 1, SkyPillar_Outside_Movement_2393DE waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 3 - setvar VAR_0x8006, 20 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 3 @ horizontal pan + setvar VAR_0x8006, 20 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate delay 20 applymovement 1, Common_Movement_WalkInPlaceLeft diff --git a/data/maps/SkyPillar_Top/scripts.inc b/data/maps/SkyPillar_Top/scripts.inc index 1dae8c867..4ab5b3424 100644 --- a/data/maps/SkyPillar_Top/scripts.inc +++ b/data/maps/SkyPillar_Top/scripts.inc @@ -104,19 +104,19 @@ SkyPillar_Top_EventScript_23979A:: @ 823979A waitmovement 0 waitse playmoncry SPECIES_RAYQUAZA, 2 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 3 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 3 @ shake delay + special ShakeCamera waitstate waitse playmoncry SPECIES_RAYQUAZA, 2 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 2 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 2 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate waitmoncry applymovement 1, SkyPillar_Top_Movement_23984B diff --git a/data/maps/SootopolisCity/scripts.inc b/data/maps/SootopolisCity/scripts.inc index d481924a2..04186ee08 100644 --- a/data/maps/SootopolisCity/scripts.inc +++ b/data/maps/SootopolisCity/scripts.inc @@ -228,11 +228,11 @@ SootopolisCity_EventScript_1E5946:: @ 81E5946 applymovement 10, SootopolisCity_Movement_1E5BB8 applymovement 9, SootopolisCity_Movement_1E5BE5 waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 10, SootopolisCity_Movement_1E5BCF applymovement 9, SootopolisCity_Movement_1E5BF0 @@ -242,11 +242,11 @@ SootopolisCity_EventScript_1E5946:: @ 81E5946 applymovement 10, SootopolisCity_Movement_1E5BC2 applymovement 9, SootopolisCity_Movement_1E5BDD waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 10, SootopolisCity_Movement_1E5BCF applymovement 9, SootopolisCity_Movement_1E5BF0 @@ -256,11 +256,11 @@ SootopolisCity_EventScript_1E5946:: @ 81E5946 applymovement 10, SootopolisCity_Movement_1E5BB8 applymovement 9, SootopolisCity_Movement_1E5BE5 waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 10, SootopolisCity_Movement_1E5BCF applymovement 9, SootopolisCity_Movement_1E5BF0 @@ -328,11 +328,11 @@ SootopolisCity_EventScript_1E5A82:: @ 81E5A82 applymovement 10, SootopolisCity_Movement_1E5BB8 applymovement 9, SootopolisCity_Movement_1E5BE5 waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 10, SootopolisCity_Movement_1E5BCF applymovement 9, SootopolisCity_Movement_1E5BF0 @@ -342,11 +342,11 @@ SootopolisCity_EventScript_1E5A82:: @ 81E5A82 applymovement 10, SootopolisCity_Movement_1E5BC2 applymovement 9, SootopolisCity_Movement_1E5BDD waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 10, SootopolisCity_Movement_1E5BCF applymovement 9, SootopolisCity_Movement_1E5BF0 @@ -356,11 +356,11 @@ SootopolisCity_EventScript_1E5A82:: @ 81E5A82 applymovement 10, SootopolisCity_Movement_1E5BB8 applymovement 9, SootopolisCity_Movement_1E5BE5 waitmovement 0 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate applymovement 10, SootopolisCity_Movement_1E5BCF applymovement 9, SootopolisCity_Movement_1E5BF0 @@ -517,19 +517,19 @@ SootopolisCity_EventScript_1E5C1E:: @ 81E5C1E waitmovement 0 waitse playmoncry SPECIES_RAYQUAZA, 2 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 3 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 3 @ shake delay + special ShakeCamera waitstate waitse playmoncry SPECIES_RAYQUAZA, 2 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 2 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 2 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate waitmoncry setweather WEATHER_NONE @@ -570,19 +570,19 @@ SootopolisCity_EventScript_1E5CCE:: @ 81E5CCE waitmovement 0 waitse playmoncry SPECIES_RAYQUAZA, 2 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 1 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 3 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 1 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 3 @ shake delay + special ShakeCamera waitstate waitse playmoncry SPECIES_RAYQUAZA, 2 - setvar VAR_0x8004, 1 - setvar VAR_0x8005, 2 - setvar VAR_0x8006, 8 - setvar VAR_0x8007, 5 - special sub_8139560 + setvar VAR_0x8004, 1 @ vertical pan + setvar VAR_0x8005, 2 @ horizontal pan + setvar VAR_0x8006, 8 @ num shakes + setvar VAR_0x8007, 5 @ shake delay + special ShakeCamera waitstate waitmoncry setweather WEATHER_NONE diff --git a/data/specials.inc b/data/specials.inc index 4ae2ebe4e..6a328ca9e 100644 --- a/data/specials.inc +++ b/data/specials.inc @@ -320,7 +320,7 @@ gSpecials:: @ 81DBA64 def_special GetShieldToyTVDecorationInfo def_special IsPokerusInParty def_special SetSootopolisGymCrackedIceMetatiles - def_special sub_8139560 + def_special ShakeCamera def_special StartGroudonKyogreBattle def_special BattleSetup_StartLegendaryBattle def_special StartRegiBattle @@ -414,7 +414,7 @@ gSpecials:: @ 81DBA64 def_special GetContestLadyMonSpecies def_special GetContestLadyCategory def_special PutLilycoveContestLadyShowOnTheAir - def_special sub_813B880 + def_special CloseBattlePikeCurtain def_special CallApprenticeFunction def_special ShouldTryGetTrainerScript def_special ShowMapNamePopup @@ -440,7 +440,7 @@ gSpecials:: @ 81DBA64 def_special sub_802E3C4 def_special sub_8027A5C def_special sub_8027AAC - def_special sub_8139980 + def_special OffsetCameraForBattle def_special GetDeptStoreDefaultFloorChoice def_special BufferVarsForIVRater def_special sub_80F905C @@ -505,7 +505,7 @@ gSpecials:: @ 81DBA64 def_special sub_80A08CC def_special Unused_SetWeatherSunny def_special sub_81AFDA0 - def_special sub_813B4E0 + def_special IsTrainerRegistered def_special ShouldDoBrailleRegicePuzzle def_special EnableNationalPokedex def_special ScriptMenu_CreateLilycoveSSTidalMultichoice @@ -517,7 +517,7 @@ gSpecials:: @ 81DBA64 def_special sub_813B534 def_special CallTrainerHillFunction def_special Script_DoRayquazaScene @ Listed twice - def_special sub_813B80C + def_special LoopWingFlapSE def_special sub_81D4BEC def_special sub_80224D0 def_special TryBufferWaldaPhrase diff --git a/src/field_specials.c b/src/field_specials.c index 0c4cc34e3..a0b7111c9 100644 --- a/src/field_specials.c +++ b/src/field_specials.c @@ -103,8 +103,8 @@ static void PCTurnOnEffect_1(s16, s8, s8); static void PCTurnOffEffect(void); static void Task_LotteryCornerComputerEffect(u8); static void LotteryCornerComputerEffect(struct Task *); -static void sub_81395BC(u8 taskId); -static void sub_8139620(u8 taskId); +static void Task_ShakeCamera(u8 taskId); +static void StopCameraShake(u8 taskId); static void Task_MoveElevator(u8 taskId); static void MoveElevatorWindowLights(u16 floorDelta, bool8 descending); static void Task_MoveElevatorWindowLights(u8 taskId); @@ -126,8 +126,8 @@ static void Task_DeoxysRockInteraction(u8 taskId); static void ChangeDeoxysRockLevel(u8 a0); static void WaitForDeoxysRockMovement(u8 taskId); static void sub_813B57C(u8 taskId); -static void sub_813B824(u8 taskId); -static void _fwalk(u8 taskId); +static void Task_LoopWingFlapSE(u8 taskId); +static void Task_CloseBattlePikeCurtain(u8 taskId); static u8 sub_813BF44(void); static void sub_813BD84(void); static u16 sub_813BB74(void); @@ -1493,44 +1493,56 @@ bool8 IsPokerusInParty(void) return TRUE; } -void sub_8139560(void) +#define horizontalPan data[0] +#define delayCounter data[1] +#define numShakes data[2] +#define delay data[3] +#define verticalPan data[4] + +void ShakeCamera(void) { - u8 taskId = CreateTask(sub_81395BC, 9); - gTasks[taskId].data[0] = gSpecialVar_0x8005; - gTasks[taskId].data[1] = 0; - gTasks[taskId].data[2] = gSpecialVar_0x8006; - gTasks[taskId].data[3] = gSpecialVar_0x8007; - gTasks[taskId].data[4] = gSpecialVar_0x8004; + u8 taskId = CreateTask(Task_ShakeCamera, 9); + gTasks[taskId].horizontalPan = gSpecialVar_0x8005; + gTasks[taskId].delayCounter = 0; + gTasks[taskId].numShakes = gSpecialVar_0x8006; + gTasks[taskId].delay = gSpecialVar_0x8007; + gTasks[taskId].verticalPan = gSpecialVar_0x8004; SetCameraPanningCallback(NULL); PlaySE(SE_W070); } -static void sub_81395BC(u8 taskId) +static void Task_ShakeCamera(u8 taskId) { s16 *data = gTasks[taskId].data; - data[1]++; - if (data[1] % data[3] == 0) + delayCounter++; + if (delayCounter % delay == 0) { - data[1] = 0; - data[2]--; - data[0] = -data[0]; - data[4] = -data[4]; - SetCameraPanning(data[0], data[4]); - if (data[2] == 0) + delayCounter = 0; + numShakes--; + horizontalPan = -horizontalPan; + verticalPan = -verticalPan; + SetCameraPanning(horizontalPan, verticalPan); + if (numShakes == 0) { - sub_8139620(taskId); + StopCameraShake(taskId); InstallCameraPanAheadCallback(); } } } -static void sub_8139620(u8 taskId) +static void StopCameraShake(u8 taskId) { DestroyTask(taskId); EnableBothScriptContexts(); } +#undef horizontalPan +#undef delayCounter +#undef numShakes +#undef delay +#undef verticalPan + bool8 FoundBlackGlasses(void) { return FlagGet(FLAG_HIDDEN_ITEM_ROUTE_116_BLACK_GLASSES); @@ -1699,7 +1711,7 @@ bool8 InMultiBattleRoom(void) return FALSE; } -void sub_8139980(void) +void OffsetCameraForBattle(void) { SetCameraPanningCallback(NULL); SetCameraPanning(8, 0); @@ -3713,7 +3725,7 @@ u32 GetMartEmployeeObjectEventId(void) return 1; } -bool32 sub_813B4E0(void) +bool32 IsTrainerRegistered(void) { int index = GetRematchIdxByTrainerIdx(gSpecialVar_0x8004); if (index >= 0) @@ -3889,40 +3901,46 @@ void Script_DoRayquazaScene(void) } } -void sub_813B80C(void) +#define playCount data[0] +#define delay data[1] + +void LoopWingFlapSE(void) { - CreateTask(sub_813B824, 8); + CreateTask(Task_LoopWingFlapSE, 8); PlaySE(SE_W017); } -static void sub_813B824(u8 taskId) +static void Task_LoopWingFlapSE(u8 taskId) { s16 *data = gTasks[taskId].data; - data[1]++; - if (data[1] == gSpecialVar_0x8005) + delay++; + if (delay == gSpecialVar_0x8005) { - data[0]++; - data[1] = 0; + playCount++; + delay = 0; PlaySE(SE_W017); } - if (data[0] == gSpecialVar_0x8004 - 1) + if (playCount == gSpecialVar_0x8004 - 1) { DestroyTask(taskId); } } -void sub_813B880(void) +#undef playCount +#undef delay + +void CloseBattlePikeCurtain(void) { - u8 taskId = CreateTask(_fwalk, 8); + u8 taskId = CreateTask(Task_CloseBattlePikeCurtain, 8); gTasks[taskId].data[0] = 4; gTasks[taskId].data[1] = 4; gTasks[taskId].data[2] = 4; gTasks[taskId].data[3] = 0; } -static void _fwalk(u8 taskId) +static void Task_CloseBattlePikeCurtain(u8 taskId) { u8 x, y; s16 *data = gTasks[taskId].data; @@ -3934,7 +3952,7 @@ static void _fwalk(u8 taskId) { for (x = 0; x < 3; x++) { - MapGridSetMetatileIdAt(gSaveBlock1Ptr->pos.x + x + 6, gSaveBlock1Ptr->pos.y + y + 4, x + 0x201 + y * 8 + data[3] * 32); + MapGridSetMetatileIdAt(gSaveBlock1Ptr->pos.x + x + 6, gSaveBlock1Ptr->pos.y + y + 4, x + 513 + y * 8 + data[3] * 32); } } DrawWholeMapView(); -- cgit v1.2.3 From 5325835ee718b4762fc21aa81a481bb63f2dbd29 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 30 Sep 2019 15:43:44 -0400 Subject: Use STR_CONV constants --- src/battle_message.c | 2 +- src/battle_script_commands.c | 2 +- src/berry_tag_screen.c | 6 +++--- src/field_specials.c | 8 ++++---- src/item_menu.c | 30 +++++++++++++++--------------- src/item_use.c | 4 ++-- src/main_menu.c | 8 ++++---- src/match_call.c | 10 +++++----- src/mauville_old_man.c | 2 +- src/menu.c | 8 ++++---- src/menu_specialized.c | 6 +++--- src/party_menu.c | 14 +++++++------- src/pokedex.c | 2 +- src/pokemon_size_record.c | 4 ++-- src/pokemon_storage_system.c | 2 +- src/pokemon_summary_screen.c | 38 +++++++++++++++++++------------------- src/reset_rtc_screen.c | 8 ++++---- src/roulette.c | 2 +- src/scrcmd.c | 6 +++--- src/shop.c | 2 +- src/trainer_card.c | 34 +++++++++++++++++----------------- 21 files changed, 99 insertions(+), 99 deletions(-) diff --git a/src/battle_message.c b/src/battle_message.c index 3fec571f4..22cdee0b8 100644 --- a/src/battle_message.c +++ b/src/battle_message.c @@ -2759,7 +2759,7 @@ static void ExpandBattleTextBuffPlaceholders(const u8 *src, u8 *dst) value = T1_READ_32(&src[srcID + 3]); break; } - ConvertIntToDecimalStringN(dst, value, 0, src[srcID + 2]); + ConvertIntToDecimalStringN(dst, value, STR_CONV_MODE_LEFT_ALIGN, src[srcID + 2]); srcID += src[srcID + 1] + 3; break; case B_BUFF_MOVE: // move name diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 35d47fa82..3c3e2480f 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -8524,7 +8524,7 @@ static void atkAD_tryspiteppreduce(void) PREPARE_MOVE_BUFFER(gBattleTextBuff1, gLastMoves[gBattlerTarget]) - ConvertIntToDecimalStringN(gBattleTextBuff2, ppToDeduct, 0, 1); + ConvertIntToDecimalStringN(gBattleTextBuff2, ppToDeduct, STR_CONV_MODE_LEFT_ALIGN, 1); PREPARE_BYTE_NUMBER_BUFFER(gBattleTextBuff2, 1, ppToDeduct) diff --git a/src/berry_tag_screen.c b/src/berry_tag_screen.c index 10a45dc65..a0fe6588a 100644 --- a/src/berry_tag_screen.c +++ b/src/berry_tag_screen.c @@ -401,7 +401,7 @@ static void PrintAllBerryData(void) static void PrintBerryNumberAndName(void) { const struct Berry *berry = GetBerryInfo(sBerryTag->berryId); - ConvertIntToDecimalStringN(gStringVar1, sBerryTag->berryId, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, sBerryTag->berryId, STR_CONV_MODE_LEADING_ZEROS, 2); StringCopy(gStringVar2, berry->name); StringExpandPlaceholders(gStringVar4, gText_UnkF908Var1Var2); PrintTextInBerryTagScreen(WIN_BERRY_NAME, gStringVar4, 0, 1, 0, 0); @@ -421,8 +421,8 @@ static void PrintBerrySize(void) fraction = (inches % 100) / 10; inches /= 100; - ConvertIntToDecimalStringN(gStringVar1, inches, 0, 2); - ConvertIntToDecimalStringN(gStringVar2, fraction, 0, 2); + ConvertIntToDecimalStringN(gStringVar1, inches, STR_CONV_MODE_LEFT_ALIGN, 2); + ConvertIntToDecimalStringN(gStringVar2, fraction, STR_CONV_MODE_LEFT_ALIGN, 2); StringExpandPlaceholders(gStringVar4, gText_Var1DotVar2); AddTextPrinterParameterized(WIN_SIZE_FIRM, 1, gStringVar4, 0x28, 1, 0, NULL); } diff --git a/src/field_specials.c b/src/field_specials.c index 0c4cc34e3..58d241a92 100644 --- a/src/field_specials.c +++ b/src/field_specials.c @@ -1621,20 +1621,20 @@ void BufferLottoTicketNumber(void) else if (gSpecialVar_Result >= 1000) { gStringVar1[0] = CHAR_0; - ConvertIntToDecimalStringN(gStringVar1 + 1, gSpecialVar_Result, 0, CountDigits(gSpecialVar_Result)); + ConvertIntToDecimalStringN(gStringVar1 + 1, gSpecialVar_Result, STR_CONV_MODE_LEFT_ALIGN, CountDigits(gSpecialVar_Result)); } else if (gSpecialVar_Result >= 100) { gStringVar1[0] = CHAR_0; gStringVar1[1] = CHAR_0; - ConvertIntToDecimalStringN(gStringVar1 + 2, gSpecialVar_Result, 0, CountDigits(gSpecialVar_Result)); + ConvertIntToDecimalStringN(gStringVar1 + 2, gSpecialVar_Result, STR_CONV_MODE_LEFT_ALIGN, CountDigits(gSpecialVar_Result)); } else if (gSpecialVar_Result >= 10) { gStringVar1[0] = CHAR_0; gStringVar1[1] = CHAR_0; gStringVar1[2] = CHAR_0; - ConvertIntToDecimalStringN(gStringVar1 + 3, gSpecialVar_Result, 0, CountDigits(gSpecialVar_Result)); + ConvertIntToDecimalStringN(gStringVar1 + 3, gSpecialVar_Result, STR_CONV_MODE_LEFT_ALIGN, CountDigits(gSpecialVar_Result)); } else { @@ -1642,7 +1642,7 @@ void BufferLottoTicketNumber(void) gStringVar1[1] = CHAR_0; gStringVar1[2] = CHAR_0; gStringVar1[3] = CHAR_0; - ConvertIntToDecimalStringN(gStringVar1 + 4, gSpecialVar_Result, 0, CountDigits(gSpecialVar_Result)); + ConvertIntToDecimalStringN(gStringVar1 + 4, gSpecialVar_Result, STR_CONV_MODE_LEFT_ALIGN, CountDigits(gSpecialVar_Result)); } } diff --git a/src/item_menu.c b/src/item_menu.c index 12f03b328..8d7658ca6 100755 --- a/src/item_menu.c +++ b/src/item_menu.c @@ -790,17 +790,17 @@ void GetItemName(s8 *dest, u16 itemId) StringCopy(gStringVar2, gMoveNames[ItemIdToBattleMoveId(itemId)]); if (itemId >= ITEM_HM01) { - ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_HM01 + 1, 2, 1); + ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_HM01 + 1, STR_CONV_MODE_LEADING_ZEROS, 1); StringExpandPlaceholders(dest, gText_ClearTo11Var1Clear5Var2); } else { - ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_TM01 + 1, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_TM01 + 1, STR_CONV_MODE_LEADING_ZEROS, 2); StringExpandPlaceholders(dest, gText_UnkF908Var1Clear7Var2); } break; case BERRIES_POCKET: - ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_CHERI_BERRY + 1, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, itemId - ITEM_CHERI_BERRY + 1, STR_CONV_MODE_LEADING_ZEROS, 2); CopyItemName(itemId, gStringVar2); StringExpandPlaceholders(dest, gText_UnkF908Var1Clear7Var2); break; @@ -851,14 +851,14 @@ void PrintItemQuantityPlusGFX(u8 rboxId, s32 item_index_in_pocket, u8 a) BlitBitmapToWindow(rboxId, gBagMenuHMIcon_Gfx, 8, a - 1, 16, 16); if (gBagPositionStruct.pocket == BERRIES_POCKET) { - ConvertIntToDecimalStringN(gStringVar1, itemQuantity, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, itemQuantity, STR_CONV_MODE_RIGHT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_xVar1); offset = GetStringRightAlignXOffset(7, gStringVar4, 0x77); BagMenu_Print(rboxId, 7, gStringVar4, offset, a, 0, 0, -1, 0); } else if (gBagPositionStruct.pocket != KEYITEMS_POCKET && (unique = ItemId_GetImportance(itemId)) == FALSE) { - ConvertIntToDecimalStringN(gStringVar1, itemQuantity, 1, 2); + ConvertIntToDecimalStringN(gStringVar1, itemQuantity, STR_CONV_MODE_RIGHT_ALIGN, 2); StringExpandPlaceholders(gStringVar4, gText_xVar1); offset = GetStringRightAlignXOffset(7, gStringVar4, 0x77); BagMenu_Print(rboxId, 7, gStringVar4, offset, a, unique, unique, -1, unique); @@ -1056,7 +1056,7 @@ void sub_81ABC3C(u8 a) void PrintItemDepositAmount(u8 a, s16 b) { u8 r3 = (gBagPositionStruct.pocket == BERRIES_POCKET) ? 3 : 2; - ConvertIntToDecimalStringN(gStringVar1, b, 2, r3); + ConvertIntToDecimalStringN(gStringVar1, b, STR_CONV_MODE_LEADING_ZEROS, r3); StringExpandPlaceholders(gStringVar4, gText_xVar1); AddTextPrinterParameterized(a, 1, gStringVar4, GetStringCenterAlignXOffset(1, gStringVar4, 0x28), 2, 0, 0); } @@ -1064,7 +1064,7 @@ void PrintItemDepositAmount(u8 a, s16 b) void sub_81ABCC0(int a, int b, int c) { u8 r3 = (gBagPositionStruct.pocket == BERRIES_POCKET) ? 3 : 2; - ConvertIntToDecimalStringN(gStringVar1, b, 2, r3); + ConvertIntToDecimalStringN(gStringVar1, b, STR_CONV_MODE_LEADING_ZEROS, r3); StringExpandPlaceholders(gStringVar4, gText_xVar1); AddTextPrinterParameterized(a, 1, gStringVar4, 0, 1, -1, 0); PrintMoneyAmount(a, 0x26, 1, c, 0); @@ -1682,7 +1682,7 @@ void BagMenu_TossItems(u8 taskId) s16* data = gTasks[taskId].data; CopyItemName(gSpecialVar_ItemId, gStringVar1); - ConvertIntToDecimalStringN(gStringVar2, data[8], 0, 3); + ConvertIntToDecimalStringN(gStringVar2, data[8], STR_CONV_MODE_LEFT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_ConfirmTossItems); FillWindowPixelBuffer(1, PIXEL_FILL(0)); BagMenu_Print(1, 1, gStringVar4, 3, 1, 0, 0, 0, 0); @@ -1725,7 +1725,7 @@ void BagMenu_ConfirmToss(u8 taskId) s16* data = gTasks[taskId].data; CopyItemName(gSpecialVar_ItemId, gStringVar1); - ConvertIntToDecimalStringN(gStringVar2, data[8], 0, 3); + ConvertIntToDecimalStringN(gStringVar2, data[8], STR_CONV_MODE_LEFT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_ThrewAwayVar2Var1s); FillWindowPixelBuffer(1, PIXEL_FILL(0)); BagMenu_Print(1, 1, gStringVar4, 3, 1, 0, 0, 0, 0); @@ -1936,7 +1936,7 @@ void sub_81AD680(u8 taskId) { s16* data = gTasks[taskId].data; - ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8], 0, 6); + ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8], STR_CONV_MODE_LEFT_ALIGN, 6); StringExpandPlaceholders(gStringVar4, gText_ICanPayVar1); DisplayItemMessage(taskId, 1, gStringVar4, sub_81AD6E4); } @@ -1996,7 +1996,7 @@ void BagMenu_ConfirmSell(u8 taskId) s16* data = gTasks[taskId].data; CopyItemName(gSpecialVar_ItemId, gStringVar2); - ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8], 0, 6); + ConvertIntToDecimalStringN(gStringVar1, (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8], STR_CONV_MODE_LEFT_ALIGN, 6); StringExpandPlaceholders(gStringVar4, gText_TurnedOverVar1ForVar2); DisplayItemMessage(taskId, 1, gStringVar4, sub_81AD8C8); } @@ -2087,7 +2087,7 @@ void sub_81ADB14(u8 taskId) else if (AddPCItem(gSpecialVar_ItemId, data[8]) == TRUE) { CopyItemName(gSpecialVar_ItemId, gStringVar1); - ConvertIntToDecimalStringN(gStringVar2, data[8], 0, 3); + ConvertIntToDecimalStringN(gStringVar2, data[8], STR_CONV_MODE_LEFT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_DepositedVar2Var1s); BagMenu_Print(1, 1, gStringVar4, 3, 1, 0, 0, 0, 0); gTasks[taskId].func = Task_ActuallyToss; @@ -2398,7 +2398,7 @@ void PrintTMHMMoveData(u16 itemId) } else { - ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveId].power, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveId].power, STR_CONV_MODE_RIGHT_ALIGN, 3); text = gStringVar1; } BagMenu_Print(4, 1, text, 7, 12, 0, 0, -1, 4); @@ -2408,11 +2408,11 @@ void PrintTMHMMoveData(u16 itemId) } else { - ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveId].accuracy, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveId].accuracy, STR_CONV_MODE_RIGHT_ALIGN, 3); text = gStringVar1; } BagMenu_Print(4, 1, text, 7, 24, 0, 0, -1, 4); - ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveId].pp, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveId].pp, STR_CONV_MODE_RIGHT_ALIGN, 3); BagMenu_Print(4, 1, gStringVar1, 7, 36, 0, 0, -1, 4); CopyWindowToVram(4, 2); } diff --git a/src/item_use.c b/src/item_use.c index 2f4374423..46f8cdf4a 100755 --- a/src/item_use.c +++ b/src/item_use.c @@ -623,7 +623,7 @@ void sub_80FDC00(u8 taskId) void ItemUseOutOfBattle_CoinCase(u8 taskId) { - ConvertIntToDecimalStringN(gStringVar1, GetCoins(), 0, 4); + ConvertIntToDecimalStringN(gStringVar1, GetCoins(), STR_CONV_MODE_LEFT_ALIGN, 4); StringExpandPlaceholders(gStringVar4, gText_CoinCase); if (!gTasks[taskId].data[3]) @@ -638,7 +638,7 @@ void ItemUseOutOfBattle_CoinCase(u8 taskId) void ItemUseOutOfBattle_PowderJar(u8 taskId) { - ConvertIntToDecimalStringN(gStringVar1, GetBerryPowder(), 0, 5); + ConvertIntToDecimalStringN(gStringVar1, GetBerryPowder(), STR_CONV_MODE_LEFT_ALIGN, 5); StringExpandPlaceholders(gStringVar4, gText_PowderQty); if (!gTasks[taskId].data[3]) diff --git a/src/main_menu.c b/src/main_menu.c index 6a0bd9e2d..0cb91ae09 100644 --- a/src/main_menu.c +++ b/src/main_menu.c @@ -2158,9 +2158,9 @@ static void MainMenu_FormatSavegameTime(void) StringExpandPlaceholders(gStringVar4, gText_ContinueMenuTime); AddTextPrinterParameterized3(2, 1, 0x6C, 17, sTextColor_PlayerGenderColor, -1, gStringVar4); - ptr = ConvertIntToDecimalStringN(str, gSaveBlock2Ptr->playTimeHours, 0, 3); + ptr = ConvertIntToDecimalStringN(str, gSaveBlock2Ptr->playTimeHours, STR_CONV_MODE_LEFT_ALIGN, 3); *ptr = 0xF0; - ConvertIntToDecimalStringN(ptr + 1, gSaveBlock2Ptr->playTimeMinutes, 2, 2); + ConvertIntToDecimalStringN(ptr + 1, gSaveBlock2Ptr->playTimeMinutes, STR_CONV_MODE_LEADING_ZEROS, 2); AddTextPrinterParameterized3(2, 1, GetStringRightAlignXOffset(1, str, 0xD0), 17, sTextColor_PlayerGenderColor, -1, str); } @@ -2177,7 +2177,7 @@ static void MainMenu_FormatSavegamePokedex(void) dexCount = GetHoennPokedexCount(1); StringExpandPlaceholders(gStringVar4, gText_ContinueMenuPokedex); AddTextPrinterParameterized3(2, 1, 0, 33, sTextColor_PlayerGenderColor, -1, gStringVar4); - ConvertIntToDecimalStringN(str, dexCount, 0, 3); + ConvertIntToDecimalStringN(str, dexCount, STR_CONV_MODE_LEFT_ALIGN, 3); AddTextPrinterParameterized3(2, 1, GetStringRightAlignXOffset(1, str, 100), 33, sTextColor_PlayerGenderColor, -1, str); } } @@ -2195,7 +2195,7 @@ static void MainMenu_FormatSavegameBadges(void) } StringExpandPlaceholders(gStringVar4, gText_ContinueMenuBadges); AddTextPrinterParameterized3(2, 1, 0x6C, 33, sTextColor_PlayerGenderColor, -1, gStringVar4); - ConvertIntToDecimalStringN(str, badgeCount, 2, 1); + ConvertIntToDecimalStringN(str, badgeCount, STR_CONV_MODE_LEADING_ZEROS, 1); AddTextPrinterParameterized3(2, 1, GetStringRightAlignXOffset(1, str, 0xD0), 33, sTextColor_PlayerGenderColor, -1, str); } diff --git a/src/match_call.c b/src/match_call.c index 3ed684938..baf71966e 100644 --- a/src/match_call.c +++ b/src/match_call.c @@ -1743,7 +1743,7 @@ static void PopulateBattleFrontierStreak(int matchCallId, u8 *destStr) i++; } - ConvertIntToDecimalStringN(destStr, gBattleFrontierStreakInfo.streak, 0, i); + ConvertIntToDecimalStringN(destStr, gBattleFrontierStreakInfo.streak, STR_CONV_MODE_LEFT_ALIGN, i); } static const u16 sBadgeFlags[] = @@ -1976,8 +1976,8 @@ void sub_8197080(u8 *destStr) numSeen = GetHoennPokedexCount(FLAG_GET_SEEN); numCaught = GetHoennPokedexCount(FLAG_GET_CAUGHT); - ConvertIntToDecimalStringN(gStringVar1, numSeen, 0, 3); - ConvertIntToDecimalStringN(gStringVar2, numCaught, 0, 3); + ConvertIntToDecimalStringN(gStringVar1, numSeen, STR_CONV_MODE_LEFT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar2, numCaught, STR_CONV_MODE_LEFT_ALIGN, 3); dexRatingLevel = GetPokedexRatingLevel(numCaught); str = StringCopy(buffer, gBirchDexRatingText_AreYouCurious); str[0] = CHAR_PROMPT_CLEAR; @@ -1994,8 +1994,8 @@ void sub_8197080(u8 *destStr) str++; numSeen = GetNationalPokedexCount(FLAG_GET_SEEN); numCaught = GetNationalPokedexCount(FLAG_GET_CAUGHT); - ConvertIntToDecimalStringN(gStringVar1, numSeen, 0, 3); - ConvertIntToDecimalStringN(gStringVar2, numCaught, 0, 3); + ConvertIntToDecimalStringN(gStringVar1, numSeen, STR_CONV_MODE_LEFT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar2, numCaught, STR_CONV_MODE_LEFT_ALIGN, 3); StringExpandPlaceholders(str, gBirchDexRatingText_OnANationwideBasis); } diff --git a/src/mauville_old_man.c b/src/mauville_old_man.c index a4d70c062..f55dd2b8b 100644 --- a/src/mauville_old_man.c +++ b/src/mauville_old_man.c @@ -1132,7 +1132,7 @@ static void StorytellerDisplayStory(u32 player) { u8 stat = sStorytellerPtr->gameStatIDs[player]; - ConvertIntToDecimalStringN(gStringVar1, StorytellerGetRecordedTrainerStat(player), 0, 10); + ConvertIntToDecimalStringN(gStringVar1, StorytellerGetRecordedTrainerStat(player), STR_CONV_MODE_LEFT_ALIGN, 10); StringCopy(gStringVar2, GetStoryActionByStat(stat)); GetStoryByStattellerPlayerName(player, gStringVar3); ConvertInternationalString(gStringVar3, sStorytellerPtr->language[player]); diff --git a/src/menu.c b/src/menu.c index 8d50d4dcc..7d91e5a0c 100644 --- a/src/menu.c +++ b/src/menu.c @@ -2148,15 +2148,15 @@ void sub_819A344(u8 a0, u8 *dest, u8 color) break; case 1: if (IsNationalPokedexEnabled()) - string = ConvertIntToDecimalStringN(string, GetNationalPokedexCount(1), 0, 3); + string = ConvertIntToDecimalStringN(string, GetNationalPokedexCount(1), STR_CONV_MODE_LEFT_ALIGN, 3); else - string = ConvertIntToDecimalStringN(string, GetHoennPokedexCount(1), 0, 3); + string = ConvertIntToDecimalStringN(string, GetHoennPokedexCount(1), STR_CONV_MODE_LEFT_ALIGN, 3); *string = EOS; break; case 2: - string = ConvertIntToDecimalStringN(string, gSaveBlock2Ptr->playTimeHours, 0, 3); + string = ConvertIntToDecimalStringN(string, gSaveBlock2Ptr->playTimeHours, STR_CONV_MODE_LEFT_ALIGN, 3); *(string++) = CHAR_COLON; - ConvertIntToDecimalStringN(string, gSaveBlock2Ptr->playTimeMinutes, 2, 2); + ConvertIntToDecimalStringN(string, gSaveBlock2Ptr->playTimeMinutes, STR_CONV_MODE_LEADING_ZEROS, 2); break; case 3: sub_81245DC(string, gMapHeader.regionMapSectionId); diff --git a/src/menu_specialized.c b/src/menu_specialized.c index 1536cf413..e9932db26 100644 --- a/src/menu_specialized.c +++ b/src/menu_specialized.c @@ -761,7 +761,7 @@ static void MoveRelearnerLoadBattleMoveDescription(u32 chosenMove) AddTextPrinterParameterized(0, 1, str, 4, 0x19, TEXT_SPEED_FF, NULL); x = 4 + GetStringWidth(1, gText_MoveRelearnerPP, 0); - ConvertIntToDecimalStringN(buffer, move->pp, 0, 2); + ConvertIntToDecimalStringN(buffer, move->pp, STR_CONV_MODE_LEFT_ALIGN, 2); AddTextPrinterParameterized(0, 1, buffer, x, 0x29, TEXT_SPEED_FF, NULL); if (move->power < 2) @@ -770,7 +770,7 @@ static void MoveRelearnerLoadBattleMoveDescription(u32 chosenMove) } else { - ConvertIntToDecimalStringN(buffer, move->power, 0, 3); + ConvertIntToDecimalStringN(buffer, move->power, STR_CONV_MODE_LEFT_ALIGN, 3); str = buffer; } AddTextPrinterParameterized(0, 1, str, 0x6A, 0x19, TEXT_SPEED_FF, NULL); @@ -781,7 +781,7 @@ static void MoveRelearnerLoadBattleMoveDescription(u32 chosenMove) } else { - ConvertIntToDecimalStringN(buffer, move->accuracy, 0, 3); + ConvertIntToDecimalStringN(buffer, move->accuracy, STR_CONV_MODE_LEFT_ALIGN, 3); str = buffer; } AddTextPrinterParameterized(0, 1, str, 0x6A, 0x29, TEXT_SPEED_FF, NULL); diff --git a/src/party_menu.c b/src/party_menu.c index f4b749bde..9d1cc35cf 100755 --- a/src/party_menu.c +++ b/src/party_menu.c @@ -2918,7 +2918,7 @@ static void sub_81B1E60(u8 taskId) if (data[3] == 0 || data[0] == 0 || data[0] == data[1]) { if (data[0] > data[5]) - ConvertIntToDecimalStringN(gStringVar2, data[0] - data[5], 0, 3); + ConvertIntToDecimalStringN(gStringVar2, data[0] - data[5], STR_CONV_MODE_LEFT_ALIGN, 3); SwitchTaskToFollowupFunc(taskId); } } @@ -3412,7 +3412,7 @@ static void DisplayPartyPokemonLevelCheck(struct Pokemon *mon, struct Struct203C static void DisplayPartyPokemonLevel(u8 level, struct Struct203CEDC *ptr) { - ConvertIntToDecimalStringN(gStringVar2, level, 0, 3); + ConvertIntToDecimalStringN(gStringVar2, level, STR_CONV_MODE_LEFT_ALIGN, 3); StringCopy(gStringVar1, gText_LevelSymbol); StringAppend(gStringVar1, gStringVar2); DisplayPartyPokemonBarDetail(ptr->windowId, gStringVar1, 0, &ptr->unk0->unk4[4]); @@ -3464,7 +3464,7 @@ static void DisplayPartyPokemonHPCheck(struct Pokemon *mon, struct Struct203CEDC static void DisplayPartyPokemonHP(u16 hp, struct Struct203CEDC *ptr) { - u8 *strOut = ConvertIntToDecimalStringN(gStringVar1, hp, 1, 3); + u8 *strOut = ConvertIntToDecimalStringN(gStringVar1, hp, STR_CONV_MODE_RIGHT_ALIGN, 3); strOut[0] = CHAR_SLASH; strOut[1] = EOS; @@ -3485,7 +3485,7 @@ static void DisplayPartyPokemonMaxHPCheck(struct Pokemon *mon, struct Struct203C static void DisplayPartyPokemonMaxHP(u16 maxhp, struct Struct203CEDC *ptr) { - ConvertIntToDecimalStringN(gStringVar2, maxhp, 1, 3); + ConvertIntToDecimalStringN(gStringVar2, maxhp, STR_CONV_MODE_RIGHT_ALIGN, 3); StringCopy(gStringVar1, gText_Slash); StringAppend(gStringVar1, gStringVar2); DisplayPartyPokemonBarDetail(ptr->windowId, gStringVar1, 0, &ptr->unk0->unk4[16]); @@ -4580,7 +4580,7 @@ static void CursorCb_Enter(u8 taskId) return; } } - ConvertIntToDecimalStringN(gStringVar1, unk, 0, 1); + ConvertIntToDecimalStringN(gStringVar1, unk, STR_CONV_MODE_LEFT_ALIGN, 1); StringExpandPlaceholders(gStringVar4, gText_NoMoreThanVar1Pkmn); PlaySE(SE_HAZURE); sub_81B1B5C(gStringVar4, 1); @@ -6004,7 +6004,7 @@ void dp05_rare_candy(u8 taskId, TaskFunc task) sub_81B754C(gUnknown_0203CEC8.slotId, mon); RemoveBagItem(gSpecialVar_ItemId, 1); GetMonNickname(mon, gStringVar1); - ConvertIntToDecimalStringN(gStringVar2, GetMonData(mon, MON_DATA_LEVEL), 0, 3); + ConvertIntToDecimalStringN(gStringVar2, GetMonData(mon, MON_DATA_LEVEL), STR_CONV_MODE_LEFT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_PkmnElevatedToLvVar2); sub_81B1B5C(gStringVar4, 1); schedule_bg_copy_tilemap_to_vram(2); @@ -6634,7 +6634,7 @@ static u8 sub_81B865C(void) { if (unk == 1) return 14; - ConvertIntToDecimalStringN(gStringVar1, unk, 0, 1); + ConvertIntToDecimalStringN(gStringVar1, unk, STR_CONV_MODE_LEFT_ALIGN, 1); return 17; } diff --git a/src/pokedex.c b/src/pokedex.c index c1787ef6a..24ffe65fa 100644 --- a/src/pokedex.c +++ b/src/pokedex.c @@ -3889,7 +3889,7 @@ void sub_80C020C(u32 num, u32 value, u32 c, u32 d) value = NationalToHoennOrder(num); else value = num; - ConvertIntToDecimalStringN(StringCopy(str, gText_UnkCtrlF908Clear01), value, 2, 3); + ConvertIntToDecimalStringN(StringCopy(str, gText_UnkCtrlF908Clear01), value, STR_CONV_MODE_LEADING_ZEROS, 3); sub_80BE8DC(str, 0x60, 0x19); natNum = NationalPokedexNumToSpecies(num); if (natNum) diff --git a/src/pokemon_size_record.c b/src/pokemon_size_record.c index f63e1ccba..914d68925 100644 --- a/src/pokemon_size_record.c +++ b/src/pokemon_size_record.c @@ -100,9 +100,9 @@ static void FormatMonSizeRecord(u8 *string, u32 size) size = (double)(size * 10) / (CM_PER_INCH * 10); #endif - string = ConvertIntToDecimalStringN(string, size / 10, 0, 8); + string = ConvertIntToDecimalStringN(string, size / 10, STR_CONV_MODE_LEFT_ALIGN, 8); string = StringAppend(string, gText_DecimalPoint); - ConvertIntToDecimalStringN(string, size % 10, 0, 1); + ConvertIntToDecimalStringN(string, size % 10, STR_CONV_MODE_LEFT_ALIGN, 1); } static u8 CompareMonSize(u16 species, u16 *sizeRecord) diff --git a/src/pokemon_storage_system.c b/src/pokemon_storage_system.c index fa9e61504..956f7c938 100644 --- a/src/pokemon_storage_system.c +++ b/src/pokemon_storage_system.c @@ -2107,7 +2107,7 @@ static void sub_80C7BE4(void) center = GetStringCenterAlignXOffset(1, boxName, 64); AddTextPrinterParameterized3(windowId, 1, center, 1, gUnknown_08571734, TEXT_SPEED_FF, boxName); - ConvertIntToDecimalStringN(text, nPokemonInBox, 1, 2); + ConvertIntToDecimalStringN(text, nPokemonInBox, STR_CONV_MODE_RIGHT_ALIGN, 2); StringAppend(text, gUnknown_08571737); center = GetStringCenterAlignXOffset(1, text, 64); AddTextPrinterParameterized3(windowId, 1, center, 17, gUnknown_08571734, TEXT_SPEED_FF, text); diff --git a/src/pokemon_summary_screen.c b/src/pokemon_summary_screen.c index 8697c557f..0173f8eba 100644 --- a/src/pokemon_summary_screen.c +++ b/src/pokemon_summary_screen.c @@ -2716,7 +2716,7 @@ static void PrintNotEggInfo(void) if (dexNum != 0xFFFF) { StringCopy(gStringVar1, &gText_UnkCtrlF908Clear01[0]); - ConvertIntToDecimalStringN(gStringVar2, dexNum, 2, 3); + ConvertIntToDecimalStringN(gStringVar2, dexNum, STR_CONV_MODE_LEADING_ZEROS, 3); StringAppend(gStringVar1, gStringVar2); if (!IsMonShiny(mon)) { @@ -2739,7 +2739,7 @@ static void PrintNotEggInfo(void) SetDexNumberColor(TRUE); } StringCopy(gStringVar1, &gText_LevelSymbol[0]); - ConvertIntToDecimalStringN(gStringVar2, summary->level, 0, 3); + ConvertIntToDecimalStringN(gStringVar2, summary->level, STR_CONV_MODE_LEFT_ALIGN, 3); StringAppend(gStringVar1, gStringVar2); SummaryScreen_PrintTextOnWindow(PSS_LABEL_WINDOW_PORTRAIT_SPECIES, gStringVar1, 0x18, 17, 0, 1); GetMonNickname(mon, gStringVar1); @@ -3054,7 +3054,7 @@ static void PrintMonOTID(void) int xPos; if (InBattleFactory() != TRUE && InSlateportBattleTent() != TRUE) { - ConvertIntToDecimalStringN(StringCopy(gStringVar1, gText_UnkCtrlF907F908), (u16)sMonSummaryScreen->summary.OTID, 2, 5); + ConvertIntToDecimalStringN(StringCopy(gStringVar1, gText_UnkCtrlF907F908), (u16)sMonSummaryScreen->summary.OTID, STR_CONV_MODE_LEADING_ZEROS, 5); xPos = GetStringRightAlignXOffset(1, gStringVar1, 56); SummaryScreen_PrintTextOnWindow(AddWindowFromTemplateList(sPageInfoTemplate, PSS_DATA_WINDOW_INFO_ID), gStringVar1, xPos, 1, 0, 1); } @@ -3141,7 +3141,7 @@ static void GetMetLevelString(u8 *output) u8 level = sMonSummaryScreen->summary.metLevel; if (level == 0) level = EGG_HATCH_LEVEL; - ConvertIntToDecimalStringN(output, level, 0, 3); + ConvertIntToDecimalStringN(output, level, STR_CONV_MODE_LEFT_ALIGN, 3); DynamicPlaceholderTextUtil_SetPlaceholderPtr(3, output); } @@ -3336,7 +3336,7 @@ static void PrintRibbonCount(void) } else { - ConvertIntToDecimalStringN(gStringVar1, sMonSummaryScreen->summary.ribbonCount, 1, 2); + ConvertIntToDecimalStringN(gStringVar1, sMonSummaryScreen->summary.ribbonCount, STR_CONV_MODE_RIGHT_ALIGN, 2); StringExpandPlaceholders(gStringVar4, gText_RibbonsVar1); text = gStringVar4; } @@ -3352,10 +3352,10 @@ static void BufferLeftColumnStats(void) u8 *attackString = Alloc(8); u8 *defenseString = Alloc(8); - ConvertIntToDecimalStringN(currentHPString, sMonSummaryScreen->summary.currentHP, 1, 3); - ConvertIntToDecimalStringN(maxHPString, sMonSummaryScreen->summary.maxHP, 1, 3); - ConvertIntToDecimalStringN(attackString, sMonSummaryScreen->summary.atk, 1, 7); - ConvertIntToDecimalStringN(defenseString, sMonSummaryScreen->summary.def, 1, 7); + ConvertIntToDecimalStringN(currentHPString, sMonSummaryScreen->summary.currentHP, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(maxHPString, sMonSummaryScreen->summary.maxHP, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(attackString, sMonSummaryScreen->summary.atk, STR_CONV_MODE_RIGHT_ALIGN, 7); + ConvertIntToDecimalStringN(defenseString, sMonSummaryScreen->summary.def, STR_CONV_MODE_RIGHT_ALIGN, 7); DynamicPlaceholderTextUtil_Reset(); DynamicPlaceholderTextUtil_SetPlaceholderPtr(0, currentHPString); @@ -3377,9 +3377,9 @@ static void PrintLeftColumnStats(void) static void BufferRightColumnStats(void) { - ConvertIntToDecimalStringN(gStringVar1, sMonSummaryScreen->summary.spatk, 1, 3); - ConvertIntToDecimalStringN(gStringVar2, sMonSummaryScreen->summary.spdef, 1, 3); - ConvertIntToDecimalStringN(gStringVar3, sMonSummaryScreen->summary.speed, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, sMonSummaryScreen->summary.spatk, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar2, sMonSummaryScreen->summary.spdef, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar3, sMonSummaryScreen->summary.speed, STR_CONV_MODE_RIGHT_ALIGN, 3); DynamicPlaceholderTextUtil_Reset(); DynamicPlaceholderTextUtil_SetPlaceholderPtr(0, gStringVar1); @@ -3400,7 +3400,7 @@ static void PrintExpPointsNextLevel(void) int offset; u32 expToNextLevel; - ConvertIntToDecimalStringN(gStringVar1, sum->exp, 1, 7); + ConvertIntToDecimalStringN(gStringVar1, sum->exp, STR_CONV_MODE_RIGHT_ALIGN, 7); offset = GetStringRightAlignXOffset(1, gStringVar1, 42) + 2; SummaryScreen_PrintTextOnWindow(windowId, gStringVar1, offset, 1, 0, 0); @@ -3409,7 +3409,7 @@ static void PrintExpPointsNextLevel(void) else expToNextLevel = 0; - ConvertIntToDecimalStringN(gStringVar1, expToNextLevel, 1, 6); + ConvertIntToDecimalStringN(gStringVar1, expToNextLevel, STR_CONV_MODE_RIGHT_ALIGN, 6); offset = GetStringRightAlignXOffset(1, gStringVar1, 42) + 2; SummaryScreen_PrintTextOnWindow(windowId, gStringVar1, offset, 17, 0, 0); } @@ -3496,8 +3496,8 @@ static void PrintMoveNameAndPP(u8 moveIndex) { pp = CalculatePPWithBonus(move, summaryStruct->summary.ppBonuses, moveIndex); SummaryScreen_PrintTextOnWindow(moveNameWindowId, gMoveNames[move], 0, moveIndex * 16 + 1, 0, 1); - ConvertIntToDecimalStringN(gStringVar1, summaryStruct->summary.pp[moveIndex], 1, 2); - ConvertIntToDecimalStringN(gStringVar2, pp, 1, 2); + ConvertIntToDecimalStringN(gStringVar1, summaryStruct->summary.pp[moveIndex], STR_CONV_MODE_RIGHT_ALIGN, 2); + ConvertIntToDecimalStringN(gStringVar2, pp, STR_CONV_MODE_RIGHT_ALIGN, 2); DynamicPlaceholderTextUtil_Reset(); DynamicPlaceholderTextUtil_SetPlaceholderPtr(0, gStringVar1); DynamicPlaceholderTextUtil_SetPlaceholderPtr(1, gStringVar2); @@ -3530,7 +3530,7 @@ static void PrintMovePowerAndAccuracy(u16 moveIndex) } else { - ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveIndex].power, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveIndex].power, STR_CONV_MODE_RIGHT_ALIGN, 3); text = gStringVar1; } @@ -3542,7 +3542,7 @@ static void PrintMovePowerAndAccuracy(u16 moveIndex) } else { - ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveIndex].accuracy, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[moveIndex].accuracy, STR_CONV_MODE_RIGHT_ALIGN, 3); text = gStringVar1; } @@ -3660,7 +3660,7 @@ static void PrintNewMoveDetailsOrCancelText(void) else SummaryScreen_PrintTextOnWindow(windowId1, gMoveNames[move], 0, 65, 0, 5); - ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[move].pp, 1, 2); + ConvertIntToDecimalStringN(gStringVar1, gBattleMoves[move].pp, STR_CONV_MODE_RIGHT_ALIGN, 2); DynamicPlaceholderTextUtil_Reset(); DynamicPlaceholderTextUtil_SetPlaceholderPtr(0, gStringVar1); DynamicPlaceholderTextUtil_SetPlaceholderPtr(1, gStringVar1); diff --git a/src/reset_rtc_screen.c b/src/reset_rtc_screen.c index 1fe9f2279..eebaab52f 100644 --- a/src/reset_rtc_screen.c +++ b/src/reset_rtc_screen.c @@ -300,19 +300,19 @@ static void PrintTime(u8 windowId, u8 x, u8 y, u16 days, u8 hours, u8 minutes, u { u8 *dest = gStringVar4; - ConvertIntToDecimalStringN(gStringVar1, days, 1, 4); + ConvertIntToDecimalStringN(gStringVar1, days, STR_CONV_MODE_RIGHT_ALIGN, 4); dest = StringCopy(dest, gStringVar1); dest = StringCopy(dest, gText_Day); - ConvertIntToDecimalStringN(gStringVar1, hours, 1, 3); + ConvertIntToDecimalStringN(gStringVar1, hours, STR_CONV_MODE_RIGHT_ALIGN, 3); dest = StringCopy(dest, gStringVar1); dest = StringCopy(dest, gText_Colon3); - ConvertIntToDecimalStringN(gStringVar1, minutes, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, minutes, STR_CONV_MODE_LEADING_ZEROS, 2); dest = StringCopy(dest, gStringVar1); dest = StringCopy(dest, gText_Colon3); - ConvertIntToDecimalStringN(gStringVar1, seconds, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, seconds, STR_CONV_MODE_LEADING_ZEROS, 2); dest = StringCopy(dest, gStringVar1); AddTextPrinterParameterized(windowId, 1, gStringVar4, x, y, TEXT_SPEED_FF, NULL); diff --git a/src/roulette.c b/src/roulette.c index 662522aae..6a18b00f2 100644 --- a/src/roulette.c +++ b/src/roulette.c @@ -3134,7 +3134,7 @@ static void Task_Roulette_0(u8 taskId) s32 temp; PrintCoinsString(gTasks[taskId].data[13]); temp = gUnknown_085B6344[(gSpecialVar_0x8004 & 1) + (gSpecialVar_0x8004 >> 7 << 1)]; - ConvertIntToDecimalStringN(gStringVar1, temp, 2, 1); + ConvertIntToDecimalStringN(gStringVar1, temp, STR_CONV_MODE_LEADING_ZEROS, 1); if (gTasks[taskId].data[13] >= temp) { if ((gSpecialVar_0x8004 & 0x80) && (gSpecialVar_0x8004 & 1)) diff --git a/src/scrcmd.c b/src/scrcmd.c index 4c8895ab8..9861f9dae 100644 --- a/src/scrcmd.c +++ b/src/scrcmd.c @@ -1612,10 +1612,10 @@ bool8 ScrCmd_buffermovename(struct ScriptContext *ctx) bool8 ScrCmd_buffernumberstring(struct ScriptContext *ctx) { u8 stringVarIndex = ScriptReadByte(ctx); - u16 v1 = VarGet(ScriptReadHalfword(ctx)); - u8 v2 = CountDigits(v1); + u16 num = VarGet(ScriptReadHalfword(ctx)); + u8 numDigits = CountDigits(num); - ConvertIntToDecimalStringN(sScriptStringVars[stringVarIndex], v1, 0, v2); + ConvertIntToDecimalStringN(sScriptStringVars[stringVarIndex], num, STR_CONV_MODE_LEFT_ALIGN, numDigits); return FALSE; } diff --git a/src/shop.c b/src/shop.c index bf15568fb..aa3b44715 100755 --- a/src/shop.c +++ b/src/shop.c @@ -1147,7 +1147,7 @@ static void BuyMenuPrintItemQuantityAndPrice(u8 taskId) FillWindowPixelBuffer(4, PIXEL_FILL(1)); PrintMoneyAmount(4, 38, 1, gShopDataPtr->totalCost, TEXT_SPEED_FF); - ConvertIntToDecimalStringN(gStringVar1, tItemCount, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, tItemCount, STR_CONV_MODE_LEADING_ZEROS, 2); StringExpandPlaceholders(gStringVar4, gText_xVar1); BuyMenuPrint(4, gStringVar4, 0, 1, 0, 0); } diff --git a/src/trainer_card.c b/src/trainer_card.c index 9c4816c42..2f0205801 100755 --- a/src/trainer_card.c +++ b/src/trainer_card.c @@ -1010,7 +1010,7 @@ static void PrintMoneyOnCard(void) else AddTextPrinterParameterized3(1, 1, 16, 57, gUnknown_0856FB0C, TEXT_SPEED_FF, gText_TrainerCardMoney); - ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.money, 0, 6); + ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.money, STR_CONV_MODE_LEFT_ALIGN, 6); StringExpandPlaceholders(gStringVar4, gText_PokedollarVar1); if (!sData->isHoenn) { @@ -1043,7 +1043,7 @@ static void PrintPokedexOnCard(void) AddTextPrinterParameterized3(1, 1, 20, 72, gUnknown_0856FB0C, TEXT_SPEED_FF, gText_TrainerCardPokedex); else AddTextPrinterParameterized3(1, 1, 16, 73, gUnknown_0856FB0C, TEXT_SPEED_FF, gText_TrainerCardPokedex); - StringCopy(ConvertIntToDecimalStringN(gStringVar4, sData->trainerCard.caughtMonsCount, 0, 3), gText_EmptyString6); + StringCopy(ConvertIntToDecimalStringN(gStringVar4, sData->trainerCard.caughtMonsCount, STR_CONV_MODE_LEFT_ALIGN, 3), gText_EmptyString6); if (!sData->isHoenn) { xOffset = GetStringRightAlignXOffset(1, gStringVar4, 144); @@ -1103,12 +1103,12 @@ static void PrintTimeOnCard(void) r7 -= r10; FillWindowPixelRect(1, PIXEL_FILL(0), r7, r4, r10, 15); - ConvertIntToDecimalStringN(gStringVar4, hours, 1, 3); + ConvertIntToDecimalStringN(gStringVar4, hours, STR_CONV_MODE_RIGHT_ALIGN, 3); AddTextPrinterParameterized3(1, 1, r7, r4, gUnknown_0856FB0C, TEXT_SPEED_FF, gStringVar4); r7 += 18; AddTextPrinterParameterized3(1, 1, r7, r4, gUnknown_0856FB40[sData->var_7], TEXT_SPEED_FF, gText_Colon2); r7 += width; - ConvertIntToDecimalStringN(gStringVar4, minutes, 2, 2); + ConvertIntToDecimalStringN(gStringVar4, minutes, STR_CONV_MODE_LEADING_ZEROS, 2); AddTextPrinterParameterized3(1, 1, r7, r4, gUnknown_0856FB0C, TEXT_SPEED_FF, gStringVar4); } @@ -1151,9 +1151,9 @@ static void PrintHofTimeOnCard(void) { if (sData->hasHofResult) { - ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.hofDebutHours, 1, 3); - ConvertIntToDecimalStringN(gStringVar2, sData->trainerCard.hofDebutMinutes, 2, 2); - ConvertIntToDecimalStringN(gStringVar3, sData->trainerCard.hofDebutSeconds, 2, 2); + ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.hofDebutHours, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar2, sData->trainerCard.hofDebutMinutes, STR_CONV_MODE_LEADING_ZEROS, 2); + ConvertIntToDecimalStringN(gStringVar3, sData->trainerCard.hofDebutSeconds, STR_CONV_MODE_LEADING_ZEROS, 2); StringExpandPlaceholders(sData->var_93, gUnknown_0856FB4C); } } @@ -1180,8 +1180,8 @@ static void PrintLinkResultsNumsOnCard(void) if (sData->hasLinkResults) { StringCopy(sData->var_D9, gUnknown_0856FB5C[sData->cardType]); - ConvertIntToDecimalStringN(sData->var_165, sData->trainerCard.linkBattleWins, 0, 4); - ConvertIntToDecimalStringN(sData->var_1AB, sData->trainerCard.linkBattleLosses, 0, 4); + ConvertIntToDecimalStringN(sData->var_165, sData->trainerCard.linkBattleWins, STR_CONV_MODE_LEFT_ALIGN, 4); + ConvertIntToDecimalStringN(sData->var_1AB, sData->trainerCard.linkBattleLosses, STR_CONV_MODE_LEFT_ALIGN, 4); } } @@ -1199,7 +1199,7 @@ static void PrintWinsLossesStringOnCard(void) static void PrintTradesNumOnCard(void) { if (sData->hasTrades) - ConvertIntToDecimalStringN(sData->var_237, sData->trainerCard.pokemonTrades, 1, 5); + ConvertIntToDecimalStringN(sData->var_237, sData->trainerCard.pokemonTrades, STR_CONV_MODE_RIGHT_ALIGN, 5); } static void PrintTradesStringOnCard(void) @@ -1211,7 +1211,7 @@ static void PrintTradesStringOnCard(void) static void PrintBerryCrushNumOnCard(void) { if (sData->cardType == CARD_TYPE_FRLG && sData->trainerCard.berryCrushPoints) - ConvertIntToDecimalStringN(sData->var_2C3, sData->trainerCard.berryCrushPoints, 1, 5); + ConvertIntToDecimalStringN(sData->var_2C3, sData->trainerCard.berryCrushPoints, STR_CONV_MODE_RIGHT_ALIGN, 5); } static void PrintBerryCrushStringOnCard(void) @@ -1223,7 +1223,7 @@ static void PrintBerryCrushStringOnCard(void) static void PrintUnionNumOnCard(void) { if (sData->cardType == CARD_TYPE_FRLG && sData->trainerCard.unionRoomNum) - ConvertIntToDecimalStringN(sData->var_34F, sData->trainerCard.unionRoomNum, 1, 5); + ConvertIntToDecimalStringN(sData->var_34F, sData->trainerCard.unionRoomNum, STR_CONV_MODE_RIGHT_ALIGN, 5); } static void PrintUnionStringOnCard(void) @@ -1236,7 +1236,7 @@ static void PrintPokeblocksNumOnCard(void) { if (sData->cardType != CARD_TYPE_FRLG && sData->trainerCard.pokeblocksWithFriends) { - ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.pokeblocksWithFriends, 1, 5); + ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.pokeblocksWithFriends, STR_CONV_MODE_RIGHT_ALIGN, 5); StringExpandPlaceholders(sData->var_395, gText_Var1DarkGreyShadowLightGrey); } } @@ -1250,7 +1250,7 @@ static void PrintPokeblockStringOnCard(void) static void PrintContestNumOnCard(void) { if (sData->cardType != CARD_TYPE_FRLG && sData->trainerCard.contestsWithFriends) - ConvertIntToDecimalStringN(sData->var_3DB, sData->trainerCard.contestsWithFriends, 1, 5); + ConvertIntToDecimalStringN(sData->var_3DB, sData->trainerCard.contestsWithFriends, STR_CONV_MODE_RIGHT_ALIGN, 5); } static void PrintContestStringOnCard(void) @@ -1266,15 +1266,15 @@ static void PrintBattleFacilityNumsOnCard(void) case CARD_TYPE_RS: if (sData->hasBattleTowerWins) { - ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.battleTowerWins, 1, 4); - ConvertIntToDecimalStringN(gStringVar2, sData->trainerCard.battleTowerStraightWins, 1, 4); + ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.battleTowerWins, STR_CONV_MODE_RIGHT_ALIGN, 4); + ConvertIntToDecimalStringN(gStringVar2, sData->trainerCard.battleTowerStraightWins, STR_CONV_MODE_RIGHT_ALIGN, 4); StringExpandPlaceholders(sData->var_421, gText_WSlashStraightSlash); } break; case CARD_TYPE_EMERALD: if (sData->trainerCard.frontierBP) { - ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.frontierBP, 1, 5); + ConvertIntToDecimalStringN(gStringVar1, sData->trainerCard.frontierBP, STR_CONV_MODE_RIGHT_ALIGN, 5); StringExpandPlaceholders(sData->var_421, gText_Var1DarkLightGreyBP); } break; -- cgit v1.2.3 From c5cb45991b65f7e4a5cfc39816a214085f740a7b Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 30 Sep 2019 15:53:24 -0400 Subject: Document PrintItemXAmount --- src/item_menu.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/item_menu.c b/src/item_menu.c index 8d7658ca6..bfecc9d3c 100755 --- a/src/item_menu.c +++ b/src/item_menu.c @@ -1053,21 +1053,21 @@ void sub_81ABC3C(u8 a) PrintItemDepositAmount(BagMenu_AddWindow(a), 1); } -void PrintItemDepositAmount(u8 a, s16 b) +void PrintItemDepositAmount(u8 windowId, s16 numDeposited) { - u8 r3 = (gBagPositionStruct.pocket == BERRIES_POCKET) ? 3 : 2; - ConvertIntToDecimalStringN(gStringVar1, b, STR_CONV_MODE_LEADING_ZEROS, r3); + u8 numDigits = (gBagPositionStruct.pocket == BERRIES_POCKET) ? 3 : 2; + ConvertIntToDecimalStringN(gStringVar1, numDeposited, STR_CONV_MODE_LEADING_ZEROS, numDigits); StringExpandPlaceholders(gStringVar4, gText_xVar1); - AddTextPrinterParameterized(a, 1, gStringVar4, GetStringCenterAlignXOffset(1, gStringVar4, 0x28), 2, 0, 0); + AddTextPrinterParameterized(windowId, 1, gStringVar4, GetStringCenterAlignXOffset(1, gStringVar4, 0x28), 2, 0, 0); } -void sub_81ABCC0(int a, int b, int c) +void PrintItemSoldAmount(int windowId, int numSold, int moneyEarned) { - u8 r3 = (gBagPositionStruct.pocket == BERRIES_POCKET) ? 3 : 2; - ConvertIntToDecimalStringN(gStringVar1, b, STR_CONV_MODE_LEADING_ZEROS, r3); + u8 numDigits = (gBagPositionStruct.pocket == BERRIES_POCKET) ? 3 : 2; + ConvertIntToDecimalStringN(gStringVar1, numSold, STR_CONV_MODE_LEADING_ZEROS, numDigits); StringExpandPlaceholders(gStringVar4, gText_xVar1); - AddTextPrinterParameterized(a, 1, gStringVar4, 0, 1, -1, 0); - PrintMoneyAmount(a, 0x26, 1, c, 0); + AddTextPrinterParameterized(windowId, 1, gStringVar4, 0, 1, -1, 0); + PrintMoneyAmount(windowId, 38, 1, moneyEarned, 0); } void Task_BagMenu(u8 taskId) @@ -1961,7 +1961,7 @@ void sub_81AD730(u8 taskId) s16* data = gTasks[taskId].data; u8 windowId = BagMenu_AddWindow(8); - sub_81ABCC0(windowId, 1, (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8]); + PrintItemSoldAmount(windowId, 1, (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8]); bag_menu_AddMoney_window(); gTasks[taskId].func = sub_81AD794; } @@ -1972,7 +1972,7 @@ void sub_81AD794(u8 taskId) if (AdjustQuantityAccordingToDPadInput(&data[8], data[2]) == TRUE) { - sub_81ABCC0(gBagMenu->unk818, data[8], (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8]); + PrintItemSoldAmount(gBagMenu->unk818, data[8], (ItemId_GetPrice(gSpecialVar_ItemId) / 2) * data[8]); } else if (gMain.newKeys & A_BUTTON) { -- cgit v1.2.3