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 <span>
8#include <vector>
9
11
12namespace yaze {
13
18namespace gfx {
19
20// Pixel format constants
21constexpr Uint32 SNES_PIXELFORMAT_INDEXED =
22 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1);
23
24constexpr Uint32 SNES_PIXELFORMAT_4BPP = SDL_DEFINE_PIXELFORMAT(
25 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
26 /*layouts=*/0, /*bits=*/4, /*bytes=*/1);
27
28constexpr Uint32 SNES_PIXELFORMAT_8BPP = SDL_DEFINE_PIXELFORMAT(
29 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
30 /*layouts=*/0, /*bits=*/8, /*bytes=*/1);
31
34 k4bpp = 1,
35 k8bpp = 2,
36};
37
38#if YAZE_LIB_PNG == 1
42bool ConvertSurfaceToPng(SDL_Surface *surface, std::vector<uint8_t> &buffer);
43
47void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
48 SDL_Surface **outSurface);
49#endif
50
59class Bitmap {
60 public:
61 Bitmap() = default;
62
66 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data);
67
71 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data,
72 const SnesPalette &palette);
73
77 void Create(int width, int height, int depth, std::span<uint8_t> data);
78
82 void Create(int width, int height, int depth,
83 const std::vector<uint8_t> &data);
84
88 void Create(int width, int height, int depth, int format,
89 const std::vector<uint8_t> &data);
90
94 void Reformat(int format);
95
99 void CreateTexture(SDL_Renderer *renderer);
100
104 void UpdateTexture(SDL_Renderer *renderer);
105
109 void UpdateTextureData();
110
114 void SetPalette(const SnesPalette &palette);
115
119 void SetPaletteWithTransparent(const SnesPalette &palette, size_t index,
120 int length = 7);
121
125 void SetPalette(const std::vector<SDL_Color> &palette);
126
130 void WriteToPixel(int position, uint8_t value);
131
135 void WriteColor(int position, const ImVec4 &color);
136
140 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
141 int &tile_data_offset);
142
146 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
147 int &tile_data_offset);
148
149 const SnesPalette &palette() const { return palette_; }
151 int width() const { return width_; }
152 int height() const { return height_; }
153 int depth() const { return depth_; }
154 auto size() const { return data_.size(); }
155 const uint8_t *data() const { return data_.data(); }
156 std::vector<uint8_t> &mutable_data() { return data_; }
157 SDL_Surface *surface() const { return surface_; }
158 SDL_Texture *texture() const { return texture_; }
159 const std::vector<uint8_t> &vector() const { return data_; }
160 uint8_t at(int i) const { return data_[i]; }
161 bool modified() const { return modified_; }
162 bool is_active() const { return active_; }
163 void set_active(bool active) { active_ = active; }
164 void set_data(const std::vector<uint8_t> &data) { data_ = data; }
166
167#if YAZE_LIB_PNG == 1
168 std::vector<uint8_t> GetPngData();
169#endif
170
171 private:
172 int width_ = 0;
173 int height_ = 0;
174 int depth_ = 0;
175
176 bool active_ = false;
177 bool modified_ = false;
178
179 // Pointer to the texture pixels
180 void *texture_pixels = nullptr;
181
182 // Pointer to the pixel data
183 uint8_t *pixel_data_ = nullptr;
184
185 // Palette for the bitmap
187
188 // Data for the bitmap
189 std::vector<uint8_t> data_;
190
191 // Surface for the bitmap (managed by Arena)
192 SDL_Surface *surface_ = nullptr;
193
194 // Texture for the bitmap (managed by Arena)
195 SDL_Texture *texture_ = nullptr;
196};
197
198// Type alias for a table of bitmaps
199using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
200
204Uint32 GetSnesPixelFormat(int format);
205
206} // namespace gfx
207} // namespace yaze
208
209#endif // YAZE_APP_GFX_BITMAP_H
const uint8_t * data() const
Definition bitmap.h:155
const SnesPalette & palette() const
Definition bitmap.h:149
SDL_Surface * surface_
Definition bitmap.h:192
void WriteToPixel(int position, uint8_t value)
Write a value to a pixel at the given position.
Definition bitmap.cc:383
SDL_Texture * texture() const
Definition bitmap.h:158
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:218
void UpdateTexture(SDL_Renderer *renderer)
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:266
const std::vector< uint8_t > & vector() const
Definition bitmap.h:159
auto size() const
Definition bitmap.h:154
void Reformat(int format)
Reformat the bitmap to use a different pixel format.
Definition bitmap.cc:258
uint8_t * pixel_data_
Definition bitmap.h:183
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:411
bool is_active() const
Definition bitmap.h:162
SnesPalette * mutable_palette()
Definition bitmap.h:150
void set_modified(bool modified)
Definition bitmap.h:165
void set_active(bool active)
Definition bitmap.h:163
void WriteColor(int position, const ImVec4 &color)
Write a color to a pixel at the given position.
Definition bitmap.cc:392
int height() const
Definition bitmap.h:152
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.h:164
uint8_t at(int i) const
Definition bitmap.h:160
void SetPalette(const SnesPalette &palette)
Set the palette for the bitmap.
Definition bitmap.cc:307
int width() const
Definition bitmap.h:151
std::vector< uint8_t > data_
Definition bitmap.h:189
int depth() const
Definition bitmap.h:153
SDL_Texture * texture_
Definition bitmap.h:195
void CreateTexture(SDL_Renderer *renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:275
void SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Set the palette with a transparent color.
Definition bitmap.cc:333
std::vector< uint8_t > & mutable_data()
Definition bitmap.h:156
void * texture_pixels
Definition bitmap.h:180
SDL_Surface * surface() const
Definition bitmap.h:157
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:427
bool modified() const
Definition bitmap.h:161
gfx::SnesPalette palette_
Definition bitmap.h:186
void UpdateTextureData()
Updates the texture data from the surface.
Definition bitmap.cc:298
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Contains classes for handling graphical data.
Definition arena.cc:8
BitmapFormat
Definition bitmap.h:32
@ kIndexed
Definition bitmap.h:33
@ k8bpp
Definition bitmap.h:35
@ k4bpp
Definition bitmap.h:34
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:28
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:21
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:24
Uint32 GetSnesPixelFormat(int format)
Get the SDL pixel format for a given bitmap format.
Definition bitmap.cc:188
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:199
Main namespace for the application.
Definition controller.cc:18