New ROM method and decompression

Added reversal of address for command 4 of decompression for overworld
tile extraction
This commit is contained in:
Justin Scofield
2022-07-12 19:51:35 -04:00
parent bb056ee4d3
commit 6decbfa047
2 changed files with 40 additions and 12 deletions

View File

@@ -67,13 +67,12 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
length = length =
(ushort)(((current_rom_[pos] << 8) | current_rom_[pos + 1]) & 0x3FF); (ushort)(((current_rom_[pos] << 8) | current_rom_[pos + 1]) & 0x3FF);
pos += 2; // Advance 2 bytes in ROM pos += 2; // Advance 2 bytes in ROM
} else { // Normal Command } else { // Normal Command
cmd = (uchar)((databyte >> 5) & 0x07); cmd = (uchar)((databyte >> 5) & 0x07);
length = (uchar)(databyte & 0x1F); length = (uchar)(databyte & 0x1F);
pos += 1; // Advance 1 byte in ROM pos += 1; // Advance 1 byte in ROM
} }
length += 1; // Every commands are at least 1 size even if 00 length += 1; // each commands is at least of size 1 even if index 00
switch (cmd) { switch (cmd) {
case 00: // Direct Copy (Could be replaced with a MEMCPY) case 00: // Direct Copy (Could be replaced with a MEMCPY)
@@ -108,6 +107,9 @@ uchar *ROM::Decompress(int pos, int size, bool reversed) {
ushort s1 = ((current_rom_[pos + 1] & 0xFF) << 8); ushort s1 = ((current_rom_[pos + 1] & 0xFF) << 8);
ushort s2 = ((current_rom_[pos] & 0xFF)); ushort s2 = ((current_rom_[pos] & 0xFF));
auto Addr = (ushort)(s1 | s2); auto Addr = (ushort)(s1 | s2);
if (reversed) {
Addr = (ushort)(s2 | s1);
}
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
buffer[buffer_pos] = buffer[Addr + i]; buffer[buffer_pos] = buffer[Addr + i];
buffer_pos++; buffer_pos++;
@@ -210,12 +212,30 @@ SDL_Texture *ROM::DrawGraphicsSheet(int offset) {
return sheet_texture; return sheet_texture;
} }
gfx::SNESPalette ROM::ExtractPalette(uint addr, int bpp) { void ROM::DrawAllGraphicsData() {
uint filePos = addr; auto buffer = (uchar*) SDL_malloc(346624);
uint palette_size = pow(2, bpp); auto data = (uchar *)SDL_malloc(2048);
auto palette_data = (char *)SDL_malloc(sizeof(char) * (palette_size * 2)); int buffer_pos = 0;
memcpy(palette_data, current_rom_ + filePos, palette_size * 2);
return gfx::SNESPalette(palette_data); for (int i = 0; i < core::constants::NumberOfSheets; i++) {
// uncompressed sheets
if (i >= 115 && i <= 126) {
data = new char[core::constants::Uncompressed3BPPSize];
int startAddress = GetGraphicsAddress(i);
for (int j = 0; j < core::constants::Uncompressed3BPPSize; j++) {
data[j] = current_rom_[j + startAddress];
}
} else {
auto gfx_addr = GetGraphicsAddress(i);
data = Decompress(gfx_addr, core::constants::UncompressedSheetSize);
}
for (int j = 0; j < sizeof(data); j++) {
buffer[j + buffer_pos] = data[j];
}
buffer_pos += sizeof(data);
}
} }
// 0-112 -> compressed 3bpp bgr -> (decompressed each) 0x600 chars // 0-112 -> compressed 3bpp bgr -> (decompressed each) 0x600 chars
@@ -260,5 +280,13 @@ char *ROM::CreateAllGfxDataRaw() {
return buffer; return buffer;
} }
gfx::SNESPalette ROM::ExtractPalette(uint addr, int bpp) {
uint filePos = addr;
uint palette_size = pow(2, bpp);
auto palette_data = (char *)SDL_malloc(sizeof(char) * (palette_size * 2));
memcpy(palette_data, current_rom_ + filePos, palette_size * 2);
return gfx::SNESPalette(palette_data);
}
} // namespace app } // namespace app
} // namespace yaze } // namespace yaze

View File

@@ -27,16 +27,16 @@ class ROM {
public: public:
void Close(); void Close();
void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer); void SetupRenderer(std::shared_ptr<SDL_Renderer> renderer);
void LoadFromFile(const std::string& path); void LoadFromFile(const std::string& path);
uchar* Decompress(int pos, int size = 0x800, bool reversed = false); uchar* Decompress(int pos, int size = 0x800, bool reversed = false);
uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0, uchar* SNES3bppTo8bppSheet(uchar* buffer_in, int sheet_id = 0,
int size = 0x1000); int size = 0x1000);
uint GetGraphicsAddress(uint8_t id) const; uint GetGraphicsAddress(uint8_t id) const;
SDL_Texture* DrawGraphicsSheet(int offset); SDL_Texture* DrawGraphicsSheet(int offset);
gfx::SNESPalette ExtractPalette(uint addr, int bpp); void DrawAllGraphicsData();
char* CreateAllGfxDataRaw(); char* CreateAllGfxDataRaw();
gfx::SNESPalette ExtractPalette(uint addr, int bpp);
uchar* data() { return current_rom_; } uchar* data() { return current_rom_; }
auto Renderer() { return sdl_renderer_; } auto Renderer() { return sdl_renderer_; }