Add tile loading and finish up decompressions.

master
sigonasr2 5 months ago
parent 45efe714d5
commit 15e8df7c01
  1. 1
      .gitignore
  2. 56
      EarthboundBattleBackgrounds/EarthboundBattleBackgrounds/main.cpp
  3. BIN
      EarthboundBattleBackgrounds/x64/Debug/EarthboundBattleBackgrounds.exe
  4. BIN
      EarthboundBattleBackgrounds/x64/Debug/EarthboundBattleBackgrounds.pdb

1
.gitignore vendored

@ -5,3 +5,4 @@
/EarthboundBattleBackgrounds/.vs/EarthboundBattleBackgrounds /EarthboundBattleBackgrounds/.vs/EarthboundBattleBackgrounds
/EarthboundBattleBackgrounds/EarthboundBattleBackgrounds/x64/Debug /EarthboundBattleBackgrounds/EarthboundBattleBackgrounds/x64/Debug
/.vs/EarthboundBattleBackgrounds/v17 /.vs/EarthboundBattleBackgrounds/v17
/EarthboundBattleBackgrounds/x64/Debug/EarthboundBattleBackgrounds.pdb

@ -40,8 +40,8 @@ struct Rom{
struct DataBlock{ struct DataBlock{
uint32_t addr,ptr; uint32_t addr,ptr;
std::string_view data; std::u8string_view data;
DataBlock(std::string_view data,uint32_t loc) DataBlock(std::u8string_view data,uint32_t loc)
:data(data){ :data(data){
addr=ptr=loc; addr=ptr=loc;
} }
@ -55,9 +55,9 @@ struct Rom{
return readInt8()|(readInt8()<<8)|(readInt8()<<16)|(readInt8()<<24); return readInt8()|(readInt8()<<8)|(readInt8()<<16)|(readInt8()<<24);
} }
uint32_t getCompressedSize(uint32_t pointer){ uint32_t getCompressedSize(uint32_t pointer){
uint32_t bpos{}; int32_t bpos{};
uint32_t pos{pointer}; int32_t pos{int32_t(pointer)};
uint32_t bpos2{}; int32_t bpos2{};
while(data[pos]!=0xFF){ while(data[pos]!=0xFF){
if(pos>=data.length())throw std::runtime_error{"Unexpected end of data."}; if(pos>=data.length())throw std::runtime_error{"Unexpected end of data."};
char commandType{data[pos]>>5}; char commandType{data[pos]>>5};
@ -120,10 +120,10 @@ struct Rom{
private: private:
void decompressBlock(uint32_t ptrStart,std::vector<uint16_t>&block){ void decompressBlock(uint32_t ptrStart,std::vector<uint16_t>&block){
size_t maxLength{block.size()}; size_t maxLength{block.size()};
uint32_t pos{ptrStart}; int32_t pos{int32_t(ptrStart)};
uint32_t bpos{}; int32_t bpos{};
uint32_t bpos2{}; int32_t bpos2{};
char tmp; int tmp;
int read{}; int read{};
while(data[pos]!=0xFF){ while(data[pos]!=0xFF){
if (pos >= data.length())return; if (pos >= data.length())return;
@ -135,6 +135,7 @@ struct Rom{
++pos; ++pos;
} }
if(bpos+len>maxLength||bpos+len<0)return; if(bpos+len>maxLength||bpos+len<0)return;
++pos;
if(commandType>=4) { if(commandType>=4) {
bpos2=(data[pos]<<8)+data[pos+1]; bpos2=(data[pos]<<8)+data[pos+1];
if(bpos2>=maxLength||bpos2<0)return; if(bpos2>=maxLength||bpos2<0)return;
@ -180,7 +181,7 @@ struct Rom{
}; };
struct BattleBackground{ struct BattleBackground{
BattleBackground(std::string_view data,uint32_t index){ BattleBackground(std::u8string_view data,uint32_t index){
DataBlock readData{data,0xDCA1+index*uint32_t(this->data.size())}; DataBlock readData{data,0xDCA1+index*uint32_t(this->data.size())};
for(int i=0;i<this->data.size();i++){ for(int i=0;i<this->data.size();i++){
this->data[i]=readData.readInt8(); this->data[i]=readData.readInt8();
@ -213,7 +214,7 @@ struct Rom{
std::vector<Pixel>colors; std::vector<Pixel>colors;
byte bpp; byte bpp;
uint32_t addr; uint32_t addr;
BackgroundPalette(std::string_view data,uint16_t index,byte bpp) BackgroundPalette(std::u8string_view data,uint16_t index,byte bpp)
:bpp(bpp){ :bpp(bpp){
DataBlock pointer{data,0xDAD9U+index*4U}; DataBlock pointer{data,0xDAD9U+index*4U};
uint32_t addr=snesToHex(pointer.readInt32()); uint32_t addr=snesToHex(pointer.readInt32());
@ -232,14 +233,41 @@ struct Rom{
using Tile=std::array<std::array<int,8>,8>; using Tile=std::array<std::array<int,8>,8>;
struct BackgroundGraphics{ struct BackgroundGraphics{
std::vector<uint16_t>graphicsData; std::vector<uint16_t>graphicsData;
std::vector<uint16_t>arrayGraphicsData;
std::vector<Tile>tiles; std::vector<Tile>tiles;
BackgroundGraphics(std::string_view data,uint16_t index,byte bpp){ byte bpp;
void buildTiles(){
int n{int(graphicsData.size())/(8*bpp)};
for(int i{};i<n;i++){
tiles.emplace_back();
int o{i*8*bpp};
for(int x{};x<8;x++){
for(int y{};y<8;y++){
int c{};
for(int bp{};bp<bpp;bp++){
int halfBp{bp/2};
int gfx{graphicsData[o+y*2+(halfBp*16+(bp&1))]};
c+=((gfx&(1<<7-x))>>7-x)<<bp;
}
tiles[i][x][y]=c;
}
}
}
}
BackgroundGraphics(std::u8string_view data,uint16_t index,byte bpp)
:bpp(bpp){
DataBlock graphicsPtr{data,0xD7A1U+index*4U}; DataBlock graphicsPtr{data,0xD7A1U+index*4U};
DataBlock loadGraphicsPtr{data,snesToHex(graphicsPtr.readInt32())}; DataBlock loadGraphicsPtr{data,snesToHex(graphicsPtr.readInt32())};
graphicsData=loadGraphicsPtr.decompress(); graphicsData=loadGraphicsPtr.decompress();
buildTiles();
DataBlock arrayPtrBlock{data,0xD93DU+index*4};
uint32_t arrayPtr{snesToHex(arrayPtrBlock.readInt32())};
DataBlock arrayBlock{data,arrayPtr};
arrayGraphicsData=arrayBlock.decompress();
} }
}; };
std::string data; std::u8string data;
inline static std::string reversedBytes; inline static std::string reversedBytes;
std::vector<BattleBackground>backgrounds; std::vector<BattleBackground>backgrounds;
std::vector<BackgroundPalette>palettes; std::vector<BackgroundPalette>palettes;
@ -270,7 +298,7 @@ struct Rom{
palettes.emplace_back(data,i,paletteBits[i]); palettes.emplace_back(data,i,paletteBits[i]);
} }
for(uint16_t i:std::views::iota(0U,graphicsBits.size())){ for(uint16_t i:std::views::iota(0U,graphicsBits.size())){
graphics.emplace_back(data,i,graphicsBits[i]);
} }
} }
}; };

Loading…
Cancel
Save