Browse Source

basics for block storage

Kajetan Johannes Hammerle 3 years ago
parent
commit
e0e2145d4f

+ 9 - 4
client/rendering/renderer/WorldRenderer.cpp

@@ -2,9 +2,9 @@
 
 WorldRenderer::WorldRenderer(const World& world) : world(world), texture("resources/textures.png") {
     TypedBuffer<Triangle> buffer(100);
-    for(int x = 0; x < World::WORLD_SIZE; x++) {
-        for(int y = 0; y < World::WORLD_SIZE; y++) {
-            for(int z = 0; z < World::WORLD_SIZE; z++) {
+    for(int x = 0; x < world.getSize(); x++) {
+        for(int y = 0; y < world.getHeight(); y++) {
+            for(int z = 0; z < world.getSize(); z++) {
                 if(world.getBlock(x, y, z).getId() != 0) {
                     addCube(buffer, x, y, z);
                 }
@@ -18,7 +18,12 @@ void WorldRenderer::render(float lag, Renderer& renderer) {
     (void) lag;
     (void) renderer;
     texture.bindTo(0);
-    mesh.draw();
+    for(int x = 0; x < 2; x++) {
+        for(int z = 0; z < 2; z++) {
+            renderer.translateTo(32.0f * x, 0.0f, 32.0f * z).update();
+            mesh.draw();
+        }
+    }
 }
 
 bool WorldRenderer::isAir(int x, int y, int z) const {

+ 40 - 0
common/world/BlockMap.h

@@ -0,0 +1,40 @@
+#ifndef BLOCKMAP_H
+#define BLOCKMAP_H
+
+#include "gaming-core/utils/BitArray.h"
+#include "gaming-core/utils/Array.h"
+#include "common/block/BlockTypes.h"
+
+template<int N, int B>
+class BlockMap {
+    static constexpr int EMPTY_BLOCK_ID = 65535;
+
+    BitArray<N, B> blocks;
+    Array<BlockId, 1 << B> map;
+
+public:
+
+    BlockMap(BlockId id) : map(EMPTY_BLOCK_ID) {
+        map[0] = id;
+    }
+
+    BlockId get(int index) const {
+        return map[blocks[index]];
+    }
+
+    bool set(int index, BlockId id) {
+        for(int i = 0; i < map.getLength(); i++) {
+            if(map[i] == id) {
+                blocks[index] = i;
+                return false;
+            } else if(map[i] == 65535) {
+                map[i] = id;
+                blocks[index] = i;
+                return false;
+            }
+        }
+        return true;
+    }
+};
+
+#endif

+ 42 - 0
common/world/BlockStorage.h

@@ -0,0 +1,42 @@
+#ifndef BLOCKSTORAGE_H
+#define BLOCKSTORAGE_H
+
+#include "common/block/BlockTypes.h"
+#include "common/world/BlockMap.h"
+
+template<int S, int H>
+class BlockStorage final {
+    static constexpr int SIZE = 1 << S;
+    static constexpr int HEIGHT = 1 << H;
+    static constexpr int SIZE_MASK = SIZE - 1;
+
+    BlockMap<SIZE * SIZE * HEIGHT, 1> data;
+
+public:
+
+    BlockStorage() : data(0) {
+    }
+
+    BlockId get(int x, int y, int z) const {
+        if(y < 0 || y >= HEIGHT) {
+            return 0;
+        }
+        x &= SIZE_MASK;
+        z &= SIZE_MASK;
+        return data.get(x * SIZE * SIZE + y * SIZE + z);
+    }
+
+    void set(int x, int y, int z, BlockId id) {
+        data.set(x * SIZE * SIZE + y * SIZE + z, id);
+    }
+
+    constexpr int getSize() const {
+        return SIZE;
+    }
+
+    constexpr int getHeight() const {
+        return HEIGHT;
+    }
+};
+
+#endif

+ 15 - 7
common/world/World.cpp

@@ -5,14 +5,14 @@
 World::World(const BlockRegistry& blockRegistry) : blockRegistry(blockRegistry) {
     Block air = blockRegistry.getBlock("air");
     Block stone = blockRegistry.getBlock("stone");
-    HighMap<WORLD_SIZE, WORLD_SIZE> map;
-    for(int x = 0; x < WORLD_SIZE; x++) {
-        for(int z = 0; z < WORLD_SIZE; z++) {
-            int height = map.getHeight(x, z, WORLD_SIZE);
+    HighMap<32, 32> map;
+    for(int x = 0; x < 32; x++) {
+        for(int z = 0; z < 32; z++) {
+            int height = map.getHeight(x, z, 32);
             for(int y = 0; y < height; y++) {
                 setBlock(x, y, z, stone);
             }
-            for(int y = height; y < WORLD_SIZE; y++) {
+            for(int y = height; y < 32; y++) {
                 setBlock(x, y, z, air);
             }
         }
@@ -20,9 +20,17 @@ World::World(const BlockRegistry& blockRegistry) : blockRegistry(blockRegistry)
 }
 
 void World::setBlock(int x, int y, int z, const Block& block) {
-    blocks[x & BITMASK][y & BITMASK][z & BITMASK] = block.getId();
+    blocks.set(x, y, z, block.getId());
 }
 
 const Block& World::getBlock(int x, int y, int z) const {
-    return blockRegistry.getBlock(blocks[x & BITMASK][y & BITMASK][z & BITMASK]);
+    return blockRegistry.getBlock(blocks.get(x, y, z));
+}
+
+int World::getSize() const {
+    return blocks.getSize();
+}
+
+int World::getHeight() const {
+    return blocks.getHeight();
 }

+ 7 - 7
common/world/World.h

@@ -2,20 +2,20 @@
 #define WORLD_H
 
 #include "common/block/BlockRegistry.h"
+#include "common/world/BlockStorage.h"
 
 class World final {
+    const BlockRegistry& blockRegistry;
+    BlockStorage<5, 5> blocks;
+    
 public:
     World(const BlockRegistry& blockRegistry);
+    
     void setBlock(int x, int y, int z, const Block& block);
     const Block& getBlock(int x, int y, int z) const;
     
-    static constexpr int WORLD_SIZE = 32;
-
-private:
-    static constexpr int BITMASK = WORLD_SIZE - 1;
-
-    const BlockRegistry& blockRegistry;
-    BlockId blocks[WORLD_SIZE][WORLD_SIZE][WORLD_SIZE];
+    int getSize() const;
+    int getHeight() const;
 };
 
 #endif

+ 1 - 1
gaming-core

@@ -1 +1 @@
-Subproject commit ce80f29511be8945a77424db9d5962198c241fa8
+Subproject commit 90f503f17761ace8600f8aa85ac8121c73a1c9da

+ 1 - 1
meson.build

@@ -4,8 +4,8 @@ sourcesCommon = [
     'common/network/Packet.cpp', 
     'common/block/Block.cpp', 
     'common/block/BlockRegistry.cpp', 
-    'gaming-core/utils/Random.cpp', 
     'common/world/World.cpp', 
+    'gaming-core/utils/Random.cpp', 
     'gaming-core/math/Vector.cpp', 
     'gaming-core/utils/Clock.cpp']