]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
23c3979feba4ecd36d9867512901b81229a10fef
[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 };
46
47
48 /// attributes of a type of block
49 struct BlockType {
50
51         const Shape *shape;
52         glm::vec3 color;
53         glm::vec3 outline_color;
54
55         Block::Type id;
56
57         bool visible;
58
59         struct Faces {
60                 bool up;
61                 bool down;
62                 bool right;
63                 bool left;
64                 bool front;
65                 bool back;
66         } fill;
67
68         explicit BlockType(
69                 bool v = false,
70                 const glm::vec3 &color = { 1, 1, 1 },
71                 const Shape *shape = &DEFAULT_SHAPE
72         );
73
74         static const NullShape DEFAULT_SHAPE;
75
76
77         void FillModel(
78                 Model::Buffer &m,
79                 const glm::mat4 &transform = glm::mat4(1.0f),
80                 Model::Index idx_offset = 0
81         ) const;
82         void FillOutlineModel(
83                 OutlineModel &m,
84                 const glm::vec3 &pos_offset = { 0, 0, 0 },
85                 OutlineModel::Index idx_offset = 0
86         ) const;
87
88 };
89
90
91 class BlockTypeRegistry {
92
93 public:
94         BlockTypeRegistry();
95
96 public:
97         Block::Type Add(const BlockType &);
98
99         size_t Size() const { return types.size(); }
100
101         BlockType *operator [](Block::Type id) { return &types[id]; }
102         const BlockType *Get(Block::Type id) const { return &types[id]; }
103
104 private:
105         std::vector<BlockType> types;
106
107 };
108
109 }
110
111 #endif