pokeemerald/src/item_icon.c
2018-07-07 14:24:19 +02:00

170 lines
4.4 KiB
C

#include "global.h"
#include "item_icon.h"
#include "malloc.h"
#include "sprite.h"
#include "decompress.h"
#include "constants/items.h"
#include "data/item_icon_table.h"
// EWRAM vars
EWRAM_DATA void *gItemIconDecompressionBuffer = NULL;
EWRAM_DATA void *gItemIcon4x4Buffer = NULL;
// const rom data
static const struct OamData sOamData_ItemIcon =
{
.y = 0,
.affineMode = 0,
.objMode = 0,
.mosaic = 0,
.bpp = 0,
.shape = 0,
.x = 0,
.matrixNum = 0,
.size = 2,
.tileNum = 0,
.priority = 1,
.paletteNum = 2,
.affineParam = 0
};
static const union AnimCmd sSpriteAnim_ItemIcon[] =
{
ANIMCMD_FRAME(0, 0),
ANIMCMD_END
};
static const union AnimCmd *const sSpriteAnimTable_ItemIcon[] =
{
sSpriteAnim_ItemIcon
};
const struct SpriteTemplate gItemIconSpriteTemplate =
{
.tileTag = 0,
.paletteTag = 0,
.oam = &sOamData_ItemIcon,
.anims = sSpriteAnimTable_ItemIcon,
.images = NULL,
.affineAnims = gDummySpriteAffineAnimTable,
.callback = SpriteCallbackDummy,
};
// code
bool8 AllocItemIconTemporaryBuffers(void)
{
gItemIconDecompressionBuffer = gItemIconDecompressionBuffer; // needed to match
gItemIconDecompressionBuffer = Alloc(0x120);
if (gItemIconDecompressionBuffer == NULL)
return FALSE;
gItemIcon4x4Buffer = gItemIcon4x4Buffer; // needed to match
gItemIcon4x4Buffer = AllocZeroed(0x200);
if (gItemIcon4x4Buffer == NULL)
{
Free(gItemIconDecompressionBuffer);
return FALSE;
}
return TRUE;
}
void FreeItemIconTemporaryBuffers(void)
{
Free(gItemIconDecompressionBuffer);
Free(gItemIcon4x4Buffer);
}
void CopyItemIconPicTo4x4Buffer(const void *src, void *dest)
{
u8 i;
for (i = 0; i < 3; i++)
CpuCopy16(src + i * 96, dest + i * 128, 0x60);
}
u8 AddItemIconSprite(u16 tilesTag, u16 paletteTag, u16 itemId)
{
if (!AllocItemIconTemporaryBuffers())
{
return MAX_SPRITES;
}
else
{
u8 spriteId;
struct SpriteSheet spriteSheet;
struct CompressedSpritePalette spritePalette;
struct SpriteTemplate *spriteTemplate;
LZDecompressWram(GetItemIconPicOrPalette(itemId, 0), gItemIconDecompressionBuffer);
CopyItemIconPicTo4x4Buffer(gItemIconDecompressionBuffer, gItemIcon4x4Buffer);
spriteSheet.data = gItemIcon4x4Buffer;
spriteSheet.size = 0x200;
spriteSheet.tag = tilesTag;
LoadSpriteSheet(&spriteSheet);
spritePalette.data = GetItemIconPicOrPalette(itemId, 1);
spritePalette.tag = paletteTag;
LoadCompressedObjectPalette(&spritePalette);
spriteTemplate = Alloc(sizeof(*spriteTemplate));
CpuCopy16(&gItemIconSpriteTemplate, spriteTemplate, sizeof(*spriteTemplate));
spriteTemplate->tileTag = tilesTag;
spriteTemplate->paletteTag = paletteTag;
spriteId = CreateSprite(spriteTemplate, 0, 0, 0);
FreeItemIconTemporaryBuffers();
Free(spriteTemplate);
return spriteId;
}
}
u8 AddCustomItemIconSprite(struct SpriteTemplate *customSpriteTemplate, u16 tilesTag, u16 paletteTag, u16 itemId)
{
if (!AllocItemIconTemporaryBuffers())
{
return MAX_SPRITES;
}
else
{
u8 spriteId;
struct SpriteSheet spriteSheet;
struct CompressedSpritePalette spritePalette;
struct SpriteTemplate *spriteTemplate;
LZDecompressWram(GetItemIconPicOrPalette(itemId, 0), gItemIconDecompressionBuffer);
CopyItemIconPicTo4x4Buffer(gItemIconDecompressionBuffer, gItemIcon4x4Buffer);
spriteSheet.data = gItemIcon4x4Buffer;
spriteSheet.size = 0x200;
spriteSheet.tag = tilesTag;
LoadSpriteSheet(&spriteSheet);
spritePalette.data = GetItemIconPicOrPalette(itemId, 1);
spritePalette.tag = paletteTag;
LoadCompressedObjectPalette(&spritePalette);
spriteTemplate = Alloc(sizeof(*spriteTemplate));
CpuCopy16(customSpriteTemplate, spriteTemplate, sizeof(*spriteTemplate));
spriteTemplate->tileTag = tilesTag;
spriteTemplate->paletteTag = paletteTag;
spriteId = CreateSprite(spriteTemplate, 0, 0, 0);
FreeItemIconTemporaryBuffers();
Free(spriteTemplate);
return spriteId;
}
}
const void *GetItemIconPicOrPalette(u16 itemId, u8 which)
{
if (itemId == 0xFFFF)
itemId = ITEM_FIELD_ARROW;
else if (itemId >= ITEMS_COUNT)
itemId = 0;
return gItemIconTable[itemId][which];
}