]> git.localhorst.tv Git - blank.git/blob - src/block.hpp
store block type as ID rather than pointer
[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 NullShape DEFAULT_SHAPE;
33
34
35         void FillVBO(
36                 const glm::vec3 &pos,
37                 std::vector<glm::vec3> &vertices,
38                 std::vector<glm::vec3> &colors,
39                 std::vector<glm::vec3> &normals
40         ) const;
41
42         void FillModel(const glm::vec3 &pos, Model &m) const {
43                 FillVBO(pos, m.vertices, m.colors, m.normals);
44                 m.Invalidate();
45         }
46
47
48         void FillOutlineVBO(
49                 std::vector<glm::vec3> &vertices,
50                 std::vector<glm::vec3> &colors
51         ) const;
52
53         void FillOutlineModel(OutlineModel &m) const {
54                 FillOutlineVBO(m.vertices, m.colors);
55                 m.Invalidate();
56         }
57
58 };
59
60
61 class BlockTypeRegistry {
62
63 public:
64         BlockTypeRegistry();
65
66 public:
67         int Add(const BlockType &);
68
69         size_t Size() const { return types.size(); }
70
71         BlockType *operator [](int id) { return &types[id]; }
72         const BlockType *Get(int id) const { return &types[id]; }
73
74 private:
75         std::vector<BlockType> types;
76
77 };
78
79
80 /// single 1x1x1 cube
81 struct Block {
82
83         using Pos = glm::vec3;
84
85         int type;
86
87         constexpr explicit Block(int type = 0)
88         : type(type) { }
89
90 };
91
92 }
93
94 #endif