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
4#include <SDL.h>
5
6#include <cstdint>
7#include <span>
8#include <unordered_map>
9#include <vector>
10
13
14namespace yaze {
15
20namespace gfx {
21
22// Pixel format constants
23constexpr Uint32 SNES_PIXELFORMAT_INDEXED =
24 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1);
25
26constexpr Uint32 SNES_PIXELFORMAT_4BPP = SDL_DEFINE_PIXELFORMAT(
27 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
28 /*layouts=*/0, /*bits=*/4, /*bytes=*/1);
29
30constexpr Uint32 SNES_PIXELFORMAT_8BPP = SDL_DEFINE_PIXELFORMAT(
31 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
32 /*layouts=*/0, /*bits=*/8, /*bytes=*/1);
33
36 k4bpp = 1,
37 k8bpp = 2,
38};
39
40
66class Bitmap {
67 public:
68 Bitmap() = default;
69
77 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data);
78
87 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data,
88 const SnesPalette &palette);
89
93 Bitmap(const Bitmap& other);
94
99
103 Bitmap(Bitmap&& other) noexcept;
104
108 Bitmap& operator=(Bitmap&& other) noexcept;
109
113 ~Bitmap() = default;
114
118 void Create(int width, int height, int depth, std::span<uint8_t> data);
119
123 void Create(int width, int height, int depth,
124 const std::vector<uint8_t> &data);
125
129 void Create(int width, int height, int depth, int format,
130 const std::vector<uint8_t> &data);
131
135 void Reformat(int format);
136
140 void CreateTexture();
141
145 void UpdateTexture();
146
153
158
162 void SetPalette(const SnesPalette &palette);
163
167 void SetPaletteWithTransparent(const SnesPalette &palette, size_t index,
168 int length = 7);
169
175
179 void ApplyStoredPalette();
180
185 void UpdateSurfacePixels();
186
190 void SetPalette(const std::vector<SDL_Color> &palette);
191
195 void WriteToPixel(int position, uint8_t value);
196
200 void WriteColor(int position, const ImVec4 &color);
201
209 void SetPixel(int x, int y, const SnesColor& color);
210
217 void Resize(int new_width, int new_height);
218
224
231 uint8_t FindColorIndex(const SnesColor& color);
232
239
249 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
250 int &tile_data_offset);
251
260 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
261 int &tile_data_offset);
262
267 int source_bpp = 8; // Original bits per pixel (3, 4, 8)
268 int palette_format = 0; // 0=full palette, 1=sub-palette with transparent
269 std::string source_type; // "graphics_sheet", "tilemap", "screen_buffer", "mode7"
270 int palette_colors = 256; // Expected palette size
271
272 BitmapMetadata() = default;
273 BitmapMetadata(int bpp, int format, const std::string& type, int colors = 256)
274 : source_bpp(bpp), palette_format(format), source_type(type), palette_colors(colors) {}
275 };
276
277 const SnesPalette &palette() const { return palette_; }
280 const BitmapMetadata& metadata() const { return metadata_; }
281
282 int width() const { return width_; }
283 int height() const { return height_; }
284 int depth() const { return depth_; }
285 auto size() const { return data_.size(); }
286 const uint8_t *data() const { return data_.data(); }
287 std::vector<uint8_t> &mutable_data() { return data_; }
288 SDL_Surface *surface() const { return surface_; }
289 TextureHandle texture() const { return texture_; }
290 const std::vector<uint8_t> &vector() const { return data_; }
291 uint8_t at(int i) const { return data_[i]; }
292 bool modified() const { return modified_; }
293 bool is_active() const { return active_; }
294 void set_active(bool active) { active_ = active; }
295 void set_data(const std::vector<uint8_t> &data);
298
299
300 private:
301 int width_ = 0;
302 int height_ = 0;
303 int depth_ = 0;
304
305 bool active_ = false;
306 bool modified_ = false;
307
308 // Pointer to the texture pixels
309 void *texture_pixels = nullptr;
310
311 // Pointer to the pixel data
313
314 // Palette for the bitmap
316
317 // Metadata for tracking source format and palette requirements
319
320 // Data for the bitmap
321 std::vector<uint8_t> data_;
322
323 // Surface for the bitmap (managed by Arena)
325
326 // Texture for the bitmap (managed by Arena)
328
329 // Optimized palette lookup cache for O(1) color index lookups
330 std::unordered_map<uint32_t, uint8_t> color_to_index_cache_;
331
332 // Dirty region tracking for efficient texture updates
333 struct DirtyRegion {
334 int min_x = 0, min_y = 0, max_x = 0, max_y = 0;
335 bool is_dirty = false;
336
337 void Reset() {
338 min_x = min_y = max_x = max_y = 0;
339 is_dirty = false;
340 }
341
342 void AddPoint(int x, int y) {
343 if (!is_dirty) {
344 min_x = max_x = x;
345 min_y = max_y = y;
346 is_dirty = true;
347 } else {
348 min_x = std::min(min_x, x);
349 min_y = std::min(min_y, y);
350 max_x = std::max(max_x, x);
351 max_y = std::max(max_y, y);
352 }
353 }
355
361 static uint32_t HashColor(const ImVec4& color);
362};
363
364// Type alias for a table of bitmaps
365using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
366
370Uint32 GetSnesPixelFormat(int format);
371
372} // namespace gfx
373} // namespace yaze
374
375#endif // YAZE_APP_GFX_BITMAP_H
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:66
const uint8_t * data() const
Definition bitmap.h:286
void set_texture(TextureHandle texture)
Definition bitmap.h:297
const SnesPalette & palette() const
Definition bitmap.h:277
SDL_Surface * surface_
Definition bitmap.h:324
Bitmap & operator=(const Bitmap &other)
Copy assignment operator.
Definition bitmap.cc:85
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
Definition bitmap.cc:475
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:162
TextureHandle texture() const
Definition bitmap.h:289
~Bitmap()=default
Destructor.
bool ValidateDataSurfaceSync()
Validate that bitmap data and surface pixels are synchronized.
Definition bitmap.cc:763
const std::vector< uint8_t > & vector() const
Definition bitmap.h:290
auto size() const
Definition bitmap.h:285
void UpdateSurfacePixels()
Update SDL surface with current pixel data from data_ vector Call this after modifying pixel data via...
Definition bitmap.cc:321
std::unordered_map< uint32_t, uint8_t > color_to_index_cache_
Definition bitmap.h:330
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
Definition bitmap.cc:228
uint8_t * pixel_data_
Definition bitmap.h:312
const BitmapMetadata & metadata() const
Definition bitmap.h:280
static uint32_t HashColor(const ImVec4 &color)
Hash a color for cache lookup.
Definition bitmap.cc:695
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:566
BitmapMetadata & metadata()
Definition bitmap.h:279
void CreateTexture()
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:242
bool is_active() const
Definition bitmap.h:293
SnesPalette * mutable_palette()
Definition bitmap.h:278
void QueueTextureUpdate(IRenderer *renderer)
Queue texture update for batch processing (improved performance)
void set_modified(bool modified)
Definition bitmap.h:296
void set_active(bool active)
Definition bitmap.h:294
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Definition bitmap.cc:522
int height() const
Definition bitmap.h:283
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.cc:743
uint8_t at(int i) const
Definition bitmap.h:291
void Resize(int new_width, int new_height)
Resize the bitmap to new dimensions (preserves existing data)
Definition bitmap.cc:645
void SetPixel(int x, int y, const SnesColor &color)
Set a pixel at the given x,y coordinates with SNES color.
Definition bitmap.cc:617
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap.
Definition bitmap.cc:334
int width() const
Definition bitmap.h:282
BitmapMetadata metadata_
Definition bitmap.h:318
void ApplyStoredPalette()
Apply the stored palette to the surface (internal helper)
Definition bitmap.cc:268
std::vector< uint8_t > data_
Definition bitmap.h:321
int depth() const
Definition bitmap.h:284
void InvalidatePaletteCache()
Invalidate the palette lookup cache (call when palette changes)
Definition bitmap.cc:715
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:403
std::vector< uint8_t > & mutable_data()
Definition bitmap.h:287
struct yaze::gfx::Bitmap::DirtyRegion dirty_region_
void * texture_pixels
Definition bitmap.h:309
void ApplyPaletteByMetadata(const SnesPalette &palette, int sub_palette_index=0)
Apply palette using metadata-driven strategy Chooses between SetPalette and SetPaletteWithTransparent...
Definition bitmap.cc:360
TextureHandle texture_
Definition bitmap.h:327
SDL_Surface * surface() const
Definition bitmap.h:288
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:582
uint8_t FindColorIndex(const SnesColor &color)
Find color index in palette using optimized hash map lookup.
Definition bitmap.cc:736
void UpdateTexture()
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:246
bool modified() const
Definition bitmap.h:292
gfx::SnesPalette palette_
Definition bitmap.h:315
void UpdateTextureData()
Updates the texture data from the surface.
Defines an abstract interface for all rendering operations.
Definition irenderer.h:35
SNES Color container.
Definition snes_color.h:109
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
BitmapFormat
Definition bitmap.h:34
@ kIndexed
Definition bitmap.h:35
@ k8bpp
Definition bitmap.h:37
@ k4bpp
Definition bitmap.h:36
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:30
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:23
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:24
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:26
Uint32 GetSnesPixelFormat(int format)
Convert bitmap format enum to SDL pixel format.
Definition bitmap.cc:34
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:365
Main namespace for the application.
Definition controller.cc:20
Metadata for tracking bitmap source format and palette requirements.
Definition bitmap.h:266
BitmapMetadata(int bpp, int format, const std::string &type, int colors=256)
Definition bitmap.h:273
void AddPoint(int x, int y)
Definition bitmap.h:342