namespace housekeeping continued again

This commit is contained in:
Justin Scofield
2022-06-20 14:00:59 -04:00
parent 27f8eb7639
commit f9e0ed17bf
26 changed files with 82 additions and 82 deletions

178
src/app/gfx/bitmap.cc Normal file
View File

@@ -0,0 +1,178 @@
#include "bitmap.h"
#include <rommapping.h>
#include "rom.h"
namespace yaze {
namespace app {
namespace gfx {
int GetPCGfxAddress(char *romData, char id) {
char **info1 = new char *[255];
int gfxPointer1 =
lorom_snes_to_pc((romData[core::Constants::gfx_1_pointer + 1] << 8) +
(romData[core::Constants::gfx_1_pointer]),
info1);
int gfxPointer2 =
lorom_snes_to_pc((romData[core::Constants::gfx_2_pointer + 1] << 8) +
(romData[core::Constants::gfx_2_pointer]),
info1);
int gfxPointer3 =
lorom_snes_to_pc((romData[core::Constants::gfx_3_pointer + 1] << 8) +
(romData[core::Constants::gfx_3_pointer]),
info1);
char gfxGamePointer1 = romData[gfxPointer1 + id];
char gfxGamePointer2 = romData[gfxPointer2 + id];
char gfxGamePointer3 = romData[gfxPointer3 + id];
return lorom_snes_to_pc(
Data::AddressFromBytes(gfxGamePointer1, gfxGamePointer2, gfxGamePointer3),
info1);
}
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
char *buffer = new char[346624];
int bufferPos = 0;
char *data = new char[2048];
unsigned int uncompressedSize = 0;
unsigned int compressedSize = 0;
for (int i = 0; i < core::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 char[core::Constants::Uncompressed3BPPSize];
int startAddress = GetPCGfxAddress(romData, (char)i);
for (int j = 0; j < core::Constants::Uncompressed3BPPSize; j++) {
data[j] = romData[j + startAddress];
}
} else {
data = alttp_decompress_gfx((char *)romData,
GetPCGfxAddress(romData, (char)i),
core::Constants::UncompressedSheetSize,
&uncompressedSize, &compressedSize);
}
for (int j = 0; j < sizeof(data); j++) {
buffer[j + bufferPos] = data[j];
}
bufferPos += sizeof(data);
}
return buffer;
}
void CreateAllGfxData(char *romData, char *allgfx16Ptr) {
char *data = CreateAllGfxDataRaw(romData);
char *newData = new char[0x6F800];
uchar *mask = new uchar[]{0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
int sheetPosition = 0;
// 8x8 tile
for (int s = 0; s < core::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]) {
char lineBits0 =
data[(y * 2) + (i * 24) + (j * 384) + sheetPosition];
char lineBits1 =
data[(y * 2) + (i * 24) + (j * 384) + 1 + sheetPosition];
char lineBits2 =
data[(y) + (i * 24) + (j * 384) + 16 + sheetPosition];
for (int x = 0; x < 4; x++) // Per Pixel X
{
char pixdata = 0;
char 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)] =
(char)((pixdata << 4) | pixdata2);
}
} else {
char lineBits0 =
data[(y * 2) + (i * 16) + (j * 256) + sheetPosition];
char lineBits1 =
data[(y * 2) + (i * 16) + (j * 256) + 1 + sheetPosition];
for (int x = 0; x < 4; x++) // Per Pixel X
{
char pixdata = 0;
char 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)] =
(char)((pixdata << 4) | pixdata2);
}
}
}
}
}
if (isbpp3[s]) {
sheetPosition += core::Constants::Uncompressed3BPPSize;
} else {
sheetPosition += core::Constants::UncompressedSheetSize;
}
}
char *allgfx16Data = (char *)allgfx16Ptr;
for (int i = 0; i < 0x6F800; i++) {
allgfx16Data[i] = newData[i];
}
}
} // namespace gfx
} // namespace app
} // namespace yaze

37
src/app/gfx/bitmap.h Normal file
View File

