Porytiles
Loading...
Searching...
No Matches
porytiles_tileset_component.cpp
Go to the documentation of this file.
2
3#include <format>
4
11
12namespace porytiles {
13
14void PorytilesTilesetComponent::insert_attribute(std::size_t metatile_id, MetatileAttribute attribute)
15{
16 if (metatile_attributes_.contains(metatile_id)) {
17 panic("id " + std::to_string(metatile_id) + " already exists in attr map");
18 }
19 metatile_attributes_.insert({metatile_id, std::move(attribute)});
20}
21
22std::optional<MetatileAttribute> PorytilesTilesetComponent::get_attribute(std::size_t metatile_id) const
23{
24 if (metatile_attributes_.contains(metatile_id)) {
25 return metatile_attributes_.at(metatile_id);
26 }
27 return std::nullopt;
28}
29
31{
32 if (pal_index >= pal::num_pals) {
33 panic(std::format("invalid pal index {}: out of range", pal_index));
34 }
35 pals_[pal_index] = std::move(pal);
36}
37
38const std::optional<Palette<Rgba32, pal::max_size>> &PorytilesTilesetComponent::pal_at(std::size_t pal_index) const
39{
40 if (pal_index >= pal::num_pals) {
41 panic(std::format("invalid pal index {}: out of range", pal_index));
42 }
43 return pals_[pal_index];
44}
45
47{
48 return bottom_.size() == 0 && middle_.size() == 0 && top_.size() == 0;
49}
50
52{
53 // Pre-check: all three images must have identical dimensions
54 if (bottom_.width() != middle_.width() || bottom_.width() != top_.width() || bottom_.height() != middle_.height() ||
55 bottom_.height() != top_.height()) {
56 panic(
57 std::format(
58 "layer images have mismatched dimensions: bottom={}x{}, middle={}x{}, top={}x{}",
59 bottom_.width(),
60 bottom_.height(),
61 middle_.width(),
62 middle_.height(),
63 top_.width(),
64 top_.height()));
65 }
66
67 const std::size_t width = bottom_.width();
68 const std::size_t height = bottom_.height();
69
70 // Iterate over each 8x8 region
71 for (std::size_t region_row = 0; region_row < height; region_row += tile::side_length_pix) {
72 for (std::size_t region_col = 0; region_col < width; region_col += tile::side_length_pix) {
73 bool bottom_has_opaque = false;
74 bool middle_has_opaque = false;
75 bool top_has_opaque = false;
76
77 // Check each pixel in the current 8x8 region
78 for (std::size_t row = region_row; row < region_row + tile::side_length_pix && row < height; ++row) {
79 for (std::size_t col = region_col; col < region_col + tile::side_length_pix && col < width; ++col) {
80 if (!bottom_.at(row, col).is_transparent(extrinsic)) {
81 bottom_has_opaque = true;
82 }
83 if (!middle_.at(row, col).is_transparent(extrinsic)) {
84 middle_has_opaque = true;
85 }
86 if (!top_.at(row, col).is_transparent(extrinsic)) {
87 top_has_opaque = true;
88 }
89
90 // Early exit if all three layers have opaque pixels in this region
91 if (bottom_has_opaque && middle_has_opaque && top_has_opaque) {
92 return LayerMode::triple;
93 }
94 }
95 }
96 }
97 }
98
99 // If no region had opaque pixels on all three layers, it's dual mode
100 return LayerMode::dual;
101}
102
104{
105 const std::string &name = anim.name();
106 if (anims_.contains(name)) {
107 panic("animation '" + name + "' already exists in PorytilesTilesetComponent");
108 }
109 anims_.insert({name, std::move(anim)});
110}
111
112} // namespace porytiles
A complete tileset animation with name, configuration, and frame data.
Definition animation.hpp:78
const std::string & name() const
Definition animation.hpp:86
A result type that maintains a chainable sequence of errors for debugging and error reporting.
Represents the attributes of a single metatile.
A generic palette container for colors that support transparency checking.
Definition palette.hpp:47
void set_pal(std::size_t pal_index, Palette< Rgba32, pal::max_size > pal)
std::optional< MetatileAttribute > get_attribute(std::size_t metatile_id) const
const std::optional< Palette< Rgba32, pal::max_size > > & pal_at(std::size_t pal_index) const
ChainableResult< LayerMode > detect_layer_mode(const Rgba32 &extrinsic) const
void insert_attribute(std::size_t metatile_id, MetatileAttribute attribute)
Insert a MetatileAttribute to the end of the attribute vector.
Represents a 32-bit RGBA color.
Definition rgba32.hpp:23
constexpr std::size_t num_pals
Definition palette.hpp:21
constexpr std::size_t side_length_pix
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43