yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_BITMAP_H
2#define YAZE_APP_GFX_BITMAP_H
3
5
6#include <cstdint>
7#include <memory>
8#include <span>
9#include <unordered_map>
10#include <vector>
11
14
15namespace yaze {
16
21namespace gfx {
22
23// Pixel format constants
24constexpr Uint32 SNES_PIXELFORMAT_INDEXED =
25 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1);
26
27constexpr Uint32 SNES_PIXELFORMAT_4BPP = SDL_DEFINE_PIXELFORMAT(
28 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
29 /*layouts=*/0, /*bits=*/4, /*bytes=*/1);
30
31constexpr Uint32 SNES_PIXELFORMAT_8BPP = SDL_DEFINE_PIXELFORMAT(
32 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
33 /*layouts=*/0, /*bits=*/8, /*bytes=*/1);
34
37 k4bpp = 1,
38 k8bpp = 2,
39};
40
67class Bitmap {
68 public:
69 Bitmap() = default;
70
80 Bitmap(int width, int height, int depth, const std::vector<uint8_t>& data);
81
90 Bitmap(int width, int height, int depth, const std::vector<uint8_t>& data,
91 const SnesPalette& palette);
92
96 Bitmap(const Bitmap& other);
97
101 Bitmap& operator=(const Bitmap& other);
102
106 Bitmap(Bitmap&& other) noexcept;
107
111 Bitmap& operator=(Bitmap&& other) noexcept;
112
116 ~Bitmap() = default;
117
121 void Create(int width, int height, int depth, std::span<uint8_t> data);
122
126 void Create(int width, int height, int depth,
127 const std::vector<uint8_t>& data);
128
132 void Create(int width, int height, int depth, int format,
133 const std::vector<uint8_t>& data);
134
138 void Reformat(int format);
139
143 void Fill(uint8_t value) {
144 std::fill(data_.begin(), data_.end(), value);
145 modified_ = true;
146 }
147
151 void CreateTexture();
152
156 void UpdateTexture();
157
164
169
198 void SetPalette(const SnesPalette& palette);
199
203 void SetPaletteWithTransparent(const SnesPalette& palette, size_t index,
204 int length = 7);
205
211 int sub_palette_index = 0);
212
216 void ApplyStoredPalette();
217
222 void UpdateSurfacePixels();
223
249 void SetPalette(const std::vector<SDL_Color>& palette);
250
254 void WriteToPixel(int position, uint8_t value);
255
262 void WriteToPixel(int x, int y, uint8_t value) {
263 if (x >= 0 && x < width_ && y >= 0 && y < height_) {
264 WriteToPixel(y * width_ + x, value);
265 }
266 }
267
274 uint8_t GetPixel(int x, int y) const {
275 if (x >= 0 && x < width_ && y >= 0 && y < height_) {
276 return data_[y * width_ + x];
277 }
278 return 0;
279 }
280
284 void WriteColor(int position, const ImVec4& color);
285
294 void SetPixel(int x, int y, const SnesColor& color);
295
302 void Resize(int new_width, int new_height);
303
310
317 uint8_t FindColorIndex(const SnesColor& color);
318
325
335 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t>& tile_data,
336 int& tile_data_offset);
337
346 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t>& tile_data,
347 int& tile_data_offset);
348
360 enum class BitmapPurpose : uint8_t {
361 kPreview, // Read-only preview (e.g. gfx-group sheet thumbnails).
362 kEditable, // User-editable scratchpad (default).
363 kSelectionSource, // Read-only tile/region picker source.
364 kCompositeOutput, // Post-render composite (room renderer output, etc).
365 };
366
371 int source_bpp = 8; // Original bits per pixel (3, 4, 8)
372 int palette_format = 0; // 0=full palette, 1=sub-palette with transparent
373 std::string
374 source_type; // "graphics_sheet", "tilemap", "screen_buffer", "mode7"
375 int palette_colors = 256; // Expected palette size
376 // Role this bitmap plays. Optional metadata for editors that want to
377 // distinguish preview / editable / selection / composite bitmaps.
379
380 BitmapMetadata() = default;
381 BitmapMetadata(int bpp, int format, const std::string& type,
382 int colors = 256)
383 : source_bpp(bpp),
384 palette_format(format),
385 source_type(type),
386 palette_colors(colors) {}
387 };
388
389 const SnesPalette& palette() const { return palette_; }
392 const BitmapMetadata& metadata() const { return metadata_; }
393
394 int width() const { return width_; }
395 int height() const { return height_; }
396 int depth() const { return depth_; }
397 auto size() const { return data_.size(); }
398 const uint8_t* data() const { return data_.data(); }
399 std::vector<uint8_t>& mutable_data() { return data_; }
400 SDL_Surface* surface() const { return surface_; }
401 TextureHandle texture() const { return texture_; }
402 const std::vector<uint8_t>& vector() const { return data_; }
403 uint8_t at(int i) const { return data_[i]; }
404 bool modified() const { return modified_; }
405 bool is_active() const { return active_; }
406 uint32_t generation() const { return generation_; }
407 void set_active(bool active) { active_ = active; }
408 void set_data(const std::vector<uint8_t>& data);
411
412 private:
413 int width_ = 0;
414 int height_ = 0;
415 int depth_ = 0;
416
417 bool active_ = false;
418 bool modified_ = false;
419
420 // Generation counter for staleness detection in deferred operations
421 // Incremented on each Create() call to detect reused/reallocated bitmaps
423 static inline uint32_t next_generation_ = 1;
424
425 // Pointer to the texture pixels
426 void* texture_pixels = nullptr;
427
428 // Pointer to the pixel data
430
446
447 // Metadata for tracking source format and palette requirements
449
450 // Data for the bitmap (indexed pixel values, 0-255)
451 std::vector<uint8_t> data_;
452
477
478 // Texture for the bitmap (managed by Arena)
480
481 // Optimized palette lookup cache for O(1) color index lookups
482 std::unordered_map<uint32_t, uint8_t> color_to_index_cache_;
483
484 // Dirty region tracking for efficient texture updates
485 struct DirtyRegion {
486 int min_x = 0, min_y = 0, max_x = 0, max_y = 0;
487 bool is_dirty = false;
488
489 void Reset() {
490 min_x = min_y = max_x = max_y = 0;
491 is_dirty = false;
492 }
493
494 void AddPoint(int x, int y) {
495 if (!is_dirty) {
496 min_x = max_x = x;
497 min_y = max_y = y;
498 is_dirty = true;
499 } else {
500 min_x = std::min(min_x, x);
501 min_y = std::min(min_y, y);
502 max_x = std::max(max_x, x);
503 max_y = std::max(max_y, y);
504 }
505 }
507
513 static uint32_t HashColor(const ImVec4& color);
514};
515
516// Type alias for a table of bitmaps - uses unique_ptr for stable pointers
517// across rehashes (prevents dangling pointers in deferred texture commands)
518using BitmapTable = std::unordered_map<int, std::unique_ptr<gfx::Bitmap>>;
519
523Uint32 GetSnesPixelFormat(int format);
524
525} // namespace gfx
526} // namespace yaze
527
528#endif // YAZE_APP_GFX_BITMAP_H
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
const uint8_t * data() const
Definition bitmap.h:398
void set_texture(TextureHandle texture)
Definition bitmap.h:410
const SnesPalette & palette() const
Definition bitmap.h:389
SDL_Surface * surface_
SDL surface for rendering (contains the authoritative palette)
Definition bitmap.h:476
Bitmap & operator=(const Bitmap &other)
Copy assignment operator.
Definition bitmap.cc:90
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
Definition bitmap.cc:581
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
Definition bitmap.cc:201
TextureHandle texture() const
Definition bitmap.h:401
~Bitmap()=default
Destructor.
BitmapPurpose
How a bitmap is used in the editor pipeline.
Definition bitmap.h:360
bool ValidateDataSurfaceSync()
Validate that bitmap data and surface pixels are synchronized.
Definition bitmap.cc:874
const std::vector< uint8_t > & vector() const
Definition bitmap.h:402
auto size() const
Definition bitmap.h:397
void UpdateSurfacePixels()
Update SDL surface with current pixel data from data_ vector Call this after modifying pixel data via...
Definition bitmap.cc:369
std::unordered_map< uint32_t, uint8_t > color_to_index_cache_
Definition bitmap.h:482
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
Definition bitmap.cc:277
void WriteToPixel(int x, int y, uint8_t value)
Write a palette index to a pixel at the given x,y coordinates.
Definition bitmap.h:262
uint8_t * pixel_data_
Definition bitmap.h:429
const BitmapMetadata & metadata() const
Definition bitmap.h:392
static uint32_t HashColor(const ImVec4 &color)
Hash a color for cache lookup.
Definition bitmap.cc:804
void Get8x8Tile(int tile_index, int x, int y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Extract an 8x8 tile from the bitmap (SNES standard tile size)
Definition bitmap.cc:676
BitmapMetadata & metadata()
Definition bitmap.h:391
void CreateTexture()
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:293
bool is_active() const
Definition bitmap.h:405
uint32_t generation_
Definition bitmap.h:422
SnesPalette * mutable_palette()
Definition bitmap.h:390
void QueueTextureUpdate(IRenderer *renderer)
Queue texture update for batch processing (improved performance)
void set_modified(bool modified)
Definition bitmap.h:409
void set_active(bool active)
Definition bitmap.h:407
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Definition bitmap.cc:632
int height() const
Definition bitmap.h:395
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.cc:853
uint8_t at(int i) const
Definition bitmap.h:403
void Resize(int new_width, int new_height)
Resize the bitmap to new dimensions (preserves existing data)
Definition bitmap.cc:754
static uint32_t next_generation_
Definition bitmap.h:423
void Fill(uint8_t value)
Fill the bitmap with a specific value.
Definition bitmap.h:143
void SetPixel(int x, int y, const SnesColor &color)
Set a pixel at the given x,y coordinates with SNES color.
Definition bitmap.cc:726
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
Definition bitmap.cc:384
int width() const
Definition bitmap.h:394
BitmapMetadata metadata_
Definition bitmap.h:448
void ApplyStoredPalette()
Apply the stored palette to the surface (internal helper)
Definition bitmap.cc:317
std::vector< uint8_t > data_
Definition bitmap.h:451
int depth() const
Definition bitmap.h:396
void InvalidatePaletteCache()
Invalidate the palette lookup cache (call when palette changes)
Definition bitmap.cc:825
uint32_t generation() const
Definition bitmap.h:406
uint8_t GetPixel(int x, int y) const
Get the palette index at the given x,y coordinates.
Definition bitmap.h:274
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:456
std::vector< uint8_t > & mutable_data()
Definition bitmap.h:399
struct yaze::gfx::Bitmap::DirtyRegion dirty_region_
void * texture_pixels
Definition bitmap.h:426
void ApplyPaletteByMetadata(const SnesPalette &palette, int sub_palette_index=0)
Apply palette using metadata-driven strategy Chooses between SetPalette and SetPaletteWithTransparent...
Definition bitmap.cc:412
TextureHandle texture_
Definition bitmap.h:479
SDL_Surface * surface() const
Definition bitmap.h:400
void Get16x16Tile(int tile_x, int tile_y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Extract a 16x16 tile from the bitmap (SNES metatile size)
Definition bitmap.cc:692
uint8_t FindColorIndex(const SnesColor &color)
Find color index in palette using optimized hash map lookup.
Definition bitmap.cc:846
void UpdateTexture()
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:297
bool modified() const
Definition bitmap.h:404
gfx::SnesPalette palette_
Internal SNES palette storage (may be empty!)
Definition bitmap.h:445
void UpdateTextureData()
Updates the texture data from the surface.
Defines an abstract interface for all rendering operations.
Definition irenderer.h:60
SNES Color container.
Definition snes_color.h:110
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
BitmapFormat
Definition bitmap.h:35
@ kIndexed
Definition bitmap.h:36
@ k8bpp
Definition bitmap.h:38
@ k4bpp
Definition bitmap.h:37
std::unordered_map< int, std::unique_ptr< gfx::Bitmap > > BitmapTable
Definition bitmap.h:518
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:31
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:24
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:47
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:27
Uint32 GetSnesPixelFormat(int format)
Convert bitmap format enum to SDL pixel format.
Definition bitmap.cc:33
SDL2/SDL3 compatibility layer.
Metadata for tracking bitmap source format and palette requirements.
Definition bitmap.h:370
BitmapMetadata(int bpp, int format, const std::string &type, int colors=256)
Definition bitmap.h:381
void AddPoint(int x, int y)
Definition bitmap.h:494