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);
227 return absl::OkStatus();
232 return absl::InvalidArgumentError(
233 "Could not load ROM: parameter `filename` is empty.");
238 std::ifstream file(
filename_, std::ios::binary);
239 if (!file.is_open()) {
240 return absl::NotFoundError(
241 absl::StrCat(
"Could not open ROM file: ",
filename_));
247 }
catch (
const std::filesystem::filesystem_error &e) {
249 file.seekg(0, std::ios::end);
251 return absl::InternalError(absl::StrCat(
252 "Could not get file size: ",
filename_,
" - ", e.what()));
254 size_ = file.tellg();
267 return absl::OkStatus();
272 return absl::InvalidArgumentError(
273 "Could not load ROM: parameter `data` is empty.");
280 return absl::OkStatus();
285 constexpr size_t kBaseRomSize = 1048576;
286 constexpr size_t kHeaderSize = 0x200;
287 if (
size_ % kBaseRomSize == kHeaderSize) {
288 auto header = std::vector<uint8_t>(
rom_data_.begin(),
295 constexpr uint32_t kTitleStringOffset = 0x7FC0;
296 constexpr uint32_t kTitleStringLength = 20;
297 title_.resize(kTitleStringLength);
298 std::copy(
rom_data_.begin() + kTitleStringOffset,
299 rom_data_.begin() + kTitleStringOffset + kTitleStringLength,
301 if (
rom_data_[kTitleStringOffset + 0x19] == 0) {
314 size_ = kBaseRomSize * 2;
316 return absl::OkStatus();
321 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
324 for (
int j = 0; j < 8; j++) {
330 for (
int j = 0; j < 4; j++) {
336 for (
int j = 0; j < 4; j++) {
343 for (
int j = 0; j < 4; j++) {
349 return absl::OkStatus();
354 main_blockset_ptr =
SnesToPc(main_blockset_ptr);
357 for (
int j = 0; j < 8; j++) {
363 for (
int j = 0; j < 4; j++) {
369 for (
int j = 0; j < 4; j++) {
376 for (
int j = 0; j < 4; j++) {
382 return absl::OkStatus();
386 absl::Status non_firing_status;
388 return absl::InternalError(
"ROM data is empty.");
392 auto backup = settings.
backup;
403 auto now = std::chrono::system_clock::now();
404 auto now_c = std::chrono::system_clock::to_time_t(now);
405 std::string backup_filename =
406 absl::StrCat(
filename,
"_backup_", std::ctime(&now_c));
409 backup_filename.erase(
410 std::remove(backup_filename.begin(), backup_filename.end(),
'\n'),
411 backup_filename.end());
414 std::replace(backup_filename.begin(), backup_filename.end(),
' ',
'_');
418 std::filesystem::copy(
filename_, backup_filename,
419 std::filesystem::copy_options::overwrite_existing);
420 }
catch (
const std::filesystem::filesystem_error &e) {
421 non_firing_status = absl::InternalError(absl::StrCat(
422 "Could not create backup file: ", backup_filename,
" - ", e.what()));
437 auto now = std::chrono::system_clock::now();
438 auto now_c = std::chrono::system_clock::to_time_t(now);
440 std::cout << filename_no_ext << std::endl;
441 filename = absl::StrCat(filename_no_ext,
"_", std::ctime(&now_c));
454 std::ofstream file(
filename.data(), std::ios::binary | std::ios::trunc);
456 return absl::InternalError(
457 absl::StrCat(
"Could not open ROM file for writing: ",
filename));
463 static_cast<const char *
>(
static_cast<const void *
>(
rom_data_.data())),
465 }
catch (
const std::ofstream::failure &e) {
466 return absl::InternalError(absl::StrCat(
467 "Error while writing to ROM file: ",
filename,
" - ", e.what()));
472 return absl::InternalError(
473 absl::StrCat(
"Error while writing to ROM file: ",
filename));
476 if (non_firing_status.ok())
dirty_ =
false;
477 return non_firing_status.ok() ? absl::OkStatus() : non_firing_status;
482 for (
size_t j = 0; j < palette.
size(); ++j) {
491 return absl::OkStatus();
497 for (size_t i = 0; i < group.size(); ++i) {
499 SavePalette(i, group.name(), *group.mutable_palette(i)));
501 return absl::OkStatus();
504 return absl::OkStatus();
508 if (offset >=
static_cast<int>(
rom_data_.size())) {
509 return absl::FailedPreconditionError(
"Offset out of range");
515 if (offset + 1 >=
static_cast<int>(
rom_data_.size())) {
516 return absl::FailedPreconditionError(
"Offset out of range");
523 if (offset + 2 >=
static_cast<int>(
rom_data_.size())) {
524 return absl::OutOfRangeError(
"Offset out of range");
532 uint32_t offset, uint32_t length)
const {
533 if (offset + length >
static_cast<uint32_t
>(
rom_data_.size())) {
534 return absl::OutOfRangeError(
"Offset and length out of range");
536 std::vector<uint8_t> result;
537 for (uint32_t i = offset; i < offset + length; i++) {
571 return absl::OkStatus();
575 if (addr >=
static_cast<int>(
rom_data_.size())) {
576 return absl::OutOfRangeError(absl::StrFormat(
577 "Attempt to write byte %#02x value failed, address %d out of range",
583 return absl::OkStatus();
587 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
588 return absl::OutOfRangeError(absl::StrFormat(
589 "Attempt to write word %#04x value failed, address %d out of range",
592 rom_data_[addr] = (uint8_t)(value & 0xFF);
593 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
596 return absl::OkStatus();
600 if (addr + 1 >=
static_cast<int>(
rom_data_.size())) {
601 return absl::OutOfRangeError(absl::StrFormat(
602 "Attempt to write short %#04x value failed, address %d out of range",
605 rom_data_[addr] = (uint8_t)(value & 0xFF);
606 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
609 return absl::OkStatus();
613 if (addr + 2 >=
static_cast<uint32_t
>(
rom_data_.size())) {
614 return absl::OutOfRangeError(absl::StrFormat(
615 "Attempt to write long %#06x value failed, address %d out of range",
618 rom_data_[addr] = (uint8_t)(value & 0xFF);
619 rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF);
620 rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF);
623 return absl::OkStatus();
627 if (addr +
static_cast<int>(
data.size()) >
629 return absl::InvalidArgumentError(absl::StrFormat(
630 "Attempt to write vector value failed, address %d out of range", addr));
632 for (
int i = 0; i < static_cast<int>(
data.size()); i++) {
637 return absl::OkStatus();
641 uint16_t bgr = ((color.
snes() >> 10) & 0x1F) | ((color.
snes() & 0x1F) << 10) |
642 (color.
snes() & 0x7C00);
647 if (st.ok())
dirty_ =
true;
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()
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