Add Gen1GameReader::getPokemonIconType()

This commit is contained in:
risingPhil 2024-09-24 22:52:26 +02:00
parent 5d8209ba54
commit 04bc493c03
4 changed files with 61 additions and 27 deletions

View File

@ -12,37 +12,30 @@
using OutputFormat = SpriteRenderer::OutputFormat;
using TileOrder = SpriteRenderer::TileOrder;
void decodeGen1Icons(IRomReader& romReader, ISaveManager& saveManager, Gen1GameType gen1Type)
void decodeGen1Icon(IRomReader& romReader, ISaveManager& saveManager, Gen1GameType gen1Type, Gen1PokemonIconType iconType, bool firstFrame)
{
Gen1GameReader gameReader(romReader, saveManager, gen1Type);
SpriteRenderer renderer;
char fileNameBuf[100];
PokemonIconType iconType;
uint8_t *outputBuffer;
bool firstFrame;
outputBuffer = gameReader.decodePokemonIcon(iconType, renderer, OutputFormat::RGB, firstFrame);
if(!outputBuffer)
{
fprintf(stderr, "ERROR: Could not decode icon for icon type %d and firstFrame %d!\n", (int)iconType, firstFrame);
return;
}
snprintf(fileNameBuf, sizeof(fileNameBuf), "%d_frame%u.png", (int)iconType, (firstFrame) ? 1 : 2);
write_png(fileNameBuf, outputBuffer, 2 * 8, 2 * 8, false);
}
void decodeGen1Icons(IRomReader& romReader, ISaveManager& saveManager, Gen1GameType gen1Type)
{
for(int i=0; i < GEN1_ICONTYPE_MAX; ++i)
{
iconType = (PokemonIconType)i;
firstFrame = true;
outputBuffer = gameReader.decodePokemonIcon(iconType, renderer, OutputFormat::RGB, firstFrame);
if(!outputBuffer)
{
fprintf(stderr, "ERROR: Could not decode icon for icon type %d and firstFrame %d!\n", (int)iconType, firstFrame);
continue;
}
snprintf(fileNameBuf, sizeof(fileNameBuf), "%d_frame1.png", i);
write_png(fileNameBuf, outputBuffer, 2 * 8, 2 * 8, false);
firstFrame = false;
outputBuffer = gameReader.decodePokemonIcon(iconType, renderer, OutputFormat::RGB, firstFrame);
if(!outputBuffer)
{
fprintf(stderr, "ERROR: Could not decode icon for type %d and firstFrame %d!\n", (int)iconType, firstFrame);
continue;
}
snprintf(fileNameBuf, sizeof(fileNameBuf), "%d_frame2.png", i);
write_png(fileNameBuf, outputBuffer, 2 * 8, 2 * 8, false);
decodeGen1Icon(romReader, saveManager, gen1Type, (Gen1PokemonIconType)i, true);
decodeGen1Icon(romReader, saveManager, gen1Type, (Gen1PokemonIconType)i, false);
}
}

View File

@ -87,7 +87,7 @@ enum Gen1PokeType
GEN1_PT_DRAGON = 0x1A
};
enum PokemonIconType
enum Gen1PokemonIconType
{
GEN1_ICONTYPE_MON = 0,
GEN1_ICONTYPE_BALL,

View File

@ -28,6 +28,11 @@ public:
*/
uint8_t getPokemonNumber(uint8_t index) const;
/**
* @brief This function returns the Gen1PokemonIconType for a pokemon at the given index
*/
Gen1PokemonIconType getPokemonIconType(uint8_t index) const;
/**
* @brief With this function, you can check if the index is valid and not referring to missingno
*/
@ -118,7 +123,7 @@ public:
* @brief This function decodes the given pokemon icon and returns the internal buffer of the given SpriteRenderer instance
* The size of the returned buffer is 16x16 pixels
*/
uint8_t* decodePokemonIcon(PokemonIconType iconType, SpriteRenderer& renderer, SpriteRenderer::OutputFormat outputFormat, bool firstFrame = true);
uint8_t* decodePokemonIcon(Gen1PokemonIconType iconType, SpriteRenderer& renderer, SpriteRenderer::OutputFormat outputFormat, bool firstFrame = true);
/**
* @brief Adds a pokemon to the save. Tries to add it to the party first. If there's no more room there, it tries to add it to the

View File

@ -99,6 +99,42 @@ uint8_t Gen1GameReader::getPokemonNumber(uint8_t index) const
return result;
}
Gen1PokemonIconType Gen1GameReader::getPokemonIconType(uint8_t index) const
{
//MonPartyData in pret/pokered and pret/pokeyellow
// strangely, this array is in pokemon _number_ order, not index
uint8_t number = getPokemonNumber(index);
const uint32_t romOffset = (gameType_ == Gen1GameType::YELLOW) ? 0x719BA : 0x7190D;
uint8_t byteVal;
Gen1PokemonIconType result;
if(number == 0xFF)
{
// invalid number!
return Gen1PokemonIconType::GEN1_ICONTYPE_MAX;
}
// number is 1-based, but the array/offset is obviously 0-based. So subtract one
--number;
romReader_.seek(romOffset);
// MonPartyData is a nybble (4 bit) array.
romReader_.advance((number / 2));
romReader_.readByte(byteVal);
if((number & 0x1) == 0)
{
// if the number is even, the upper 4 bits need to be used
result = (Gen1PokemonIconType)(byteVal >> 4);
}
else
{
// if odd, the lower 4 bits need to be used
result = (Gen1PokemonIconType)(byteVal & 0x0F);
}
return result;
}
bool Gen1GameReader::isValidIndex(uint8_t index) const
{
switch(index)
@ -432,7 +468,7 @@ uint8_t* Gen1GameReader::decodeSprite(uint8_t bankIndex, uint16_t pointer)
*
* Anyway, we need to deal with this mess and that makes our code below a bit messy as well.
*/
uint8_t* Gen1GameReader::decodePokemonIcon(PokemonIconType iconType, SpriteRenderer& renderer, SpriteRenderer::OutputFormat outputFormat, bool firstFrame)
uint8_t* Gen1GameReader::decodePokemonIcon(Gen1PokemonIconType iconType, SpriteRenderer& renderer, SpriteRenderer::OutputFormat outputFormat, bool firstFrame)
{
const uint32_t romOffset = (gameType_ == Gen1GameType::YELLOW) ? 0x7184D : 0x717C0;
const uint8_t MAX_NUM_TILES = 8;
@ -518,7 +554,7 @@ uint8_t* Gen1GameReader::decodePokemonIcon(PokemonIconType iconType, SpriteRende
if(entryIndex == 0xFF)
{
if(iconType == PokemonIconType::GEN1_ICONTYPE_HELIX && firstFrame)
if(iconType == Gen1PokemonIconType::GEN1_ICONTYPE_HELIX && firstFrame)
{
// The helix sprite is not part of the MonPartySpritePointers table for some reason
bankIndex = 4;