Skip to content

Commit

Permalink
SPR_loadAllFrame(..) now detect duplicated tilesets and only load the…
Browse files Browse the repository at this point in the history
…m once in VRAM
  • Loading branch information
Stephane-D committed Nov 21, 2024
1 parent 01b8417 commit eda3c9d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
Binary file modified lib/libmd.a
Binary file not shown.
Binary file modified lib/libmd_debug.a
Binary file not shown.
58 changes: 52 additions & 6 deletions src/sprite_eng.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,27 @@ void NO_INLINE SPR_defragVRAM()
END_PROFIL(PROFIL_VRAM_DEFRAG)
}

static u16 getTilesetIndex(TileSet** tilesets, u16* indexes, TileSet* tileset, u16 index)
{
TileSet** t = tilesets;
u16* i = indexes;

// try fo find if tileset already exist
while(*t)
{
// find the tileset, return its index
if (*t == tileset) return *i;
// next
t++; i++;
}

// set new tileset index
*t = tileset;
*i = index;

return index;
}

u16** NO_INLINE SPR_loadAllFrames(const SpriteDefinition* sprDef, u16 index, u16* totalNumTile)
{
u16 numFrameTot = 0;
Expand All @@ -604,16 +625,32 @@ u16** NO_INLINE SPR_loadAllFrames(const SpriteDefinition* sprDef, u16 index, u16
// store total num tile if needed
if (totalNumTile) *totalNumTile = numTileTot;

// used to detect duplicate
TileSet** tilesets = MEM_alloc(numFrameTot * sizeof(TileSet*));
u16* tilesetIndexes = MEM_alloc(numFrameTot * sizeof(u16));
// allocate result table indexes[numAnim][numFrame]
u16** indexes = MEM_alloc((numAnimation * sizeof(u16*)) + (numFrameTot * sizeof(u16)));

// not enough memory
if ((tilesets == NULL) || (tilesetIndexes == NULL) || (indexes == NULL))
{
if (tilesets) MEM_free(tilesets);
if (tilesetIndexes) MEM_free(tilesetIndexes);
if (indexes) MEM_free(indexes);

return NULL;
}

// clear tilesets table
memset(tilesets, 0, numFrameTot * sizeof(TileSet*));

// store pointer
u16** result = indexes;
// init frames indexes pointer
u16* indFrames = (u16*) (indexes + numAnimation);

// start index
u16 tileInd = index;

anim = sprDef->animations;

for(u16 indAnim = 0; indAnim < numAnimation; indAnim++)
Expand All @@ -627,20 +664,29 @@ u16** NO_INLINE SPR_loadAllFrames(const SpriteDefinition* sprDef, u16 index, u16
for(u16 indFrame = 0; indFrame < numFrame; indFrame++)
{
const TileSet* tileset = (*frame)->tileset;
const u16 ind = getTilesetIndex(tilesets, tilesetIndexes, (TileSet*) tileset, tileInd);

// load tileset
VDP_loadTileSet(tileset, tileInd, DMA);
// new tileset ?
if (ind == tileInd)
{
// load tileset
VDP_loadTileSet(tileset, tileInd, DMA);
// next tileset
tileInd += tileset->numTile;
}
// store frame tile index
*indFrames++ = tileInd;
// next tileset
tileInd += tileset->numTile;
*indFrames++ = ind;
// next frame
frame++;
}

anim++;
}

MEM_free(tilesets);
MEM_free(tilesetIndexes);
MEM_pack();

return result;
}

Expand Down
58 changes: 52 additions & 6 deletions src/sprite_eng_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,27 @@ void NO_INLINE SPR_defragVRAM()
END_PROFIL(PROFIL_VRAM_DEFRAG)
}

static u16 getTilesetIndex(TileSet** tilesets, u16* indexes, TileSet* tileset, u16 index)
{
TileSet** t = tilesets;
u16* i = indexes;

// try fo find if tileset already exist
while(*t)
{
// find the tileset, return its index
if (*t == tileset) return *i;
// next
t++; i++;
}

// set new tileset index
*t = tileset;
*i = index;

return index;
}

u16** NO_INLINE SPR_loadAllFrames(const SpriteDefinition* sprDef, u16 index, u16* totalNumTile)
{
u16 numFrameTot = 0;
Expand All @@ -608,16 +629,32 @@ u16** NO_INLINE SPR_loadAllFrames(const SpriteDefinition* sprDef, u16 index, u16
// store total num tile if needed
if (totalNumTile) *totalNumTile = numTileTot;

// used to detect duplicate
TileSet** tilesets = MEM_alloc(numFrameTot * sizeof(TileSet*));
u16* tilesetIndexes = MEM_alloc(numFrameTot * sizeof(u16));
// allocate result table indexes[numAnim][numFrame]
u16** indexes = MEM_alloc((numAnimation * sizeof(u16*)) + (numFrameTot * sizeof(u16)));

// not enough memory
if ((tilesets == NULL) || (tilesetIndexes == NULL) || (indexes == NULL))
{
if (tilesets) MEM_free(tilesets);
if (tilesetIndexes) MEM_free(tilesetIndexes);
if (indexes) MEM_free(indexes);

return NULL;
}

// clear tilesets table
memset(tilesets, 0, numFrameTot * sizeof(TileSet*));

// store pointer
u16** result = indexes;
// init frames indexes pointer
u16* indFrames = (u16*) (indexes + numAnimation);

// start index
u16 tileInd = index;

anim = sprDef->animations;

for(u16 indAnim = 0; indAnim < numAnimation; indAnim++)
Expand All @@ -631,20 +668,29 @@ u16** NO_INLINE SPR_loadAllFrames(const SpriteDefinition* sprDef, u16 index, u16
for(u16 indFrame = 0; indFrame < numFrame; indFrame++)
{
const TileSet* tileset = (*frame)->tileset;
const u16 ind = getTilesetIndex(tilesets, tilesetIndexes, (TileSet*) tileset, tileInd);

// load tileset
VDP_loadTileSet(tileset, tileInd, DMA);
// new tileset ?
if (ind == tileInd)
{
// load tileset
VDP_loadTileSet(tileset, tileInd, DMA);
// next tileset
tileInd += tileset->numTile;
}
// store frame tile index
*indFrames++ = tileInd;
// next tileset
tileInd += tileset->numTile;
*indFrames++ = ind;
// next frame
frame++;
}

anim++;
}

MEM_free(tilesets);
MEM_free(tilesetIndexes);
MEM_pack();

return result;
}

Expand Down

0 comments on commit eda3c9d

Please sign in to comment.