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(false, false, filename);
116 !status.ok()) {
117 throw std::runtime_error(status.message().data());
118 }
119 }
120}
121
122yaze_bitmap yaze_load_bitmap(const char *filename) {
123 yaze_bitmap bitmap;
124 bitmap.width = 0;
125 bitmap.height = 0;
126 bitmap.bpp = 0;
127 bitmap.data = nullptr;
128 return bitmap;
129}
130
132 int palette_set, int palette,
133 int color) {
134 snes_color color_struct;
135 color_struct.red = 0;
136 color_struct.green = 0;
137 color_struct.blue = 0;
138
139 if (rom->impl) {
140 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
141 auto get_color =
142 internal_rom->palette_group()
143 .get_group(yaze::gfx::kPaletteGroupAddressesKeys[palette_set])
144 ->palette(palette)[color];
145 color_struct = get_color.rom_color();
146
147 return color_struct;
148 }
149
150 return color_struct;
151}
152
154 if (rom->impl == nullptr) {
155 return nullptr;
156 }
157
158 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
159 auto internal_overworld = new yaze::zelda3::Overworld(internal_rom);
160 if (!internal_overworld->Load(internal_rom).ok()) {
161 return nullptr;
162 }
163
164 zelda3_overworld *overworld = new zelda3_overworld();
165 overworld->impl = internal_overworld;
166 int map_id = 0;
167 for (const auto &ow_map : internal_overworld->overworld_maps()) {
168 overworld->maps[map_id] = new zelda3_overworld_map();
169 overworld->maps[map_id]->id = map_id;
170 map_id++;
171 }
172 return overworld;
173}
174
176 if (rom->impl == nullptr) {
177 return nullptr;
178 }
179 yaze::Rom *internal_rom = static_cast<yaze::Rom *>(rom->impl);
181 return rooms;
182}
The Rom class is used to load, save, and modify Rom data.
Definition rom.h:59
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
Definition rom.cc:172
auto palette_group() const
Definition rom.h:174
absl::Status SaveToFile(bool backup, bool save_new=false, std::string filename="")
Saves the Rom data to a file.
Definition rom.cc:409
auto data() const
Definition rom.h:163
auto size() const
Definition rom.h:162
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 snes.h:14
uint16_t green
Definition snes.h:17
uint16_t red
Definition snes.h:15
uint16_t blue
Definition snes.h:16
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:153
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:122
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:175
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:131
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