Browse Source

block map and block storage merged

Kajetan Johannes Hammerle 2 years ago
parent
commit
298b583865
5 changed files with 46 additions and 55 deletions
  1. 0 32
      common/world/BlockMap.cpp
  2. 0 19
      common/world/BlockMap.h
  3. 32 1
      common/world/BlockStorage.cpp
  4. 14 2
      common/world/BlockStorage.h
  5. 0 1
      meson.build

+ 0 - 32
common/world/BlockMap.cpp

@@ -1,32 +0,0 @@
-#include "common/world/BlockMap.h"
-
-static constexpr Block::Id EMPTY_BLOCK = 65535;
-
-BlockMap::BlockMap(int length, Block::Id id) : blocks(length, 1) {
-    blocks.fill(0);
-    map.reserve(2);
-    map.add(id);
-    map.add(EMPTY_BLOCK);
-}
-
-Block::Id BlockMap::get(int index) const {
-    return map[blocks.get(index)];
-}
-
-void BlockMap::set(int index, Block::Id id) {
-    for(int i = 0; i < map.getLength(); i++) {
-        if(map[i] == id) {
-            blocks.set(index, i);
-            return;
-        } else if(map[i] == EMPTY_BLOCK) {
-            map[i] = id;
-            blocks.set(index, i);
-            return;
-        }
-    }
-    blocks.resize(blocks.getBits() + 1);
-    int mapId = map.getLength();
-    map.resize(mapId * 2, EMPTY_BLOCK);
-    map[mapId] = id;
-    blocks.set(index, mapId);
-}

+ 0 - 19
common/world/BlockMap.h

@@ -1,19 +0,0 @@
-#ifndef BLOCKMAP_H
-#define BLOCKMAP_H
-
-#include "common/Block.h"
-#include "utils/BitArray.h"
-#include "utils/List.h"
-
-class BlockMap {
-    BitArray blocks;
-    List<Block::Id> map;
-
-public:
-    BlockMap(int length, Block::Id id);
-
-    Block::Id get(int index) const;
-    void set(int index, Block::Id id);
-};
-
-#endif

+ 32 - 1
common/world/BlockStorage.cpp

@@ -1,5 +1,36 @@
 #include "common/world/BlockStorage.h"
 
+static constexpr Block::Id EMPTY_BLOCK = 65535;
+
+BlockStorage::Map::Map(int length, Block::Id id) : blocks(length, 1) {
+    blocks.fill(0);
+    map.reserve(2);
+    map.add(id);
+    map.add(EMPTY_BLOCK);
+}
+
+Block::Id BlockStorage::Map::get(int index) const {
+    return map[blocks.get(index)];
+}
+
+void BlockStorage::Map::set(int index, Block::Id id) {
+    for(int i = 0; i < map.getLength(); i++) {
+        if(map[i] == id) {
+            blocks.set(index, i);
+            return;
+        } else if(map[i] == EMPTY_BLOCK) {
+            map[i] = id;
+            blocks.set(index, i);
+            return;
+        }
+    }
+    blocks.resize(blocks.getBits() + 1);
+    int mapId = map.getLength();
+    map.resize(mapId * 2, EMPTY_BLOCK);
+    map[mapId] = id;
+    blocks.set(index, mapId);
+}
+
 BlockStorage::BlockStorage(int sizeBits, int heightBits)
     : size(1 << sizeBits), height(1 << heightBits), sizeMask(size - 1) {
     maps.resize((size * size * height) / SEGMENT);
@@ -35,7 +66,7 @@ void BlockStorage::set(int x, int y, int z, Block::Id id) {
         if(id == 0) {
             return;
         }
-        maps[segmentIndex] = new BlockMap(SEGMENT, 0);
+        maps[segmentIndex] = new Map(SEGMENT, 0);
     }
     maps[segmentIndex]->set(index & SEGMENT_MASK, id);
 }

+ 14 - 2
common/world/BlockStorage.h

@@ -1,8 +1,9 @@
 #ifndef BLOCKSTORAGE_H
 #define BLOCKSTORAGE_H
 
-#include "common/world/BlockMap.h"
+#include "common/Block.h"
 #include "memory/UniquePointer.h"
+#include "utils/BitArray.h"
 #include "utils/List.h"
 
 class BlockStorage final {
@@ -10,7 +11,18 @@ class BlockStorage final {
     int height;
     int sizeMask;
 
-    List<UniquePointer<BlockMap>> maps;
+    class Map {
+        BitArray blocks;
+        List<Block::Id> map;
+
+    public:
+        Map(int length, Block::Id id);
+
+        Block::Id get(int index) const;
+        void set(int index, Block::Id id);
+    };
+
+    List<UniquePointer<Map>> maps;
 
     static constexpr int SEGMENT_BITS = 4;
     static constexpr int SEGMENT_MASK = (1 << SEGMENT_BITS) - 1;

+ 0 - 1
meson.build

@@ -2,7 +2,6 @@ project('cubes plus plus', 'cpp')
 
 src_common = [
     'common/Block.cpp', 
-    'common/world/BlockMap.cpp',
     'common/world/BlockStorage.cpp',
     'common/world/World.cpp', 
     'common/world/HighMap.cpp',