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
9#include "absl/status/status.h"
12#include "util/macro.h"
13
14namespace yaze {
15
20namespace gfx {
21
22// Same as SDL_PIXELFORMAT_INDEX8 for reference
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 Bitmap() = default;
69
70 Bitmap(int width, int height, int depth, int data_size);
71 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data)
74 }
75 Bitmap(int width, int height, int depth, const std::vector<uint8_t> &data,
76 const SnesPalette &palette)
77 : width_(width),
80 data_(data),
83 if (!ApplyPalette(palette).ok()) {
84 std::cerr << "Error applying palette in bitmap constructor." << std::endl;
85 }
86 }
87
88#if YAZE_LIB_PNG == 1
89 std::vector<uint8_t> GetPngData();
90#endif
91
92 void SaveSurfaceToFile(std::string_view filename);
93
94 void Initialize(int width, int height, int depth, std::span<uint8_t> &data);
95
96 void Create(int width, int height, int depth, int data_size) {
97 Create(width, height, depth, std::vector<uint8_t>(data_size, 0));
98 }
99 void Create(int width, int height, int depth, std::span<uint8_t> data);
100 void Create(int width, int height, int depth,
101 const std::vector<uint8_t> &data);
102 void Create(int width, int height, int depth, int format,
103 const std::vector<uint8_t> &data);
104
105 void Reformat(int format);
106
113 void CreateTexture(SDL_Renderer *renderer);
114
118 void UpdateTexture(SDL_Renderer *renderer);
119
123 absl::Status ApplyPalette(const SnesPalette &palette);
125 size_t index, int length = 7);
126 void ApplyPalette(const std::vector<SDL_Color> &palette);
128 int palette_id);
129
130 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
131 int &tile_data_offset);
132
133 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
134 int &tile_data_offset);
135
136 void WriteToPixel(int position, uint8_t value) {
137 if (pixel_data_ == nullptr) {
138 pixel_data_ = data_.data();
139 }
140 pixel_data_[position] = value;
141 modified_ = true;
142 }
143
144 void WriteColor(int position, const ImVec4 &color);
145
146 void Cleanup() {
147 active_ = false;
148 width_ = 0;
149 height_ = 0;
150 depth_ = 0;
151 data_size_ = 0;
152 palette_.clear();
153 }
154
155 auto palette() const { return palette_; }
156 auto mutable_palette() { return &palette_; }
157
158 int width() const { return width_; }
159 int height() const { return height_; }
160 auto depth() const { return depth_; }
161 auto size() const { return data_size_; }
162 auto data() const { return data_.data(); }
163 auto &mutable_data() { return data_; }
164 auto surface() const { return surface_.get(); }
165
166 auto vector() const { return data_; }
167 auto at(int i) const { return data_[i]; }
168 auto texture() const { return texture_.get(); }
169 auto modified() const { return modified_; }
170 auto is_active() const { return active_; }
171 void set_active(bool active) { active_ = active; }
172 void set_data(const std::vector<uint8_t> &data) { data_ = data; }
174
175 private:
176 int width_ = 0;
177 int height_ = 0;
178 int depth_ = 0;
179 int data_size_ = 0;
180
181 bool active_ = false;
182 bool modified_ = false;
183 void *texture_pixels = nullptr;
184
185 uint8_t *pixel_data_ = nullptr;
186 std::vector<uint8_t> data_;
187
189 std::shared_ptr<SDL_Texture> texture_ = nullptr;
190 std::shared_ptr<SDL_Surface> surface_ = nullptr;
191};
192
193using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
194
195} // namespace gfx
196} // namespace yaze
197
198#endif // YAZE_APP_GFX_BITMAP_H
void WriteToPixel(int position, uint8_t value)
Definition bitmap.h:136
auto at(int i) const
Definition bitmap.h:167
void UpdateTexture(SDL_Renderer *renderer)
Updates the underlying SDL_Texture when it already exists.
Definition bitmap.cc:314
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data, const SnesPalette &palette)
Definition bitmap.h:75
auto size() const
Definition bitmap.h:161
void Reformat(int format)
Definition bitmap.cc:267
uint8_t * pixel_data_
Definition bitmap.h:185
absl::Status ApplyPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Definition bitmap.cc:382
absl::Status ApplyPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id)
Definition bitmap.cc:358
void SaveSurfaceToFile(std::string_view filename)
Definition bitmap.cc:208
void Get8x8Tile(int tile_index, int x, int y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Definition bitmap.cc:435
void Initialize(int width, int height, int depth, std::span< uint8_t > &data)
Definition bitmap.cc:216
auto vector() const
Definition bitmap.h:166
void set_modified(bool modified)
Definition bitmap.h:173
auto mutable_palette()
Definition bitmap.h:156
void set_active(bool active)
Definition bitmap.h:171
void WriteColor(int position, const ImVec4 &color)
Definition bitmap.cc:468
int height() const
Definition bitmap.h:159
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.h:172
std::shared_ptr< SDL_Texture > texture_
Definition bitmap.h:189
int width() const
Definition bitmap.h:158
std::vector< uint8_t > data_
Definition bitmap.h:186
auto & mutable_data()
Definition bitmap.h:163
std::shared_ptr< SDL_Surface > surface_
Definition bitmap.h:190
void CreateTexture(SDL_Renderer *renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:281
auto palette() const
Definition bitmap.h:155
auto is_active() const
Definition bitmap.h:170
auto surface() const
Definition bitmap.h:164
auto modified() const
Definition bitmap.h:169
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data)
Definition bitmap.h:71
void * texture_pixels
Definition bitmap.h:183
absl::Status ApplyPalette(const SnesPalette &palette)
Copy color data from the SnesPalette into the SDL_Palette.
Definition bitmap.cc:329
auto data() const
Definition bitmap.h:162
void Create(int width, int height, int depth, int data_size)
Definition bitmap.h:96
void Get16x16Tile(int tile_x, int tile_y, std::vector< uint8_t > &tile_data, int &tile_data_offset)
Definition bitmap.cc:451
gfx::SnesPalette palette_
Definition bitmap.h:188
auto texture() const
Definition bitmap.h:168
auto depth() const
Definition bitmap.h:160
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Contains classes for handling graphical data.
Definition bitmap.cc:16
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
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:23
constexpr Uint32 SNES_PIXELFORMAT_4BPP
Definition bitmap.h:30
constexpr Uint32 SNES_PIXELFORMAT_2BPP
Definition bitmap.h:26
std::unordered_map< int, gfx::Bitmap > BitmapTable
Definition bitmap.h:193
Main namespace for the application.
Definition controller.cc:18