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
29class ShapeMask {
30 public:
31 ShapeMask() = default;
32
40 explicit ShapeMask(const std::array<uint8_t, tile::side_length_pix> &rows) : rows_{rows} {}
41
42 // (lexicographic on rows array)
43 auto operator<=>(const ShapeMask &) const = default;
44
55 [[nodiscard]] ShapeMask flip(bool h, bool v) const;
56
65 void set(std::size_t row, std::size_t col);
66
75 void unset(std::size_t row, std::size_t col);
76
86 [[nodiscard]] bool get(std::size_t row, std::size_t col) const;
87
94 [[nodiscard]] bool is_transparent() const;
95
96 [[nodiscard]] const std::array<uint8_t, 8> &rows() const
97 {
98 return rows_;
99 }
100
101 private:
102 std::array<uint8_t, tile::side_length_pix> rows_{};
103};
104
105} // namespace porytiles
106
112template <>
113struct std::hash<porytiles::ShapeMask> {
114 size_t operator()(const porytiles::ShapeMask &tm) const noexcept
115 {
116 // Simple hash combining all 8 bytes
117 size_t h = 0;
118 for (const uint8_t byte : tm.rows()) {
119 h = h * 31 + byte;
120 }
121 return h;
122 }
123};
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