@@ -0,0 +1,37 @@
#ifndef YAZE_APP_UTILS_BITMAP_H
#define YAZE_APP_UTILS_BITMAP_H
#include <SDL2/SDL.h>
#include "Core/constants.h"
namespace yaze {
namespace app {
namespace gfx {
class Bitmap {
public:
Bitmap() = default;
Bitmap(int width, int height, char *data)
: width_(width), height_(height), pixel_data_(data) {}
int GetWidth() const { return width_; }
int GetHeight() const { return height_; }
private:
int width_;
int height_;
char *pixel_data_;
};
static bool isbpp3[core::Constants::NumberOfSheets];
int GetPCGfxAddress(char *romData, char id);
char *CreateAllGfxDataRaw(char *romData);
void CreateAllGfxData(char *romData, char *allgfx16Ptr);
} // namespace gfx
} // namespace app
} // namespace yaze
#endif

113
src/app/gfx/palette.cc Normal file
View File

@@ -0,0 +1,113 @@
#include "palette.h"
#include <cstdlib>
#include <cstring>
namespace yaze {
namespace app {
namespace gfx {
SNESColor::SNESColor() : rgb(ImVec4(0.f, 0.f, 0.f, 0.f)) {}
SNESColor::SNESColor(ImVec4 val) : rgb(val) {
m_color col;
col.red = (uchar)val.x;
col.blue = (uchar)val.y;
col.green = (uchar)val.z;
snes = convertcolor_rgb_to_snes(col);
}
void SNESColor::setRgb(ImVec4 val) {
rgb = val;
m_color col;
col.red = val.x;
col.blue = val.y;
col.green = val.z;
snes = convertcolor_rgb_to_snes(col);
}
void SNESColor::setSNES(uint16_t val) {
snes = val;
m_color col = convertcolor_snes_to_rgb(val);
rgb = ImVec4(col.red, col.green, col.blue, 1.f);
}
SNESPalette::SNESPalette(uint8_t mSize) : size_(mSize) {
for (unsigned int i = 0; i < mSize; i++) {
SNESColor col;
colors.push_back(col);
}
}
SNESPalette::SNESPalette(char* data) {
assert((sizeof(data) % 4 == 0) && (sizeof(data) <= 32));
size_ = sizeof(data) / 2;
for (unsigned i = 0; i < sizeof(data); i += 2) {
SNESColor col;
col.snes = static_cast<uchar>(data[i + 1]) << 8;
col.snes = col.snes | static_cast<uchar>(data[i]);
m_color mColor = convertcolor_snes_to_rgb(col.snes);
col.rgb = ImVec4(mColor.red, mColor.green, mColor.blue, 1.f);
colors.push_back(col);
}
}
SNESPalette::SNESPalette(const unsigned char* snes_pal) {
assert((sizeof(snes_pal) % 4 == 0) && (sizeof(snes_pal) <= 32));
size_ = sizeof(snes_pal) / 2;
for (unsigned i = 0; i < sizeof(snes_pal); i += 2) {
SNESColor col;
col.snes = snes_pal[i + 1] << (uint16_t) 8;
col.snes = col.snes | snes_pal[i];
m_color mColor = convertcolor_snes_to_rgb(col.snes);
col.rgb = ImVec4(mColor.red, mColor.green, mColor.blue, 1.f);
colors.push_back(col);
}
}
SNESPalette::SNESPalette(std::vector<ImVec4> cols) {
for (const auto& each : cols) {
SNESColor scol;
scol.setRgb(each);
colors.push_back(scol);
}
size_ = cols.size();
}
char* SNESPalette::encode() {
// char* data(size * 2, 0);
char* data = new char[size_ * 2];
for (unsigned int i = 0; i < size_; i++) {
// std::cout << QString::number(colors[i].snes, 16);
data[i * 2] = (char)(colors[i].snes & 0xFF);
data[i * 2 + 1] = (char)(colors[i].snes >> 8);
}
return data;
}
SDL_Palette* SNESPalette::GetSDL_Palette() {
std::cout << "Converting SNESPalette to SDL_Palette" << std::endl;
auto sdl_palette = std::make_shared<SDL_Palette>();
sdl_palette->ncolors = size_;
auto* sdl_colors = new SDL_Color[size_];
for (int i = 0; i < size_; i++) {
sdl_colors[i].r = (uint8_t)colors[i].rgb.x * 100;
sdl_colors[i].g = (uint8_t)colors[i].rgb.y * 100;
sdl_colors[i].b = (uint8_t)colors[i].rgb.z * 100;
std::cout << "Color " << i << " added (R:" << sdl_colors[i].r
<< " G:" << sdl_colors[i].g << " B:" << sdl_colors[i].b << ")"
<< std::endl;
}
sdl_palette->colors = sdl_colors;
// store the pointers to free them later
sdl_palettes_.push_back(sdl_palette);
colors_arrays_.push_back(sdl_colors);
return sdl_palette.get();
}
} // namespace gfx
} // namespace app
} // namespace yaze

