diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-02 01:41:17 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2015-02-02 01:41:17 +0100 |
commit | 8c1629af42a10ff7e23bb40100a259bf334a9c60 (patch) | |
tree | 123a427489f9317285996dc24f91c350d793e317 /src/World | |
parent | 1e5e31581689d372fbf32bfdcc0a89417dbf6b70 (diff) | |
download | MinedMap-8c1629af42a10ff7e23bb40100a259bf334a9c60.tar MinedMap-8c1629af42a10ff7e23bb40100a259bf334a9c60.zip |
Change region reader to a visitor pattern
Not keeping the whole region in memory reduces the needed space
to less than 5MB (from about 140).
Diffstat (limited to 'src/World')
-rw-r--r-- | src/World/Region.cpp | 13 | ||||
-rw-r--r-- | src/World/Region.hpp | 16 |
2 files changed, 15 insertions, 14 deletions
diff --git a/src/World/Region.cpp b/src/World/Region.cpp index fec732c..4551d73 100644 --- a/src/World/Region.cpp +++ b/src/World/Region.cpp @@ -54,7 +54,7 @@ Region::ChunkMap Region::processHeader(const uint8_t header[4096]) { return map; } -Region::Region(const char *filename) { +void Region::visitChunks(const char *filename, ChunkVisitor visitor) { std::ifstream file; file.exceptions(std::ios::failbit | std::ios::badbit); file.open(filename, std::ios::in | std::ios::binary); @@ -68,6 +68,8 @@ Region::Region(const char *filename) { chunkMap = processHeader(header); } + bool seen[SIZE][SIZE] = {}; + size_t i = 1, c = 0; while (!file.eof()) { auto it = chunkMap.find(i); @@ -80,16 +82,19 @@ Region::Region(const char *filename) { size_t x, z, len; std::tie(x, z, len) = it->second; - if (chunks[x][z]) + if (seen[x][z]) throw std::invalid_argument("duplicate chunk"); + seen[x][z] = true; + c++; + uint8_t buffer[len * 4096]; file.read((char *)buffer, len * 4096); - chunks[x][z].reset(new Chunk(Buffer(buffer, len * 4096))); + Chunk chunk(Buffer(buffer, len * 4096)); + visitor(x, z, &chunk); i += len; - c++; } if (c != chunkMap.size()) diff --git a/src/World/Region.hpp b/src/World/Region.hpp index 0843724..d88dbe1 100644 --- a/src/World/Region.hpp +++ b/src/World/Region.hpp @@ -28,6 +28,7 @@ #include "Chunk.hpp" +#include <functional> #include <memory> #include <stdexcept> #include <tuple> @@ -41,23 +42,18 @@ class Region { public: static const size_t SIZE = 32; + typedef std::function<void (size_t, size_t, const Chunk *)> ChunkVisitor; + + Region() = delete; + private: typedef std::tuple<size_t, size_t, size_t> ChunkDesc; typedef std::unordered_map<size_t, ChunkDesc> ChunkMap; - std::unique_ptr<Chunk> chunks[SIZE][SIZE]; - static ChunkMap processHeader(const uint8_t header[4096]); public: - Region(const char *filename); - - const Chunk * operator()(size_t x, size_t z) { - if (x >= SIZE || z >= SIZE) - throw std::range_error("Region(): bad coordinates"); - - return chunks[x][z].get(); - } + static void visitChunks(const char *filename, ChunkVisitor visitor); }; } |