Canvas, Palettes, Bitmap updates

This commit is contained in:
scawful
2023-12-25 17:32:56 -06:00
parent 4d05f95312
commit a73c944529
11 changed files with 208 additions and 129 deletions

View File

@@ -348,8 +348,8 @@ void Bitmap::ApplyPalette(const SNESPalette &palette) {
void Bitmap::ApplyPaletteWithTransparent(const SNESPalette &palette,
int index) {
palette_ = palette;
auto start_index = index * 7;
palette_ = palette.sub_palette(start_index, start_index + 7);
std::vector<ImVec4> colors;
colors.push_back(ImVec4(0, 0, 0, 0));
for (int i = start_index; i < start_index + 7; ++i) {

View File

@@ -81,6 +81,40 @@ class Bitmap {
}
}
Uint8 ConvertImVec4ToIndex8(const ImVec4 &color, SDL_Surface *surface) {
if (surface->format->format != SDL_PIXELFORMAT_INDEX8) {
throw std::runtime_error(
"Surface is not in SDL_PIXELFORMAT_INDEX8 format.");
}
// Convert ImVec4 (RGBA) to SDL_Color (RGBA)
SDL_Color sdl_color;
sdl_color.r = static_cast<Uint8>(color.x * 255);
sdl_color.g = static_cast<Uint8>(color.y * 255);
sdl_color.b = static_cast<Uint8>(color.z * 255);
sdl_color.a = static_cast<Uint8>(color.w * 255);
// Map SDL_Color to the nearest color index in the surface's palette
return SDL_MapRGB(surface->format, sdl_color.r, sdl_color.g, sdl_color.b);
}
void WriteColor(int position, const ImVec4 &color) {
// Convert ImVec4 (RGBA) to SDL_Color (RGBA)
SDL_Color sdl_color;
sdl_color.r = static_cast<Uint8>(color.x * 255);
sdl_color.g = static_cast<Uint8>(color.y * 255);
sdl_color.b = static_cast<Uint8>(color.z * 255);
sdl_color.a = static_cast<Uint8>(color.w * 255);
// Map SDL_Color to the nearest color index in the surface's palette
Uint8 index =
SDL_MapRGB(surface_->format, sdl_color.r, sdl_color.g, sdl_color.b);
// Write the color index to the pixel data
this->pixel_data_[position] = index;
modified_ = true;
}
void Cleanup() {
// Reset texture_
if (texture_) {
@@ -106,6 +140,15 @@ class Bitmap {
palette_.Clear();
}
auto sdl_palette() {
if (surface_ == nullptr) {
throw std::runtime_error("Surface is null.");
}
return surface_->format->palette;
}
auto palette() const { return palette_; }
auto palette_size() const { return palette_.size(); }
int width() const { return width_; }
int height() const { return height_; }
auto depth() const { return depth_; }

View File

@@ -187,6 +187,14 @@ class SNESPalette {
colors[i].SetModified(true);
}
SNESPalette sub_palette(int start, int end) const {
SNESPalette pal;
for (int i = start; i < end; i++) {
pal.AddColor(colors[i]);
}
return pal;
}
private:
int size_ = 0; /**< The size of the palette. */
std::vector<SNESColor> colors; /**< The colors in the palette. */