Merge branch 'optimise-summary' into 'main'

chore: optimise the summary screen

See merge request tbld/game!5
This commit is contained in:
Skye Chappelle 2022-05-24 14:29:46 +00:00
commit d754515c78
3 changed files with 45 additions and 89 deletions

View File

@ -133,12 +133,9 @@ struct Pokemon
struct MonSpritesGfxManager struct MonSpritesGfxManager
{ {
u32 numSprites:4; u8 numSprites;
u32 numSprites2:4; // Never read u8 numFrames;
u32 numFrames:8; bool16 active;
u32 active:8;
u32 dataSize:4;
u32 mode:4; // MON_SPR_GFX_MODE_*
void *spriteBuffer; void *spriteBuffer;
u8 **spritePointers; u8 **spritePointers;
struct SpriteTemplate *templates; struct SpriteTemplate *templates;
@ -436,8 +433,8 @@ void HandleSetPokedexFlag(u16 nationalNum, u8 caseId, u32 personality);
const u8 *GetTrainerClassNameFromId(u16 trainerId); const u8 *GetTrainerClassNameFromId(u16 trainerId);
const u8 *GetTrainerNameFromId(u16 trainerId); const u8 *GetTrainerNameFromId(u16 trainerId);
bool8 HasTwoFramesAnimation(u16 species); bool8 HasTwoFramesAnimation(u16 species);
struct MonSpritesGfxManager *CreateMonSpritesGfxManager(u8 managerId, u8 mode); struct MonSpritesGfxManager *CreateMonSpritesGfxManager(void);
void DestroyMonSpritesGfxManager(u8 managerId); void DestroyMonSpritesGfxManager(void);
u8 *MonSpritesGfxManager_GetSpritePtr(u8 managerId, u8 spriteNum); u8 *MonSpritesGfxManager_GetSpritePtr(u8 spriteNum);
#endif // GUARD_POKEMON_H #endif // GUARD_POKEMON_H

View File

@ -69,7 +69,7 @@ EWRAM_DATA u8 gEnemyPartyCount = 0;
EWRAM_DATA struct Pokemon gPlayerParty[PARTY_SIZE] = {0}; EWRAM_DATA struct Pokemon gPlayerParty[PARTY_SIZE] = {0};
EWRAM_DATA struct Pokemon gEnemyParty[PARTY_SIZE] = {0}; EWRAM_DATA struct Pokemon gEnemyParty[PARTY_SIZE] = {0};
EWRAM_DATA struct SpriteTemplate gMultiuseSpriteTemplate = {0}; EWRAM_DATA struct SpriteTemplate gMultiuseSpriteTemplate = {0};
EWRAM_DATA static struct MonSpritesGfxManager *sMonSpritesGfxManagers[MON_SPR_GFX_MANAGERS_COUNT] = {NULL}; EWRAM_DATA static struct MonSpritesGfxManager *sMonSpritesGfxManager = NULL;
#include "data/battle_moves.h" #include "data/battle_moves.h"
@ -3468,12 +3468,8 @@ void SetMultiuseSpriteTemplateToPokemon(u16 speciesTag, u8 battlerPosition)
{ {
if (gMonSpritesGfxPtr != NULL) if (gMonSpritesGfxPtr != NULL)
gMultiuseSpriteTemplate = gMonSpritesGfxPtr->templates[battlerPosition]; gMultiuseSpriteTemplate = gMonSpritesGfxPtr->templates[battlerPosition];
else if (sMonSpritesGfxManagers[MON_SPR_GFX_MANAGER_A]) else if (sMonSpritesGfxManager)
gMultiuseSpriteTemplate = sMonSpritesGfxManagers[MON_SPR_GFX_MANAGER_A]->templates[battlerPosition]; gMultiuseSpriteTemplate = sMonSpritesGfxManager->templates[battlerPosition];
else if (sMonSpritesGfxManagers[MON_SPR_GFX_MANAGER_B])
gMultiuseSpriteTemplate = sMonSpritesGfxManagers[MON_SPR_GFX_MANAGER_B]->templates[battlerPosition];
else
gMultiuseSpriteTemplate = gBattlerSpriteTemplates[battlerPosition];
gMultiuseSpriteTemplate.paletteTag = speciesTag; gMultiuseSpriteTemplate.paletteTag = speciesTag;
if (battlerPosition == B_POSITION_PLAYER_LEFT || battlerPosition == B_POSITION_PLAYER_RIGHT) if (battlerPosition == B_POSITION_PLAYER_LEFT || battlerPosition == B_POSITION_PLAYER_RIGHT)
@ -6882,11 +6878,11 @@ static bool8 ShouldSkipFriendshipChange(void)
#define ALLOC_FAIL_STRUCT (1 << 1) #define ALLOC_FAIL_STRUCT (1 << 1)
#define GFX_MANAGER_ACTIVE 0xA3 // Arbitrary value #define GFX_MANAGER_ACTIVE 0xA3 // Arbitrary value
#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_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 #define GFX_MANAGER_NUM_FRAMES 2 // Only 2 frames are needed, so only 2 frames are used in tumbledemerald
static void InitMonSpritesGfx_Battle(struct MonSpritesGfxManager* gfx) static void InitMonSpritesGfx_Battle(struct MonSpritesGfxManager* gfx)
{ {
u16 i, j; u32 i, j;
for (i = 0; i < gfx->numSprites; i++) for (i = 0; i < gfx->numSprites; i++)
{ {
gfx->templates[i] = gBattlerSpriteTemplates[i]; gfx->templates[i] = gBattlerSpriteTemplates[i];
@ -6912,41 +6908,21 @@ static void InitMonSpritesGfx_FullParty(struct MonSpritesGfxManager* gfx)
} }
} }
struct MonSpritesGfxManager *CreateMonSpritesGfxManager(u8 managerId, u8 mode) struct MonSpritesGfxManager *CreateMonSpritesGfxManager(void)
{ {
u8 i; u32 i;
u8 failureFlags; u8 failureFlags;
struct MonSpritesGfxManager *gfx; struct MonSpritesGfxManager *gfx;
failureFlags = 0; failureFlags = 0;
managerId %= MON_SPR_GFX_MANAGERS_COUNT;
gfx = AllocZeroed(sizeof(*gfx)); gfx = AllocZeroed(sizeof(*gfx));
if (gfx == NULL) if (gfx == NULL)
return NULL; return NULL;
switch (mode)
{
case MON_SPR_GFX_MODE_FULL_PARTY:
gfx->numSprites = PARTY_SIZE + 1;
gfx->numSprites2 = PARTY_SIZE + 1;
gfx->numFrames = GFX_MANAGER_NUM_FRAMES;
gfx->dataSize = 1;
gfx->mode = MON_SPR_GFX_MODE_FULL_PARTY;
break;
// case MON_SPR_GFX_MODE_BATTLE:
case MON_SPR_GFX_MODE_NORMAL:
default:
gfx->numSprites = MAX_BATTLERS_COUNT; gfx->numSprites = MAX_BATTLERS_COUNT;
gfx->numSprites2 = MAX_BATTLERS_COUNT;
gfx->numFrames = GFX_MANAGER_NUM_FRAMES; gfx->numFrames = GFX_MANAGER_NUM_FRAMES;
gfx->dataSize = 1; gfx->spriteBuffer = AllocZeroed(GFX_MANAGER_SPR_SIZE * gfx->numSprites);
gfx->mode = MON_SPR_GFX_MODE_NORMAL; gfx->spritePointers = AllocZeroed(gfx->numSprites * 4);
break;
}
// Set up sprite / sprite pointer buffers
gfx->spriteBuffer = AllocZeroed(gfx->dataSize * GFX_MANAGER_SPR_SIZE * gfx->numSprites);
gfx->spritePointers = AllocZeroed(gfx->numSprites * 32); // ? Only * 4 is necessary, perhaps they were thinking bits.
if (gfx->spriteBuffer == NULL || gfx->spritePointers == NULL) if (gfx->spriteBuffer == NULL || gfx->spritePointers == NULL)
{ {
failureFlags |= ALLOC_FAIL_BUFFER; failureFlags |= ALLOC_FAIL_BUFFER;
@ -6954,7 +6930,7 @@ struct MonSpritesGfxManager *CreateMonSpritesGfxManager(u8 managerId, u8 mode)
else else
{ {
for (i = 0; i < gfx->numSprites; i++) for (i = 0; i < gfx->numSprites; i++)
gfx->spritePointers[i] = gfx->spriteBuffer + (gfx->dataSize * GFX_MANAGER_SPR_SIZE * i); gfx->spritePointers[i] = gfx->spriteBuffer + (GFX_MANAGER_SPR_SIZE * i);
} }
// Set up sprite structs // Set up sprite structs
@ -6968,18 +6944,7 @@ struct MonSpritesGfxManager *CreateMonSpritesGfxManager(u8 managerId, u8 mode)
{ {
for (i = 0; i < gfx->numFrames * gfx->numSprites; i++) for (i = 0; i < gfx->numFrames * gfx->numSprites; i++)
gfx->frameImages[i].size = MON_PIC_SIZE; gfx->frameImages[i].size = MON_PIC_SIZE;
switch (gfx->mode)
{
case MON_SPR_GFX_MODE_FULL_PARTY:
InitMonSpritesGfx_FullParty(gfx);
break;
case MON_SPR_GFX_MODE_NORMAL:
case MON_SPR_GFX_MODE_BATTLE:
default:
InitMonSpritesGfx_Battle(gfx); InitMonSpritesGfx_Battle(gfx);
break;
}
} }
// If either of the allocations failed free their respective members // If either of the allocations failed free their respective members
@ -7002,27 +6967,21 @@ struct MonSpritesGfxManager *CreateMonSpritesGfxManager(u8 managerId, u8 mode)
} }
else else
{ {
gfx->active = GFX_MANAGER_ACTIVE; gfx->active = TRUE;
sMonSpritesGfxManagers[managerId] = gfx; sMonSpritesGfxManager = gfx;
} }
return sMonSpritesGfxManagers[managerId]; return sMonSpritesGfxManager;
} }
void DestroyMonSpritesGfxManager(u8 managerId) void DestroyMonSpritesGfxManager(void)
{ {
struct MonSpritesGfxManager *gfx; struct MonSpritesGfxManager *gfx = sMonSpritesGfxManager;
managerId %= MON_SPR_GFX_MANAGERS_COUNT;
gfx = sMonSpritesGfxManagers[managerId];
if (gfx == NULL) if (gfx == NULL)
return; return;
if (gfx->active != GFX_MANAGER_ACTIVE) if (gfx->active)
{
memset(gfx, 0, sizeof(*gfx));
}
else
{ {
TRY_FREE_AND_SET_NULL(gfx->frameImages); TRY_FREE_AND_SET_NULL(gfx->frameImages);
TRY_FREE_AND_SET_NULL(gfx->templates); TRY_FREE_AND_SET_NULL(gfx->templates);
@ -7031,20 +6990,20 @@ void DestroyMonSpritesGfxManager(u8 managerId)
memset(gfx, 0, sizeof(*gfx)); memset(gfx, 0, sizeof(*gfx));
Free(gfx); Free(gfx);
} }
else
memset(gfx, 0, sizeof(*gfx));
} }
u8 *MonSpritesGfxManager_GetSpritePtr(u8 managerId, u8 spriteNum) u8 *MonSpritesGfxManager_GetSpritePtr(u8 spriteNum)
{ {
struct MonSpritesGfxManager *gfx = sMonSpritesGfxManagers[managerId % MON_SPR_GFX_MANAGERS_COUNT]; struct MonSpritesGfxManager *gfx = sMonSpritesGfxManager;
if (gfx->active != GFX_MANAGER_ACTIVE)
{ if (gfx->active)
return NULL;
}
else
{ {
if (spriteNum >= gfx->numSprites) if (spriteNum >= gfx->numSprites)
spriteNum = 0; spriteNum = 0;
return gfx->spritePointers[spriteNum]; return gfx->spritePointers[spriteNum];
} }
return NULL;
} }

View File

@ -1111,7 +1111,7 @@ void ShowPokemonSummaryScreen(u8 mode, void *mons, u8 monIndex, u8 maxMonIndex,
SummaryScreen_SetAnimDelayTaskId(TASK_NONE); SummaryScreen_SetAnimDelayTaskId(TASK_NONE);
if (gMonSpritesGfxPtr == NULL) if (gMonSpritesGfxPtr == NULL)
CreateMonSpritesGfxManager(MON_SPR_GFX_MANAGER_A, MON_SPR_GFX_MODE_NORMAL); CreateMonSpritesGfxManager();
SetMainCallback2(CB2_InitSummaryScreen); SetMainCallback2(CB2_InitSummaryScreen);
} }
@ -1500,7 +1500,7 @@ static void CloseSummaryScreen(u8 taskId)
StopCryAndClearCrySongs(); StopCryAndClearCrySongs();
m4aMPlayVolumeControl(&gMPlayInfo_BGM, TRACKS_ALL, 0x100); m4aMPlayVolumeControl(&gMPlayInfo_BGM, TRACKS_ALL, 0x100);
if (gMonSpritesGfxPtr == NULL) if (gMonSpritesGfxPtr == NULL)
DestroyMonSpritesGfxManager(MON_SPR_GFX_MANAGER_A); DestroyMonSpritesGfxManager();
FreeSummaryScreen(); FreeSummaryScreen();
DestroyTask(taskId); DestroyTask(taskId);
} }
@ -3902,12 +3902,12 @@ static u8 LoadMonGfxAndSprite(struct Pokemon *mon, s16 *state)
{ {
if (sMonSummaryScreen->monList.mons == gPlayerParty || sMonSummaryScreen->mode == SUMMARY_MODE_BOX || sMonSummaryScreen->unk40EF == TRUE) if (sMonSummaryScreen->monList.mons == gPlayerParty || sMonSummaryScreen->mode == SUMMARY_MODE_BOX || sMonSummaryScreen->unk40EF == TRUE)
HandleLoadSpecialPokePic_2(&gMonFrontPicTable[summary->species2], HandleLoadSpecialPokePic_2(&gMonFrontPicTable[summary->species2],
MonSpritesGfxManager_GetSpritePtr(MON_SPR_GFX_MANAGER_A, B_POSITION_OPPONENT_LEFT), MonSpritesGfxManager_GetSpritePtr(B_POSITION_OPPONENT_LEFT),
summary->species2, summary->species2,
summary->pid); summary->pid);
else else
HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonFrontPicTable[summary->species2], HandleLoadSpecialPokePic_DontHandleDeoxys(&gMonFrontPicTable[summary->species2],
MonSpritesGfxManager_GetSpritePtr(MON_SPR_GFX_MANAGER_A, B_POSITION_OPPONENT_LEFT), MonSpritesGfxManager_GetSpritePtr(B_POSITION_OPPONENT_LEFT),
summary->species2, summary->species2,
summary->pid); summary->pid);
} }