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

@@ -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_; }