Porytiles
Loading...
Searching...
No Matches
shape_mask.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdint>
5
7
8namespace porytiles {
9
31class ShapeMask {
32 public:
33 ShapeMask() = default;
34
44 explicit ShapeMask(const std::array<uint8_t, tile::side_length_pix> &rows) : rows_{rows} {}
45
46 // (lexicographic on rows array)
47 auto operator<=>(const ShapeMask &) const = default;
48
61 [[nodiscard]] ShapeMask flip(bool h, bool v) const;
62
73 void set(std::size_t row, std::size_t col);
74
85 void unset(std::size_t row, std::size_t col);
86
98 [[nodiscard]] bool get(std::size_t row, std::size_t col) const;
99
108 [[nodiscard]] bool is_transparent() const;
109
110 [[nodiscard]] const std::array<uint8_t, 8> &rows() const
111 {
112 return rows_;
113 }
114
115 private:
116 std::array<uint8_t, tile::side_length_pix> rows_{};
117};
118
119} // namespace porytiles
120
128template <>
129struct std::hash<porytiles::ShapeMask> {
130 size_t operator()(const porytiles::ShapeMask &tm) const noexcept
131 {
132 // Simple hash combining all 8 bytes
133 size_t h = 0;
134 for (const uint8_t byte : tm.rows()) {
135 h = h * 31 + byte;
136 }
137 return h;
138 }
139};
Represents which pixels in an 8x8 tile are non-transparent.
const std::array< uint8_t, 8 > & rows() const
ShapeMask(const std::array< uint8_t, tile::side_length_pix > &rows)
Constructs a ShapeMask from an array of row data.
bool is_transparent() const
Checks if this entire ShapeMask is transparent.
ShapeMask flip(bool h, bool v) const
Returns a flipped version of this mask.
void unset(std::size_t row, std::size_t col)
Sets the bit at the specified row and column to 0.
bool get(std::size_t row, std::size_t col) const
Gets the bit value at the specified row and column.
auto operator<=>(const ShapeMask &) const =default
void set(std::size_t row, std::size_t col)
Sets the bit at the specified row and column to 1.
size_t operator()(const porytiles::ShapeMask &tm) const noexcept