11 std::ifstream metatiles_bin{path, std::ios::binary};
16 const std::vector<unsigned char> data_buf{std::istreambuf_iterator(metatiles_bin), {}};
18 if (data_buf.size() % 2 != 0) {
19 return FormattableError{
"Size of metatiles.bin is not a multiple of 2 bytes, possibly corrupted."};
22 std::vector<TilemapEntry> entries;
23 entries.reserve(data_buf.size() / 2);
25 for (std::size_t byte_index = 0; byte_index < data_buf.size(); byte_index += 2) {
27 const std::uint16_t lower_byte = data_buf.at(byte_index);
28 const std::uint16_t upper_byte = data_buf.at(byte_index + 1);
29 const std::uint16_t entry_bits = (upper_byte << 8) | lower_byte;
48 entry.h_flip((entry_bits >> 10) & 0x0001);
49 entry.v_flip((entry_bits >> 11) & 0x0001);
50 entry.pal_index((entry_bits >> 12) & 0x000F);
52 entries.push_back(std::move(entry));
60 std::ifstream metatile_attr_bin{path, std::ios::binary};
61 if (!metatile_attr_bin) {
66 const std::vector<unsigned char> data_buf{std::istreambuf_iterator(metatile_attr_bin), {}};
70 "Size of metatile_attributes.bin is not a multiple of {} bytes, possibly corrupted.",
74 std::vector<MetatileAttribute> attributes;
76 attributes.reserve(metatile_count);
78 for (std::size_t metatile_index = 0; metatile_index < metatile_count; metatile_index++) {
81 std::uint16_t attribute = (byte1 << 8) | byte0;
84 if (!layer_type_result.has_value()) {
89 MetatileAttribute metatile_attribute{layer_type_result.value(),
static_cast<std::uint16_t
>(attribute & 0x00FF)};
90 attributes.push_back(std::move(metatile_attribute));
98 std::ifstream metatile_attr_bin{path, std::ios::binary};
99 if (!metatile_attr_bin) {
104 const std::vector<unsigned char> data_buf{std::istreambuf_iterator(metatile_attr_bin), {}};
108 "Size of metatile_attributes.bin is not a multiple of {} bytes, possibly corrupted.",
112 std::vector<MetatileAttribute> attributes;
114 attributes.reserve(metatile_count);
116 for (std::size_t metatile_index = 0; metatile_index < metatile_count; metatile_index++) {
118 const std::uint32_t byte0 = data_buf.at(base);
119 const std::uint32_t byte1 = data_buf.at(base + 1);
120 const std::uint32_t byte2 = data_buf.at(base + 2);
121 const std::uint32_t byte3 = data_buf.at(base + 3);
122 const std::uint32_t attribute = (byte3 << 24) | (byte2 << 16) | (byte1 << 8) | byte0;
133 const auto behavior =
static_cast<std::uint16_t
>(attribute & 0x000001FF);
134 const auto terrain =
static_cast<std::uint8_t
>((attribute >> 9) & 0x1F);
135 const auto attribute_2 =
static_cast<std::uint8_t
>((attribute >> 14) & 0x0F);
136 const auto attribute_3 =
static_cast<std::uint8_t
>((attribute >> 18) & 0x3F);
137 const auto encounter_type =
static_cast<std::uint8_t
>((attribute >> 24) & 0x07);
138 const auto attribute_5 =
static_cast<std::uint8_t
>((attribute >> 27) & 0x03);
139 const auto layer_type_raw =
static_cast<unsigned int>((attribute >> 29) & 0x03);
140 const bool attribute_7 = (attribute >> 31) & 0x01;
143 if (!layer_type_result.has_value()) {
150 layer_type_result.value(),
158 attributes.push_back(std::move(metatile_attribute));
164ChainableResult<std::unique_ptr<Image<IndexPixel>>>
168 if (!image_result.has_value()) {
173 return std::move(image_result.value());
176ChainableResult<Palette<Rgba32, pal::max_size>>
179 auto pal_result = loader.
load(path);
180 if (!pal_result.has_value()) {
185 return pal_result.value();
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A service interface that loads a fixed-length Palette from a given file.
virtual ChainableResult< Palette< Rgba32, pal::max_size > > load(const std::filesystem::path &path) const =0
An image loader that reads PNG files to create an Image with an index pixel type.
ChainableResult< std::unique_ptr< Image< IndexPixel > > > load_from_file(const std::filesystem::path &path) const
static const Style bold
Bold text formatting.
Represents a tilemap entry referencing a tile with palette and flip attributes.
std::size_t tile_index() const
constexpr std::size_t bytes_per_attr_emerald
constexpr std::size_t bytes_per_attr_firered
ChainableResult< std::vector< MetatileAttribute > > parse_firered_metatile_attributes(const std::filesystem::path &path)
Parses a metatile_attributes.bin file for FireRed format.
ChainableResult< LayerType > layer_type_from_int(unsigned int i)
Converts an integer to LayerType.
ChainableResult< Palette< Rgba32, pal::max_size > > load_porymap_palette(const std::filesystem::path &path, const FilePalLoader &loader)
Loads a Porymap palette file (e.g., 00.pal).
ChainableResult< std::vector< MetatileAttribute > > parse_emerald_metatile_attributes(const std::filesystem::path &path)
Parses a metatile_attributes.bin file for Emerald format.
ChainableResult< std::vector< TilemapEntry > > parse_metatiles_bin(const std::filesystem::path &path)
Parses a metatiles.bin file into TilemapEntry objects.
ChainableResult< std::unique_ptr< Image< IndexPixel > > > load_indexed_png(const std::filesystem::path &path, const PngIndexedImageLoader &loader)
Loads an indexed PNG file (e.g., tiles.png).