From b8a44bf10a5bfbe35381b744d86f535a14c0969a Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 11 Oct 2021 10:35:53 -0400 Subject: [PATCH] Calculate pokemon substruct size, missing MON_PIC_SIZE --- include/pokemon.h | 13 +++++++++++-- src/battle_gfx_sfx_util.c | 4 ++-- src/pokemon.c | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/pokemon.h b/include/pokemon.h index 229898d71..315416c38 100644 --- a/include/pokemon.h +++ b/include/pokemon.h @@ -76,13 +76,22 @@ struct PokemonSubstruct3 /* 0x0B */ u32 eventLegal:1; // controls Mew & Deoxys obedience; if set, Pokémon is a fateful encounter in Gen 4+; set for in-game event island legendaries, some distributed events, and Pokémon from XD: Gale of Darkness. }; +// Number of bytes in the largest Pokémon substruct. +// They are assumed to be the same size, and will be padded to +// the largest size by the union. +// By default they are all 12 bytes. +#define NUM_SUBSTRUCT_BYTES (max(sizeof(struct PokemonSubstruct0), \ + max(sizeof(struct PokemonSubstruct1), \ + max(sizeof(struct PokemonSubstruct2), \ + sizeof(struct PokemonSubstruct3))))) + union PokemonSubstruct { struct PokemonSubstruct0 type0; struct PokemonSubstruct1 type1; struct PokemonSubstruct2 type2; struct PokemonSubstruct3 type3; - u16 raw[6]; + u16 raw[NUM_SUBSTRUCT_BYTES / 2]; // /2 because it's u16, not u8 }; struct BoxPokemon @@ -102,7 +111,7 @@ struct BoxPokemon union { - u32 raw[12]; + u32 raw[(NUM_SUBSTRUCT_BYTES * 4) / 4]; // *4 because there are 4 substructs, /4 because it's u32, not u8 union PokemonSubstruct substructs[4]; } secure; }; diff --git a/src/battle_gfx_sfx_util.c b/src/battle_gfx_sfx_util.c index 77c50dc18..c01c76b7c 100644 --- a/src/battle_gfx_sfx_util.c +++ b/src/battle_gfx_sfx_util.c @@ -1248,11 +1248,11 @@ void AllocateMonSpritesGfx(void) gMonSpritesGfxPtr = NULL; gMonSpritesGfxPtr = AllocZeroed(sizeof(*gMonSpritesGfxPtr)); - gMonSpritesGfxPtr->firstDecompressed = AllocZeroed(0x8000); + gMonSpritesGfxPtr->firstDecompressed = AllocZeroed(MON_PIC_SIZE * 4 * MAX_BATTLERS_COUNT); for (i = 0; i < MAX_BATTLERS_COUNT; i++) { - gMonSpritesGfxPtr->sprites.ptr[i] = gMonSpritesGfxPtr->firstDecompressed + (i * 0x2000); + gMonSpritesGfxPtr->sprites.ptr[i] = gMonSpritesGfxPtr->firstDecompressed + (i * MON_PIC_SIZE * 4); *(gMonSpritesGfxPtr->templates + i) = gBattlerSpriteTemplates[i]; for (j = 0; j < 4; j++) diff --git a/src/pokemon.c b/src/pokemon.c index 06a7a01f1..011e288db 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -6876,7 +6876,7 @@ static bool8 ShouldSkipFriendshipChange(void) #define ALLOC_FAIL_BUFFER (1 << 0) #define ALLOC_FAIL_STRUCT (1 << 1) #define GFX_MANAGER_ACTIVE 0xA3 // Arbitrary value -#define GFX_MANAGER_SPR_SIZE (MON_PIC_SIZE * 4) // * 4 is unnecessary, MON_PIC_SIZE is sufficient +#define GFX_MANAGER_SPR_SIZE (MON_PIC_SIZE * 4) // Only Castform uses more than MON_PIC_SIZE, despite not displaying its forms. #define GFX_MANAGER_NUM_FRAMES 4 // Only 2 frames are needed static void InitMonSpritesGfx_Battle(struct MonSpritesGfxManager* gfx)