yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
snes_palette.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_PALETTE_H
2#define YAZE_APP_GFX_PALETTE_H
3
4#include <cstdint>
5#include <cstdlib>
6#include <cstring>
7#include <iostream>
8#include <memory>
9#include <vector>
10
11#include "absl/base/casts.h"
12#include "absl/status/status.h"
13#include "absl/status/statusor.h"
14#include "app/core/constants.h"
15#include "app/gfx/snes_color.h"
16#include "incl/snes_color.h"
17#include "imgui/imgui.h"
18
19namespace yaze {
20namespace app {
21namespace gfx {
22
23constexpr int kNumPalettes = 14;
24
41
42static constexpr absl::string_view kPaletteCategoryNames[] = {
43 "Sword", "Shield", "Clothes", "World Colors",
44 "Area Colors", "Global Sprites", "Sprites Aux1", "Sprites Aux2",
45 "Sprites Aux3", "Dungeons", "World Map", "Dungeon Map",
46 "Triforce", "Crystal"};
47
48static constexpr absl::string_view kPaletteGroupNames[] = {
49 "swords", "shields", "armors", "ow_main",
50 "ow_aux", "global_sprites", "sprites_aux1", "sprites_aux2",
51 "sprites_aux3", "dungeon_main", "ow_mini_map", "ow_mini_map",
52 "3d_object", "3d_object"};
53
54constexpr const char* kPaletteGroupAddressesKeys[] = {
55 "ow_main", "ow_aux", "ow_animated", "hud",
56 "global_sprites", "armors", "swords", "shields",
57 "sprites_aux1", "sprites_aux2", "sprites_aux3", "dungeon_main",
58 "grass", "3d_object", "ow_mini_map",
59};
60
61constexpr int kOverworldPaletteMain = 0xDE6C8;
62constexpr int kOverworldPaletteAux = 0xDE86C;
63constexpr int kOverworldPaletteAnimated = 0xDE604;
64constexpr int kGlobalSpritesLW = 0xDD218;
65constexpr int kGlobalSpritePalettesDW = 0xDD290;
66constexpr int kArmorPalettes =
67 0xDD308;
68constexpr int kSpritesPalettesAux1 = 0xDD39E; // 7 colors each
69constexpr int kSpritesPalettesAux2 = 0xDD446; // 7 colors each
70constexpr int kSpritesPalettesAux3 = 0xDD4E0; // 7 colors each
71constexpr int kSwordPalettes = 0xDD630; // 3 colors each - 4 entries
72constexpr int kShieldPalettes = 0xDD648; // 4 colors each - 3 entries
73constexpr int kHudPalettes = 0xDD660;
74constexpr int kDungeonMapPalettes = 0xDD70A; // 21 colors
75constexpr int kDungeonMainPalettes =
76 0xDD734; // (15*6) colors each - 20 entries
77constexpr int kDungeonMapBgPalettes = 0xDE544; // 16*6
78
79// Mirrored Value at 0x75645 : 0x75625
80constexpr int kHardcodedGrassLW = 0x5FEA9;
81constexpr int kHardcodedGrassDW = 0x05FEB3; // 0x7564F
82constexpr int kHardcodedGrassSpecial = 0x75640;
83constexpr int kOverworldMiniMapPalettes = 0x55B27;
84constexpr int kTriforcePalette = 0x64425;
85constexpr int kCrystalPalette = 0xF4CD3;
87 0x140000;
89constexpr int CustomAreaSpecificBGASM = 0x140150;
90
91// 1 byte, not 0 if enabled
92constexpr int kCustomAreaSpecificBGEnabled = 0x140140;
93
94uint32_t GetPaletteAddress(const std::string& group_name, size_t palette_index,
95 size_t color_index);
96
110 public:
111 template <typename T>
112 explicit SnesPalette(const std::vector<T>& data) {
113 for (const auto& item : data) {
114 colors.emplace_back(SnesColor(item));
115 }
116 }
117
118 SnesPalette() = default;
119 explicit SnesPalette(char* snesPal);
120 explicit SnesPalette(const unsigned char* snes_pal);
121 explicit SnesPalette(const std::vector<ImVec4>&);
122 explicit SnesPalette(const std::vector<snes_color>&);
123 explicit SnesPalette(const std::vector<SnesColor>&);
124
125 void Create(const std::vector<SnesColor>& cols) {
126 for (const auto& each : cols) {
127 colors.emplace_back(each);
128 }
129 }
130
131 void AddColor(const SnesColor& color) { colors.emplace_back(color); }
132 void AddColor(const snes_color& color) { colors.emplace_back(color); }
133 void AddColor(uint16_t color) { colors.emplace_back(color); }
134
135 absl::StatusOr<SnesColor> GetColor(int i) const {
136 if (i > colors.size()) {
137 return absl::InvalidArgumentError("SnesPalette: Index out of bounds");
138 }
139 return colors[i];
140 }
141
142 auto mutable_color(int i) { return &colors[i]; }
143
144 void clear() { colors.clear(); }
145 auto size() const { return colors.size(); }
146 auto empty() const { return colors.empty(); }
147
149 if (i > colors.size()) {
150 std::cout << "SNESPalette: Index out of bounds" << std::endl;
151 return colors[0];
152 }
153 return colors[i];
154 }
155
156 void operator()(int i, const SnesColor& color) {
157 if (i >= colors.size()) {
158 std::cout << "SNESPalette: Index out of bounds" << std::endl;
159 }
160 colors[i] = color;
161 }
162
163 void operator()(int i, const ImVec4& color) {
164 if (i >= colors.size()) {
165 std::cout << "SNESPalette: Index out of bounds" << std::endl;
166 return;
167 }
168 colors[i].set_rgb(color);
169 colors[i].set_modified(true);
170 }
171
172 SnesPalette sub_palette(int start, int end) const {
173 SnesPalette pal;
174 for (int i = start; i < end; i++) {
175 pal.AddColor(colors[i]);
176 }
177 return pal;
178 }
179
180 private:
181 std::vector<SnesColor> colors;
182};
183
184SnesPalette ReadPaletteFromRom(int offset, int num_colors, const uint8_t* rom);
185
186std::array<float, 4> ToFloatArray(const SnesColor& color);
187
195 PaletteGroup() = default;
196
197 void AddPalette(SnesPalette pal) { palettes.emplace_back(pal); }
198
199 void AddColor(SnesColor color) {
200 if (palettes.empty()) {
201 palettes.emplace_back();
202 }
203 palettes[0].AddColor(color);
204 }
205
206 void clear() { palettes.clear(); }
207 void SetName(const std::string& name) { name_ = name; }
208 auto name() const { return name_; }
209 auto size() const { return palettes.size(); }
210 auto mutable_palette(int i) { return &palettes[i]; }
211 auto palette(int i) const { return palettes[i]; }
212
214 if (i > palettes.size()) {
215 std::cout << "PaletteGroup: Index out of bounds" << std::endl;
216 return palettes[0];
217 }
218 return palettes[i];
219 }
220
221 const SnesPalette& operator[](int i) const {
222 if (i > palettes.size()) {
223 std::cout << "PaletteGroup: Index out of bounds" << std::endl;
224 return palettes[0];
225 }
226 return palettes[i];
227 }
228
229 private:
230 std::string name_;
231 std::vector<SnesPalette> palettes;
232};
233
257
258 auto get_group(const std::string& group_name) {
259 if (group_name == "ow_main") {
260 return &overworld_main;
261 } else if (group_name == "ow_aux") {
262 return &overworld_aux;
263 } else if (group_name == "ow_animated") {
264 return &overworld_animated;
265 } else if (group_name == "hud") {
266 return &hud;
267 } else if (group_name == "global_sprites") {
268 return &global_sprites;
269 } else if (group_name == "armors") {
270 return &armors;
271 } else if (group_name == "swords") {
272 return &swords;
273 } else if (group_name == "shields") {
274 return &shields;
275 } else if (group_name == "sprites_aux1") {
276 return &sprites_aux1;
277 } else if (group_name == "sprites_aux2") {
278 return &sprites_aux2;
279 } else if (group_name == "sprites_aux3") {
280 return &sprites_aux3;
281 } else if (group_name == "dungeon_main") {
282 return &dungeon_main;
283 } else if (group_name == "grass") {
284 return &grass;
285 } else if (group_name == "3d_object") {
286 return &object_3d;
287 } else if (group_name == "ow_mini_map") {
288 return &overworld_mini_map;
289 } else {
290 throw std::out_of_range("PaletteGroupMap: Group not found");
291 }
292 }
293
294 template <typename Func>
295 absl::Status for_each(Func&& func) {
298 RETURN_IF_ERROR(func(hud));
300 RETURN_IF_ERROR(func(armors));
301 RETURN_IF_ERROR(func(swords));
307 RETURN_IF_ERROR(func(grass));
310 return absl::OkStatus();
311 }
312
330
331 bool empty() {
332 return overworld_main.size() == 0 && overworld_aux.size() == 0 &&
333 overworld_animated.size() == 0 && hud.size() == 0 &&
334 global_sprites.size() == 0 && armors.size() == 0 &&
335 swords.size() == 0 && shields.size() == 0 &&
336 sprites_aux1.size() == 0 && sprites_aux2.size() == 0 &&
337 sprites_aux3.size() == 0 && dungeon_main.size() == 0 &&
338 grass.size() == 0 && object_3d.size() == 0 &&
340 }
341};
342
343absl::StatusOr<PaletteGroup> CreatePaletteGroupFromColFile(
344 std::vector<SnesColor>& colors);
345
349absl::StatusOr<PaletteGroup> CreatePaletteGroupFromLargePalette(
350 SnesPalette& palette, int num_colors = 8);
351
361absl::Status LoadAllPalettes(const std::vector<uint8_t>& rom_data,
362 PaletteGroupMap& groups);
363
409
410} // namespace gfx
411} // namespace app
412} // namespace yaze
413
414#endif // YAZE_APP_GFX_PALETTE_H
SNES Color container.
Definition snes_color.h:38
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
SnesPalette sub_palette(int start, int end) const
void AddColor(uint16_t color)
void AddColor(const SnesColor &color)
void operator()(int i, const ImVec4 &color)
void AddColor(const snes_color &color)
SnesColor & operator[](int i)
std::vector< SnesColor > colors
absl::StatusOr< SnesColor > GetColor(int i) const
SnesPalette(const std::vector< T > &data)
void Create(const std::vector< SnesColor > &cols)
void operator()(int i, const SnesColor &color)
#define RETURN_IF_ERROR(expression)
Definition constants.h:69
uint32_t GetPaletteAddress(const std::string &group_name, size_t palette_index, size_t color_index)
constexpr int CustomAreaSpecificBGASM
constexpr int kSwordPalettes
constexpr int kDungeonMapPalettes
constexpr int kOverworldMiniMapPalettes
constexpr int CustomAreaSpecificBGPalette
constexpr int kShieldPalettes
constexpr int kDungeonMapBgPalettes
constexpr int kHudPalettes
constexpr int kSpritesPalettesAux3
std::array< float, 4 > ToFloatArray(const SnesColor &color)
SnesPalette ReadPaletteFromRom(int offset, int num_colors, const uint8_t *rom)
constexpr int kArmorPalettes
constexpr int kOverworldPaletteAnimated
absl::Status LoadAllPalettes(const std::vector< uint8_t > &rom_data, PaletteGroupMap &groups)
Loads all the palettes for the game.
constexpr int kHardcodedGrassDW
constexpr int kSpritesPalettesAux1
constexpr int kGlobalSpritePalettesDW
constexpr int kCrystalPalette
constexpr int kNumPalettes
constexpr int kTriforcePalette
absl::StatusOr< PaletteGroup > CreatePaletteGroupFromColFile(std::vector< SnesColor > &palette_rows)
constexpr int kHardcodedGrassLW
constexpr int kOverworldPaletteMain
constexpr int kCustomAreaSpecificBGEnabled
constexpr int kOverworldPaletteAux
constexpr int kSpritesPalettesAux2
constexpr int kHardcodedGrassSpecial
absl::StatusOr< PaletteGroup > CreatePaletteGroupFromLargePalette(SnesPalette &palette, int num_colors)
Take a SNESPalette, divide it into palettes of 8 colors.
constexpr int kGlobalSpritesLW
constexpr int kDungeonMainPalettes
Definition common.cc:21
Primitive of 16-bit RGB SNES color.
Definition snes_color.h:13
Represents a mapping of palette groups.
auto get_group(const std::string &group_name)
absl::Status for_each(Func &&func)
Represents a group of palettes.
std::vector< SnesPalette > palettes
const SnesPalette & operator[](int i) const
void AddPalette(SnesPalette pal)
void AddColor(SnesColor color)
void SetName(const std::string &name)
SnesPalette operator[](int i)
Represents a set of palettes used in a SNES graphics system.
Paletteset()=default
Default constructor for Paletteset.
gfx::SnesPalette animated
Paletteset(gfx::SnesPalette main, gfx::SnesPalette animated, gfx::SnesPalette aux1, gfx::SnesPalette aux2, gfx::SnesColor background, gfx::SnesPalette hud, gfx::SnesPalette spr, gfx::SnesPalette spr2, gfx::SnesPalette comp)
Constructor for Paletteset.
gfx::SnesPalette composite