50
src/app/gfx/palette.h Normal file
View File

@@ -0,0 +1,50 @@
#ifndef YAZE_APP_gfx_PALETTE_H
#define YAZE_APP_gfx_PALETTE_H
#include <SDL2/SDL.h>
#include <imgui/imgui.h>
#include <palette.h>
#include <tile.h>
#include <cstdint>
#include <iostream>
#include <memory>
#include <vector>
namespace yaze {
namespace app {
namespace gfx {
struct SNESColor {
SNESColor();
explicit SNESColor(ImVec4);
uint16_t snes = 0;
ImVec4 rgb;
void setRgb(ImVec4);
void setSNES(uint16_t);
uint8_t approxSNES();
ImVec4 approxRGB();
};
class SNESPalette {
public:
SNESPalette() = default;
explicit SNESPalette(uint8_t mSize);
explicit SNESPalette(char* snesPal);
explicit SNESPalette(const unsigned char* snes_pal);
explicit SNESPalette(std::vector<ImVec4>);
char* encode();
SDL_Palette* GetSDL_Palette();
int size_ = 0;
std::vector<SNESColor> colors;
std::vector<std::shared_ptr<SDL_Palette>> sdl_palettes_;
std::vector<SDL_Color*> colors_arrays_;
};
} // namespace gfx
} // namespace app
} // namespace yaze
#endif // YAZE_APP_gfx_PALETTE_H

113
src/app/gfx/tile.cc Normal file
View File

