pokeemerald/src/decompress.c

305 lines
9.6 KiB
C
Raw Normal View History

2017-09-02 19:55:39 +02:00
#include "global.h"
#include "malloc.h"
2019-04-04 23:53:06 +02:00
#include "data.h"
#include "decompress.h"
2017-09-11 14:42:13 +02:00
#include "pokemon.h"
#include "pokemon_debug.h"
2019-04-04 23:53:06 +02:00
#include "text.h"
2017-09-02 19:55:39 +02:00
EWRAM_DATA ALIGNED(4) u8 gDecompressionBuffer[0x4000] = {0};
2018-11-07 19:35:31 +01:00
void LZDecompressWram(const u32 *src, void *dest)
2017-09-02 19:55:39 +02:00
{
LZ77UnCompWram(src, dest);
}
2018-11-07 19:35:31 +01:00
void LZDecompressVram(const u32 *src, void *dest)
2017-09-02 19:55:39 +02:00
{
LZ77UnCompVram(src, dest);
}
2018-12-17 23:00:08 +01:00
u16 LoadCompressedSpriteSheet(const struct CompressedSpriteSheet *src)
2017-09-02 19:55:39 +02:00
{
struct SpriteSheet dest;
LZ77UnCompWram(src->data, gDecompressionBuffer);
dest.data = gDecompressionBuffer;
dest.size = src->size;
dest.tag = src->tag;
return LoadSpriteSheet(&dest);
}
2018-12-17 23:00:08 +01:00
void LoadCompressedSpriteSheetOverrideBuffer(const struct CompressedSpriteSheet *src, void *buffer)
2017-09-02 19:55:39 +02:00
{
struct SpriteSheet dest;
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.size = src->size;
dest.tag = src->tag;
LoadSpriteSheet(&dest);
}
2018-12-17 23:00:08 +01:00
void LoadCompressedSpritePalette(const struct CompressedSpritePalette *src)
2017-09-02 19:55:39 +02:00
{
struct SpritePalette dest;
LZ77UnCompWram(src->data, gDecompressionBuffer);
2022-07-29 16:52:35 +02:00
dest.data = (void *) gDecompressionBuffer;
2017-09-02 19:55:39 +02:00
dest.tag = src->tag;
LoadSpritePalette(&dest);
}
2022-06-01 18:41:57 +02:00
void LoadCompressedSpritePaletteOverrideBuffer(const struct CompressedSpritePalette *src, void *buffer)
2017-09-02 19:55:39 +02:00
{
struct SpritePalette dest;
2022-06-01 18:41:57 +02:00
LZ77UnCompWram(src->data, buffer);
2017-09-02 19:55:39 +02:00
dest.data = buffer;
2022-06-01 18:41:57 +02:00
dest.tag = src->tag;
2017-09-02 19:55:39 +02:00
LoadSpritePalette(&dest);
}
2022-07-29 16:52:35 +02:00
void DecompressPicFromTable(const struct CompressedSpriteSheet *src, void *buffer, s32 species)
2017-09-02 19:55:39 +02:00
{
2018-12-30 17:58:42 +01:00
if (species > NUM_SPECIES)
LZ77UnCompWram(gMonFrontPicTable[SPECIES_NONE].data, buffer);
2017-09-02 19:55:39 +02:00
else
LZ77UnCompWram(src->data, buffer);
}
2021-09-11 06:30:09 +02:00
void DecompressPicFromTableGender(void* buffer, s32 species, u32 personality)
{
if (gMonFrontPicTableFemale[species].data != NULL && IsPersonalityFemale(species, personality))
LZ77UnCompWram(gMonFrontPicTableFemale[species].data, buffer);
else if (gMonFrontPicTable[species].data != NULL)
LZ77UnCompWram(gMonFrontPicTable[species].data, buffer);
2021-09-11 06:30:09 +02:00
else
LZ77UnCompWram(gMonFrontPicTable[SPECIES_NONE].data, buffer);
2021-09-11 06:30:09 +02:00
}
void HandleLoadSpecialPokePic(bool32 isFrontPic, void *dest, s32 species, u32 personality)
2017-09-02 19:55:39 +02:00
{
LoadSpecialPokePic(dest, species, personality, isFrontPic);
2017-09-02 19:55:39 +02:00
}
void LoadSpecialPokePic(void *dest, s32 species, u32 personality, bool8 isFrontPic)
2017-09-02 19:55:39 +02:00
{
if (species > NUM_SPECIES)
species = SPECIES_NONE;
else if (species == SPECIES_UNOWN)
species = GetUnownSpeciesId(personality);
2023-01-07 15:39:56 +01:00
if (isFrontPic)
{
if (gMonFrontPicTableFemale[species].data != NULL && IsPersonalityFemale(species, personality))
LZ77UnCompWram(gMonFrontPicTableFemale[species].data, dest);
else if (gMonFrontPicTable[species].data != NULL)
LZ77UnCompWram(gMonFrontPicTable[species].data, dest);
else
LZ77UnCompWram(gMonFrontPicTable[SPECIES_NONE].data, dest);
}
else
{
if (gMonBackPicTableFemale[species].data != NULL && IsPersonalityFemale(species, personality))
LZ77UnCompWram(gMonBackPicTableFemale[species].data, dest);
else if (gMonBackPicTable[species].data != NULL)
LZ77UnCompWram(gMonBackPicTable[species].data, dest);
else
LZ77UnCompWram(gMonBackPicTable[SPECIES_NONE].data, dest);
}
if (species == SPECIES_SPINDA && isFrontPic)
{
DrawSpindaSpots(personality, dest, FALSE);
DrawSpindaSpots(personality, dest, TRUE);
}
}
2017-09-02 19:55:39 +02:00
void Unused_LZDecompressWramIndirect(const void **src, void *dest)
{
LZ77UnCompWram(*src, dest);
}
static void StitchObjectsOn8x8Canvas(s32 object_size, s32 object_count, u8 *src_tiles, u8 *dest_tiles)
2017-09-02 19:55:39 +02:00
{
/*
This function appears to emulate behaviour found in the GB(C) versions regarding how the Pokemon images
are stitched together to be displayed on the battle screen.
Given "compacted" tiles, an object count and a bounding box/object size, place the tiles in such a way
that the result will have each object centered in a 8x8 tile canvas.
*/
s32 i, j, k, l;
u8 *src = src_tiles, *dest = dest_tiles;
u8 bottom_off;
if (object_size & 1)
{
// Object size is odd
bottom_off = (object_size >> 1) + 4;
for (l = 0; l < object_count; l++)
{
// Clear all unused rows of tiles plus the half-tile required due to centering
for (j = 0; j < 8-object_size; j++)
{
for (k = 0; k < 8; k++)
{
for (i = 0; i < 16; i++)
{
if (j % 2 == 0)
{
// Clear top half of top tile and bottom half of bottom tile when on even j
((dest+i) + (k << 5))[((j >> 1) << 8)] = 0;
((bottom_off << 8) + (dest+i) + (k << 5) + 16)[((j >> 1) << 8)] = 0;
}
else
{
// Clear bottom half of top tile and top half of tile following bottom tile when on odd j
((dest+i) + (k << 5) + 16)[((j >> 1) << 8)] = 0;
((bottom_off << 8) + (dest+i) + (k << 5) + 256)[((j >> 1) << 8)] = 0;
}
}
}
}
// Clear the columns to the left and right that wont be used completely
// Unlike the previous loops, this will clear the later used space as well
for (j = 0; j < 2; j++)
{
for (i = 0; i < 8; i++)
{
for (k = 0; k < 32; k++)
{
// Left side
((dest+k) + (i << 8))[(j << 5)] = 0;
// Right side
((dest+k) + (i << 8))[(j << 5)+192] = 0;
}
}
}
// Skip the top row and first tile on the second row for objects of size 5
if (object_size == 5) dest += 0x120;
// Copy tile data
for (j = 0; j < object_size; j++)
{
for (k = 0; k < object_size; k++)
{
for (i = 0; i < 4; i++)
{
// Offset the tile by +4px in both x and y directions
(dest + (i << 2))[18] = (src + (i << 2))[0];
(dest + (i << 2))[19] = (src + (i << 2))[1];
(dest + (i << 2))[48] = (src + (i << 2))[2];
(dest + (i << 2))[49] = (src + (i << 2))[3];
(dest + (i << 2))[258] = (src + (i << 2))[16];
(dest + (i << 2))[259] = (src + (i << 2))[17];
(dest + (i << 2))[288] = (src + (i << 2))[18];
(dest + (i << 2))[289] = (src + (i << 2))[19];
}
src += 32;
dest += 32;
}
// At the end of a row, skip enough tiles to get to the beginning of the next row
if (object_size == 7) dest += 0x20;
else if (object_size == 5) dest += 0x60;
}
// Skip remaining unused space to go to the beginning of the next object
if (object_size == 7) dest += 0x100;
else if (object_size == 5) dest += 0x1e0;
}
}
else
{
// Object size is even
for (i = 0; i < object_count; i++)
{
// For objects of size 6, the first and last row and column will be cleared
// While the remaining space will be filled with actual data
if (object_size == 6)
{
for (k = 0; k < 256; k++) {
*dest = 0;
dest++;
}
}
for (j = 0; j < object_size; j++)
{
if (object_size == 6)
{
for (k = 0; k < 32; k++) {
*dest = 0;
dest++;
}
}
// Copy tile data
for (k = 0; k < 32 * object_size; k++) {
*dest = *src;
src++;
dest++;
}
if (object_size == 6)
{
for (k = 0; k < 32; k++) {
*dest = 0;
dest++;
}
}
}
if (object_size == 6)
{
for (k = 0; k < 256; k++) {
*dest = 0;
dest++;
}
}
}
}
2017-09-02 19:55:39 +02:00
}
2018-11-07 19:35:31 +01:00
u32 GetDecompressedDataSize(const u32 *ptr)
2017-09-02 19:55:39 +02:00
{
const u8 *ptr8 = (const u8 *)ptr;
2018-11-07 19:35:31 +01:00
return (ptr8[3] << 16) | (ptr8[2] << 8) | (ptr8[1]);
2017-09-02 19:55:39 +02:00
}
2022-07-29 17:15:33 +02:00
bool8 LoadCompressedSpriteSheetUsingHeap(const struct CompressedSpriteSheet *src)
2017-09-02 19:55:39 +02:00
{
struct SpriteSheet dest;
2022-07-29 16:52:35 +02:00
void *buffer;
2017-09-02 19:55:39 +02:00
2022-09-29 17:33:39 +02:00
buffer = AllocZeroed(src->data[0] >> 8);
2017-09-02 19:55:39 +02:00
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.size = src->size;
dest.tag = src->tag;
LoadSpriteSheet(&dest);
Free(buffer);
return FALSE;
}
2018-12-17 23:00:08 +01:00
bool8 LoadCompressedSpritePaletteUsingHeap(const struct CompressedSpritePalette *src)
2017-09-02 19:55:39 +02:00
{
struct SpritePalette dest;
2022-07-29 16:52:35 +02:00
void *buffer;
2017-09-02 19:55:39 +02:00
2022-09-29 17:33:39 +02:00
buffer = AllocZeroed(src->data[0] >> 8);
2017-09-02 19:55:39 +02:00
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.tag = src->tag;
LoadSpritePalette(&dest);
Free(buffer);
return FALSE;
}