yaze 0.2.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 <memory>
8#include <span>
9#include <string_view>
10#include <vector>
11
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_2BPP = SDL_DEFINE_PIXELFORMAT(
27 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
28 /*layouts=*/0, /*bits=*/2, /*bytes=*/1);
29
30constexpr Uint32 SNES_PIXELFORMAT_4BPP = SDL_DEFINE_PIXELFORMAT(
31 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
32 /*layouts=*/0, /*bits=*/4, /*bytes=*/1);
33
34constexpr Uint32 SNES_PIXELFORMAT_8BPP = SDL_DEFINE_PIXELFORMAT(
35 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
36 /*layouts=*/0, /*bits=*/8, /*bytes=*/1);
37
40 k2bpp = 1,
41 k4bpp = 2,
42 k8bpp = 3,
43};
44
45#if YAZE_LIB_PNG == 1
49bool ConvertSurfaceToPng(SDL_Surface *surface, std::vector<uint8_t> &buffer);
50
54void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
55 SDL_Surface **outSurface);
56#endif
57
66class Bitmap {
67 public:
68 // Constructors
69 Bitmap() = default;
70
74 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data);
75
79 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data,
80 const SnesPalette &palette);
81
85 void Initialize(int width, int height, int depth, std::span<uint8_t> &data);
86
90 void Create(int width, int height, int depth, std::span<uint8_t> data);
91
95 void Create(int width, int height, int depth,
96 const std::vector<uint8_t> &data);
97
101 void Create(int width, int height, int depth, int format,
102 const std::vector<uint8_t> &data);
103
107 void Reformat(int format);
108
112 void SaveSurfaceToFile(std::string_view filename);
113
114 // Texture management
121 void CreateTexture(SDL_Renderer *renderer);
122
126 void UpdateTexture(SDL_Renderer *renderer);
127
131 void UpdateTextureData();
132
136 void CleanupUnusedTexture(uint64_t current_time, uint64_t timeout);
137
138 // Palette management
142 void SetPalette(const SnesPalette &palette);
143
147 void SetPaletteWithTransparent(const SnesPalette &palette, size_t index,
148 int length = 7);
149
153 void SetPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id);
154
158 void SetPalette(const std::vector<SDL_Color> &palette);
159
160 // Pixel operations
164 void WriteToPixel(int position, uint8_t value);
165
169 void WriteColor(int position, const ImVec4 &color);
170
171 // Tile operations
175 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
176 int &tile_data_offset);
177
181 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
182 int &tile_data_offset);
183
187 void Cleanup();
188
192 void Clear();
193
194 const SnesPalette &palette() const { return palette_; }
196 int width() const { return width_; }
197 int height() const { return height_; }
198 int depth() const { return depth_; }
199 int size() const { return data_size_; }
200 const uint8_t *data() const { return data_.data(); }
201 std::vector<uint8_t> &mutable_data() { return data_; }
202 SDL_Surface *surface() const { return surface_.get(); }
203 SDL_Texture *texture() const { return texture_.get(); }
204 const std::vector<uint8_t> &vector() const { return data_; }
205 uint8_t at(int i) const { return data_[i]; }
206 bool modified() const { return modified_; }
207 bool is_active() const { return active_; }
208 void set_active(bool active) { active_ = active; }
209 void set_data(const std::vector<uint8_t> &data) { data_ = data; }
211
212#if YAZE_LIB_PNG == 1
216 std::vector<uint8_t> GetPngData();
217#endif
218
219 private:
220 int width_ = 0;
221 int height_ = 0;
222 int depth_ = 0;
223 int data_size_ = 0;
224
225 bool active_ = false;
226 bool modified_ = false;
227
228 // Track if this texture is currently in use
229 bool texture_in_use_ = false;
230
231 // Track the last time this texture was used
232 uint64_t last_used_time_ = 0;
233
234 // Pointer to the texture pixels
235 void *texture_pixels = nullptr;
236
237 // Pointer to the pixel data
238 uint8_t *pixel_data_ = nullptr;
239
240 // Palette for the bitmap
242
243 // Data for the bitmap
244 std::vector<uint8_t> data_;
245
246 // Surface for the bitmap
247 std::shared_ptr<SDL_Surface> surface_ = nullptr;
248
249 // Texture for the bitmap
250 std::shared_ptr<SDL_Texture> texture_ = nullptr;
251};
252
253// Type alias for a table of bitmaps
254using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
255
256// Utility functions that operate on Bitmap objects
260std::vector<gfx::Bitmap> ExtractTile8Bitmaps(const gfx::Bitmap &source_bmp,
261 const gfx::SnesPalette &palette,
262 uint8_t palette_index);
263
267Uint32 GetSnesPixelFormat(int format);
268
272SDL_Surface *AllocateSurface(int width, int height, int depth, Uint32 format);
273
277SDL_Texture *AllocateTexture(SDL_Renderer *renderer, Uint32 format, int access,
278 int width, int height);
279
280} // namespace gfx
281} // namespace yaze
282
283#endif // YAZE_APP_GFX_BITMAP_H
Represents a bitmap image.
Definition bitmap.h:66
const uint8_t * data() const
Definition bitmap.h:200
const SnesPalette & palette() const
Definition bitmap.h:194
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
Definition bitmap.cc:501
SDL_Texture * texture() const
Definition bitmap.h:203
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:257
void UpdateTexture(SDL_Renderer *renderer)
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:308
const std::vector< uint8_t > & vector() const
Definition bitmap.h:204
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
Definition bitmap.cc:299
uint64_t last_used_time_
Definition bitmap.h:232
uint8_t * pixel_data_
Definition bitmap.h:238
void SetPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id)
Set the palette from a palette group.
Definition bitmap.cc:429
void SaveSurfaceToFile(std::string_view filename)
Save the bitmap surface to a file.
Definition bitmap.cc:245
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.
Definition bitmap.cc:528
bool is_active() const
Definition bitmap.h:207
SnesPalette * mutable_palette()
Definition bitmap.h:195
void Initialize(int width, int height, int depth, std::span< uint8_t > &data)
Initialize the bitmap with the given dimensions and data.
Definition bitmap.cc:249
void set_modified(bool modified)
Definition bitmap.h:210
void set_active(bool active)
Definition bitmap.h:208
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Definition bitmap.cc:509
int height() const
Definition bitmap.h:197
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.h:209
uint8_t at(int i) const
Definition bitmap.h:205
std::shared_ptr< SDL_Texture > texture_
Definition bitmap.h:250
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap.
Definition bitmap.cc:403
int width() const
Definition bitmap.h:196
std::vector< uint8_t > data_
Definition bitmap.h:244
int depth() const
Definition bitmap.h:198
std::shared_ptr< SDL_Surface > surface_
Definition bitmap.h:247
void CreateTexture(SDL_Renderer *renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:323
void Cleanup()
Clean up the bitmap resources.
Definition bitmap.cc:561
int size() const
Definition bitmap.h:199
void CleanupUnusedTexture(uint64_t current_time, uint64_t timeout)
Clean up unused textures after a timeout.
Definition bitmap.cc:395
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:451
std::vector< uint8_t > & mutable_data()
Definition bitmap.h:201
void * texture_pixels
Definition bitmap.h:235
SDL_Surface * surface() const
Definition bitmap.h:202
bool texture_in_use_
Definition bitmap.h:229
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.
Definition bitmap.cc:544
bool modified() const
Definition bitmap.h:206
gfx::SnesPalette palette_
Definition bitmap.h:241
void UpdateTextureData()
Updates the texture data from the surface.
Definition bitmap.cc:367
void Clear()
Clear the bitmap data.
Definition bitmap.cc:570
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Contains classes for handling graphical data.
Definition bitmap.cc:18
BitmapFormat
Definition bitmap.h:38
@ kIndexed
Definition bitmap.h:39
@ k2bpp
Definition bitmap.h:40
@ k8bpp
Definition bitmap.h:42
@ k4bpp
Definition bitmap.h:41
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:34
SDL_Texture * AllocateTexture(SDL_Renderer *renderer, Uint32 format, int access, int width, int height)
Allocate an SDL texture with the given dimensions and format.
Definition bitmap.cc:214
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:23
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:30
Uint32 GetSnesPixelFormat(int format)
Get the SDL pixel format for a given bitmap format.
Definition bitmap.cc:188
constexpr Uint32 SNES_PIXELFORMAT_2BPP
Definition bitmap.h:26
std::vector< gfx::Bitmap > ExtractTile8Bitmaps(const gfx::Bitmap &source_bmp, const gfx::SnesPalette &palette, uint8_t palette_index)
Extract 8x8 tiles from a source bitmap.
Definition bitmap.cc:589
SDL_Surface * AllocateSurface(int width, int height, int depth, Uint32 format)
Allocate an SDL surface with the given dimensions and format.
Definition bitmap.cc:203
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:254
Main namespace for the application.
Definition controller.cc:18