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
10#include "absl/status/status.h"
13#include "util/macro.h"
14
15namespace yaze {
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 (!SetPalette(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
95 void Initialize(int width, int height, int depth, std::span<uint8_t> &data);
96
97 void Create(int width, int height, int depth, int data_size) {
98 Create(width, height, depth, std::vector<uint8_t>(data_size, 0));
99 }
100 void Create(int width, int height, int depth, std::span<uint8_t> data);
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(SDL_Renderer *renderer);
115
119 void UpdateTexture(SDL_Renderer *renderer);
120
124 absl::Status SetPalette(const SnesPalette &palette);
126 size_t index, int length = 7);
127 void SetPalette(const std::vector<SDL_Color> &palette);
129 int palette_id);
130
131 void Get8x8Tile(int tile_index, int x, int y, std::vector<uint8_t> &tile_data,
132 int &tile_data_offset);
133
134 void Get16x16Tile(int tile_x, int tile_y, std::vector<uint8_t> &tile_data,
135 int &tile_data_offset);
136
137 void WriteToPixel(int position, uint8_t value) {
138 if (pixel_data_ == nullptr) {
139 pixel_data_ = data_.data();
140 }
141 pixel_data_[position] = value;
142 modified_ = true;
143 }
144
145 void WriteColor(int position, const ImVec4 &color);
146
147 void Cleanup() {
148 active_ = false;
149 width_ = 0;
150 height_ = 0;
151 depth_ = 0;
152 data_size_ = 0;
153 palette_.clear();
154 }
155
156 auto palette() const { return palette_; }
157 auto mutable_palette() { return &palette_; }
158
159 int width() const { return width_; }
160 int height() const { return height_; }
161 auto depth() const { return depth_; }
162 auto size() const { return data_size_; }
163 auto data() const { return data_.data(); }
164 auto &mutable_data() { return data_; }
165 auto surface() const { return surface_.get(); }
166
167 auto vector() const { return data_; }
168 auto at(int i) const { return data_[i]; }
169 auto texture() const { return texture_.get(); }
170 auto modified() const { return modified_; }
171 auto is_active() const { return active_; }
172 void set_active(bool active) { active_ = active; }
173 void set_data(const std::vector<uint8_t> &data) { data_ = data; }
175
176 private:
177 int width_ = 0;
178 int height_ = 0;
179 int depth_ = 0;
180 int data_size_ = 0;
181
182 bool active_ = false;
183 bool modified_ = false;
184 void *texture_pixels = nullptr;
185
186 uint8_t *pixel_data_ = nullptr;
187 std::vector<uint8_t> data_;
188
190 std::shared_ptr<SDL_Texture> texture_ = nullptr;
191 std::shared_ptr<SDL_Surface> surface_ = nullptr;
192};
193
194using BitmapTable = std::unordered_map<int, gfx::Bitmap>;
195
196} // namespace gfx
197} // namespace yaze
198
199#endif // YAZE_APP_GFX_BITMAP_H
void WriteToPixel(int position, uint8_t value)
Definition bitmap.h:137
auto at(int i) const
Definition bitmap.h:168
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:76
auto size() const
Definition bitmap.h:162
void Reformat(int format)
Definition bitmap.cc:267
uint8_t * pixel_data_
Definition bitmap.h:186
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
absl::Status SetPalette(const SnesPalette &palette)
Copy color data from the SnesPalette into the SDL_Palette.
Definition bitmap.cc:329
void Initialize(int width, int height, int depth, std::span< uint8_t > &data)
Definition bitmap.cc:216
auto vector() const
Definition bitmap.h:167
void set_modified(bool modified)
Definition bitmap.h:174
auto mutable_palette()
Definition bitmap.h:157
void set_active(bool active)
Definition bitmap.h:172
void WriteColor(int position, const ImVec4 &color)
Definition bitmap.cc:468
int height() const
Definition bitmap.h:160
void set_data(const std::vector< uint8_t > &data)
Definition bitmap.h:173
std::shared_ptr< SDL_Texture > texture_
Definition bitmap.h:190
int width() const
Definition bitmap.h:159
std::vector< uint8_t > data_
Definition bitmap.h:187
auto & mutable_data()
Definition bitmap.h:164
std::shared_ptr< SDL_Surface > surface_
Definition bitmap.h:191
void CreateTexture(SDL_Renderer *renderer)
Creates the underlying SDL_Texture to be displayed.
Definition bitmap.cc:281
auto palette() const
Definition bitmap.h:156
auto is_active() const
Definition bitmap.h:171
auto surface() const
Definition bitmap.h:165
auto modified() const
Definition bitmap.h:170
Bitmap(int width, int height, int depth, const std::vector< uint8_t > &data)
Definition bitmap.h:72
void * texture_pixels
Definition bitmap.h:184
auto data() const
Definition bitmap.h:163
void Create(int width, int height, int depth, int data_size)
Definition bitmap.h:97
absl::Status SetPaletteWithTransparent(const SnesPalette &palette, size_t index, int length=7)
Definition bitmap.cc:382
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:189
auto texture() const
Definition bitmap.h:169
auto depth() const
Definition bitmap.h:161
absl::Status SetPaletteFromPaletteGroup(const SnesPalette &palette, int palette_id)
Definition bitmap.cc:358
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:39
@ kIndexed
Definition bitmap.h:40
@ k2bpp
Definition bitmap.h:41
@ k8bpp
Definition bitmap.h:43
@ k4bpp
Definition bitmap.h:42
constexpr Uint32 SNES_PIXELFORMAT_8BPP
Definition bitmap.h:35
constexpr Uint32 SNES_PIXELFORMAT_INDEXED
Definition bitmap.h:24
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:194
Main namespace for the application.
Definition controller.cc:18