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/container/flat_hash_map.h"
10#include "absl/status/status.h"
11#include "absl/status/statusor.h"
12#include "absl/strings/str_cat.h"
13#include "app/core/constants.h"
16
17namespace yaze {
18namespace app {
19
24namespace gfx {
25
26// Same as SDL_PIXELFORMAT_INDEX8 for reference
27constexpr Uint32 SNES_PIXELFORMAT_INDEXED =
28 SDL_DEFINE_PIXELFORMAT(SDL_PIXELTYPE_INDEX8, 0, 0, 8, 1);
29
30constexpr Uint32 SNES_PIXELFORMAT_2BPP = SDL_DEFINE_PIXELFORMAT(
31 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
32 /*layouts=*/0, /*bits=*/2, /*bytes=*/1);
33
34constexpr Uint32 SNES_PIXELFORMAT_4BPP = SDL_DEFINE_PIXELFORMAT(
35 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
36 /*layouts=*/0, /*bits=*/4, /*bytes=*/1);
37
38constexpr Uint32 SNES_PIXELFORMAT_8BPP = SDL_DEFINE_PIXELFORMAT(
39 /*type=*/SDL_PIXELTYPE_INDEX8, /*order=*/0,
40 /*layouts=*/0, /*bits=*/8, /*bytes=*/1);
41
44 k2bpp = 1,
45 k4bpp = 2,
46 k8bpp = 3,
47};
48
49#if YAZE_LIB_PNG == 1
53bool ConvertSurfaceToPNG(SDL_Surface *surface, std::vector<uint8_t> &buffer);
54
58void ConvertPngToSurface(const std::vector<uint8_t> &png_data,
59 SDL_Surface **outSurface);
60#endif
61
70class Bitmap {
71 public:
72 Bitmap() = default;
73
74 Bitmap(int width, int height, int depth, int data_size);
75 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data)
78 }
79 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data,
80 const SnesPalette &palette)
81 : width_(width),
84 data_(data),
87 if (!ApplyPalette(palette).ok()) {
88 std::cerr << "Error applying palette in bitmap constructor." << std::endl;
89 }
90 }
91
92#if YAZE_LIB_PNG == 1
93 std::vector<uint8_t> GetPngData();
94#endif
95
96 void SaveSurfaceToFile(std::string_view filename);
97
101 void Create(int width, int height, int depth,
102 const std::vector<uint8_t> &data);
103 void Create(int width, int height, int depth, int format,
104 const std::vector<uint8_t> &data);
105
106 void Reformat(int format);
107
114 void CreateTexture(std::shared_ptr<SDL_Renderer> renderer);
115
119 void UpdateTexture(std::shared_ptr<SDL_Renderer> renderer);
120 void CreateTexture(SDL_Renderer *renderer);
121 void UpdateTexture(SDL_Renderer *renderer, bool use_sdl_update = false);
122
126 absl::Status ApplyPalette(const SnesPalette &palette);
128 size_t index, int length = 7);
129 void ApplyPalette(const std::vector<SDL_Color> &palette);
131 int palette_id);
132
133 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
134 int &tile_data_offset);
135
136 void Get16x16Tile(int tile_index, int x, int y,
137 std::vector<uint8_t> &tile_data, int &tile_data_offset);
138 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
139 int &tile_data_offset);
140
141 void WriteToPixel(int position, uchar value) {
142 if (pixel_data_ == nullptr) {
143 pixel_data_ = data_.data();
144 }
145 pixel_data_[position] = value;
146 modified_ = true;
147 }
148
149 void WriteWordToPixel(int position, uint16_t value) {
150 if (pixel_data_ == nullptr) {
151 pixel_data_ = data_.data();
152 }
153 pixel_data_[position] = value & 0xFF;
154 pixel_data_[position + 1] = (value >> 8) & 0xFF;
155 modified_ = true;
156 }
157
158 void WriteColor(int position, const ImVec4 &color);
159
160 void Cleanup() {
161 active_ = false;
162 width_ = 0;
163 height_ = 0;
164 depth_ = 0;
165 data_size_ = 0;
166 palette_.clear();
167 }
168
169 auto sdl_palette() {
170 if (surface_ == nullptr) {
171 throw std::runtime_error("Surface is null.");
172 }
173 return surface_->format->palette;
174 }
175 auto palette() const { return palette_; }
176 auto mutable_palette() { return &palette_; }
177 auto palette_size() const { return palette_.size(); }
178
179 int width() const { return width_; }
180 int height() const { return height_; }
181 auto depth() const { return depth_; }
182 auto size() const { return data_size_; }
183 auto data() const { return data_.data(); }
184 auto &mutable_data() { return data_; }
186 auto surface() const { return surface_.get(); }
187 auto mutable_surface() { return surface_.get(); }
188 auto converted_surface() const { return converted_surface_.get(); }
190
191 auto vector() const { return data_; }
192 auto at(int i) const { return data_[i]; }
193 auto texture() const { return texture_.get(); }
194 auto modified() const { return modified_; }
195 auto is_active() const { return active_; }
196 void set_active(bool active) { active_ = active; }
197 void set_data(const std::vector<uint8_t> &data) { data_ = data; }
199
200 private:
201 int width_ = 0;
202 int height_ = 0;
203 int depth_ = 0;
204 int data_size_ = 0;
205
206 bool active_ = false;
207 bool modified_ = false;
208 void *texture_pixels = nullptr;
209
210 uint8_t *pixel_data_ = nullptr;
211 std::vector<uint8_t> data_;
212
213 std::vector<uint8_t> png_data_;
214
216 std::shared_ptr<SDL_Texture> texture_ = nullptr;
217 std::shared_ptr<SDL_Surface> surface_ = nullptr;
218 std::shared_ptr<SDL_Surface> converted_surface_ = nullptr;
219};
220
221using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
222
223} // namespace gfx
224} // namespace app
225} // namespace yaze
226
227#endif // YAZE_APP_GFX_BITMAP_H
Represents a bitmap image.
Definition bitmap.h:70
void Reformat(int format)
Definition bitmap.cc:268
uint8_t * pixel_data_
Definition bitmap.h:210
auto mutable_converted_surface()
Definition bitmap.h:189
std::shared_ptr< SDL_Surface > surface_
Definition bitmap.h:217
void Get16x16Tile(int tile_index, int x, int y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Definition bitmap.cc:469
auto vector() const
Definition bitmap.h:191
absl::Status ApplyPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Definition bitmap.cc:399
auto at(int i) const
Definition bitmap.h:192
auto palette_size() const
Definition bitmap.h:177
void set_active(bool active)
Definition bitmap.h:196
void Get8x8Tile(int tile_index, int x, int y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Definition bitmap.cc:452
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data)
Definition bitmap.h:75
std::vector< uint8_t > png_data_
Definition bitmap.h:213
auto surface() const
Definition bitmap.h:186
auto texture() const
Definition bitmap.h:193
int height() const
Definition bitmap.h:180
void SaveSurfaceToFile(std::string_view filename)
Definition bitmap.cc:224
void CreateTexture(std::shared_ptr< SDL_Renderer > renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:334
absl::Status ApplyPalette(const SnesPalette &palette)
Copy color data from the SnesPalette into the SDL_Palette.
Definition bitmap.cc:346
std::vector< uint8_t > data_
Definition bitmap.h:211
auto mutable_pixel_data()
Definition bitmap.h:185
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:232
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.h:197
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data, const SnesPalette &palette)
Definition bitmap.h:79
auto modified() const
Definition bitmap.h:194
void WriteToPixel(int position, uchar value)
Definition bitmap.h:141
auto converted_surface() const
Definition bitmap.h:188
int width() const
Definition bitmap.h:179
auto data() const
Definition bitmap.h:183
absl::Status ApplyPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id)
Definition bitmap.cc:375
gfx::SnesPalette palette_
Definition bitmap.h:215
void WriteWordToPixel(int position, uint16_t value)
Definition bitmap.h:149
std::shared_ptr< SDL_Surface > converted_surface_
Definition bitmap.h:218
auto depth() const
Definition bitmap.h:181
void UpdateTexture(std::shared_ptr< SDL_Renderer > renderer)
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:340
auto & mutable_data()
Definition bitmap.h:184
auto palette() const
Definition bitmap.h:175
auto is_active() const
Definition bitmap.h:195
void WriteColor(int position, const ImVec4 &color)
Definition bitmap.cc:502
auto size() const
Definition bitmap.h:182
std::shared_ptr< SDL_Texture > texture_
Definition bitmap.h:216
void set_modified(bool modified)
Definition bitmap.h:198
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
unsigned char uchar
Definition constants.h:121
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:34
constexpr Uint32 SNES_PIXELFORMAT_2BPP
Definition bitmap.h:30
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:221
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:38
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:27
Definition common.cc:21