yaze 0.2.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze.cc
Go to the documentation of this file.
1#include "yaze.h"
2
3#include <iostream>
4#include <memory>
5#include <sstream>
6
10#include "app/rom.h"
12#include "util/flag.h"
13#include "yaze_config.h"
14
15DEFINE_FLAG(std::string, rom_file, "",
16 "Path to the ROM file to load. "
17 "If not specified, the app will run without a ROM.");
18
19int yaze_app_main(int argc, char **argv) {
21 RETURN_IF_EXCEPTION(parser.Parse(argc, argv));
22 std::string rom_filename = "";
23 if (!FLAGS_rom_file->Get().empty()) {
24 rom_filename = FLAGS_rom_file->Get();
25 }
26
27#ifdef __APPLE__
28 return yaze_run_cocoa_app_delegate(rom_filename.c_str());
29#endif
30
31 auto controller = std::make_unique<yaze::core::Controller>();
32 EXIT_IF_ERROR(controller->OnEntry(rom_filename))
33 while (controller->IsActive()) {
34 controller->OnInput();
35 if (auto status = controller->OnLoad(); !status.ok()) {
36 std::cerr << status.message() << std::endl;
37 break;
38 }
39 controller->DoRender();
40 }
41 controller->OnExit();
42 return EXIT_SUCCESS;
43}
44
45void yaze_check_version(const char *version) {
46 std::string current_version;
47 std::stringstream ss;
48 ss << YAZE_VERSION_MAJOR << "." << YAZE_VERSION_MINOR << "."
49 << YAZE_VERSION_PATCH;
50 ss >> current_version;
51
52 if (version != current_version) {
53 std::cout << "Yaze version mismatch: expected " << current_version
54 << ", got " << version << std::endl;
55 exit(1);
56 }
57}
58
59yaze_status yaze_init(yaze_editor_context *yaze_ctx, char *rom_filename) {
60 yaze_ctx->rom = yaze_load_rom(rom_filename);
61 if (yaze_ctx->rom == nullptr) {
62 yaze_ctx->error_message = "Failed to load ROM";
63 return yaze_status::YAZE_ERROR;
64 }
65
66 return yaze_status::YAZE_OK;
67}
68
70 if (yaze_ctx->rom) {
71 yaze_unload_rom(yaze_ctx->rom);
72 }
73 return yaze_status::YAZE_OK;
74}
75
76zelda3_rom *yaze_load_rom(const char *filename) {
77 yaze::Rom *internal_rom;
78 internal_rom = new yaze::Rom();
79 if (!internal_rom->LoadFromFile(filename).ok()) {
80 delete internal_rom;
81 return nullptr;
82 }
83
84 zelda3_rom *rom = new zelda3_rom();
85 rom->filename = filename;
86 rom->impl = internal_rom;
87 rom->data = const_cast<uint8_t *>(internal_rom->data());
88 rom->size = internal_rom->size();
89 return rom;
90}
91
93 if (rom->impl) {
94 delete static_cast<yaze::Rom *>(rom->impl);
95 }
96
97 if (rom) {
98 delete rom;
99 }
100}
101
102void yaze_save_rom(zelda3_rom *rom, const char *filename) {
103 if (rom->impl) {
104 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
105 if (auto status = internal_rom->SaveToFile(yaze::Rom::SaveSettings{
106 .backup = true, .save_new = false, .filename = filename});
107 !status.ok()) {
108 throw std::runtime_error(status.message().data());
109 }
110 }
111}
112
113yaze_bitmap yaze_load_bitmap(const char *filename) {
114 yaze_bitmap bitmap;
115 bitmap.width = 0;
116 bitmap.height = 0;
117 bitmap.bpp = 0;
118 bitmap.data = nullptr;
119 return bitmap;
120}
121
123 int palette_set, int palette,
124 int color) {
125 snes_color color_struct;
126 color_struct.red = 0;
127 color_struct.green = 0;
128 color_struct.blue = 0;
129
130 if (rom->impl) {
131 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
132 auto get_color =
133 internal_rom->palette_group()
134 .get_group(yaze::gfx::kPaletteGroupAddressesKeys[palette_set])
135 ->palette(palette)[color];
136 color_struct = get_color.rom_color();
137
138 return color_struct;
139 }
140
141 return color_struct;
142}
143
145 if (rom->impl == nullptr) {
146 return nullptr;
147 }
148
149 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
150 auto internal_overworld = new yaze::zelda3::Overworld(internal_rom);
151 if (!internal_overworld->Load(internal_rom).ok()) {
152 return nullptr;
153 }
154
155 zelda3_overworld *overworld = new zelda3_overworld();
156 overworld->impl = internal_overworld;
157 int map_id = 0;
158 for (const auto &ow_map : internal_overworld->overworld_maps()) {
159 overworld->maps[map_id] = new zelda3_overworld_map();
160 overworld->maps[map_id]->id = map_id;
161 map_id++;
162 }
163 return overworld;
164}
165
167 if (rom->impl == nullptr) {
168 return nullptr;
169 }
170 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
172 return rooms;
173}
174
176 if (rom->impl == nullptr) {
177 return yaze_status::YAZE_ERROR;
178 }
179
180 // Use LoadAllTextData from message_data.h
181 std::vector<yaze::editor::MessageData> message_data =
183 for (const auto &message : message_data) {
184 messages[message.ID] = new zelda3_message();
185 messages[message.ID]->id = message.ID;
186 messages[message.ID]->address = message.Address;
187 messages[message.ID]->raw_string = reinterpret_cast<uint8_t *>(
188 const_cast<char *>(message.RawString.data()));
189 messages[message.ID]->contents_parsed = reinterpret_cast<uint8_t *>(
190 const_cast<char *>(message.ContentsParsed.data()));
191 messages[message.ID]->data =
192 reinterpret_cast<uint8_t *>(const_cast<uint8_t *>(message.Data.data()));
193 messages[message.ID]->data_parsed = reinterpret_cast<uint8_t *>(
194 const_cast<uint8_t *>(message.DataParsed.data()));
195 }
196
197 return yaze_status::YAZE_OK;
198}
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:58
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:231
auto palette_group() const
Definition rom.h:187
absl::Status SaveToFile(const SaveSettings &settings)
Definition rom.cc:386
auto data() const
Definition rom.h:177
auto size() const
Definition rom.h:176
void Parse(int argc, char **argv)
Definition flag.h:119
Represents the full Overworld data, light and dark world.
Definition overworld.h:111
#define DEFINE_FLAG(type, name, default_val, help_text)
Definition flag.h:106
#define RETURN_IF_EXCEPTION(expression)
Definition macro.h:101
#define EXIT_IF_ERROR(expression)
Definition macro.h:33
std::vector< MessageData > ReadAllTextData(uint8_t *rom, int pos)
constexpr const char * kPaletteGroupAddressesKeys[]
FlagRegistry * global_flag_registry()
Definition flag.h:99
Primitive of 16-bit RGB SNES color.
Definition yaze.h:43
uint16_t green
Definition yaze.h:46
uint16_t red
Definition yaze.h:44
uint16_t blue
Definition yaze.h:45
uint8_t * data
Definition yaze.h:35
int height
Definition yaze.h:33
uint8_t bpp
Definition yaze.h:34
int width
Definition yaze.h:32
zelda3_rom * rom
Definition yaze.h:15
const char * error_message
Definition yaze.h:16
Primitive of a message.
Definition zelda.h:102
uint8_t address
Definition zelda.h:104
uint8_t id
Definition zelda.h:103
uint8_t * data_parsed
Definition zelda.h:108
uint8_t * contents_parsed
Definition zelda.h:106
uint8_t * data
Definition zelda.h:107
uint8_t * raw_string
Definition zelda.h:105
Primitive of the overworld.
Definition zelda.h:132
void * impl
Definition zelda.h:133
zelda3_overworld_map ** maps
Definition zelda.h:134
uint8_t * data
Definition zelda.h:89
const char * filename
Definition zelda.h:88
uint64_t size
Definition zelda.h:90
void * impl
Definition zelda.h:91
zelda3_overworld * yaze_load_overworld(const zelda3_rom *rom)
Load the overworld from the ROM.
Definition yaze.cc:144
void yaze_unload_rom(zelda3_rom *rom)
Definition yaze.cc:92
yaze_status yaze_shutdown(yaze_editor_context *yaze_ctx)
Definition yaze.cc:69
yaze_status yaze_load_messages(zelda3_rom *rom, zelda3_message **messages)
Load all messages from the ROM.
Definition yaze.cc:175
yaze_bitmap yaze_load_bitmap(const char *filename)
Definition yaze.cc:113
int yaze_app_main(int argc, char **argv)
Definition yaze.cc:19
zelda3_dungeon_room * yaze_load_all_rooms(const zelda3_rom *rom)
Load all rooms from the ROM.
Definition yaze.cc:166
yaze_status yaze_init(yaze_editor_context *yaze_ctx, char *rom_filename)
Definition yaze.cc:59
void yaze_check_version(const char *version)
Definition yaze.cc:45
snes_color yaze_get_color_from_paletteset(const zelda3_rom *rom, int palette_set, int palette, int color)
Get a color from a palette set.
Definition yaze.cc:122
zelda3_rom * yaze_load_rom(const char *filename)
Definition yaze.cc:76
void yaze_save_rom(zelda3_rom *rom, const char *filename)
Definition yaze.cc:102
yaze_status
Definition yaze.h:19
struct zelda3_overworld_map zelda3_overworld_map
Primitive of an overworld map.
struct zelda3_message zelda3_message
Primitive of a message.
struct zelda3_overworld zelda3_overworld
Primitive of the overworld.
struct zelda3_rom zelda3_rom