Rename SNESPalette, SNESColor to SnesPalette, SnesColor
Create snes_color.h and snes_color.cc to separate concerns
This commit is contained in:
@@ -268,8 +268,14 @@ void Bitmap::UpdateTexture(SDL_Renderer *renderer) {
|
||||
SDL_LockTexture(texture_.get(), nullptr, (void **)&texture_pixels,
|
||||
&converted_surface_->pitch);
|
||||
|
||||
memcpy(texture_pixels, converted_surface_->pixels,
|
||||
converted_surface_->h * converted_surface_->pitch);
|
||||
try {
|
||||
memcpy(texture_pixels, converted_surface_->pixels,
|
||||
converted_surface_->h * converted_surface_->pitch);
|
||||
// SDL_UpdateTexture(texture_.get(), nullptr, converted_surface_->pixels,
|
||||
// converted_surface_->pitch);
|
||||
} catch (const std::exception &e) {
|
||||
SDL_Log("Exception: %s\n", e.what());
|
||||
}
|
||||
|
||||
SDL_UnlockTexture(texture_.get());
|
||||
}
|
||||
@@ -313,7 +319,7 @@ void Bitmap::LoadFromPngData(const std::vector<uint8_t> &png_data, int width,
|
||||
}
|
||||
|
||||
// Convert SNESPalette to SDL_Palette for surface.
|
||||
void Bitmap::ApplyPalette(const SNESPalette &palette) {
|
||||
void Bitmap::ApplyPalette(const SnesPalette &palette) {
|
||||
palette_ = palette;
|
||||
SDL_UnlockSurface(surface_.get());
|
||||
for (int i = 0; i < palette.size(); ++i) {
|
||||
@@ -332,7 +338,7 @@ void Bitmap::ApplyPalette(const SNESPalette &palette) {
|
||||
SDL_LockSurface(surface_.get());
|
||||
}
|
||||
|
||||
void Bitmap::ApplyPaletteFromPaletteGroup(const SNESPalette &palette,
|
||||
void Bitmap::ApplyPaletteFromPaletteGroup(const SnesPalette &palette,
|
||||
int palette_id) {
|
||||
auto start_index = palette_id * 8;
|
||||
palette_ = palette.sub_palette(start_index, start_index + 8);
|
||||
@@ -353,8 +359,8 @@ void Bitmap::ApplyPaletteFromPaletteGroup(const SNESPalette &palette,
|
||||
SDL_LockSurface(surface_.get());
|
||||
}
|
||||
|
||||
void Bitmap::ApplyPaletteWithTransparent(const SNESPalette &palette,
|
||||
int index) {
|
||||
void Bitmap::ApplyPaletteWithTransparent(const SnesPalette &palette, int index,
|
||||
int length) {
|
||||
auto start_index = index * 7;
|
||||
palette_ = palette.sub_palette(start_index, start_index + 7);
|
||||
std::vector<ImVec4> colors;
|
||||
|
||||
@@ -44,10 +44,11 @@ class Bitmap {
|
||||
void LoadFromPngData(const std::vector<uint8_t> &png_data, int width,
|
||||
int height);
|
||||
|
||||
void ApplyPalette(const SNESPalette &palette);
|
||||
void ApplyPaletteWithTransparent(const SNESPalette &palette, int index);
|
||||
void ApplyPalette(const SnesPalette &palette);
|
||||
void ApplyPaletteWithTransparent(const SnesPalette &palette, int index,
|
||||
int length = 7);
|
||||
void ApplyPalette(const std::vector<SDL_Color> &palette);
|
||||
void ApplyPaletteFromPaletteGroup(const SNESPalette &palette, int palette_id);
|
||||
void ApplyPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id);
|
||||
|
||||
void WriteToPixel(int position, uchar value) {
|
||||
if (pixel_data_ == nullptr) {
|
||||
@@ -222,7 +223,7 @@ class Bitmap {
|
||||
|
||||
std::vector<uint8_t> png_data_;
|
||||
|
||||
gfx::SNESPalette palette_;
|
||||
gfx::SnesPalette palette_;
|
||||
std::shared_ptr<SDL_Texture> texture_ = nullptr;
|
||||
std::shared_ptr<SDL_Surface> surface_ = nullptr;
|
||||
std::shared_ptr<SDL_Surface> converted_surface_ = nullptr;
|
||||
|
||||
60
src/app/gfx/snes_color.cc
Normal file
60
src/app/gfx/snes_color.cc
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
#include "app/gfx/snes_color.h"
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
namespace gfx {
|
||||
|
||||
constexpr uint16_t SNES_RED_MASK = 32;
|
||||
constexpr uint16_t SNES_GREEN_MASK = 32;
|
||||
constexpr uint16_t SNES_BLUE_MASK = 32;
|
||||
|
||||
constexpr uint16_t SNES_GREEN_SHIFT = 32;
|
||||
constexpr uint16_t SNES_BLUE_SHIFT = 1024;
|
||||
|
||||
snes_color ConvertSNEStoRGB(uint16_t color_snes) {
|
||||
snes_color result;
|
||||
|
||||
result.red = (color_snes % SNES_RED_MASK) * 8;
|
||||
result.green = ((color_snes / SNES_GREEN_MASK) % SNES_GREEN_MASK) * 8;
|
||||
result.blue = ((color_snes / SNES_BLUE_SHIFT) % SNES_BLUE_MASK) * 8;
|
||||
|
||||
result.red += result.red / SNES_RED_MASK;
|
||||
result.green += result.green / SNES_GREEN_MASK;
|
||||
result.blue += result.blue / SNES_BLUE_MASK;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint16_t ConvertRGBtoSNES(const snes_color& color) {
|
||||
uint16_t red = color.red / 8;
|
||||
uint16_t green = color.green / 8;
|
||||
uint16_t blue = color.blue / 8;
|
||||
return (blue * SNES_BLUE_SHIFT) + (green * SNES_GREEN_SHIFT) + red;
|
||||
}
|
||||
|
||||
uint16_t ConvertRGBtoSNES(const ImVec4& color) {
|
||||
snes_color new_color;
|
||||
new_color.red = color.x * 255;
|
||||
new_color.green = color.y * 255;
|
||||
new_color.blue = color.z * 255;
|
||||
return ConvertRGBtoSNES(new_color);
|
||||
}
|
||||
|
||||
SnesColor ReadColorFromRom(int offset, const uint8_t* rom) {
|
||||
short color = (uint16_t)((rom[offset + 1]) << 8) | rom[offset];
|
||||
snes_color new_color;
|
||||
new_color.red = (color & 0x1F) * 8;
|
||||
new_color.green = ((color >> 5) & 0x1F) * 8;
|
||||
new_color.blue = ((color >> 10) & 0x1F) * 8;
|
||||
SnesColor snes_color(new_color);
|
||||
return snes_color;
|
||||
}
|
||||
|
||||
} // namespace gfx
|
||||
} // namespace app
|
||||
} // namespace yaze
|
||||
106
src/app/gfx/snes_color.h
Normal file
106
src/app/gfx/snes_color.h
Normal file
@@ -0,0 +1,106 @@
|
||||
#ifndef YAZE_APP_GFX_SNES_COLOR_H_
|
||||
#define YAZE_APP_GFX_SNES_COLOR_H_
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
namespace gfx {
|
||||
|
||||
struct snes_color {
|
||||
uint16_t red; /**< Red component of the color. */
|
||||
uint16_t blue; /**< Blue component of the color. */
|
||||
uint16_t green; /**< Green component of the color. */
|
||||
};
|
||||
typedef struct snes_color snes_color;
|
||||
|
||||
snes_color ConvertSNEStoRGB(uint16_t snes_color);
|
||||
uint16_t ConvertRGBtoSNES(const snes_color& color);
|
||||
uint16_t ConvertRGBtoSNES(const ImVec4& color);
|
||||
|
||||
// class SnesColor {
|
||||
// public:
|
||||
// SnesColor() : rgb(0.f, 0.f, 0.f, 0.f), snes(0) {}
|
||||
// SnesColor(const ImVec4 val) : rgb(val) {
|
||||
// snes_color color;
|
||||
// color.red = val.x / 255;
|
||||
// color.green = val.y / 255;
|
||||
// color.blue = val.z / 255;
|
||||
// snes = ConvertRGBtoSNES(color);
|
||||
// }
|
||||
// SnesColor(const snes_color internal_format) {
|
||||
// rgb.x = internal_format.red;
|
||||
// rgb.y = internal_format.green;
|
||||
// rgb.z = internal_format.blue;
|
||||
// snes = ConvertRGBtoSNES(internal_format);
|
||||
// }
|
||||
|
||||
// ImVec4 rgb; /**< The color in RGB format. */
|
||||
// uint16_t snes; /**< The color in SNES format. */
|
||||
// bool modified = false;
|
||||
// bool transparent = false;
|
||||
// };
|
||||
|
||||
class SnesColor {
|
||||
public:
|
||||
SnesColor() : rgb(0.f, 0.f, 0.f, 0.f), snes(0) {}
|
||||
|
||||
explicit SnesColor(const ImVec4 val) : rgb(val) {
|
||||
snes_color color;
|
||||
color.red = val.x / 255;
|
||||
color.green = val.y / 255;
|
||||
color.blue = val.z / 255;
|
||||
snes = ConvertRGBtoSNES(color);
|
||||
}
|
||||
|
||||
explicit SnesColor(const snes_color val)
|
||||
: rgb(val.red, val.green, val.blue, 255.f),
|
||||
snes(ConvertRGBtoSNES(val)),
|
||||
rom_color(val) {}
|
||||
|
||||
ImVec4 GetRGB() const { return rgb; }
|
||||
void SetRGB(const ImVec4 val) {
|
||||
rgb.x = val.x / 255;
|
||||
rgb.y = val.y / 255;
|
||||
rgb.z = val.z / 255;
|
||||
snes_color color;
|
||||
color.red = val.x;
|
||||
color.green = val.y;
|
||||
color.blue = val.z;
|
||||
rom_color = color;
|
||||
snes = ConvertRGBtoSNES(color);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
snes_color GetRomRGB() const { return rom_color; }
|
||||
|
||||
uint16_t GetSNES() const { return snes; }
|
||||
void SetSNES(uint16_t val) {
|
||||
snes = val;
|
||||
snes_color col = ConvertSNEStoRGB(val);
|
||||
rgb = ImVec4(col.red, col.green, col.blue, 0.f);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
bool IsModified() const { return modified; }
|
||||
bool IsTransparent() const { return transparent; }
|
||||
void SetTransparent(bool t) { transparent = t; }
|
||||
void SetModified(bool m) { modified = m; }
|
||||
|
||||
private:
|
||||
ImVec4 rgb;
|
||||
uint16_t snes;
|
||||
snes_color rom_color;
|
||||
bool modified = false;
|
||||
bool transparent = false;
|
||||
};
|
||||
|
||||
SnesColor ReadColorFromRom(int offset, const uint8_t* rom);
|
||||
|
||||
} // namespace gfx
|
||||
} // namespace app
|
||||
} // namespace yaze
|
||||
|
||||
#endif // YAZE_APP_GFX_SNES_COLOR_H_
|
||||
@@ -13,13 +13,14 @@
|
||||
#include "absl/container/flat_hash_map.h" // for flat_hash_map
|
||||
#include "absl/status/status.h" // for Status
|
||||
#include "app/core/constants.h"
|
||||
#include "app/gfx/snes_color.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
namespace gfx {
|
||||
|
||||
// Define a hash map to hold the addresses of different palette groups
|
||||
const absl::flat_hash_map<std::string, uint32_t> paletteGroupAddresses = {
|
||||
const absl::flat_hash_map<std::string, uint32_t> kPaletteGroupAddressMap = {
|
||||
{"ow_main", core::overworldPaletteMain},
|
||||
{"ow_aux", core::overworldPaletteAuxialiary},
|
||||
{"ow_animated", core::overworldPaletteAnimated},
|
||||
@@ -38,7 +39,7 @@ const absl::flat_hash_map<std::string, uint32_t> paletteGroupAddresses = {
|
||||
};
|
||||
|
||||
// Define a hash map to hold the number of colors in each palette group
|
||||
const absl::flat_hash_map<std::string, uint32_t> paletteGroupColorCounts = {
|
||||
const absl::flat_hash_map<std::string, uint32_t> kPaletteGroupColorCounts = {
|
||||
{"ow_main", 35}, {"ow_aux", 21}, {"ow_animated", 7},
|
||||
{"hud", 32}, {"global_sprites", 60}, {"armors", 15},
|
||||
{"swords", 3}, {"shields", 4}, {"sprites_aux1", 7},
|
||||
@@ -46,41 +47,6 @@ const absl::flat_hash_map<std::string, uint32_t> paletteGroupColorCounts = {
|
||||
{"grass", 1}, {"3d_object", 8}, {"ow_mini_map", 128},
|
||||
};
|
||||
|
||||
constexpr uint16_t SNES_RED_MASK = 32;
|
||||
constexpr uint16_t SNES_GREEN_MASK = 32;
|
||||
constexpr uint16_t SNES_BLUE_MASK = 32;
|
||||
|
||||
constexpr uint16_t SNES_GREEN_SHIFT = 32;
|
||||
constexpr uint16_t SNES_BLUE_SHIFT = 1024;
|
||||
|
||||
uint16_t ConvertRGBtoSNES(const snes_color& color) {
|
||||
uint16_t red = color.red / 8;
|
||||
uint16_t green = color.green / 8;
|
||||
uint16_t blue = color.blue / 8;
|
||||
return (blue * SNES_BLUE_SHIFT) + (green * SNES_GREEN_SHIFT) + red;
|
||||
}
|
||||
|
||||
uint16_t ConvertRGBtoSNES(const ImVec4& color) {
|
||||
snes_color new_color;
|
||||
new_color.red = color.x * 255;
|
||||
new_color.green = color.y * 255;
|
||||
new_color.blue = color.z * 255;
|
||||
return ConvertRGBtoSNES(new_color);
|
||||
}
|
||||
|
||||
snes_color ConvertSNEStoRGB(uint16_t color_snes) {
|
||||
snes_color result;
|
||||
|
||||
result.red = (color_snes % SNES_RED_MASK) * 8;
|
||||
result.green = ((color_snes / SNES_GREEN_MASK) % SNES_GREEN_MASK) * 8;
|
||||
result.blue = ((color_snes / SNES_BLUE_SHIFT) % SNES_BLUE_MASK) * 8;
|
||||
|
||||
result.red += result.red / SNES_RED_MASK;
|
||||
result.green += result.green / SNES_GREEN_MASK;
|
||||
result.blue += result.blue / SNES_BLUE_MASK;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<snes_color> Extract(const char* data, unsigned int offset,
|
||||
unsigned int palette_size) {
|
||||
@@ -103,28 +69,19 @@ std::vector<char> Convert(const std::vector<snes_color>& palette) {
|
||||
return data;
|
||||
}
|
||||
|
||||
SNESColor ReadColorFromROM(int offset, const uchar* rom) {
|
||||
short color = (ushort)((rom[offset + 1]) << 8) | rom[offset];
|
||||
snes_color new_color;
|
||||
new_color.red = (color & 0x1F) * 8;
|
||||
new_color.green = ((color >> 5) & 0x1F) * 8;
|
||||
new_color.blue = ((color >> 10) & 0x1F) * 8;
|
||||
SNESColor snes_color(new_color);
|
||||
return snes_color;
|
||||
}
|
||||
|
||||
SNESColor GetCgxColor(uint16_t color) {
|
||||
SnesColor GetCgxColor(uint16_t color) {
|
||||
ImVec4 rgb;
|
||||
rgb.x = (color & 0x1F) * 8;
|
||||
rgb.y = ((color & 0x3E0) >> 5) * 8;
|
||||
rgb.z = ((color & 0x7C00) >> 10) * 8;
|
||||
SNESColor toret;
|
||||
SnesColor toret;
|
||||
toret.SetRGB(rgb);
|
||||
return toret;
|
||||
}
|
||||
|
||||
std::vector<SNESColor> GetColFileData(uchar* data) {
|
||||
std::vector<SNESColor> colors;
|
||||
std::vector<SnesColor> GetColFileData(uchar* data) {
|
||||
std::vector<SnesColor> colors;
|
||||
colors.reserve(256);
|
||||
colors.resize(256);
|
||||
|
||||
@@ -137,64 +94,67 @@ std::vector<SNESColor> GetColFileData(uchar* data) {
|
||||
|
||||
// ============================================================================
|
||||
|
||||
SNESPalette::SNESPalette(uint8_t mSize) : size_(mSize) {
|
||||
SnesPalette::SnesPalette(uint8_t mSize) : size_(mSize) {
|
||||
for (unsigned int i = 0; i < mSize; i++) {
|
||||
SNESColor col;
|
||||
SnesColor col;
|
||||
colors.push_back(col);
|
||||
}
|
||||
size_ = mSize;
|
||||
}
|
||||
|
||||
SNESPalette::SNESPalette(char* data) : size_(sizeof(data) / 2) {
|
||||
SnesPalette::SnesPalette(char* data) : size_(sizeof(data) / 2) {
|
||||
assert((sizeof(data) % 4 == 0) && (sizeof(data) <= 32));
|
||||
for (unsigned i = 0; i < sizeof(data); i += 2) {
|
||||
SNESColor col;
|
||||
SnesColor col;
|
||||
col.SetSNES(static_cast<uchar>(data[i + 1]) << 8);
|
||||
col.SetSNES(col.GetSNES() | static_cast<uchar>(data[i]));
|
||||
snes_color mColor = ConvertSNEStoRGB(col.GetSNES());
|
||||
col.SetRGB(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
|
||||
colors.push_back(col);
|
||||
}
|
||||
size_ = sizeof(data) / 2;
|
||||
}
|
||||
|
||||
SNESPalette::SNESPalette(const unsigned char* snes_pal)
|
||||
SnesPalette::SnesPalette(const unsigned char* snes_pal)
|
||||
: size_(sizeof(snes_pal) / 2) {
|
||||
assert((sizeof(snes_pal) % 4 == 0) && (sizeof(snes_pal) <= 32));
|
||||
for (unsigned i = 0; i < sizeof(snes_pal); i += 2) {
|
||||
SNESColor col;
|
||||
SnesColor col;
|
||||
col.SetSNES(snes_pal[i + 1] << (uint16_t)8);
|
||||
col.SetSNES(col.GetSNES() | snes_pal[i]);
|
||||
snes_color mColor = ConvertSNEStoRGB(col.GetSNES());
|
||||
col.SetRGB(ImVec4(mColor.red, mColor.green, mColor.blue, 1.f));
|
||||
colors.push_back(col);
|
||||
}
|
||||
size_ = sizeof(snes_pal) / 2;
|
||||
}
|
||||
|
||||
SNESPalette::SNESPalette(const std::vector<ImVec4>& cols) {
|
||||
SnesPalette::SnesPalette(const std::vector<ImVec4>& cols) {
|
||||
for (const auto& each : cols) {
|
||||
SNESColor scol;
|
||||
SnesColor scol;
|
||||
scol.SetRGB(each);
|
||||
colors.push_back(scol);
|
||||
}
|
||||
size_ = cols.size();
|
||||
}
|
||||
|
||||
SNESPalette::SNESPalette(const std::vector<snes_color>& cols) {
|
||||
SnesPalette::SnesPalette(const std::vector<snes_color>& cols) {
|
||||
for (const auto& each : cols) {
|
||||
SNESColor scol;
|
||||
SnesColor scol;
|
||||
scol.SetSNES(ConvertRGBtoSNES(each));
|
||||
colors.push_back(scol);
|
||||
}
|
||||
size_ = cols.size();
|
||||
}
|
||||
|
||||
SNESPalette::SNESPalette(const std::vector<SNESColor>& cols) {
|
||||
SnesPalette::SnesPalette(const std::vector<SnesColor>& cols) {
|
||||
for (const auto& each : cols) {
|
||||
colors.push_back(each);
|
||||
}
|
||||
size_ = cols.size();
|
||||
}
|
||||
|
||||
SDL_Palette* SNESPalette::GetSDL_Palette() {
|
||||
SDL_Palette* SnesPalette::GetSDL_Palette() {
|
||||
auto sdl_palette = std::make_shared<SDL_Palette>();
|
||||
sdl_palette->ncolors = size_;
|
||||
|
||||
@@ -211,9 +171,9 @@ SDL_Palette* SNESPalette::GetSDL_Palette() {
|
||||
return sdl_palette.get();
|
||||
}
|
||||
|
||||
SNESPalette ReadPaletteFromROM(int offset, int num_colors, const uchar* rom) {
|
||||
SnesPalette ReadPaletteFromROM(int offset, int num_colors, const uchar* rom) {
|
||||
int color_offset = 0;
|
||||
std::vector<gfx::SNESColor> colors(num_colors);
|
||||
std::vector<gfx::SnesColor> colors(num_colors);
|
||||
|
||||
while (color_offset < num_colors) {
|
||||
short color = (ushort)((rom[offset + 1]) << 8) | rom[offset];
|
||||
@@ -229,17 +189,17 @@ SNESPalette ReadPaletteFromROM(int offset, int num_colors, const uchar* rom) {
|
||||
offset += 2;
|
||||
}
|
||||
|
||||
gfx::SNESPalette palette(colors);
|
||||
gfx::SnesPalette palette(colors);
|
||||
return palette;
|
||||
}
|
||||
|
||||
uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
|
||||
size_t color_index) {
|
||||
// Retrieve the base address for the palette group
|
||||
uint32_t base_address = paletteGroupAddresses.at(group_name);
|
||||
uint32_t base_address = kPaletteGroupAddressMap.at(group_name);
|
||||
|
||||
// Retrieve the number of colors for each palette in the group
|
||||
uint32_t colors_per_palette = paletteGroupColorCounts.at(group_name);
|
||||
uint32_t colors_per_palette = kPaletteGroupColorCounts.at(group_name);
|
||||
|
||||
// Calculate the address for thes specified color in the ROM
|
||||
uint32_t address = base_address + (palette_index * colors_per_palette * 2) +
|
||||
@@ -248,7 +208,7 @@ uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
|
||||
return address;
|
||||
}
|
||||
|
||||
std::array<float, 4> ToFloatArray(const SNESColor& color) {
|
||||
std::array<float, 4> ToFloatArray(const SnesColor& color) {
|
||||
std::array<float, 4> colorArray;
|
||||
colorArray[0] = color.GetRGB().x / 255.0f;
|
||||
colorArray[1] = color.GetRGB().y / 255.0f;
|
||||
@@ -260,11 +220,11 @@ std::array<float, 4> ToFloatArray(const SNESColor& color) {
|
||||
PaletteGroup::PaletteGroup(uint8_t mSize) : size_(mSize) {}
|
||||
|
||||
PaletteGroup CreatePaletteGroupFromColFile(
|
||||
std::vector<SNESColor>& palette_rows) {
|
||||
std::vector<SnesColor>& palette_rows) {
|
||||
PaletteGroup toret;
|
||||
|
||||
for (int i = 0; i < palette_rows.size(); i += 8) {
|
||||
SNESPalette palette;
|
||||
SnesPalette palette;
|
||||
for (int j = 0; j < 8; j++) {
|
||||
palette.AddColor(palette_rows[i + j].GetRomRGB());
|
||||
}
|
||||
@@ -275,11 +235,11 @@ PaletteGroup CreatePaletteGroupFromColFile(
|
||||
|
||||
// Take a SNESPalette with N many colors and divide it into palettes of 8 colors
|
||||
// each
|
||||
PaletteGroup CreatePaletteGroupFromLargePalette(SNESPalette& palette) {
|
||||
PaletteGroup CreatePaletteGroupFromLargePalette(SnesPalette& palette) {
|
||||
PaletteGroup toret;
|
||||
|
||||
for (int i = 0; i < palette.size(); i += 8) {
|
||||
SNESPalette new_palette;
|
||||
SnesPalette new_palette;
|
||||
if (i + 8 < palette.size()) {
|
||||
for (int j = 0; j < 8; j++) {
|
||||
new_palette.AddColor(palette[i + j]);
|
||||
|
||||
@@ -14,18 +14,12 @@
|
||||
#include "absl/base/casts.h"
|
||||
#include "absl/status/status.h"
|
||||
#include "app/core/constants.h"
|
||||
#include "app/gfx/snes_color.h"
|
||||
|
||||
namespace yaze {
|
||||
namespace app {
|
||||
namespace gfx {
|
||||
|
||||
struct snes_color {
|
||||
uint16_t red; /**< Red component of the color. */
|
||||
uint16_t blue; /**< Blue component of the color. */
|
||||
uint16_t green; /**< Green component of the color. */
|
||||
};
|
||||
using snes_color = struct snes_color;
|
||||
|
||||
struct snes_palette {
|
||||
uint id; /**< ID of the palette. */
|
||||
uint size; /**< Size of the palette. */
|
||||
@@ -33,10 +27,6 @@ struct snes_palette {
|
||||
};
|
||||
using snes_palette = struct snes_palette;
|
||||
|
||||
uint16_t ConvertRGBtoSNES(const snes_color& color);
|
||||
uint16_t ConvertRGBtoSNES(const ImVec4& color);
|
||||
snes_color ConvertSNEStoRGB(uint16_t snes_color);
|
||||
|
||||
/**
|
||||
* @brief Extracts a vector of SNES colors from a data buffer.
|
||||
*
|
||||
@@ -56,92 +46,38 @@ std::vector<snes_color> Extract(const char* data, unsigned int offset,
|
||||
*/
|
||||
std::vector<char> Convert(const std::vector<snes_color>& palette);
|
||||
|
||||
struct SNESColor {
|
||||
SNESColor() : rgb(0.f, 0.f, 0.f, 0.f), snes(0) {}
|
||||
SnesColor GetCgxColor(uint16_t color);
|
||||
std::vector<SnesColor> GetColFileData(uint8_t* data);
|
||||
|
||||
explicit SNESColor(const ImVec4 val) : rgb(val) {
|
||||
snes_color color;
|
||||
color.red = val.x / 255;
|
||||
color.green = val.y / 255;
|
||||
color.blue = val.z / 255;
|
||||
snes = ConvertRGBtoSNES(color);
|
||||
}
|
||||
|
||||
explicit SNESColor(const snes_color val)
|
||||
: rgb(val.red, val.green, val.blue, 255.f),
|
||||
snes(ConvertRGBtoSNES(val)),
|
||||
rom_color(val) {}
|
||||
|
||||
ImVec4 GetRGB() const { return rgb; }
|
||||
void SetRGB(const ImVec4 val) {
|
||||
rgb.x = val.x / 255;
|
||||
rgb.y = val.y / 255;
|
||||
rgb.z = val.z / 255;
|
||||
snes_color color;
|
||||
color.red = val.x;
|
||||
color.green = val.y;
|
||||
color.blue = val.z;
|
||||
rom_color = color;
|
||||
snes = ConvertRGBtoSNES(color);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
snes_color GetRomRGB() const { return rom_color; }
|
||||
|
||||
uint16_t GetSNES() const { return snes; }
|
||||
void SetSNES(uint16_t val) {
|
||||
snes = val;
|
||||
snes_color col = ConvertSNEStoRGB(val);
|
||||
rgb = ImVec4(col.red, col.green, col.blue, 0.f);
|
||||
modified = true;
|
||||
}
|
||||
|
||||
bool IsModified() const { return modified; }
|
||||
bool IsTransparent() const { return transparent; }
|
||||
void SetTransparent(bool t) { transparent = t; }
|
||||
void SetModified(bool m) { modified = m; }
|
||||
|
||||
private:
|
||||
ImVec4 rgb;
|
||||
uint16_t snes;
|
||||
snes_color rom_color;
|
||||
bool modified = false;
|
||||
bool transparent = false;
|
||||
};
|
||||
|
||||
gfx::SNESColor ReadColorFromROM(int offset, const uint8_t* rom);
|
||||
|
||||
SNESColor GetCgxColor(uint16_t color);
|
||||
std::vector<SNESColor> GetColFileData(uint8_t* data);
|
||||
|
||||
class SNESPalette {
|
||||
class SnesPalette {
|
||||
public:
|
||||
template <typename T>
|
||||
explicit SNESPalette(const std::vector<T>& data) {
|
||||
explicit SnesPalette(const std::vector<T>& data) {
|
||||
for (const auto& item : data) {
|
||||
colors.push_back(SNESColor(item));
|
||||
colors.push_back(SnesColor(item));
|
||||
}
|
||||
size_ = data.size();
|
||||
}
|
||||
|
||||
SNESPalette() = default;
|
||||
SnesPalette() = default;
|
||||
|
||||
explicit SNESPalette(uint8_t mSize);
|
||||
explicit SNESPalette(char* snesPal);
|
||||
explicit SNESPalette(const unsigned char* snes_pal);
|
||||
explicit SNESPalette(const std::vector<ImVec4>&);
|
||||
explicit SNESPalette(const std::vector<snes_color>&);
|
||||
explicit SNESPalette(const std::vector<SNESColor>&);
|
||||
explicit SnesPalette(uint8_t mSize);
|
||||
explicit SnesPalette(char* snesPal);
|
||||
explicit SnesPalette(const unsigned char* snes_pal);
|
||||
explicit SnesPalette(const std::vector<ImVec4>&);
|
||||
explicit SnesPalette(const std::vector<snes_color>&);
|
||||
explicit SnesPalette(const std::vector<SnesColor>&);
|
||||
|
||||
SDL_Palette* GetSDL_Palette();
|
||||
|
||||
void Create(const std::vector<SNESColor>& cols) {
|
||||
void Create(const std::vector<SnesColor>& cols) {
|
||||
for (const auto& each : cols) {
|
||||
colors.push_back(each);
|
||||
}
|
||||
size_ = cols.size();
|
||||
}
|
||||
|
||||
void AddColor(SNESColor color) {
|
||||
void AddColor(SnesColor color) {
|
||||
colors.push_back(color);
|
||||
size_++;
|
||||
}
|
||||
@@ -166,7 +102,7 @@ class SNESPalette {
|
||||
|
||||
auto size() const { return colors.size(); }
|
||||
|
||||
SNESColor& operator[](int i) {
|
||||
SnesColor& operator[](int i) {
|
||||
if (i > size_) {
|
||||
std::cout << "SNESPalette: Index out of bounds" << std::endl;
|
||||
return colors[0];
|
||||
@@ -174,7 +110,7 @@ class SNESPalette {
|
||||
return colors[i];
|
||||
}
|
||||
|
||||
void operator()(int i, const SNESColor& color) {
|
||||
void operator()(int i, const SnesColor& color) {
|
||||
if (i >= size_) {
|
||||
throw std::out_of_range("SNESPalette: Index out of bounds");
|
||||
}
|
||||
@@ -189,8 +125,8 @@ class SNESPalette {
|
||||
colors[i].SetModified(true);
|
||||
}
|
||||
|
||||
SNESPalette sub_palette(int start, int end) const {
|
||||
SNESPalette pal;
|
||||
SnesPalette sub_palette(int start, int end) const {
|
||||
SnesPalette pal;
|
||||
for (int i = start; i < end; i++) {
|
||||
pal.AddColor(colors[i]);
|
||||
}
|
||||
@@ -199,13 +135,13 @@ class SNESPalette {
|
||||
|
||||
private:
|
||||
int size_ = 0; /**< The size of the palette. */
|
||||
std::vector<SNESColor> colors; /**< The colors in the palette. */
|
||||
std::vector<SnesColor> colors; /**< The colors in the palette. */
|
||||
};
|
||||
|
||||
SNESPalette ReadPaletteFromROM(int offset, int num_colors, const uint8_t* rom);
|
||||
SnesPalette ReadPaletteFromROM(int offset, int num_colors, const uint8_t* rom);
|
||||
uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
|
||||
size_t color_index);
|
||||
std::array<float, 4> ToFloatArray(const SNESColor& color);
|
||||
std::array<float, 4> ToFloatArray(const SnesColor& color);
|
||||
|
||||
struct PaletteGroup {
|
||||
PaletteGroup() = default;
|
||||
@@ -214,13 +150,13 @@ struct PaletteGroup {
|
||||
|
||||
auto mutable_palette(int i) { return &palettes[i]; }
|
||||
|
||||
absl::Status AddPalette(SNESPalette pal) {
|
||||
absl::Status AddPalette(SnesPalette pal) {
|
||||
palettes.emplace_back(pal);
|
||||
size_ = palettes.size();
|
||||
return absl::OkStatus();
|
||||
}
|
||||
|
||||
absl::Status AddColor(SNESColor color) {
|
||||
absl::Status AddColor(SnesColor color) {
|
||||
if (size_ == 0) {
|
||||
palettes.emplace_back();
|
||||
}
|
||||
@@ -235,7 +171,7 @@ struct PaletteGroup {
|
||||
|
||||
auto size() const { return palettes.size(); }
|
||||
|
||||
SNESPalette operator[](int i) {
|
||||
SnesPalette operator[](int i) {
|
||||
if (i > size_) {
|
||||
std::cout << "PaletteGroup: Index out of bounds" << std::endl;
|
||||
return palettes[0];
|
||||
@@ -243,7 +179,7 @@ struct PaletteGroup {
|
||||
return palettes[i];
|
||||
}
|
||||
|
||||
const SNESPalette& operator[](int i) const {
|
||||
const SnesPalette& operator[](int i) const {
|
||||
if (i > size_) {
|
||||
std::cout << "PaletteGroup: Index out of bounds" << std::endl;
|
||||
return palettes[0];
|
||||
@@ -251,7 +187,7 @@ struct PaletteGroup {
|
||||
return palettes[i];
|
||||
}
|
||||
|
||||
absl::Status operator()(int i, const SNESColor& color) {
|
||||
absl::Status operator()(int i, const SnesColor& color) {
|
||||
if (i >= size_) {
|
||||
return absl::InvalidArgumentError("PaletteGroup: Index out of bounds");
|
||||
}
|
||||
@@ -269,19 +205,19 @@ struct PaletteGroup {
|
||||
|
||||
private:
|
||||
int size_ = 0;
|
||||
std::vector<SNESPalette> palettes;
|
||||
std::vector<SnesPalette> palettes;
|
||||
};
|
||||
|
||||
PaletteGroup CreatePaletteGroupFromColFile(std::vector<SNESColor>& colors);
|
||||
PaletteGroup CreatePaletteGroupFromColFile(std::vector<SnesColor>& colors);
|
||||
|
||||
PaletteGroup CreatePaletteGroupFromLargePalette(SNESPalette& palette);
|
||||
PaletteGroup CreatePaletteGroupFromLargePalette(SnesPalette& palette);
|
||||
|
||||
struct Paletteset {
|
||||
Paletteset() = default;
|
||||
Paletteset(gfx::SNESPalette main, gfx::SNESPalette animated,
|
||||
gfx::SNESPalette aux1, gfx::SNESPalette aux2,
|
||||
gfx::SNESColor background, gfx::SNESPalette hud,
|
||||
gfx::SNESPalette spr, gfx::SNESPalette spr2)
|
||||
Paletteset(gfx::SnesPalette main, gfx::SnesPalette animated,
|
||||
gfx::SnesPalette aux1, gfx::SnesPalette aux2,
|
||||
gfx::SnesColor background, gfx::SnesPalette hud,
|
||||
gfx::SnesPalette spr, gfx::SnesPalette spr2, gfx::SnesPalette comp)
|
||||
: main(main),
|
||||
animated(animated),
|
||||
aux1(aux1),
|
||||
@@ -289,15 +225,17 @@ struct Paletteset {
|
||||
background(background),
|
||||
hud(hud),
|
||||
spr(spr),
|
||||
spr2(spr2) {}
|
||||
gfx::SNESPalette main;
|
||||
gfx::SNESPalette animated;
|
||||
gfx::SNESPalette aux1;
|
||||
gfx::SNESPalette aux2;
|
||||
gfx::SNESColor background;
|
||||
gfx::SNESPalette hud;
|
||||
gfx::SNESPalette spr;
|
||||
gfx::SNESPalette spr2;
|
||||
spr2(spr2),
|
||||
composite(comp) {}
|
||||
gfx::SnesPalette main;
|
||||
gfx::SnesPalette animated;
|
||||
gfx::SnesPalette aux1;
|
||||
gfx::SnesPalette aux2;
|
||||
gfx::SnesColor background;
|
||||
gfx::SnesPalette hud;
|
||||
gfx::SnesPalette spr;
|
||||
gfx::SnesPalette spr2;
|
||||
gfx::SnesPalette composite;
|
||||
};
|
||||
|
||||
} // namespace gfx
|
||||
|
||||
@@ -123,7 +123,7 @@ class Tilesheet {
|
||||
auto num_tiles() const { return num_tiles_; }
|
||||
auto tile_width() const { return tile_width_; }
|
||||
auto tile_height() const { return tile_height_; }
|
||||
auto set_palette(gfx::SNESPalette& palette) { palette_ = palette; }
|
||||
auto set_palette(gfx::SnesPalette& palette) { palette_ = palette; }
|
||||
auto palette() const { return palette_; }
|
||||
auto tile_type() const { return tile_type_; }
|
||||
auto tile_info() const { return tile_info_; }
|
||||
@@ -217,7 +217,7 @@ class Tilesheet {
|
||||
tileDataOffset);
|
||||
}
|
||||
|
||||
gfx::SNESPalette palette_;
|
||||
gfx::SnesPalette palette_;
|
||||
std::vector<uint8_t> internal_data_;
|
||||
std::shared_ptr<Bitmap> bitmap_;
|
||||
struct InternalTile16 {
|
||||
|
||||
Reference in New Issue
Block a user