From: Daniel Karbach Date: Mon, 9 Mar 2015 20:54:59 +0000 (+0100) Subject: store block type as ID rather than pointer X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=7caa2326d25d4fc5ba98318dfccb508bb3e16820;p=blank.git store block type as ID rather than pointer --- diff --git a/src/app.cpp b/src/app.cpp index 10a72b8..9580d33 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -118,7 +118,7 @@ void Application::Update(int dt) { glm::vec3 pos = Chunk::ToCoords(blkid); outline_visible = true; outline.Clear(); - chunk->BlockAt(blkid).type->FillOutlineModel(outline); + chunk->Type(chunk->BlockAt(blkid)).FillOutlineModel(outline); outline_transform = glm::translate(chunk->Transform(world.Player().ChunkCoords()), pos); outline_transform = glm::scale(outline_transform, glm::vec3(1.0001f)); } else { @@ -127,14 +127,14 @@ void Application::Update(int dt) { if (pick) { if (chunk) { - place_id = chunk->BlockAt(blkid).type->id; + place_id = chunk->BlockAt(blkid).type; hud.Display(*world.BlockTypes()[place_id]); } pick = false; } if (remove) { if (chunk) { - chunk->BlockAt(blkid).type = world.BlockTypes()[remove_id]; + chunk->BlockAt(blkid).type = remove_id; chunk->Invalidate(); } remove = false; @@ -147,7 +147,7 @@ void Application::Update(int dt) { mod_chunk = &world.Next(*chunk, normal); next_pos -= normal * glm::vec3(Chunk::Extent()); } - mod_chunk->BlockAt(next_pos).type = world.BlockTypes()[place_id]; + mod_chunk->BlockAt(next_pos).type = place_id; mod_chunk->Invalidate(); } place = false; diff --git a/src/block.cpp b/src/block.cpp index 02e8476..e22d3e6 100644 --- a/src/block.cpp +++ b/src/block.cpp @@ -3,7 +3,6 @@ namespace blank { -const BlockType BlockType::DEFAULT; const NullShape BlockType::DEFAULT_SHAPE; void BlockType::FillVBO( @@ -27,7 +26,7 @@ void BlockType::FillOutlineVBO( BlockTypeRegistry::BlockTypeRegistry() { - Add(BlockType::DEFAULT); + Add(BlockType()); } int BlockTypeRegistry::Add(const BlockType &t) { diff --git a/src/block.hpp b/src/block.hpp index a757740..0f567c4 100644 --- a/src/block.hpp +++ b/src/block.hpp @@ -29,7 +29,6 @@ struct BlockType { const glm::vec3 &outline_color = { -1, -1, -1 }) : id(-1), visible(v), shape(shape), color(color), outline_color(outline_color) { } - static const BlockType DEFAULT; static const NullShape DEFAULT_SHAPE; @@ -83,10 +82,10 @@ struct Block { using Pos = glm::vec3; - const BlockType *type; + int type; - constexpr explicit Block(const BlockType *t = &BlockType::DEFAULT) - : type(t) { } + constexpr explicit Block(int type = 0) + : type(type) { } }; diff --git a/src/chunk.cpp b/src/chunk.cpp index c30e2cd..e52a19f 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -6,8 +6,9 @@ namespace blank { -Chunk::Chunk() -: blocks() +Chunk::Chunk(const BlockTypeRegistry &types) +: types(&types) +, blocks() , model() , position(0, 0, 0) , dirty(false) { @@ -15,13 +16,15 @@ Chunk::Chunk() } Chunk::Chunk(Chunk &&other) -: blocks(std::move(other.blocks)) +: types(other.types) +, blocks(std::move(other.blocks)) , model(std::move(other.model)) , dirty(other.dirty) { } Chunk &Chunk::operator =(Chunk &&other) { + types = other.types; blocks = std::move(other.blocks); model = std::move(other.model); dirty = other.dirty; @@ -66,13 +69,13 @@ bool Chunk::Intersection( for (int z = 0; z < Depth(); ++z) { for (int y = 0; y < Height(); ++y) { for (int x = 0; x < Width(); ++x, ++id) { - if (!blocks[id].type->visible) { + if (!Type(blocks[id]).visible) { continue; } float cur_dist; glm::vec3 cur_norm; Block::Pos pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f); - if (blocks[id].type->shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) { + if (Type(blocks[id]).shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) { if (cur_dist < closest_dist) { closest_id = id; closest_dist = cur_dist; @@ -111,7 +114,7 @@ glm::mat4 Chunk::Transform(const Pos &offset) const { int Chunk::VertexCount() const { int count = 0; for (const auto &block : blocks) { - count += block.type->shape->VertexCount(); + count += Type(block).shape->VertexCount(); } return count; } @@ -121,7 +124,7 @@ void Chunk::Update() { model.Reserve(VertexCount()); for (size_t i = 0; i < Size(); ++i) { - blocks[i].type->FillModel(ToCoords(i), model); + Type(blocks[i]).FillModel(ToCoords(i), model); } model.Invalidate(); diff --git a/src/chunk.hpp b/src/chunk.hpp index 3c30180..28ba4e3 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -18,7 +18,7 @@ public: using Pos = glm::tvec3; public: - Chunk(); + explicit Chunk(const BlockTypeRegistry &); Chunk(Chunk &&); Chunk &operator =(Chunk &&); @@ -59,6 +59,8 @@ public: Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); } const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); } + const BlockType &Type(const Block &b) const { return *types->Get(b.type); } + bool Intersection( const Ray &, const glm::mat4 &M, @@ -77,6 +79,7 @@ private: void Update(); private: + const BlockTypeRegistry *types; std::vector blocks; Model model; Pos position; diff --git a/src/world.cpp b/src/world.cpp index b67bef3..e041115 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -57,11 +57,11 @@ void World::Generate(const Chunk::Pos &from, const Chunk::Pos &to) { if (ChunkAvailable(pos)) { continue; } else if (x == 0 && y == 0 && z == 0) { - loaded.emplace_back(); + loaded.emplace_back(blockType); loaded.back().Position(pos); Generate(loaded.back()); } else { - to_generate.emplace_back(); + to_generate.emplace_back(blockType); to_generate.back().Position(pos); } } @@ -76,9 +76,9 @@ void World::Generate(Chunk &chunk) { glm::vec3 coords(pos * Chunk::Extent()); if (pos.x == 0 && pos.y == 0 && pos.z == 0) { for (size_t i = 1; i < blockType.Size(); ++i) { - chunk.BlockAt(i) = Block(blockType[i]); - chunk.BlockAt(i + 257) = Block(blockType[i]); - chunk.BlockAt(i + 514) = Block(blockType[i]); + chunk.BlockAt(i) = Block(i); + chunk.BlockAt(i + 257) = Block(i); + chunk.BlockAt(i + 514) = Block(i); } } else { for (int z = 0; z < Chunk::Depth(); ++z) { @@ -89,7 +89,7 @@ void World::Generate(Chunk &chunk) { float val = blockNoise(gen_pos); if (val > 0.8f) { int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4; - chunk.BlockAt(block_pos) = Block(blockType[col_val * 3 + 1]); + chunk.BlockAt(block_pos) = Block(col_val * 3 + 1); } } } @@ -179,7 +179,7 @@ Chunk &World::Next(const Chunk &to, const glm::tvec3 &dir) { return *chunk; } - loaded.emplace_back(); + loaded.emplace_back(blockType); loaded.back().Position(tgt_pos); Generate(loaded.back()); return loaded.back();