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
9#include "app/rom.h"
11#include "util/flag.h"
12#include "yaze_config.h"
13
14DEFINE_FLAG(std::string, rom_file, "",
15 "Path to the ROM file to load. "
16 "If not specified, the app will run without a ROM.");
17
18int yaze_app_main(int argc, char **argv) {
20 RETURN_IF_EXCEPTION(parser.Parse(argc, argv));
21 std::string rom_filename = "";
22 if (!FLAGS_rom_file->Get().empty()) {
23 rom_filename = FLAGS_rom_file->Get();
24 }
25
26#ifdef __APPLE__
27 return yaze_run_cocoa_app_delegate(rom_filename.c_str());
28#endif
29
30 auto controller = std::make_unique<yaze::core::Controller>();
31 EXIT_IF_ERROR(controller->OnEntry(rom_filename))
32 while (controller->IsActive()) {
33 controller->OnInput();
34 if (auto status = controller->OnLoad(); !status.ok()) {
35 std::cerr << status.message() << std::endl;
36 break;
37 }
38 controller->DoRender();
39 }
40 controller->OnExit();
41 return EXIT_SUCCESS;
42}
43
44void yaze_check_version(const char *version) {
45 std::string current_version;
46 std::stringstream ss;
47 ss << YAZE_VERSION_MAJOR << "." << YAZE_VERSION_MINOR << "."
48 << YAZE_VERSION_PATCH;
49 ss >> current_version;
50
51 if (version != current_version) {
52 std::cout << "Yaze version mismatch: expected " << current_version
53 << ", got " << version << std::endl;
54 exit(1);
55 }
56}
57
59 if (yaze_ctx->project->rom_filename == nullptr) {
60 yaze_ctx->error_message = "ROM filename is null";
61 return yaze_status::YAZE_ERROR;
62 }
63
64 yaze_ctx->rom = yaze_load_rom(yaze_ctx->project->rom_filename);
65 if (yaze_ctx->rom == nullptr) {
66 yaze_ctx->error_message = "Failed to load ROM";
67 return yaze_status::YAZE_ERROR;
68 }
69
70 return yaze_status::YAZE_OK;
71}
72
74 if (yaze_ctx->rom) {
75 yaze_unload_rom(yaze_ctx->rom);
76 }
77 return yaze_status::YAZE_OK;
78}
79
80yaze_project yaze_load_project(const char *filename) {
81 yaze_project project;
82 project.filepath = filename;
83 return project;
84}
85
86zelda3_rom *yaze_load_rom(const char *filename) {
87 yaze::Rom *internal_rom;
88 internal_rom = new yaze::Rom();
89 if (!internal_rom->LoadFromFile(filename).ok()) {
90 delete internal_rom;
91 return nullptr;
92 }
93
94 zelda3_rom *rom = new zelda3_rom();
95 rom->filename = filename;
96 rom->impl = internal_rom;
97 rom->data = internal_rom->data();
98 rom->size = internal_rom->size();
99 return rom;
100}
101
103 if (rom->impl) {
104 delete static_cast<yaze::Rom *>(rom->impl);
105 }
106
107 if (rom) {
108 delete rom;
109 }
110}
111
112void yaze_save_rom(zelda3_rom *rom, const char *filename) {
113 if (rom->impl) {
114 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
115 if (auto status = internal_rom->SaveToFile(yaze::Rom::SaveSettings{
116 .backup = true, .save_new = false, .filename = filename});
117 !status.ok()) {
118 throw std::runtime_error(status.message().data());
119 }
120 }
121}
122
123yaze_bitmap yaze_load_bitmap(const char *filename) {
124 yaze_bitmap bitmap;
125 bitmap.width = 0;
126 bitmap.height = 0;
127 bitmap.bpp = 0;
128 bitmap.data = nullptr;
129 return bitmap;
130}
131
133 int palette_set, int palette,
134 int color) {
135 snes_color color_struct;
136 color_struct.red = 0;
137 color_struct.green = 0;
138 color_struct.blue = 0;
139
140 if (rom->impl) {
141 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
142 auto get_color =
143 internal_rom->palette_group()
144 .get_group(yaze::gfx::kPaletteGroupAddressesKeys[palette_set])
145 ->palette(palette)[color];
146 color_struct = get_color.rom_color();
147
148 return color_struct;
149 }
150
151 return color_struct;
152}
153
155 if (rom->impl == nullptr) {
156 return nullptr;
157 }
158
159 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
160 auto internal_overworld = new yaze::zelda3::Overworld(internal_rom);
161 if (!internal_overworld->Load(internal_rom).ok()) {
162 return nullptr;
163 }
164
165 zelda3_overworld *overworld = new zelda3_overworld();
166 overworld->impl = internal_overworld;
167 int map_id = 0;
168 for (const auto &ow_map : internal_overworld->overworld_maps()) {
169 overworld->maps[map_id] = new zelda3_overworld_map();
170 overworld->maps[map_id]->id = map_id;
171 map_id++;
172 }
173 return overworld;
174}
175
177 if (rom->impl == nullptr) {
178 return nullptr;
179 }
180 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
182 return rooms;
183}
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:112
#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
constexpr const char * kPaletteGroupAddressesKeys[]
FlagRegistry * global_flag_registry()
Definition flag.h:99
Primitive of 16-bit RGB SNES color.
Definition yaze.h:56
uint16_t green
Definition yaze.h:59
uint16_t red
Definition yaze.h:57
uint16_t blue
Definition yaze.h:58
uint8_t * data
Definition yaze.h:48
int height
Definition yaze.h:46
uint8_t bpp
Definition yaze.h:47
int width
Definition yaze.h:45
yaze_project * project
Definition yaze.h:18
zelda3_rom * rom
Definition yaze.h:17
const char * error_message
Definition yaze.h:19
const char * rom_filename
Definition yaze.h:37
const char * filepath
Definition yaze.h:36
Primitive of the overworld.
Definition zelda.h:119
void * impl
Definition zelda.h:120
zelda3_overworld_map ** maps
Definition zelda.h:121
const char * filename
Definition zelda.h:88
size_t size
Definition zelda.h:90
void * impl
Definition zelda.h:91
const uint8_t * data
Definition zelda.h:89
zelda3_overworld * yaze_load_overworld(const zelda3_rom *rom)
Definition yaze.cc:154
void yaze_unload_rom(zelda3_rom *rom)
Definition yaze.cc:102
yaze_status yaze_shutdown(yaze_editor_context *yaze_ctx)
Definition yaze.cc:73
yaze_bitmap yaze_load_bitmap(const char *filename)
Definition yaze.cc:123
int yaze_app_main(int argc, char **argv)
Definition yaze.cc:18
yaze_status yaze_init(yaze_editor_context *yaze_ctx)
Definition yaze.cc:58
zelda3_dungeon_room * yaze_load_all_rooms(const zelda3_rom *rom)
Definition yaze.cc:176
void yaze_check_version(const char *version)
Definition yaze.cc:44
snes_color yaze_get_color_from_paletteset(const zelda3_rom *rom, int palette_set, int palette, int color)
Definition yaze.cc:132
yaze_project yaze_load_project(const char *filename)
Definition yaze.cc:80
zelda3_rom * yaze_load_rom(const char *filename)
Definition yaze.cc:86
void yaze_save_rom(zelda3_rom *rom, const char *filename)
Definition yaze.cc:112
yaze_status
Definition yaze.h:22
struct zelda3_overworld_map zelda3_overworld_map
Primitive of an overworld map.
struct zelda3_overworld zelda3_overworld
Primitive of the overworld.
struct zelda3_rom zelda3_rom