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
173 void ApplyStoredPalette();
174
178 void SetPalette(const std::vector<SDL_Color> &palette);
179
183 void WriteToPixel(int position, uint8_t value);
184
188 void WriteColor(int position, const ImVec4 &color);
189
197 void SetPixel(int x, int y, const SnesColor& color);
198
205 void Resize(int new_width, int new_height);
206
212
219 uint8_t FindColorIndex(const SnesColor& color);
220
227
237 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
238 int &tile_data_offset);
239
248 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
249 int &tile_data_offset);
250
251 const SnesPalette &palette() const { return palette_; }
253 int width() const { return width_; }
254 int height() const { return height_; }
255 int depth() const { return depth_; }
256 auto size() const { return data_.size(); }
257 const uint8_t *data() const { return data_.data(); }
258 std::vector<uint8_t> &mutable_data() { return data_; }
259 SDL_Surface *surface() const { return surface_; }
260 TextureHandle texture() const { return texture_; }
261 const std::vector<uint8_t> &vector() const { return data_; }
262 uint8_t at(int i) const { return data_[i]; }
263 bool modified() const { return modified_; }
264 bool is_active() const { return active_; }
265 void set_active(bool active) { active_ = active; }
266 void set_data(const std::vector<uint8_t> &data);
269
270
271 private:
272 int width_ = 0;
273 int height_ = 0;
274 int depth_ = 0;
275
276 bool active_ = false;
277 bool modified_ = false;
278
279 // Pointer to the texture pixels
280 void *texture_pixels = nullptr;
281
282 // Pointer to the pixel data
284
285 // Palette for the bitmap
287
288 // Data for the bitmap
289 std::vector<uint8_t> data_;
290
291 // Surface for the bitmap (managed by Arena)
293
294 // Texture for the bitmap (managed by Arena)
296
297 // Optimized palette lookup cache for O(1) color index lookups
298 std::unordered_map<uint32_t, uint8_t> color_to_index_cache_;
299
300 // Dirty region tracking for efficient texture updates
301 struct DirtyRegion {
302 int min_x = 0, min_y = 0, max_x = 0, max_y = 0;
303 bool is_dirty = false;
304
305 void Reset() {
306 min_x = min_y = max_x = max_y = 0;
307 is_dirty = false;
308 }
309
310 void AddPoint(int x, int y) {
311 if (!is_dirty) {
312 min_x = max_x = x;
313 min_y = max_y = y;
314 is_dirty = true;
315 } else {
316 min_x = std::min(min_x, x);
317 min_y = std::min(min_y, y);
318 max_x = std::max(max_x, x);
319 max_y = std::max(max_y, y);
320 }
321 }
323
329 static uint32_t HashColor(const ImVec4& color);
330};
331
332// Type alias for a table of bitmaps
333using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
334
338Uint32 GetSnesPixelFormat(int format);
339
340} // namespace gfx
341} // namespace yaze
342
343#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:257
void set_texture(TextureHandle texture)
Definition bitmap.h:268
const SnesPalette & palette() const
Definition bitmap.h:251
SDL_Surface * surface_
Definition bitmap.h:292
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:377
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:260
~Bitmap()=default
Destructor.
bool ValidateDataSurfaceSync()
Validate that bitmap data and surface pixels are synchronized.
Definition bitmap.cc:665
const std::vector< uint8_t > & vector() const
Definition bitmap.h:261
auto size() const
Definition bitmap.h:256
std::unordered_map< uint32_t, uint8_t > color_to_index_cache_
Definition bitmap.h:298
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
Definition bitmap.cc:228
uint8_t * pixel_data_
Definition bitmap.h:283
static uint32_t HashColor(const ImVec4 &color)
Hash a color for cache lookup.
Definition bitmap.cc:597
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:468
void CreateTexture()
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:242
bool is_active() const
Definition bitmap.h:264
SnesPalette * mutable_palette()
Definition bitmap.h:252
void QueueTextureUpdate(IRenderer *renderer)
Queue texture update for batch processing (improved performance)
void set_modified(bool modified)
Definition bitmap.h:267
void set_active(bool active)
Definition bitmap.h:265
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Definition bitmap.cc:424
int height() const
Definition bitmap.h:254
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.cc:645
uint8_t at(int i) const
Definition bitmap.h:262
void Resize(int new_width, int new_height)
Resize the bitmap to new dimensions (preserves existing data)
Definition bitmap.cc:547
void SetPixel(int x, int y, const SnesColor &color)
Set a pixel at the given x,y coordinates with SNES color.
Definition bitmap.cc:519
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap.
Definition bitmap.cc:292
int width() const
Definition bitmap.h:253
void ApplyStoredPalette()
Apply the stored palette to the surface (internal helper)
Definition bitmap.cc:252
std::vector< uint8_t > data_
Definition bitmap.h:289
int depth() const
Definition bitmap.h:255
void InvalidatePaletteCache()
Invalidate the palette lookup cache (call when palette changes)
Definition bitmap.cc:617
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:303
std::vector< uint8_t > & mutable_data()
Definition bitmap.h:258
struct yaze::gfx::Bitmap::DirtyRegion dirty_region_
void * texture_pixels
Definition bitmap.h:280
TextureHandle texture_
Definition bitmap.h:295
SDL_Surface * surface() const
Definition bitmap.h:259
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:484
uint8_t FindColorIndex(const SnesColor &color)
Find color index in palette using optimized hash map lookup.
Definition bitmap.cc:638
void UpdateTexture()
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:246
bool modified() const
Definition bitmap.h:263
gfx::SnesPalette palette_
Definition bitmap.h:286
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:38
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:333
Main namespace for the application.
void AddPoint(int x, int y)
Definition bitmap.h:310