yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
palette.cc
Go to the documentation of this file.
1#include "absl/flags/declare.h"
2#include "absl/flags/flag.h"
5#include "cli/cli.h"
7#include "zelda3/game_data.h"
8
9ABSL_DECLARE_FLAG(std::string, rom);
10
11namespace yaze {
12namespace cli {
13
14// Forward declarations for legacy functions
15absl::Status HandlePaletteExportLegacy(const std::vector<std::string>& arg_vec);
16absl::Status HandlePaletteImportLegacy(const std::vector<std::string>& arg_vec);
17
18// Legacy Palette constructor removed - using new CommandHandler system
19
20// Legacy Palette class removed - using new CommandHandler system
21// This implementation should be moved to PaletteCommandHandler
22absl::Status HandlePaletteLegacy(const std::vector<std::string>& arg_vec) {
23 if (arg_vec.empty()) {
24 return absl::InvalidArgumentError(
25 "Usage: palette <export|import> [options]");
26 }
27
28 const std::string& action = arg_vec[0];
29 std::vector<std::string> new_args(arg_vec.begin() + 1, arg_vec.end());
30
31 if (action == "export") {
32 return HandlePaletteExportLegacy(new_args);
33 } else if (action == "import") {
34 return HandlePaletteImportLegacy(new_args);
35 }
36
37 return absl::InvalidArgumentError("Invalid action for palette command.");
38}
39
40// Legacy Palette TUI removed - using new CommandHandler system
41void HandlePaletteTUI(ftxui::ScreenInteractive& screen) {
42 // TODO: Implement palette editor TUI
43 (void)screen; // Suppress unused parameter warning
44}
45
46// Legacy PaletteExport class removed - using new CommandHandler system
48 const std::vector<std::string>& arg_vec) {
49 std::string group_name;
50 int palette_id = -1;
51 std::string output_file;
52
53 for (size_t i = 0; i < arg_vec.size(); ++i) {
54 const std::string& arg = arg_vec[i];
55 if ((arg == "--group") && i + 1 < arg_vec.size()) {
56 group_name = arg_vec[++i];
57 } else if ((arg == "--id") && i + 1 < arg_vec.size()) {
58 palette_id = std::stoi(arg_vec[++i]);
59 } else if ((arg == "--to") && i + 1 < arg_vec.size()) {
60 output_file = arg_vec[++i];
61 }
62 }
63
64 if (group_name.empty() || palette_id == -1 || output_file.empty()) {
65 return absl::InvalidArgumentError(
66 "Usage: palette export --group <group> --id <id> --to <file>");
67 }
68
69 std::string rom_file = absl::GetFlag(FLAGS_rom);
70 if (rom_file.empty()) {
71 return absl::InvalidArgumentError(
72 "ROM file must be provided via --rom flag.");
73 }
74
75 Rom rom;
76 rom.LoadFromFile(rom_file);
77 if (!rom.is_loaded()) {
78 return absl::AbortedError("Failed to load ROM.");
79 }
80
81 // Load game data for palette access
82 zelda3::GameData game_data;
83 auto load_status = zelda3::LoadGameData(rom, game_data);
84 if (!load_status.ok()) {
85 return absl::AbortedError("Failed to load game data.");
86 }
87
88 auto palette_group = game_data.palette_groups.get_group(group_name);
89 if (!palette_group) {
90 return absl::NotFoundError("Palette group not found.");
91 }
92
93 auto palette = palette_group->palette(palette_id);
94 if (palette.empty()) {
95 return absl::NotFoundError("Palette not found.");
96 }
97
98 std::vector<SDL_Color> sdl_palette;
99 for (const auto& color : palette) {
100 SDL_Color sdl_color;
101 sdl_color.r = color.rgb().x;
102 sdl_color.g = color.rgb().y;
103 sdl_color.b = color.rgb().z;
104 sdl_color.a = 255;
105 sdl_palette.push_back(sdl_color);
106 }
107
108 auto status = gfx::SaveCol(output_file, sdl_palette);
109 if (!status.ok()) {
110 return status;
111 }
112
113 std::cout << "Successfully exported palette " << palette_id << " from group "
114 << group_name << " to " << output_file << std::endl;
115
116 return absl::OkStatus();
117}
118
119// Legacy PaletteImport class removed - using new CommandHandler system
121 const std::vector<std::string>& arg_vec) {
122 std::string group_name;
123 int palette_id = -1;
124 std::string input_file;
125
126 for (size_t i = 0; i < arg_vec.size(); ++i) {
127 const std::string& arg = arg_vec[i];
128 if ((arg == "--group") && i + 1 < arg_vec.size()) {
129 group_name = arg_vec[++i];
130 } else if ((arg == "--id") && i + 1 < arg_vec.size()) {
131 palette_id = std::stoi(arg_vec[++i]);
132 } else if ((arg == "--from") && i + 1 < arg_vec.size()) {
133 input_file = arg_vec[++i];
134 }
135 }
136
137 if (group_name.empty() || palette_id == -1 || input_file.empty()) {
138 return absl::InvalidArgumentError(
139 "Usage: palette import --group <group> --id <id> --from <file>");
140 }
141
142 std::string rom_file = absl::GetFlag(FLAGS_rom);
143 if (rom_file.empty()) {
144 return absl::InvalidArgumentError(
145 "ROM file must be provided via --rom flag.");
146 }
147
148 Rom rom;
149 rom.LoadFromFile(rom_file);
150 if (!rom.is_loaded()) {
151 return absl::AbortedError("Failed to load ROM.");
152 }
153
154 // Load game data for palette access
155 zelda3::GameData game_data;
156 auto load_status = zelda3::LoadGameData(rom, game_data);
157 if (!load_status.ok()) {
158 return absl::AbortedError("Failed to load game data.");
159 }
160
161 auto sdl_palette = gfx::DecodeColFile(input_file);
162 if (sdl_palette.empty()) {
163 return absl::AbortedError("Failed to load palette file.");
164 }
165
167 for (const auto& sdl_color : sdl_palette) {
168 snes_palette.AddColor(
169 gfx::SnesColor(sdl_color.r, sdl_color.g, sdl_color.b));
170 }
171
172 auto* palette_group = game_data.palette_groups.get_group(group_name);
173 if (!palette_group) {
174 return absl::NotFoundError("Palette group not found.");
175 }
176
177 // Replace the palette at the specified index
178 auto* pal = palette_group->mutable_palette(palette_id);
179 *pal = snes_palette;
180
181 // TODO: Implement saving the modified palette back to the ROM.
182 auto save_status = rom.SaveToFile({.save_new = false});
183 if (!save_status.ok()) {
184 return save_status;
185 }
186
187 std::cout << "Successfully imported palette " << palette_id << " to group "
188 << group_name << " from " << input_file << std::endl;
189 std::cout << "✅ ROM saved to: " << rom.filename() << std::endl;
190
191 return absl::OkStatus();
192}
193
194} // namespace cli
195} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
absl::Status LoadFromFile(const std::string &filename, const LoadOptions &options=LoadOptions::Defaults())
Definition rom.cc:74
auto filename() const
Definition rom.h:141
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:164
bool is_loaded() const
Definition rom.h:128
SNES Color container.
Definition snes_color.h:110
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
struct snes_palette snes_palette
SNES color palette.
void HandlePaletteTUI(ftxui::ScreenInteractive &screen)
Definition palette.cc:41
absl::Status HandlePaletteLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:22
absl::Status HandlePaletteExportLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:47
absl::Status HandlePaletteImportLegacy(const std::vector< std::string > &arg_vec)
Definition palette.cc:120
absl::Status SaveCol(std::string_view filename, const std::vector< SDL_Color > &palette)
Save Col file (palette data)
std::vector< SDL_Color > DecodeColFile(const std::string_view filename)
Decode color file.
absl::Status LoadGameData(Rom &rom, GameData &data, const LoadOptions &options)
Loads all Zelda3-specific game data from a generic ROM.
Definition game_data.cc:80
ABSL_DECLARE_FLAG(std::string, rom)
SNES color palette.
Definition yaze.h:250
PaletteGroup * get_group(const std::string &group_name)
gfx::PaletteGroupMap palette_groups
Definition game_data.h:89