]> git.localhorst.tv Git - blank.git/commitdiff
store block type as ID rather than pointer
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 9 Mar 2015 20:54:59 +0000 (21:54 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Mon, 9 Mar 2015 20:54:59 +0000 (21:54 +0100)
src/app.cpp
src/block.cpp
src/block.hpp
src/chunk.cpp
src/chunk.hpp
src/world.cpp

index 10a72b8e89e698f2abf52164df4bc91a6b164c96..9580d33560024171ab9db84e957777b697f2c970 100644 (file)
@@ -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;
index 02e8476c4a2af7cf9ec407fd76040acf8c057dba..e22d3e63d8ce309f71903a00a69cb985461483dc 100644 (file)
@@ -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) {
index a757740b77000e6768c2519b6a696c4eef833a74..0f567c4b5fdf3400b5209ed64c76766a00f4fb88 100644 (file)
@@ -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) { }
 
 };
 
index c30e2cd83aa047b90646e96e769f92df3daa487b..e52a19f81d2c4898681f2213adfa4dc0e2b6fde2 100644 (file)
@@ -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();
index 3c301803495038beeb99f59db847db5ffa6d1498..28ba4e357aa6a87137b62ecfdc61b73a6205f17a 100644 (file)
@@ -18,7 +18,7 @@ public:
        using Pos = glm::tvec3<int>;
 
 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<Block> blocks;
        Model model;
        Pos position;
index b67bef31e9a71aa17c7c5bbcedf8f7aed5786cd7..e0411159ffec2e7a5f3cdc08702554d19bd55b67 100644 (file)
@@ -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<int> &dir) {
                return *chunk;
        }
 
-       loaded.emplace_back();
+       loaded.emplace_back(blockType);
        loaded.back().Position(tgt_pos);
        Generate(loaded.back());
        return loaded.back();