From 8771dde21514e754a7eb774f7047d17c226d6212 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 30 May 2011 23:15:34 +0200 Subject: Use a simple binary format for the map files, reduces file size drastically. --- terrain_loader.cpp | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/terrain_loader.cpp b/terrain_loader.cpp index 6374a7a..39a1cc4 100644 --- a/terrain_loader.cpp +++ b/terrain_loader.cpp @@ -5,6 +5,8 @@ #include #include +#include + using namespace noise; TerrainLoader::TerrainLoader(int seed, fs::path root) { @@ -56,36 +58,37 @@ bool TerrainLoader::has_chunk(int x, int y) { return fs::exists(root / (boost::format("%d.%d.chunk") % x % y).str()); } +// TODO: Handle big-endian platforms void TerrainLoader::save_chunk(float *chunk, int x, int y, int width, int height) { fs::path p = root / (boost::format("%d.%d.chunk") % x % y).str(); - fs::ofstream os(p); + fs::ofstream os(p, std::ios::out | std::ios::binary); - os << width << std::endl; - os << height << std::endl; + uint64_t w = width; + uint64_t h = height; + os.write((const char*)&w, sizeof(w)); + os.write((const char*)&h, sizeof(h)); - for(int x = 0; x < width; x++) - for(int y = 0; y < height; y++) - os << chunk[x*height + y] << std::endl; + os.write((const char*)chunk, width*height * sizeof(float)); os.close(); } +// TODO: Handle big-endian platforms // NOTE: assumes width <= chunk_size+1, likewise for height float *TerrainLoader::load_chunk(int x, int y, int width, int height) { fs::path p = root / (boost::format("%d.%d.chunk") % x % y).str(); - fs::ifstream is(p); + fs::ifstream is(p, std::ios::in | std::ios::binary); + + uint64_t w, h; + w = h = 0; + is.read((char*)&w, sizeof(w)); + is.read((char*)&h, sizeof(h)); - int w, h; - is >> w; - is >> h; + // TODO: should return w and h, fix calling code + if(w != width || h != height) + throw std::runtime_error("width or height doesn't match"); float *chunk = new float[width*height]; - for(int x = 0; x < w; x++) - for(int y = 0; y < h; y++) { - float v; - is >> v; - if(x < width && y < height) - chunk[x*height + y] = v; - } + is.read((char*)chunk, width*height*sizeof(float)); is.close(); return chunk; -- cgit v1.2.3