15#include "absl/status/status.h"
16#include "absl/status/statusor.h"
17#include "absl/strings/str_cat.h"
18#include "absl/strings/string_view.h"
35 uint32_t ptr2, uint32_t ptr3) {
41 std::vector<uint8_t> sheet;
42 const uint8_t sheets[] = {0x71, 0x72, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE};
43 for (
const auto &sheet_id : sheets) {
51 for (
const auto &each_pixel : converted_sheet) {
52 sheet.push_back(each_pixel);
60 const uint32_t kLinkGfxOffset = 0x80000;
61 const uint16_t kLinkGfxLength = 0x800;
62 std::array<gfx::Bitmap, kNumLinkSheets> link_graphics;
78 std::vector<uint8_t> data(0x2000);
79 for (
int i = 0; i < 0x2000; i++) {
80 data[i] = rom.
data()[0x70000 + i];
83 std::vector<uint8_t> new_data(0x4000);
84 std::vector<uint8_t> mask = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
85 int sheet_position = 0;
88 for (
int s = 0; s < 4; s++) {
89 for (
int j = 0; j < 4; j++) {
90 for (
int i = 0; i < 16; i++) {
91 for (
int y = 0; y < 8; y++) {
93 data[(y * 2) + (i * 16) + (j * 256) + sheet_position];
95 data[(y * 2) + (i * 16) + (j * 256) + 1 + sheet_position];
97 for (
int x = 0; x < 4; x++) {
101 if ((line_bits0 & mask[(x * 2)]) == mask[(x * 2)]) {
104 if ((line_bits1 & mask[(x * 2)]) == mask[(x * 2)]) {
108 if ((line_bits0 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
111 if ((line_bits1 & mask[(x * 2) + 1]) == mask[(x * 2) + 1]) {
115 new_data[(y * 64) + (x) + (i * 4) + (j * 512) + (s * 2048)] =
116 (uint8_t)((pixdata << 4) | pixdata2);
122 sheet_position += 0x400;
125 std::vector<uint8_t> fontgfx16_data(0x4000);
126 for (
int i = 0; i < 0x4000; i++) {
127 fontgfx16_data[i] = new_data[i];
131 font_gfx.
Create(128, 128, 64, fontgfx16_data);
136 Rom &rom,
bool defer_render) {
137 std::array<gfx::Bitmap, kNumGfxSheets> graphics_sheets;
138 std::vector<uint8_t> sheet;
142 if (i >= 115 && i <= 126) {
151 }
else if (i == 113 || i == 114 || i >= 218) {
166 if (graphics_sheets[i].is_active()) {
169 graphics_sheets[i].SetPaletteWithTransparent(
172 graphics_sheets[i].SetPaletteWithTransparent(
178 graphics_sheets[i].CreateTexture(
Renderer::Get().renderer());
181 for (
int j = 0; j < graphics_sheets[i].size(); ++j) {
186 for (
int j = 0; j < graphics_sheets[0].size(); ++j) {
191 return graphics_sheets;
195 Rom &rom, std::array<gfx::Bitmap, kNumGfxSheets> &gfx_sheets) {
197 if (gfx_sheets[i].is_active()) {
199 std::vector<uint8_t> final_data;
200 bool compressed =
true;
201 if (i >= 115 && i <= 126) {
203 }
else if (i == 113 || i == 114 || i >= 218) {
208 std::cout <<
"Sheet ID " << i <<
" BPP: " << to_bpp << std::endl;
209 auto sheet_data = gfx_sheets[i].vector();
210 std::cout <<
"Sheet data size: " << sheet_data.size() << std::endl;
215 final_data.data(), final_data.size(), &size, 1);
216 for (
int j = 0; j < size; j++) {
217 sheet_data[j] = compressed_data[j];
224 std::copy(final_data.begin(), final_data.end(), rom.
begin() + offset);
225 std::ranges::copy(final_data, rom.
begin() + offset);
228 return absl::OkStatus();
233 return absl::InvalidArgumentError(
234 "Could not load ROM: parameter `filename` is empty.");
239 std::ifstream file(
filename_, std::ios::binary);
240 if (!file.is_open()) {
241 return absl::NotFoundError(
242 absl::StrCat(
"Could not open ROM file: ",
filename_));
248 }
catch (
const std::filesystem::filesystem_error &e) {
250 file.seekg(0, std::ios::end);
252 return absl::InternalError(absl::StrCat(
253 "Could not get file size: ",
filename_,
" - ", e.what()));
255 size_ = file.tellg();
268 return absl::OkStatus();
273 return absl::InvalidArgumentError(
274 "Could not load ROM: parameter `data` is empty.");
281 return absl::OkStatus();
286 constexpr size_t kBaseRomSize = 1048576;
287 constexpr size_t kHeaderSize = 0x200;
288 if (
size_ % kBaseRomSize == kHeaderSize) {
289 auto header = std::vector<uint8_t>(
rom_data_.begin(),
296 constexpr uint32_t kTitleStringOffset = 0x7FC0;
297 constexpr uint32_t kTitleStringLength = 20;
298 title_.resize(kTitleStringLength);
299 std::copy(
rom_data_.begin() + kTitleStringOffset,
300 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
302 if (
rom_data_[kTitleStringOffset + 0x19] == 0) {
315 size_ = kBaseRomSize * 2;
317 return absl::OkStatus();
322 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
325 for (
int j = 0; j < 8; j++) {
331 for (
int j = 0; j < 4; j++) {
337 for (
int j = 0; j < 4; j++) {
344 for (
int j = 0; j < 4; j++) {
350 return absl::OkStatus();
355 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
358 for (
int j = 0; j < 8; j++) {
364 for (
int j = 0; j < 4; j++) {
370 for (
int j = 0; j < 4; j++) {
377 for (
int j = 0; j < 4; j++) {
383 return absl::OkStatus();
387 absl::Status non_firing_status;
389 return absl::InternalError(
"ROM data is empty.");
393 auto backup = settings.
backup;
404 auto now = std::chrono::system_clock::now();
405 auto now_c = std::chrono::system_clock::to_time_t(now);
406 std::string backup_filename =
407 absl::StrCat(
filename,
"_backup_", std::ctime(&now_c));
410 backup_filename.erase(
411 std::remove(backup_filename.begin(), backup_filename.end(),
'\n'),
412 backup_filename.end());
415 std::replace(backup_filename.begin(), backup_filename.end(),
' ',
'_');
419 std::filesystem::copy(
filename_, backup_filename,
420 std::filesystem::copy_options::overwrite_existing);
421 }
catch (
const std::filesystem::filesystem_error &e) {
422 non_firing_status = absl::InternalError(absl::StrCat(
423 "Could not create backup file: ", backup_filename,
" - ", e.what()));
438 auto now = std::chrono::system_clock::now();
439 auto now_c = std::chrono::system_clock::to_time_t(now);
441 std::cout << filename_no_ext << std::endl;
442 filename = absl::StrCat(filename_no_ext,
"_", std::ctime(&now_c));
455 std::ofstream file(
filename.data(), std::ios::binary | std::ios::app);
458 file.open(
filename.data(), std::ios::binary);
460 return absl::InternalError(
461 absl::StrCat(
"Could not open or create ROM file: ",
filename));
468 static_cast<const char *
>(
static_cast<const void *
>(
rom_data_.data())),
470 }
catch (
const std::ofstream::failure &e) {
471 return absl::InternalError(absl::StrCat(
472 "Error while writing to ROM file: ",
filename,
" - ", e.what()));
477 return absl::InternalError(
478 absl::StrCat(
"Error while writing to ROM file: ",
filename));
481 if (!non_firing_status.ok()) {
482 return non_firing_status;
485 return absl::OkStatus();
490 for (
size_t j = 0; j < palette.
size(); ++j) {
499 return absl::OkStatus();
505 for (size_t i = 0; i < group.size(); ++i) {
507 SavePalette(i, group.name(), *group.mutable_palette(i)));
509 return absl::OkStatus();
512 return absl::OkStatus();
516 if (offset >=
static_cast<int>(
rom_data_.size())) {
517 return absl::FailedPreconditionError(
"Offset out of range");
523 if (offset + 1 >=
static_cast<int>(
rom_data_.size())) {
524 return absl::FailedPreconditionError(
"Offset out of range");
531 if (offset + 2 >=
static_cast<int>(
rom_data_.size())) {
532 return absl::OutOfRangeError(
"Offset out of range");
540 uint32_t offset, uint32_t length)
const {
541 if (offset + length >
static_cast<uint32_t
>(
rom_data_.size())) {
542 return absl::OutOfRangeError(
"Offset and length out of range");
544 std::vector<uint8_t> result;
545 for (uint32_t i = offset; i < offset + length; i++) {
579 return absl::OkStatus();
583 if (addr >=
static_cast<int>(
rom_data_.size())) {
584 return absl::OutOfRangeError(absl::StrFormat(
585 "Attempt to write byte %#02x value failed, address %d out of range",
590 return absl::OkStatus();
594 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
595 return absl::OutOfRangeError(absl::StrFormat(
596 "Attempt to write word %#04x value failed, address %d out of range",
599 rom_data_[addr] = (uint8_t)(value & 0xFF);
600 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
602 return absl::OkStatus();
606 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
607 return absl::OutOfRangeError(absl::StrFormat(
608 "Attempt to write short %#04x value failed, address %d out of range",
611 rom_data_[addr] = (uint8_t)(value & 0xFF);
612 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
614 return absl::OkStatus();
618 if (addr + 2 >=
static_cast<uint32_t
>(
rom_data_.size())) {
619 return absl::OutOfRangeError(absl::StrFormat(
620 "Attempt to write long %#06x value failed, address %d out of range",
623 rom_data_[addr] = (uint8_t)(value & 0xFF);
624 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
625 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
627 return absl::OkStatus();
631 if (addr +
static_cast<int>(
data.size()) >
633 return absl::InvalidArgumentError(absl::StrFormat(
634 "Attempt to write vector value failed, address %d out of range", addr));
636 for (
int i = 0; i < static_cast<int>(
data.size()); i++) {
640 return absl::OkStatus();
644 uint16_t bgr = ((color.
snes() >> 10) & 0x1F) | ((color.
snes() & 0x1F) << 10) |
645 (color.
snes() & 0x7C00);
The Rom class is used to load, save, and modify Rom data.
absl::StatusOr< std::vector< uint8_t > > ReadByteVector(uint32_t offset, uint32_t length) const
absl::Status LoadFromFile(const std::string &filename, bool z3_load=true)
auto mutable_graphics_buffer()
std::vector< uint8_t > rom_data_
gfx::PaletteGroupMap palette_groups_
auto palette_group() const
absl::StatusOr< gfx::Tile16 > ReadTile16(uint32_t tile16_id)
absl::Status WriteColor(uint32_t address, const gfx::SnesColor &color)
absl::Status LoadFromData(const std::vector< uint8_t > &data, bool z3_load=true)
absl::Status WriteByte(int addr, uint8_t value)
absl::Status LoadZelda3()
std::array< std::array< uint8_t, 4 >, kNumSpritesets > spriteset_ids
std::array< std::array< uint8_t, 4 >, kNumPalettesets > paletteset_ids
absl::StatusOr< uint16_t > ReadWord(int offset)
absl::Status WriteVector(int addr, std::vector< uint8_t > data)
absl::Status WriteTile16(int tile16_id, const gfx::Tile16 &tile)
absl::Status SaveToFile(const SaveSettings &settings)
std::array< std::array< uint8_t, 4 >, kNumRoomBlocksets > room_blockset_ids
absl::StatusOr< uint32_t > ReadLong(int offset)
zelda3_version_pointers version_constants() const
absl::Status SaveGfxGroups()
ResourceLabelManager resource_label_manager_
absl::Status LoadGfxGroups()
absl::Status SavePalette(int index, const std::string &group_name, gfx::SnesPalette &palette)
absl::StatusOr< uint8_t > ReadByte(int offset)
absl::Status WriteShort(int addr, uint16_t value)
absl::Status WriteWord(int addr, uint16_t value)
absl::Status WriteLong(uint32_t addr, uint32_t value)
std::array< std::array< uint8_t, 8 >, kNumMainBlocksets > main_blockset_ids
absl::Status SaveAllPalettes()
static std::shared_ptr< Rom > shared_rom_
The Renderer class represents the renderer for the Yaze application.
void RenderBitmap(gfx::Bitmap *bitmap)
Represents a bitmap image.
void Create(int width, int height, int depth, std::span< uint8_t > data)
Create a bitmap with the given dimensions and data.
constexpr void set_modified(bool m)
constexpr bool is_modified() const
constexpr uint16_t snes() const
Represents a palette of colors for the Super Nintendo Entertainment System (SNES).
Tile composition of four 8x8 tiles.
#define RETURN_IF_ERROR(expression)
#define ASSIGN_OR_RETURN(type_variable_name, expression)
absl::StatusOr< std::vector< uint8_t > > DecompressV2(const uint8_t *data, int offset, int size, int mode)
Decompresses a buffer of data using the LC_LZ2 algorithm.
constexpr int kTilesheetHeight
constexpr int kTilesheetWidth
uint16_t TileInfoToWord(TileInfo tile_info)
constexpr int kTilesheetDepth
std::vector< uint8_t > Bpp8SnesToIndexed(std::vector< uint8_t > data, uint64_t bpp)
std::vector< uint8_t > SnesTo8bppSheet(std::span< uint8_t > sheet, int bpp, int num_sheets)
uint32_t GetPaletteAddress(const std::string &group_name, size_t palette_index, size_t color_index)
TileInfo WordToTileInfo(uint16_t word)
std::vector< uint8_t > HyruleMagicCompress(uint8_t const *const src, int const oldsize, int *const size, int const flag)
absl::Status LoadAllPalettes(const std::vector< uint8_t > &rom_data, PaletteGroupMap &groups)
Loads all the palettes for the game.
std::string HexWord(uint16_t word, HexStringParams params)
std::string HexByte(uint8_t byte, HexStringParams params)
std::string HexLong(uint32_t dword, HexStringParams params)
Main namespace for the application.
constexpr uint32_t kGfxGroupsPointer
absl::StatusOr< std::vector< uint8_t > > Load2BppGraphics(const Rom &rom)
Loads 2bpp graphics from Rom data.
absl::StatusOr< std::array< gfx::Bitmap, kNumLinkSheets > > LoadLinkGraphics(const Rom &rom)
Loads the players 4bpp graphics sheet from Rom data.
constexpr int Uncompressed3BPPSize
constexpr uint32_t kNumLinkSheets
constexpr uint32_t kEntranceGfxGroup
constexpr uint32_t kNumSpritesets
int AddressFromBytes(uint8_t bank, uint8_t high, uint8_t low) noexcept
constexpr uint32_t kTile16Ptr
constexpr uint32_t kNumMainBlocksets
constexpr uint32_t kNumRoomBlocksets
absl::StatusOr< gfx::Bitmap > LoadFontGraphics(const Rom &rom)
constexpr uint32_t kNumGfxSheets
absl::StatusOr< std::array< gfx::Bitmap, kNumGfxSheets > > LoadAllGraphicsData(Rom &rom, bool defer_render)
This function iterates over all graphics sheets in the Rom and loads them into memory....
uint32_t GetGraphicsAddress(const uint8_t *data, uint8_t addr, uint32_t ptr1, uint32_t ptr2, uint32_t ptr3)
constexpr uint32_t kNumPalettesets
absl::Status SaveAllGraphicsData(Rom &rom, std::array< gfx::Bitmap, kNumGfxSheets > &gfx_sheets)
uint32_t SnesToPc(uint32_t addr) noexcept
Represents a group of palettes.
uint32_t kOverworldGfxPtr1
uint32_t kOverworldGfxPtr2
uint32_t kSpriteBlocksetPointer
uint32_t kOverworldGfxPtr3
uint32_t kDungeonPalettesGroups