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
353 int source_bpp = 8; // Original bits per pixel (3, 4, 8)
354 int palette_format = 0; // 0=full palette, 1=sub-palette with transparent
355 std::string
356 source_type; // "graphics_sheet", "tilemap", "screen_buffer", "mode7"
357 int palette_colors = 256; // Expected palette size
358
359 BitmapMetadata() = default;
360 BitmapMetadata(int bpp, int format, const std::string& type,
361 int colors = 256)
362 : source_bpp(bpp),
363 palette_format(format),
364 source_type(type),
365 palette_colors(colors) {}
366 };
367
368 const SnesPalette& palette() const { return palette_; }
371 const BitmapMetadata& metadata() const { return metadata_; }
372
373 int width() const { return width_; }
374 int height() const { return height_; }
375 int depth() const { return depth_; }
376 auto size() const { return data_.size(); }
377 const uint8_t* data() const { return data_.data(); }
378 std::vector<uint8_t>& mutable_data() { return data_; }
379 SDL_Surface* surface() const { return surface_; }
380 TextureHandle texture() const { return texture_; }
381 const std::vector<uint8_t>& vector() const { return data_; }
382 uint8_t at(int i) const { return data_[i]; }
383 bool modified() const { return modified_; }
384 bool is_active() const { return active_; }
385 uint32_t generation() const { return generation_; }
386 void set_active(bool active) { active_ = active; }
387 void set_data(const std::vector<uint8_t>& data);
390
391 private:
392 int width_ = 0;
393 int height_ = 0;
394 int depth_ = 0;
395
396 bool active_ = false;
397 bool modified_ = false;
398
399 // Generation counter for staleness detection in deferred operations
400 // Incremented on each Create() call to detect reused/reallocated bitmaps
402 static inline uint32_t next_generation_ = 1;
403
404 // Pointer to the texture pixels
405 void* texture_pixels = nullptr;
406
407 // Pointer to the pixel data
409
425
426 // Metadata for tracking source format and palette requirements
428
429 // Data for the bitmap (indexed pixel values, 0-255)
430 std::vector<uint8_t> data_;
431
456
457 // Texture for the bitmap (managed by Arena)
459
460 // Optimized palette lookup cache for O(1) color index lookups
461 std::unordered_map<uint32_t, uint8_t> color_to_index_cache_;
462
463 // Dirty region tracking for efficient texture updates
464 struct DirtyRegion {
465 int min_x = 0, min_y = 0, max_x = 0, max_y = 0;
466 bool is_dirty = false;
467
468 void Reset() {
469 min_x = min_y = max_x = max_y = 0;
470 is_dirty = false;
471 }
472
473 void AddPoint(int x, int y) {
474 if (!is_dirty) {
475 min_x = max_x = x;
476 min_y = max_y = y;
477 is_dirty = true;
478 } else {
479 min_x = std::min(min_x, x);
480 min_y = std::min(min_y, y);
481 max_x = std::max(max_x, x);
482 max_y = std::max(max_y, y);
483 }
484 }
486
492 static uint32_t HashColor(const ImVec4& color);
493};
494
495// Type alias for a table of bitmaps - uses unique_ptr for stable pointers
496// across rehashes (prevents dangling pointers in deferred texture commands)
497using BitmapTable = std::unordered_map<int, std::unique_ptr<gfx::Bitmap>>;
498
502Uint32 GetSnesPixelFormat(int format);
503
504} // namespace gfx
505} // namespace yaze
506
507#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:377
void set_texture(TextureHandle texture)
Definition bitmap.h:389
const SnesPalette & palette() const
Definition bitmap.h:368
SDL_Surface * surface_
SDL surface for rendering (contains the authoritative palette)
Definition bitmap.h:455
Bitmap & operator=(const Bitmap &other)
Copy assignment operator.
Definition bitmap.cc:89
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
Definition bitmap.cc:579
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:199
TextureHandle texture() const
Definition bitmap.h:380
~Bitmap()=default
Destructor.
bool ValidateDataSurfaceSync()
Validate that bitmap data and surface pixels are synchronized.
Definition bitmap.cc:872
const std::vector< uint8_t > & vector() const
Definition bitmap.h:381
auto size() const
Definition bitmap.h:376
void UpdateSurfacePixels()
Update SDL surface with current pixel data from data_ vector Call this after modifying pixel data via...
Definition bitmap.cc:367
std::unordered_map< uint32_t, uint8_t > color_to_index_cache_
Definition bitmap.h:461
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
Definition bitmap.cc:275
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:408
const BitmapMetadata & metadata() const
Definition bitmap.h:371
static uint32_t HashColor(const ImVec4 &color)
Hash a color for cache lookup.
Definition bitmap.cc:802
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:674
BitmapMetadata & metadata()
Definition bitmap.h:370
void CreateTexture()
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:291
bool is_active() const
Definition bitmap.h:384
uint32_t generation_
Definition bitmap.h:401
SnesPalette * mutable_palette()
Definition bitmap.h:369
void QueueTextureUpdate(IRenderer *renderer)
Queue texture update for batch processing (improved performance)
void set_modified(bool modified)
Definition bitmap.h:388
void set_active(bool active)
Definition bitmap.h:386
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Definition bitmap.cc:630
int height() const
Definition bitmap.h:374
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.cc:851
uint8_t at(int i) const
Definition bitmap.h:382
void Resize(int new_width, int new_height)
Resize the bitmap to new dimensions (preserves existing data)
Definition bitmap.cc:752
static uint32_t next_generation_
Definition bitmap.h:402
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:724
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap using SNES palette format.
Definition bitmap.cc:382
int width() const
Definition bitmap.h:373
BitmapMetadata metadata_
Definition bitmap.h:427
void ApplyStoredPalette()
Apply the stored palette to the surface (internal helper)
Definition bitmap.cc:315
std::vector< uint8_t > data_
Definition bitmap.h:430
int depth() const
Definition bitmap.h:375
void InvalidatePaletteCache()
Invalidate the palette lookup cache (call when palette changes)
Definition bitmap.cc:823
uint32_t generation() const
Definition bitmap.h:385
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:454
std::vector< uint8_t > & mutable_data()
Definition bitmap.h:378
struct yaze::gfx::Bitmap::DirtyRegion dirty_region_
void * texture_pixels
Definition bitmap.h:405
void ApplyPaletteByMetadata(const SnesPalette &palette, int sub_palette_index=0)
Apply palette using metadata-driven strategy Chooses between SetPalette and SetPaletteWithTransparent...
Definition bitmap.cc:410
TextureHandle texture_
Definition bitmap.h:458
SDL_Surface * surface() const
Definition bitmap.h:379
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:690
uint8_t FindColorIndex(const SnesColor &color)
Find color index in palette using optimized hash map lookup.
Definition bitmap.cc:844
void UpdateTexture()
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:295
bool modified() const
Definition bitmap.h:383
gfx::SnesPalette palette_
Internal SNES palette storage (may be empty!)
Definition bitmap.h:424
void UpdateTextureData()
Updates the texture data from the surface.
Defines an abstract interface for all rendering operations.
Definition irenderer.h:40
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:497
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:27
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:352
BitmapMetadata(int bpp, int format, const std::string &type, int colors=256)
Definition bitmap.h:360
void AddPoint(int x, int y)
Definition bitmap.h:473