]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
noexcept all the things
[blank.git] / src / block.hpp
1 #ifndef BLANK_BLOCK_HPP_
2 #define BLANK_BLOCK_HPP_
3
4 #include "geometry.hpp"
5 #include "model.hpp"
6 #include "shape.hpp"
7
8 #include <vector>
9 #include <glm/glm.hpp>
10
11
12 namespace blank {
13
14 /// single 1x1x1 cube
15 struct Block {
16
17         using Type = unsigned short;
18         using Pos = glm::vec3;
19
20         enum Face {
21                 FACE_UP,
22                 FACE_DOWN,
23                 FACE_RIGHT,
24                 FACE_LEFT,
25                 FACE_FRONT,
26                 FACE_BACK,
27                 FACE_COUNT,
28         };
29         enum Turn {
30                 TURN_NONE,
31                 TURN_LEFT,
32                 TURN_AROUND,
33                 TURN_RIGHT,
34                 TURN_COUNT,
35         };
36
37         static constexpr int ORIENT_COUNT = FACE_COUNT * TURN_COUNT;
38
39         Type type;
40         unsigned char orient;
41
42         constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE) noexcept
43         : type(type), orient(face * TURN_COUNT + turn) { }
44
45         const glm::mat4 &Transform() const noexcept { return orient2transform[orient]; }
46
47         Face GetFace() const noexcept { return Face(orient / TURN_COUNT); }
48         void SetFace(Face face) noexcept { orient = face * TURN_COUNT + GetTurn(); }
49         Turn GetTurn() const noexcept { return Turn(orient % TURN_COUNT); }
50         void SetTurn(Turn turn) noexcept { orient = GetFace() * TURN_COUNT + turn; }
51
52         Face OrientedFace(Face f) const noexcept { return orient2face[orient][f]; }
53
54         static Face Opposite(Face f) noexcept {
55                 return Face(f ^ 1);
56         }
57
58         static glm::tvec3<int> FaceNormal(Face face) noexcept {
59                 return face2normal[face];
60         }
61
62         static Face NormalFace(const glm::vec3 &norm) noexcept {
63                 const glm::vec3 anorm(abs(norm));
64                 if (anorm.x > anorm.y) {
65                         if (anorm.x > anorm.z) {
66                                 return norm.x > 0.0f ? FACE_RIGHT : FACE_LEFT;
67                         } else {
68                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
69                         }
70                 } else {
71                         if (anorm.y > anorm.z) {
72                                 return norm.y > 0.0f ? FACE_UP : FACE_DOWN;
73                         } else {
74                                 return norm.z > 0.0f ? FACE_FRONT : FACE_BACK;
75                         }
76                 }
77         }
78
79         struct FaceSet {
80
81                 explicit FaceSet(unsigned char v = 0)
82                 : value(v) { }
83
84                 bool IsSet(Face f) const {
85                         return value & Mask(f);
86                 }
87                 void Set(Face f) {
88                         value |= Mask(f);
89                 }
90                 void Unset(Face f) {
91                         value |= ~Mask(f);
92                 }
93
94                 void Clear() {
95                         value = 0;
96                 }
97                 void Fill() {
98                         value = Mask(FACE_COUNT) - 1;
99                 }
100
101                 bool Empty() const {
102                         return value == 0;
103                 }
104                 bool All() const {
105                         return value == Mask(FACE_COUNT) - 1;
106                 }
107
108                 unsigned char Mask(Face f) const {
109                         return 1 << f;
110                 }
111
112                 unsigned char value;
113
114         };
115
116 private:
117         static const glm::tvec3<int> face2normal[6];
118         static const glm::mat4 orient2transform[ORIENT_COUNT];
119         static const Face orient2face[ORIENT_COUNT][FACE_COUNT];
120
121 };
122
123
124 /// attributes of a type of block
125 struct BlockType {
126
127         const Shape *shape;
128         glm::vec3 color;
129         glm::vec3 outline_color;
130
131         Block::Type id;
132
133         int luminosity;
134
135         bool visible;
136         bool block_light;
137
138         struct Faces {
139                 bool face[Block::FACE_COUNT];
140                 Faces &operator =(const Faces &other) noexcept {
141                         for (int i = 0; i < Block::FACE_COUNT; ++i) {
142                                 face[i] = other.face[i];
143                         }
144                         return *this;
145                 }
146                 bool operator [](Block::Face f) const noexcept {
147                         return face[f];
148                 }
149         } fill;
150
151         explicit BlockType(
152                 bool v = false,
153                 const glm::vec3 &color = { 1, 1, 1 },
154                 const Shape *shape = &DEFAULT_SHAPE
155         ) noexcept;
156
157         static const NullShape DEFAULT_SHAPE;
158
159         bool FaceFilled(const Block &block, Block::Face face) const noexcept {
160                 return fill[block.OrientedFace(face)];
161         }
162
163         void FillModel(
164                 Model::Buffer &m,
165                 const glm::mat4 &transform = glm::mat4(1.0f),
166                 Model::Index idx_offset = 0
167         ) const noexcept;
168         void FillBlockModel(
169                 BlockModel::Buffer &m,
170                 const glm::mat4 &transform = glm::mat4(1.0f),
171                 BlockModel::Index idx_offset = 0
172         ) const noexcept;
173         void FillOutlineModel(
174                 OutlineModel &m,
175                 const glm::vec3 &pos_offset = { 0, 0, 0 },
176                 OutlineModel::Index idx_offset = 0
177         ) const noexcept;
178
179 };
180
181
182 class BlockTypeRegistry {
183
184 public:
185         BlockTypeRegistry();
186
187 public:
188         Block::Type Add(const BlockType &);
189
190         size_t Size() const noexcept { return types.size(); }
191
192         BlockType &operator [](Block::Type id) { return types[id]; }
193         const BlockType &Get(Block::Type id) const { return types[id]; }
194
195 private:
196         std::vector<BlockType> types;
197
198 };
199
200 }
201
202 #endif