Added sneshacking library and started integrating into the code because it's honestly a better idea than refactoring the code and testing it when the code has already been testing so I'll likely be removing my version of it with due time

This commit is contained in:
Justin Scofield
2022-06-11 20:05:15 -04:00
parent b887563407
commit c11efc1b4c
13 changed files with 254 additions and 221 deletions

View File

@@ -2,11 +2,11 @@
#define YAZE_APPLICATION_UTILS_BITMAP_H
#include <SDL2/SDL.h>
// #include <SDL2/SDL_opengl.h>
#include <SDL_opengl.h>
#include <memory>
#include "GL/glew.h"
// #include "GL/glew.h"
#include "Utils/ROM.h"
namespace yaze {
@@ -17,7 +17,7 @@ using byte = unsigned char;
using namespace Core;
class Bitmap {
public:
public:
Bitmap() = default;
Bitmap(int width, int height, byte *data);
@@ -28,7 +28,7 @@ public:
bool LoadBitmapFromROM(unsigned char *texture_data, GLuint *out_texture,
int *out_width, int *out_height);
private:
private:
int width_;
int height_;
byte *pixel_data_;
@@ -36,12 +36,12 @@ private:
static bool isbpp3[Constants::NumberOfSheets];
int GetPCGfxAddress(byte* romData, byte id);
byte* CreateAllGfxDataRaw(byte* romData);
void CreateAllGfxData(byte* romData, byte* allgfx16Ptr);
int GetPCGfxAddress(byte *romData, byte id);
byte *CreateAllGfxDataRaw(byte *romData);
void CreateAllGfxData(byte *romData, byte *allgfx16Ptr);
} // namespace Graphics
} // namespace Application
} // namespace yaze
} // namespace Graphics
} // namespace Application
} // namespace yaze
#endif

View File

@@ -1,12 +1,12 @@
#include "Tile.h"
#include <png.h>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "png.h"
namespace yaze {
namespace Application {
namespace Graphics {
@@ -39,6 +39,56 @@ char *hexString(const char *str, const unsigned int size) {
return toret;
}
void export_all_gfx_to_png(byte *tiledata) {
auto tile = unpack_bpp3_tile(tiledata, 0);
Graphics::r_palette *pal = palette_create(8, 0);
for (unsigned int i = 0; i < 8; i++) {
pal->colors[i].red = i * 30;
pal->colors[i].blue = i * 30;
pal->colors[i].green = i * 30;
}
FILE *fp = fopen("test.png", "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);
png_init_io(png_ptr, fp);
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));
for (unsigned int i = 0; i < pal->size; i++) {
png_palette[i].blue = pal->colors[i].blue;
png_palette[i].green = pal->colors[i].green;
png_palette[i].red = pal->colors[i].red;
}
png_set_IHDR(png_ptr, info_ptr, 8, 8, 8, PNG_COLOR_TYPE_PALETTE,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
png_set_PLTE(png_ptr, info_ptr, png_palette, pal->size);
png_write_info(png_ptr, info_ptr);
png_set_packing(png_ptr);
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));
memcpy(row_pointers[i], tile.data + i * 8, 8);
}
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
png_free(png_ptr, png_palette);
png_free(png_ptr, row_pointers);
}
void export_tile_to_png(tile8 rawtile, const r_palette pal,
const char *filename) {
FILE *fp = fopen(filename, "wb");
@@ -80,37 +130,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 byte *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 byte *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 byte *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 byte *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 byte *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 byte *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 byte *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) {
@@ -189,17 +239,14 @@ 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));
@@ -216,6 +263,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

View File

@@ -18,17 +18,17 @@ extern "C" {
typedef struct {
unsigned int id;
char data[64];
byte data[64];
unsigned int palette_id;
} tile8;
tile8 unpack_bpp8_tile(const char* data, const unsigned int offset);
tile8 unpack_bpp4_tile(const char* data, const unsigned int offset);
tile8 unpack_bpp3_tile(const char* data, const unsigned int offset);
tile8 unpack_bpp2_tile(const char* data, const unsigned int offset);
tile8 unpack_bpp1_tile(const char* data, const unsigned int offset);
tile8 unpack_bpp8_tile(const byte* data, const unsigned int offset);
tile8 unpack_bpp4_tile(const byte* data, const unsigned int offset);
tile8 unpack_bpp3_tile(const byte* data, const unsigned int offset);
tile8 unpack_bpp2_tile(const byte* data, const unsigned int offset);
tile8 unpack_bpp1_tile(const byte* data, const unsigned int offset);
tile8 unpack_bpp_tile(const char* data, const unsigned int offset,
tile8 unpack_bpp_tile(const byte* data, const unsigned int offset,
const unsigned int bpp);
byte* pack_bpp1_tile(const tile8 tile);
@@ -40,6 +40,8 @@ byte* pack_bpp8_tile(const tile8 tile);
byte* pack_bpp_tile(const tile8 tile, const unsigned int bpp,
unsigned int* size);
void export_all_gfx_to_png(byte* tiledata);
void export_tile_to_png(tile8 rawtile, const r_palette pal,
const char* filename);
}