yaze 0.2.0
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
9#include "absl/status/status.h"
10#include "app/core/constants.h"
13
14namespace yaze {
15namespace app {
16
21namespace gfx {
22
23// Same as SDL_PIXELFORMAT_INDEX8 for reference
24constexpr Uint32 SNES_PIXELFORMAT_INDEXED =
25 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1);
26
27constexpr Uint32 SNES_PIXELFORMAT_2BPP = SDL_DEFINE_PIXELFORMAT(
28 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
29 /*layouts=*/0, /*bits=*/2, /*bytes=*/1);
30
31constexpr Uint32 SNES_PIXELFORMAT_4BPP = SDL_DEFINE_PIXELFORMAT(
32 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
33 /*layouts=*/0, /*bits=*/4, /*bytes=*/1);
34
35constexpr Uint32 SNES_PIXELFORMAT_8BPP = SDL_DEFINE_PIXELFORMAT(
36 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
37 /*layouts=*/0, /*bits=*/8, /*bytes=*/1);
38
41 k2bpp = 1,
42 k4bpp = 2,
43 k8bpp = 3,
44};
45
46#if YAZE_LIB_PNG == 1
50bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector<uint8_t> &buffer);
51
55void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
56 SDL_Surface **outSurface);
57#endif
58
67class Bitmap {
68 public:
69 Bitmap() = default;
70
71 Bitmap(int width, int height, int depth, int data_size);
72 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data)
75 }
76 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data,
77 const SnesPalette &palette)
78 : width_(width),
81 data_(data),
84 if (!ApplyPalette(palette).ok()) {
85 std::cerr << "Error applying palette in bitmap constructor." << std::endl;
86 }
87 }
88
89#if YAZE_LIB_PNG == 1
90 std::vector<uint8_t> GetPngData();
91#endif
92
93 void SaveSurfaceToFile(std::string_view filename);
94
98 void Create(int width, int height, int depth,
99 const std::vector<uint8_t> &data);
100 void Create(int width, int height, int depth, int format,
101 const std::vector<uint8_t> &data);
102
103 void Reformat(int format);
104
111 void CreateTexture(SDL_Renderer *renderer);
112
116 void UpdateTexture(SDL_Renderer *renderer);
117
121 absl::Status ApplyPalette(const SnesPalette &palette);
123 size_t index, int length = 7);
124 void ApplyPalette(const std::vector<SDL_Color> &palette);
126 int palette_id);
127
128 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
129 int &tile_data_offset);
130
131 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
132 int &tile_data_offset);
133
134 void WriteToPixel(int position, uchar value) {
135 if (pixel_data_ == nullptr) {
136 pixel_data_ = data_.data();
137 }
138 pixel_data_[position] = value;
139 modified_ = true;
140 }
141
142 void WriteWordToPixel(int position, uint16_t value) {
143 if (pixel_data_ == nullptr) {
144 pixel_data_ = data_.data();
145 }
146 pixel_data_[position] = value & 0xFF;
147 pixel_data_[position + 1] = (value >> 8) & 0xFF;
148 modified_ = true;
149 }
150
151 void WriteColor(int position, const ImVec4 &color);
152
153 void Cleanup() {
154 active_ = false;
155 width_ = 0;
156 height_ = 0;
157 depth_ = 0;
158 data_size_ = 0;
159 palette_.clear();
160 }
161
162 auto sdl_palette() {
163 if (surface_ == nullptr) {
164 throw std::runtime_error("Surface is null.");
165 }
166 return surface_->format->palette;
167 }
168 auto palette() const { return palette_; }
169 auto mutable_palette() { return &palette_; }
170 auto palette_size() const { return palette_.size(); }
171
172 int width() const { return width_; }
173 int height() const { return height_; }
174 auto depth() const { return depth_; }
175 auto size() const { return data_size_; }
176 auto data() const { return data_.data(); }
177 auto &mutable_data() { return data_; }
179 auto surface() const { return surface_.get(); }
180 auto mutable_surface() { return surface_.get(); }
181 auto converted_surface() const { return converted_surface_.get(); }
183
184 auto vector() const { return data_; }
185 auto at(int i) const { return data_[i]; }
186 auto texture() const { return texture_.get(); }
187 auto modified() const { return modified_; }
188 auto is_active() const { return active_; }
189 void set_active(bool active) { active_ = active; }
190 void set_data(const std::vector<uint8_t> &data) { data_ = data; }
192
193 private:
194 int width_ = 0;
195 int height_ = 0;
196 int depth_ = 0;
197 int data_size_ = 0;
198
199 bool active_ = false;
200 bool modified_ = false;
201 void *texture_pixels = nullptr;
202
203 uint8_t *pixel_data_ = nullptr;
204 std::vector<uint8_t> data_;
205
206 std::vector<uint8_t> png_data_;
207
209 std::shared_ptr<SDL_Texture> texture_ = nullptr;
210 std::shared_ptr<SDL_Surface> surface_ = nullptr;
211 std::shared_ptr<SDL_Surface> converted_surface_ = nullptr;
212};
213
214using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
215
216} // namespace gfx
217} // namespace app
218} // namespace yaze
219
220#endif // YAZE_APP_GFX_BITMAP_H
Represents a bitmap image.
Definition bitmap.h:67
void Reformat(int format)
Definition bitmap.cc:266
uint8_t * pixel_data_
Definition bitmap.h:203
auto mutable_converted_surface()
Definition bitmap.h:182
std::shared_ptr< SDL_Surface > surface_
Definition bitmap.h:210
auto vector() const
Definition bitmap.h:184
absl::Status ApplyPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Definition bitmap.cc:380
auto at(int i) const
Definition bitmap.h:185
auto palette_size() const
Definition bitmap.h:170
void set_active(bool active)
Definition bitmap.h:189
void Get8x8Tile(int tile_index, int x, int y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Definition bitmap.cc:433
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data)
Definition bitmap.h:72
std::vector< uint8_t > png_data_
Definition bitmap.h:206
auto surface() const
Definition bitmap.h:179
auto texture() const
Definition bitmap.h:186
int height() const
Definition bitmap.h:173
void SaveSurfaceToFile(std::string_view filename)
Definition bitmap.cc:222
absl::Status ApplyPalette(const SnesPalette &palette)
Copy color data from the SnesPalette into the SDL_Palette.
Definition bitmap.cc:327
std::vector< uint8_t > data_
Definition bitmap.h:204
auto mutable_pixel_data()
Definition bitmap.h:178
void Create(int width, int height, int depth, const std::vector< uint8_t > &data)
Creates a bitmap object with the provided graphical data.
Definition bitmap.cc:230
void UpdateTexture(SDL_Renderer *renderer)
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:310
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.h:190
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data, const SnesPalette &palette)
Definition bitmap.h:76
auto modified() const
Definition bitmap.h:187
void WriteToPixel(int position, uchar value)
Definition bitmap.h:134
auto converted_surface() const
Definition bitmap.h:181
int width() const
Definition bitmap.h:172
auto data() const
Definition bitmap.h:176
absl::Status ApplyPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id)
Definition bitmap.cc:356
void CreateTexture(SDL_Renderer *renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:280
gfx::SnesPalette palette_
Definition bitmap.h:208
void WriteWordToPixel(int position, uint16_t value)
Definition bitmap.h:142
std::shared_ptr< SDL_Surface > converted_surface_
Definition bitmap.h:211
auto depth() const
Definition bitmap.h:174
auto & mutable_data()
Definition bitmap.h:177
auto palette() const
Definition bitmap.h:168
auto is_active() const
Definition bitmap.h:188
void WriteColor(int position, const ImVec4 &color)
Definition bitmap.cc:467
auto size() const
Definition bitmap.h:175
void Get16x16Tile(int tile_x, int tile_y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Definition bitmap.cc:450
std::shared_ptr< SDL_Texture > texture_
Definition bitmap.h:209
void set_modified(bool modified)
Definition bitmap.h:191
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
unsigned char uchar
Definition constants.h:114
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:31
constexpr Uint32 SNES_PIXELFORMAT_2BPP
Definition bitmap.h:27
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:214
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:35
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:24
Definition common.cc:22