Bunch of updates refactoring adding some sneshacking code some gui stuff

This commit is contained in:
Justin Scofield
2022-06-11 16:01:55 -04:00
parent 0de51934ab
commit a6fb58dba1
11 changed files with 1544 additions and 1244 deletions

View File

@@ -1,16 +1,181 @@
#include "Bitmap.h"
#include "Utils/ROM.h"
#include "Utils/Compression.h"
namespace yaze {
namespace Application {
namespace Graphics {
Bitmap::Bitmap(int width, int height, byte* data)
int GetPCGfxAddress(byte *romData, byte id) {
char** info1, **info2,** info3, **info4;
int gfxPointer1 =
Utils::lorom_snes_to_pc((romData[Constants::gfx_1_pointer + 1] << 8) +
(romData[Constants::gfx_1_pointer]), info1);
int gfxPointer2 =
Utils::lorom_snes_to_pc((romData[Constants::gfx_2_pointer + 1] << 8) +
(romData[Constants::gfx_2_pointer]), info2);
int gfxPointer3 =
Utils::lorom_snes_to_pc((romData[Constants::gfx_3_pointer + 1] << 8) +
(romData[Constants::gfx_3_pointer]), info3);
byte gfxGamePointer1 = romData[gfxPointer1 + id];
byte gfxGamePointer2 = romData[gfxPointer2 + id];
byte gfxGamePointer3 = romData[gfxPointer3 + id];
return Utils::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
Utils::ALTTPCompression alttp_compressor_;
byte *buffer = new byte[346624];
int bufferPos = 0;
byte *data = new byte[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
);
// uncompressed sheets
if (i >= 115 && i <= 126) {
data = new byte[Constants::Uncompressed3BPPSize];
int startAddress = GetPCGfxAddress(romData, (byte)i);
for (int j = 0; j < Constants::Uncompressed3BPPSize; j++) {
data[j] = romData[j + startAddress];
}
} else {
data = alttp_compressor_.DecompressGfx(
romData, GetPCGfxAddress(romData, (byte)i),
Constants::UncompressedSheetSize, &uncompressedSize, &compressedSize);
}
for (int j = 0; j < sizeof(data); j++) {
buffer[j + bufferPos] = data[j];
}
bufferPos += sizeof(data);
}
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};
int sheetPosition = 0;
// 8x8 tile
for (int s = 0; s < Constants::NumberOfSheets; s++) // Per Sheet
{
for (int j = 0; j < 4; j++) // Per Tile Line Y
{
for (int i = 0; i < 16; i++) // Per Tile Line X
{
for (int y = 0; y < 8; y++) // Per Pixel Line
{
if (isbpp3[s]) {
byte lineBits0 =
data[(y * 2) + (i * 24) + (j * 384) + sheetPosition];
byte lineBits1 =
data[(y * 2) + (i * 24) + (j * 384) + 1 + sheetPosition];
byte lineBits2 =
data[(y) + (i * 24) + (j * 384) + 16 + sheetPosition];
for (int x = 0; x < 4; x++) // Per Pixel X
{
byte pixdata = 0;
byte pixdata2 = 0;
if ((lineBits0 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 1;
}
if ((lineBits1 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 2;
}
if ((lineBits2 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 4;
}
if ((lineBits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
pixdata2 += 1;
}
if ((lineBits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
pixdata2 += 2;
}
if ((lineBits2 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
pixdata2 += 4;
}
newData[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
(byte)((pixdata << 4) | pixdata2);
}
} else {
byte lineBits0 =
data[(y * 2) + (i * 16) + (j * 256) + sheetPosition];
byte lineBits1 =
data[(y * 2) + (i * 16) + (j * 256) + 1 + sheetPosition];
for (int x = 0; x < 4; x++) // Per Pixel X
{
byte pixdata = 0;
byte pixdata2 = 0;
if ((lineBits0 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 1;
}
if ((lineBits1 & mask[(x * 2)]) == mask[(x * 2)]) {
pixdata += 2;
}
if ((lineBits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
pixdata2 += 1;
}
if ((lineBits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
pixdata2 += 2;
}
newData[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
(byte)((pixdata << 4) | pixdata2);
}
}
}
}
}
if (isbpp3[s]) {
sheetPosition += Constants::Uncompressed3BPPSize;
} else {
sheetPosition += Constants::UncompressedSheetSize;
}
}
byte *allgfx16Data = (byte *) allgfx16Ptr;
for (int i = 0; i < 0x6F800; i++) {
allgfx16Data[i] = newData[i];
}
}
Bitmap::Bitmap(int width, int height, byte *data)
: width_(width), height_(height), pixel_data_(data) {}
void Bitmap::Create(GLuint* out_texture) {
// // Read the pixel data from the ROM
void Bitmap::Create(GLuint *out_texture) {
// // Read the pixel data from the ROM
// SDL_RWops * src = SDL_RWFromMem(pixel_data_, 0);
// // Create the surface from that RW stream
// // Create the surface from that RW stream
// SDL_Surface* surface = SDL_LoadBMP_RW(src, SDL_FALSE);
// GLenum mode = 0;
// Uint8 bpp = surface->format->BytesPerPixel;
@@ -45,21 +210,18 @@ void Bitmap::Create(GLuint* out_texture) {
*out_texture = image_texture;
}
int Bitmap::GetWidth() {
return width_;
}
int Bitmap::GetHeight() {
return height_;
}
int Bitmap::GetWidth() { return width_; }
int Bitmap::GetHeight() { return height_; }
// Simple helper function to load an image into a OpenGL texture with common
// settings
bool Bitmap::LoadBitmapFromROM(unsigned char* texture_data, GLuint* out_texture,
int* out_width, int* out_height) {
bool Bitmap::LoadBitmapFromROM(unsigned char *texture_data, GLuint *out_texture,
int *out_width, int *out_height) {
// 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;
@@ -70,9 +232,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__)
@@ -88,6 +250,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

@@ -14,6 +14,7 @@ namespace Application {
namespace Graphics {
using byte = unsigned char;
using namespace Core;
class Bitmap {
public:
@@ -31,8 +32,14 @@ private:
int width_;
int height_;
byte *pixel_data_;
SDL_PixelFormat pixel_format_;
};
static bool isbpp3[Constants::NumberOfSheets];
int GetPCGfxAddress(byte* romData, byte id);
byte* CreateAllGfxDataRaw(byte* romData);
void CreateAllGfxData(byte* romData, byte* allgfx16Ptr);
} // namespace Graphics
} // namespace Application
} // namespace yaze

View File

@@ -28,8 +28,8 @@ ushort TileInfo::toShort() {
return value;
}
char* hexString(const char* str, const unsigned int size) {
char* toret = (char*)malloc(size * 3 + 1);
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++) {
@@ -40,8 +40,8 @@ char* hexString(const char* str, const unsigned int size) {
}
void export_tile_to_png(tile8 rawtile, const r_palette pal,
const char* filename) {
FILE* fp = fopen(filename, "wb");
const char *filename) {
FILE *fp = fopen(filename, "wb");
png_structp png_ptr =
png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
@@ -49,8 +49,8 @@ void export_tile_to_png(tile8 rawtile, const r_palette pal,
png_set_strip_alpha(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_color* png_palette =
(png_color*)png_malloc(png_ptr, pal.size * sizeof(png_color));
png_color *png_palette =
(png_color *)png_malloc(png_ptr, pal.size * sizeof(png_color));
for (unsigned int i = 0; i < pal.size; i++) {
png_palette[i].blue = pal.colors[i].blue;
@@ -65,9 +65,9 @@ void export_tile_to_png(tile8 rawtile, const r_palette pal,
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
png_byte* row_pointers[8];
png_byte *row_pointers[8];
for (unsigned int i = 0; i < 8; i++) {
row_pointers[i] = (png_byte*)png_malloc(png_ptr, sizeof(png_byte));
row_pointers[i] = (png_byte *)png_malloc(png_ptr, sizeof(png_byte));
memcpy(row_pointers[i], rawtile.data + i * 8, 8);
}
@@ -80,37 +80,37 @@ void export_tile_to_png(tile8 rawtile, const r_palette pal,
png_free(png_ptr, row_pointers);
}
tile8 unpack_bpp1_tile(const char* data, const unsigned int offset) {
tile8 unpack_bpp1_tile(const char *data, const unsigned int offset) {
return (unpack_bpp_tile(data, offset, 1));
}
tile8 unpack_bpp2_tile(const char* data, const unsigned int offset) {
tile8 unpack_bpp2_tile(const char *data, const unsigned int offset) {
return (unpack_bpp_tile(data, offset, 2));
}
tile8 unpack_bpp3_tile(const char* data, const unsigned int offset) {
tile8 unpack_bpp3_tile(const char *data, const unsigned int offset) {
return (unpack_bpp_tile(data, offset, 3));
}
tile8 unpack_bpp4_tile(const char* data, const unsigned int offset) {
tile8 unpack_bpp4_tile(const char *data, const unsigned int offset) {
return (unpack_bpp_tile(data, offset, 4));
}
tile8 unpack_bpp8_tile(const char* data, const unsigned int offset) {
tile8 unpack_bpp8_tile(const char *data, const unsigned int offset) {
return (unpack_bpp_tile(data, offset, 8));
}
tile8 unpack_mode7_tile(const char* data, const unsigned int offset) {
tile8 unpack_mode7_tile(const char *data, const unsigned int offset) {
tile8 tile;
memcpy(tile.data, data + offset, 64);
return tile;
}
tile8 unpack_bpp_tile(const char* data, const unsigned int offset,
tile8 unpack_bpp_tile(const char *data, const unsigned int offset,
const unsigned bpp) {
tile8 tile;
assert(bpp >= 1 && bpp <= 8);
unsigned int bpp_pos[8]; // More for conveniance and readibility
unsigned int bpp_pos[8]; // More for conveniance and readibility
for (int col = 0; col < 8; col++) {
for (int row = 0; row < 8; row++) {
if (bpp == 1) {
@@ -155,33 +155,33 @@ tile8 unpack_bpp_tile(const char* data, const unsigned int offset,
return tile;
}
byte* pack_bpp1_tile(const tile8 tile) {
byte *pack_bpp1_tile(const tile8 tile) {
unsigned int p = 1;
return pack_bpp_tile(tile, 1, &p);
}
byte* pack_bpp2_tile(const tile8 tile) {
byte *pack_bpp2_tile(const tile8 tile) {
unsigned int p = 1;
return pack_bpp_tile(tile, 2, &p);
}
byte* pack_bpp3_tile(const tile8 tile) {
byte *pack_bpp3_tile(const tile8 tile) {
unsigned int p = 1;
return pack_bpp_tile(tile, 3, &p);
}
byte* pack_bpp4_tile(const tile8 tile) {
byte *pack_bpp4_tile(const tile8 tile) {
unsigned int p = 1;
return pack_bpp_tile(tile, 4, &p);
}
byte* pack_bpp8_tile(const tile8 tile) {
byte *pack_bpp8_tile(const tile8 tile) {
unsigned int p = 1;
return pack_bpp_tile(tile, 8, &p);
}
byte* pack_bpp_tile(tile8 tile, const unsigned int bpp, unsigned int* size) {
byte* output = (byte*)malloc(bpp * 8);
byte *pack_bpp_tile(tile8 tile, const unsigned int bpp, unsigned int *size) {
byte *output = (byte *)malloc(bpp * 8);
memset(output, 0, bpp * 8);
unsigned maxcolor = 2 << bpp;
*size = 0;
@@ -189,14 +189,17 @@ byte* pack_bpp_tile(tile8 tile, const unsigned int bpp, unsigned int* size) {
for (unsigned int col = 0; col < 8; col++) {
for (unsigned int row = 0; row < 8; row++) {
byte color = tile.data[col * 8 + row];
if (color > maxcolor) return NULL;
if (color > maxcolor)
return NULL;
if (bpp == 1) output[col] += (byte)((color & 1) << (7 - row));
if (bpp == 1)
output[col] += (byte)((color & 1) << (7 - row));
if (bpp >= 2) {
output[col * 2] += (byte)((color & 1) << (7 - row));
output[col * 2 + 1] += (byte)(((color & 2) == 2) << (7 - row));
}
if (bpp == 3) output[16 + col] += (byte)(((color & 4) == 4) << (7 - row));
if (bpp == 3)
output[16 + col] += (byte)(((color & 4) == 4) << (7 - row));
if (bpp >= 4) {
output[16 + col * 2] += (byte)(((color & 4) == 4) << (7 - row));
output[16 + col * 2 + 1] += (byte)(((color & 8) == 8) << (7 - row));
@@ -213,6 +216,6 @@ byte* pack_bpp_tile(tile8 tile, const unsigned int bpp, unsigned int* size) {
return output;
}
} // namespace Graphics
} // namespace Application
} // namespace yaze
} // namespace Graphics
} // namespace Application
} // namespace yaze