Porytiles
Loading...
Searching...
No Matches
tile.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5
7
8namespace porytiles {
9
10constexpr std::size_t kTileSideLength = 8;
11constexpr std::size_t kTileSize = kTileSideLength * kTileSideLength;
12
14template <typename P>
15class Tile {
16 std::array<P, kTileSize> pix_;
17
18 protected:
19 [[nodiscard]] const std::array<P, kTileSize> &pix() const {
20 return pix_;
21 }
22
23 public:
24 virtual ~Tile() = default;
25
26 explicit Tile() : pix_{} {}
27
28 [[nodiscard]] virtual bool IsTransparent(const P &transparency) const {
29 return std::ranges::all_of(pix(), [=](const auto &pixel) { return pixel == transparency; });
30 }
31
32 [[nodiscard]] P At(std::size_t i) const {
33 if (i >= kTileSize) {
34 Panic(fmt::format("Index {} out of bounds", i));
35 }
36 return pix_[i];
37 }
38
39 [[nodiscard]] P At(std::size_t row, std::size_t col) const {
40 if (row >= kTileSideLength) {
41 Panic(fmt::format("Row index {} out of bounds", row));
42 }
43 if (col >= kTileSideLength) {
44 Panic(fmt::format("Col index {} out of bounds", col));
45 }
46 return pix_[row * kTileSideLength + col];
47 }
48
49 void Set(std::size_t i, const P &p) {
50 if (i >= kTileSize) {
51 Panic(fmt::format("Index {} out of bounds", i));
52 }
53 pix_[i] = p;
54 }
55
56 void Set(std::size_t row, std::size_t col, const P &p) {
57 if (row >= kTileSideLength) {
58 Panic(fmt::format("Row index {} out of bounds", row));
59 }
60 if (col >= kTileSideLength) {
61 Panic(fmt::format("Col index {} out of bounds", col));
62 }
63 pix_[row * kTileSideLength + col] = p;
64 }
65};
66
67} // namespace porytiles
A single 8x8 pixel tile with an arbitrary pixel data type.
Definition tile.hpp:15
virtual ~Tile()=default
const std::array< P, kTileSize > & pix() const
Definition tile.hpp:19
void Set(std::size_t row, std::size_t col, const P &p)
Definition tile.hpp:56
P At(std::size_t i) const
Definition tile.hpp:32
virtual bool IsTransparent(const P &transparency) const
Definition tile.hpp:28
P At(std::size_t row, std::size_t col) const
Definition tile.hpp:39
void Set(std::size_t i, const P &p)
Definition tile.hpp:49
void Panic(const StringViewSourceLoc &s) noexcept
Definition panic.hpp:31
constexpr std::size_t kTileSideLength
Definition tile.hpp:10
constexpr std::size_t kTileSize
Definition tile.hpp:11