VoxelEngine
 
Loading...
Searching...
No Matches
DenseGrid.h
1#pragma once
2
3#include "../../hash.h"
4#include "IChunkData.h"
5
6
7#include <glm/glm.hpp>
8
9#include <unordered_map>
10#include <vector>
11
12
13#include "../../block/BlockState.h"
14#include "../../block/MultiBlock.h"
15
16
17#define MAKE_VEC_COMP(ret, name, op) \
18 ret name(int x, int y, int z) const { \
19 return op; \
20 } \
21 ret name(const glm::ivec3& pos) const { \
22 return name(pos.x, pos.y, pos.z); \
23 }
24
25
26namespace engine {
28 struct DenseGrid : public IChunkData {
29 BlockID uniform = InvalidBlockID;
30 std::vector<BlockID> data;
31 std::unordered_map<glm::ivec3, BlockState> states;
32 std::unordered_map<glm::ivec3, MultiBlock> multiBlocks;
33
34 DenseGrid(glm::ivec3 dims)
35 : IChunkData(dims),
36 data(dims.x * dims.y * dims.z, 0),
37 states() {}
38
39 void compress(BlockID block) {
40 uniform = block;
41 data.resize(0);
42 multiBlocks.clear();
43 // Keep states as is
44 }
45
46 void decompress() {
47 data.resize(dims.x * dims.y * dims.z, uniform);
48 uniform = InvalidBlockID;
49 }
50
51 // 3D to 1D index mapping (Z-Y-X order for cache locality)
52 size_t index(int x, int y, int z) const { return x + dims.x * (y + dims.y * z); }
53 size_t index(const glm::ivec3& pos) const { return index(pos.x, pos.y, pos.z); }
54
55
57 BlockID getBlock(const glm::ivec3& pos) const override final;
58 MultiBlock* getMultiBlock(const glm::ivec3& pos) override final;
59
60 void setBlock(const glm::ivec3& pos, BlockID block) override final;
61 void setBlock(const glm::ivec3& pos, BlockID block, BlockState state) override final;
62 void setMultiBlock(const glm::ivec3& pos, MultiBlock&& multiBlock) override final;
63
64
66 BlockState* getState(const glm::ivec3& pos) override final;
67 const BlockState* getState(const glm::ivec3& pos) const override final;
68
69 void setState(const glm::ivec3& pos, BlockState&& state) override final;
70 void clearState(const glm::ivec3& pos) override final;
71 BlockState* getOrCreateState(const glm::ivec3& pos) override final;
72
73 void serialize(std::ostream& out) const override final;
74 void deserialize(std::istream& in) override final;
75 };
76
77} // namespace engine
Definition BlockState.h:17
Interface for chunk data representation.
Definition IChunkData.h:17
void clear(const glm::ivec3 &pos)
Definition IChunkData.h:46
A MultiBlock represents multiple blocks at the same position.
Definition MultiBlock.h:19
DenseGrid is a storage of blocks in a dense grid aka array..
Definition DenseGrid.h:28
BlockID getBlock(const glm::ivec3 &pos) const override final
Definition DenseGrid.cpp:7
BlockState * getState(const glm::ivec3 &pos) override final
Definition DenseGrid.cpp:40