]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
split world file
[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 /// attributes of a type of block
15 struct BlockType {
16
17         int id;
18
19         bool visible;
20
21         const Shape *shape;
22         glm::vec3 color;
23         glm::vec3 outline_color;
24
25         explicit BlockType(
26                 bool v = false,
27                 const glm::vec3 &color = { 1, 1, 1 },
28                 const Shape *shape = &DEFAULT_SHAPE,
29                 const glm::vec3 &outline_color = { -1, -1, -1 })
30         : id(-1), visible(v), shape(shape), color(color), outline_color(outline_color) { }
31
32         static const BlockType DEFAULT;
33         static const NullShape DEFAULT_SHAPE;
34
35
36         void FillVBO(
37                 const glm::vec3 &pos,
38                 std::vector<glm::vec3> &vertices,
39                 std::vector<glm::vec3> &colors,
40                 std::vector<glm::vec3> &normals
41         ) const;
42
43         void FillModel(const glm::vec3 &pos, Model &m) const {
44                 FillVBO(pos, m.vertices, m.colors, m.normals);
45                 m.Invalidate();
46         }
47
48
49         void FillOutlineVBO(
50                 std::vector<glm::vec3> &vertices,
51                 std::vector<glm::vec3> &colors
52         ) const;
53
54         void FillOutlineModel(OutlineModel &m) const {
55                 FillOutlineVBO(m.vertices, m.colors);
56                 m.Invalidate();
57         }
58
59 };
60
61
62 class BlockTypeRegistry {
63
64 public:
65         BlockTypeRegistry();
66
67 public:
68         int Add(const BlockType &);
69
70         size_t Size() const { return types.size(); }
71
72         BlockType *operator [](int id) { return &types[id]; }
73         const BlockType *Get(int id) const { return &types[id]; }
74
75 private:
76         std::vector<BlockType> types;
77
78 };
79
80
81 /// single 1x1x1 cube
82 struct Block {
83
84         const BlockType *type;
85
86         constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT)
87         : type(t) { }
88
89 };
90
91 }
92
93 #endif