Refactoring the codebase and moving closer to decompression

This commit is contained in:
Justin Scofield
2022-06-12 18:20:32 -04:00
parent e154b53fc2
commit b325669ca8
18 changed files with 494 additions and 664 deletions

View File

@@ -1,63 +1,66 @@
#include "Bitmap.h"
#include "Utils/ROM.h"
#include "Utils/Compression.h"
#include "rommapping.h"
namespace yaze {
namespace Application {
namespace Graphics {
int GetPCGfxAddress(byte *romData, byte id) {
char** info1, **info2,** info3, **info4;
int GetPCGfxAddress(char *romData, char id) {
char **info1, **info2, **info3, **info4;
int gfxPointer1 =
lorom_snes_to_pc((romData[Constants::gfx_1_pointer + 1] << 8) +
(romData[Constants::gfx_1_pointer]), info1);
(romData[Constants::gfx_1_pointer]),
info1);
int gfxPointer2 =
lorom_snes_to_pc((romData[Constants::gfx_2_pointer + 1] << 8) +
(romData[Constants::gfx_2_pointer]), info2);
(romData[Constants::gfx_2_pointer]),
info2);
int gfxPointer3 =
lorom_snes_to_pc((romData[Constants::gfx_3_pointer + 1] << 8) +
(romData[Constants::gfx_3_pointer]), info3);
(romData[Constants::gfx_3_pointer]),
info3);
byte gfxGamePointer1 = romData[gfxPointer1 + id];
byte gfxGamePointer2 = romData[gfxPointer2 + id];
byte gfxGamePointer3 = romData[gfxPointer3 + id];
char gfxGamePointer1 = romData[gfxPointer1 + id];
char gfxGamePointer2 = romData[gfxPointer2 + id];
char gfxGamePointer3 = romData[gfxPointer3 + id];
return lorom_snes_to_pc(Utils::AddressFromBytes(gfxGamePointer1, gfxGamePointer2,
gfxGamePointer3), info4);
return lorom_snes_to_pc(
Utils::AddressFromBytes(gfxGamePointer1, gfxGamePointer2,
gfxGamePointer3),
info4);
}
byte *CreateAllGfxDataRaw(byte *romData) {
// 0-112 -> compressed 3bpp bgr -> (decompressed each) 0x600 bytes
// 113-114 -> compressed 2bpp -> (decompressed each) 0x800 bytes
// 115-126 -> uncompressed 3bpp sprites -> (each) 0x600 bytes
// 127-217 -> compressed 3bpp sprites -> (decompressed each) 0x600 bytes
// 218-222 -> compressed 2bpp -> (decompressed each) 0x800 bytes
char *CreateAllGfxDataRaw(char *romData) {
// 0-112 -> compressed 3bpp bgr -> (decompressed each) 0x600 chars
// 113-114 -> compressed 2bpp -> (decompressed each) 0x800 chars
// 115-126 -> uncompressed 3bpp sprites -> (each) 0x600 chars
// 127-217 -> compressed 3bpp sprites -> (decompressed each) 0x600 chars
// 218-222 -> compressed 2bpp -> (decompressed each) 0x800 chars
Utils::ALTTPCompression alttp_compressor_;
byte *buffer = new byte[346624];
char *buffer = new char[346624];
int bufferPos = 0;
byte *data = new byte[2048];
char *data = new char[2048];
unsigned int uncompressedSize = 0;
unsigned int compressedSize = 0;
for (int i = 0; i < Constants::NumberOfSheets; i++) {
isbpp3[i] = ((i >= 0 && i <= 112) || // Compressed 3bpp bg
(i >= 115 && i <= 126) || // Uncompressed 3bpp sprites
(i >= 127 && i <= 217) // Compressed 3bpp sprites
isbpp3[i] = ((i >= 0 && i <= 112) || // Compressed 3bpp bg
(i >= 115 && i <= 126) || // Uncompressed 3bpp sprites
(i >= 127 && i <= 217) // Compressed 3bpp sprites
);
// uncompressed sheets
if (i >= 115 && i <= 126) {
data = new byte[Constants::Uncompressed3BPPSize];
int startAddress = GetPCGfxAddress(romData, (byte)i);
data = new char[Constants::Uncompressed3BPPSize];
int startAddress = GetPCGfxAddress(romData, (char)i);
for (int j = 0; j < Constants::Uncompressed3BPPSize; j++) {
data[j] = romData[j + startAddress];
}
} else {
data = alttp_compressor_.DecompressGfx(
romData, GetPCGfxAddress(romData, (byte)i),
data = alttp_decompress_gfx(
(char *)romData, GetPCGfxAddress(romData, (char)i),
Constants::UncompressedSheetSize, &uncompressedSize, &compressedSize);
}
@@ -71,34 +74,35 @@ byte *CreateAllGfxDataRaw(byte *romData) {
return buffer;
}
void CreateAllGfxData(byte *romData, byte* allgfx16Ptr) {
byte* data = CreateAllGfxDataRaw(romData);
byte* newData =
new byte[0x6F800]; // NEED TO GET THE APPROPRIATE SIZE FOR THAT
byte* mask = new byte[]{0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
void CreateAllGfxData(char *romData, char *allgfx16Ptr) {
char *data = CreateAllGfxDataRaw(romData);
char *newData =
new char[0x6F800]; // NEED TO GET THE APPROPRIATE SIZE FOR THAT
unsigned char *mask =
new unsigned char[]{0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
int sheetPosition = 0;
// 8x8 tile
for (int s = 0; s < Constants::NumberOfSheets; s++) // Per Sheet
for (int s = 0; s < Constants::NumberOfSheets; s++) // Per Sheet
{
for (int j = 0; j < 4; j++) // Per Tile Line Y
for (int j = 0; j < 4; j++) // Per Tile Line Y
{
for (int i = 0; i < 16; i++) // Per Tile Line X
for (int i = 0; i < 16; i++) // Per Tile Line X
{
for (int y = 0; y < 8; y++) // Per Pixel Line
for (int y = 0; y < 8; y++) // Per Pixel Line
{
if (isbpp3[s]) {
byte lineBits0 =
char lineBits0 =
data[(y * 2) + (i * 24) + (j * 384) + sheetPosition];
byte lineBits1 =
char lineBits1 =
data[(y * 2) + (i * 24) + (j * 384) + 1 + sheetPosition];
byte lineBits2 =
char lineBits2 =
data[(y) + (i * 24) + (j * 384) + 16 + sheetPosition];
for (int x = 0; x < 4; x++) // Per Pixel X
for (int x = 0; x < 4; x++) // Per Pixel X
{
byte pixdata = 0;
byte pixdata2 = 0;
char pixdata = 0;
char pixdata2 = 0;
if ((lineBits0 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 1;
@@ -121,18 +125,18 @@ void CreateAllGfxData(byte *romData, byte* allgfx16Ptr) {
}
newData[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
(byte)((pixdata << 4) | pixdata2);
(char)((pixdata << 4) | pixdata2);
}
} else {
byte lineBits0 =
char lineBits0 =
data[(y * 2) + (i * 16) + (j * 256) + sheetPosition];
byte lineBits1 =
char lineBits1 =
data[(y * 2) + (i * 16) + (j * 256) + 1 + sheetPosition];
for (int x = 0; x < 4; x++) // Per Pixel X
for (int x = 0; x < 4; x++) // Per Pixel X
{
byte pixdata = 0;
byte pixdata2 = 0;
char pixdata = 0;
char pixdata2 = 0;
if ((lineBits0 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 1;
@@ -149,7 +153,7 @@ void CreateAllGfxData(byte *romData, byte* allgfx16Ptr) {
}
newData[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
(byte)((pixdata << 4) | pixdata2);
(char)((pixdata << 4) | pixdata2);
}
}
}
@@ -163,14 +167,14 @@ void CreateAllGfxData(byte *romData, byte* allgfx16Ptr) {
}
}
byte *allgfx16Data = (byte *) allgfx16Ptr;
char *allgfx16Data = (char *)allgfx16Ptr;
for (int i = 0; i < 0x6F800; i++) {
allgfx16Data[i] = newData[i];
}
}
Bitmap::Bitmap(int width, int height, byte *data)
Bitmap::Bitmap(int width, int height, char *data)
: width_(width), height_(height), pixel_data_(data) {}
void Bitmap::Create(GLuint *out_texture) {
@@ -179,7 +183,7 @@ void Bitmap::Create(GLuint *out_texture) {
// // Create the surface from that RW stream
// SDL_Surface* surface = SDL_LoadBMP_RW(src, SDL_FALSE);
// GLenum mode = 0;
// Uint8 bpp = surface->format->BytesPerPixel;
// Uint8 bpp = surface->format->charsPerPixel;
// Uint32 rm = surface->format->Rmask;
// if (bpp == 3 && rm == 0x000000ff) mode = GL_RGB;
// if (bpp == 3 && rm == 0x00ff0000) mode = GL_BGR;
@@ -221,8 +225,7 @@ bool Bitmap::LoadBitmapFromROM(unsigned char *texture_data, GLuint *out_texture,
// Load from file
int image_width = 0;
int image_height = 0;
if (texture_data == NULL)
return false;
if (texture_data == NULL) return false;
// Create a OpenGL texture identifier
GLuint image_texture;
@@ -233,9 +236,9 @@ bool Bitmap::LoadBitmapFromROM(unsigned char *texture_data, GLuint *out_texture,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE); // This is required on WebGL for non
// power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Same
GL_CLAMP_TO_EDGE); // This is required on WebGL for non
// power-of-two textures
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Same
// Upload pixels into texture
#if defined(GL_UNPACK_ROW_LENGTH) && !defined(__EMSCRIPTEN__)
@@ -251,6 +254,6 @@ bool Bitmap::LoadBitmapFromROM(unsigned char *texture_data, GLuint *out_texture,
return true;
}
} // namespace Graphics
} // namespace Application
} // namespace yaze
} // namespace Graphics
} // namespace Application
} // namespace yaze

View File

@@ -13,13 +13,12 @@ namespace yaze {
namespace Application {
namespace Graphics {
using byte = unsigned char;
using namespace Core;
class Bitmap {
public:
Bitmap() = default;
Bitmap(int width, int height, byte *data);
Bitmap(int width, int height, char *data);
void Create(GLuint *out_texture);
int GetWidth();
@@ -31,14 +30,14 @@ class Bitmap {
private:
int width_;
int height_;
byte *pixel_data_;
char *pixel_data_;
};
static bool isbpp3[Constants::NumberOfSheets];
int GetPCGfxAddress(byte *romData, byte id);
byte *CreateAllGfxDataRaw(byte *romData);
void CreateAllGfxData(byte *romData, byte *allgfx16Ptr);
int GetPCGfxAddress(char *romData, char id);
char *CreateAllGfxDataRaw(char *romData);
void CreateAllGfxData(char *romData, char *allgfx16Ptr);
} // namespace Graphics
} // namespace Application

View File

@@ -47,8 +47,7 @@ SNESPalette::SNESPalette(uint8_t mSize) {
}
SNESPalette::SNESPalette(char* data) {
// assert((data.size() % 4 == 0) && data.size() <= 32);
// size = data.size() / 2;
assert((sizeof(data) % 4 == 0) && (sizeof(data) <= 32));
size = sizeof(data) / 2;
for (unsigned i = 0; i < sizeof(data); i += 2) {
SNESColor col;

View File

@@ -10,7 +10,6 @@
#include <iostream>
#include <vector>
namespace yaze {
namespace Application {
namespace Graphics {
@@ -34,7 +33,6 @@ class SNESPalette {
SNESPalette(std::vector<ImVec4>);
char* encode();
SDL_Palette* GetSDL_Palette();
uint8_t size;

View File

@@ -19,7 +19,6 @@ void Scene::buildSurface(const std::vector<tile8>& tiles, SNESPalette& mPalette,
SDL_PixelFormat* format = newImage->format;
format->palette = mPalette.GetSDL_Palette();
char* ptr = (char*)newImage->pixels;
for (int i = 0; i < 8; i++) {

View File

@@ -13,47 +13,17 @@ namespace yaze {
namespace Application {
namespace Graphics {
std::unordered_map<std::string, TilesPattern> TilesPattern::m_Patterns;
ushort TileInfo::toShort() {
ushort value = 0;
// vhopppcc cccccccc
if (over_ == 1) {
value |= 0x2000;
};
if (horizontal_mirror_ == 1) {
value |= 0x4000;
};
if (vertical_mirror_ == 1) {
value |= 0x8000;
};
value |= (ushort)((palette_ << 10) & 0x1C00);
value |= (ushort)(id_ & 0x3FF);
return value;
}
char *hexString(const char *str, const unsigned int size) {
char *toret = (char *)malloc(size * 3 + 1);
unsigned int i;
for (i = 0; i < size; i++) {
sprintf(toret + i * 3, "%02X ", (unsigned char)str[i]);
}
toret[size * 3] = 0;
return toret;
}
TilesPattern::TilesPattern() {
tilesPerRow = 16;
numberOfTiles = 16;
transformVector.push_back(std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9,
11, 12, 13, 14, 15, 16});
// std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 11, 12, 13, 14, 15, 16});
// transformVector.push_back(std::vector<int>{0, 1, 2, 3});
// transformVector.push_back(std::vector<int>{4, 5, 6, 7});
// transformVector.push_back(std::vector<int>{8, 9, 11, 12});
// transformVector.push_back(std::vector<int>{13, 14, 15, 16});
transformVector.push_back(std::vector<int>{0, 1, 2, 3});
transformVector.push_back(std::vector<int>{4, 5, 6, 7});
transformVector.push_back(std::vector<int>{8, 9, 11, 12});
transformVector.push_back(std::vector<int>{13, 14, 15, 16});
}
// [pattern]
// name = "32x32 B (4x4)"
// number_of_tile = 16
@@ -97,62 +67,6 @@ void TilesPattern::default_settings() {
std::cout << transformVector.size() << std::endl;
}
// bool TilesPattern::load(std::string patternFile) {
// QSettings pFile(patternFile, QSettings::IniFormat);
// name = pFile.value("pattern/name").toString();
// description = pFile.value("pattern/description").toString();
// numberOfTiles = pFile.value("pattern/number_of_tile").toInt();
// std::cout << name;
// // unsigned int nbOfTile = pFile.value("_/number_of_tile").toUInt();
// std::string patternString = pFile.value("pattern/pattern").toString();
// std::cout << patternString;
// // Pattern String is a array description
// transformVector.clear();
// QRegExp arrayRegExp("(\\[[\\s|0-F|a-f|,]+\\])");
// int pos = 0;
// while (arrayRegExp.indexIn(patternString, pos) != -1) {
// std::string arrayString = arrayRegExp.cap(1);
// std::vector<int> tmpVect;
// // std::cout << arrayString;
// unsigned int stringPos = 1;
// while (arrayString[stringPos] != ']') {
// while (arrayString[stringPos].isSpace()) stringPos++;
// QRegExp hex("([0-F|a-f]+)");
// bool ok;
// if (hex.indexIn(arrayString, stringPos) == stringPos) {
// tmpVect.append(hex.cap(1).toInt(&ok, 16));
// }
// while (arrayString[stringPos].isSpace()) stringPos++;
// stringPos++; // should be the comma
// }
// pos += arrayRegExp.matchedLength();
// transformVector.append(tmpVect);
// }
// std::cout << transformVector.size() << transformVector;
// return true;
// }
bool TilesPattern::loadPatterns() {
// foreach (std::string fileName, patternDirectory.entryList(QDir::Files)) {
// TilesPattern tp;
// std::cout << "Loading " << fileName;
// if (!tp.load(patternDirectory.absoluteFilePath(fileName))) return false;
// m_Patterns[tp.name] = tp;
// }
return true;
}
TilesPattern TilesPattern::pattern(std::string name) {
return m_Patterns[name];
}
std::unordered_map<std::string, TilesPattern> TilesPattern::Patterns() {
return m_Patterns;
}
std::vector<std::vector<tile8> > TilesPattern::transform(
const std::vector<tile8> &tiles) const {
unsigned int repeatOffsetY = 0;
@@ -163,7 +77,7 @@ std::vector<std::vector<tile8> > TilesPattern::transform(
std::vector<std::vector<tile8> > toret;
unsigned int transPerRow = tilesPerRow / tVectWidth;
unsigned int nbTransform = tiles.size() / numberOfTiles;
printf("Tiles size : %d - nbtransform : %d - pattern number of tiles : %d",
printf("Tiles size : %d\nnbtransform : %d\npattern number of tiles : %d\n",
tiles.size(), nbTransform, numberOfTiles);
if (transPerRow > nbTransform)
@@ -173,15 +87,11 @@ std::vector<std::vector<tile8> > TilesPattern::transform(
((unsigned int)(((double)nbTransform / (double)transPerRow) + 0.5)) *
tVectHeight);
std::vector<std::vector<tile8> > vec(toret);
auto it = vec.begin();
for (auto &each : vec) {
for (auto &each : toret) {
each.resize(tilesPerRow);
}
// while (it.hasNext()) {
// it.next().resize(tilesPerRow);
// }
// std::cout << toret[0].size() << "x" << toret.size();
std::cout << toret[0].size() << " x " << toret.size();
while (repeat != nbTransform) {
std::cout << "repeat" << repeat;
for (unsigned int j = 0; j < tVectHeight; j++) {
@@ -190,7 +100,8 @@ std::vector<std::vector<tile8> > TilesPattern::transform(
unsigned int posX = i + repeatOffsetX;
unsigned int posY = j + repeatOffsetY;
printf("X: %d - Y: %d - posTile : %d", posX, posY, posTile);
toret[posY][posX] = tiles[posTile];
// toret[posY][posX] = tiles[posTile];
toret.at(posY).at(posX) = tiles[posTile];
}
}
if (repeatOffsetX + tVectWidth == tilesPerRow) {
@@ -240,21 +151,7 @@ std::vector<std::vector<tile8> > TilesPattern::transform(
return pattern.transform(tiles);
}
std::vector<std::vector<tile8> > TilesPattern::transform(
const std::string id, const std::vector<tile8> &tiles) {
return m_Patterns[id].transform(tiles);
}
std::vector<tile8> TilesPattern::reverse(const TilesPattern &pattern,
const std::vector<tile8> &tiles) {
return pattern.reverse(tiles);
}
TilePreset::TilePreset() {
name = "";
romName = "";
romType = "";
pcTilesLocation = -1;
SNESTilesLocation = 0;
length = 0;

View File

@@ -17,6 +17,9 @@ using byte = unsigned char;
using ushort = unsigned short;
using uint = unsigned int;
// vhopppcc cccccccc
// [0, 1]
// [2, 3]
class TileInfo {
public:
ushort id_;
@@ -24,38 +27,24 @@ class TileInfo {
ushort vertical_mirror_;
ushort horizontal_mirror_;
byte palette_;
TileInfo() {} // vhopppcc cccccccc
TileInfo() {}
TileInfo(ushort id, byte palette, ushort v, ushort h, ushort o)
: id_(id),
palette_(palette),
vertical_mirror_(v),
horizontal_mirror_(h),
over_(o) {}
ushort toShort();
};
class Tile32 {
public:
//[0,1]
//[2,3]
ushort tile0_;
ushort tile1_;
ushort tile2_;
ushort tile3_;
Tile32(ushort tile0, ushort tile1, ushort tile2, ushort tile3)
: tile0_(tile0), tile1_(tile1), tile2_(tile2), tile3_(tile3) {}
explicit Tile32(unsigned long tiles)
: tile0_(tiles),
tile1_(tiles >> 16),
tile2_(tiles >> 32),
tile3_(tiles >> 48) {}
unsigned long getLongValue() {
return ((unsigned long)tile3_ << 48) | ((unsigned long)tile2_ << 32) |
((unsigned long)tile1_ << 16) | (unsigned long)(tile0_);
}
Tile32(ushort t0, ushort t1, ushort t2, ushort t3)
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
};
class Tile16 {
@@ -65,31 +54,14 @@ class Tile16 {
TileInfo tile2_;
TileInfo tile3_;
std::vector<TileInfo> tiles_info;
//[0,1]
//[2,3]
Tile16(TileInfo tile0, TileInfo tile1, TileInfo tile2, TileInfo tile3)
: tile0_(tile0), tile1_(tile1), tile2_(tile2), tile3_(tile3) {
Tile16(TileInfo t0, TileInfo t1, TileInfo t2, TileInfo t3)
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {
tiles_info.push_back(tile0_);
tiles_info.push_back(tile1_);
tiles_info.push_back(tile2_);
tiles_info.push_back(tile3_);
}
explicit Tile16(unsigned long tiles) {
// tile0_ = GFX.gettilesinfo((ushort)tiles);
// tile1_ = GFX.gettilesinfo((ushort)(tiles >> 16));
// tile2_ = GFX.gettilesinfo((ushort)(tiles >> 32));
// tile3_ = GFX.gettilesinfo((ushort)(tiles >> 48));
}
unsigned long getLongValue() {
return ((unsigned long)(tile3_.toShort()) << 48) |
((unsigned long)(tile2_.toShort()) << 32) |
((unsigned long)(tile1_.toShort()) << 16) |
(unsigned long)((tile0_.toShort()));
;
}
};
class TilesPattern {
@@ -103,24 +75,15 @@ class TilesPattern {
void default_settings();
static bool loadPatterns();
static TilesPattern pattern(std::string name);
static std::unordered_map<std::string, TilesPattern> Patterns();
static std::vector<std::vector<tile8> > transform(
const TilesPattern& pattern, const std::vector<tile8>& tiles);
static std::vector<std::vector<tile8> > transform(
const std::string id, const std::vector<tile8>& tiles);
static std::vector<tile8> reverse(const TilesPattern& pattern,
const std::vector<tile8>& tiles);
protected:
std::vector<std::vector<tile8> > transform(
const std::vector<tile8>& tiles) const;
std::vector<tile8> reverse(const std::vector<tile8>& tiles) const;
std::vector<std::vector<int> > transformVector;
private:
static std::unordered_map<std::string, TilesPattern> m_Patterns;
};
class TilePreset {
@@ -130,19 +93,15 @@ class TilePreset {
bool save(const std::string& file);
bool load(const std::string& file);
std::string name;
std::string romName;
std::string romType;
TilesPattern tilesPattern;
unsigned int SNESTilesLocation;
int pcTilesLocation;
unsigned int SNESPaletteLocation;
unsigned int pcPaletteLocation;
bool paletteNoZeroColor;
unsigned int length;
int pcTilesLocation;
uint16_t SNESTilesLocation;
uint16_t SNESPaletteLocation;
uint32_t pcPaletteLocation;
uint32_t length;
uint32_t bpp;
unsigned int bpp;
TilesPattern tilesPattern;
std::string compression;
};