]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
3ccd74f71937f523473536910d1a28d4cfaf964b
[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         Type type;
38         unsigned char orient;
39
40         constexpr explicit Block(Type type = 0, Face face = FACE_UP, Turn turn = TURN_NONE)
41         : type(type), orient(face * TURN_COUNT + turn) { }
42
43         const glm::mat4 &Transform() const;
44
45         Face GetFace() const { return Face(orient / 4); }
46         void SetFace(Face face) { orient = face * TURN_COUNT + GetTurn(); }
47         Turn GetTurn() const { return Turn(orient % 4); }
48         void SetTurn(Turn turn) { orient = GetFace() * TURN_COUNT + turn; }
49
50 };
51
52
53 /// attributes of a type of block
54 struct BlockType {
55
56         const Shape *shape;
57         glm::vec3 color;
58         glm::vec3 outline_color;
59
60         Block::Type id;
61
62         bool visible;
63
64         // FIXME: fill faces don't respect block orientation
65         struct Faces {
66                 bool up;
67                 bool down;
68                 bool right;
69                 bool left;
70                 bool front;
71                 bool back;
72         } fill;
73
74         explicit BlockType(
75                 bool v = false,
76                 const glm::vec3 &color = { 1, 1, 1 },
77                 const Shape *shape = &DEFAULT_SHAPE
78         );
79
80         static const NullShape DEFAULT_SHAPE;
81
82
83         void FillModel(
84                 Model::Buffer &m,
85                 const glm::mat4 &transform = glm::mat4(1.0f),
86                 Model::Index idx_offset = 0
87         ) const;
88         void FillOutlineModel(
89                 OutlineModel &m,
90                 const glm::vec3 &pos_offset = { 0, 0, 0 },
91                 OutlineModel::Index idx_offset = 0
92         ) const;
93
94 };
95
96
97 class BlockTypeRegistry {
98
99 public:
100         BlockTypeRegistry();
101
102 public:
103         Block::Type Add(const BlockType &);
104
105         size_t Size() const { return types.size(); }
106
107         BlockType *operator [](Block::Type id) { return &types[id]; }
108         const BlockType *Get(Block::Type id) const { return &types[id]; }
109
110 private:
111         std::vector<BlockType> types;
112
113 };
114
115 }
116
117 #endif