@@ -0,0 +1,113 @@
#include "tile.h"
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <regex>
namespace yaze {
namespace app {
namespace gfx {
TilesPattern::TilesPattern() {
tiles_per_row_ = 16;
number_of_tiles_ = 16;
// transform_vector_.push_back(std::vector<int>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
// 8,
// 9, 10, 11, 12, 13, 14, 15});
// transform_vector_.push_back(std::vector<int>{});
// transform_vector_.push_back(std::vector<int>{});
// transform_vector_.push_back(std::vector<int>{});
transform_vector_.push_back(std::vector<int>{0, 1, 2, 3});
transform_vector_.push_back(std::vector<int>{4, 5, 6, 7});
transform_vector_.push_back(std::vector<int>{8, 9, 10, 11});
transform_vector_.push_back(std::vector<int>{12, 13, 14, 15});
}
std::vector<std::vector<tile8> > TilesPattern::transform(
const std::vector<tile8> &tiles) const {
uint repeatOffsetY = 0;
uint repeatOffsetX = 0;
uint tVectHeight = transform_vector_.size();
uint tVectWidth = transform_vector_[0].size();
uint repeat = 0;
std::vector<std::vector<tile8> > toret;
uint transPerRow = tiles_per_row_ / tVectWidth;
uint nbTransform = tiles.size() / number_of_tiles_;
printf("Tiles size : %d\nnbtransform : %d\npattern number of tiles : %d\n",
tiles.size(), nbTransform, number_of_tiles_);
if (transPerRow > nbTransform)
toret.resize(tVectHeight);
else
toret.resize(((uint)(((double)nbTransform / (double)transPerRow) + 0.5)) *
tVectHeight);
for (auto &each : toret) {
each.resize(tiles_per_row_);
}
std::cout << toret[0].size() << " x " << toret.size();
while (repeat != nbTransform) {
std::cout << "repeat" << repeat;
for (uint j = 0; j < tVectHeight; j++) {
for (uint i = 0; i < tVectWidth; i++) {
uint posTile = transform_vector_[j][i] + number_of_tiles_ * repeat;
uint posX = i + repeatOffsetX;
uint posY = j + repeatOffsetY;
printf("X: %d - Y: %d - posTile : %d \n", posX, posY, posTile);
toret.at(posY).at(posX) = tiles[posTile];
}
}
if (repeatOffsetX + tVectWidth == tiles_per_row_) {
repeatOffsetX = 0;
repeatOffsetY += tVectHeight;
} else
repeatOffsetX += tVectWidth;
repeat++;
}
std::cout << "End of transform" << std::endl;
return toret;
}
std::vector<tile8> TilesPattern::reverse(
const std::vector<tile8> &tiles) const {
uint repeatOffsetY = 0;
uint repeatOffsetX = 0;
uint tVectHeight = transform_vector_.size();
uint tVectWidth = transform_vector_[0].size();
uint repeat = 0;
uint nbTransPerRow = tiles_per_row_ / tVectWidth;
uint nbTiles = tiles.size();
std::vector<tile8> toretVec(tiles.size());
for (uint i = 0; i < nbTiles; i++) {
uint lineNb = i / tiles_per_row_;
uint lineInTab = lineNb % tVectHeight;
uint colInTab = i % tVectWidth;
uint tileNb = transform_vector_[lineInTab][colInTab];
uint lineBlock = i / (nbTransPerRow * number_of_tiles_);
uint blockNB =
(i % (nbTransPerRow * number_of_tiles_) % tiles_per_row_) / tVectWidth;
std::cout << colInTab << lineInTab << " = " << tileNb;
uint pos = tileNb + (lineBlock + blockNB) * number_of_tiles_;
std::cout << i << "Goes to : " << pos;
toretVec[pos] = tiles[i];
}
return toretVec;
}
std::vector<std::vector<tile8> > TilesPattern::transform(
const TilesPattern &pattern, const std::vector<tile8> &tiles) {
return pattern.transform(tiles);
}
} // namespace gfx
} // namespace app
} // namespace yaze

103
src/app/gfx/tile.h Normal file
View File

@@ -0,0 +1,103 @@
#ifndef YAZE_APP_DATA_TILE_H
#define YAZE_APP_DATA_TILE_H
#include <tile.h>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <vector>
#include "Core/constants.h"
#include "gfx/palette.h"
namespace yaze {
namespace app {
namespace gfx {
// vhopppcc cccccccc
// [0, 1]
// [2, 3]
class TileInfo {
public:
ushort id_;
ushort over_;
ushort vertical_mirror_;
ushort horizontal_mirror_;
uchar palette_;
TileInfo() = default;
TileInfo(ushort id, uchar palette, ushort v, ushort h, ushort o)
: id_(id),
over_(o),
vertical_mirror_(v),
horizontal_mirror_(h),
palette_(palette) {}
};
class Tile32 {
public:
ushort tile0_;
ushort tile1_;
ushort tile2_;
ushort tile3_;
Tile32(ushort t0, ushort t1, ushort t2, ushort t3)
: tile0_(t0), tile1_(t1), tile2_(t2), tile3_(t3) {}
};
class Tile16 {
public:
TileInfo tile0_;
TileInfo tile1_;
TileInfo tile2_;
TileInfo tile3_;
std::vector<TileInfo> tiles_info;
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_);
}
};
class TilesPattern {
public:
TilesPattern();
std::string name;
std::string description;
unsigned int tiles_per_row_;
unsigned int number_of_tiles_;
static TilesPattern pattern(std::string name);
static std::vector<std::vector<tile8>> transform(
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;
private:
std::vector<std::vector<int>> transform_vector_;
};
class TilePreset {
public:
TilePreset() = default;
TilesPattern tilesPattern;
bool no_zero_color_ = false;
int pc_tiles_location_ = 0;
int pc_palette_location_ = 0;
int bits_per_pixel_ = 0;
int length_ = 0;
int SNESTilesLocation = 0;
int SNESPaletteLocation = 0;
};
} // namespace gfx
} // namespace app
} // namespace yaze
#endif