diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-06-01 19:02:52 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-06-01 19:02:52 +0200 |
commit | dc104e37842049e040f7e39f4e1aab56cde1488c (patch) | |
tree | 7a2b98b1007fe7325752ae5a8e728d1410072dfb /terrain_loader.cpp |
Moved common files here.
Diffstat (limited to 'terrain_loader.cpp')
-rw-r--r-- | terrain_loader.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/terrain_loader.cpp b/terrain_loader.cpp new file mode 100644 index 0000000..ef61e1e --- /dev/null +++ b/terrain_loader.cpp @@ -0,0 +1,58 @@ +#include "terrain_loader.h" + +#include <boost/format.hpp> +#include <boost/filesystem/fstream.hpp> + +#include <stdexcept> + +TerrainLoader::TerrainLoader(fs::path root) { + this->root = root; +} + +TerrainLoader::~TerrainLoader() { +} +float *TerrainLoader::get_chunk(int64_t x, int64_t y, unsigned int width, unsigned int height) { + if(has_chunk(x, y)) + return load_chunk(x, y, width, height); + return NULL; +} + +bool TerrainLoader::has_chunk(int64_t x, int64_t y) { + return fs::exists(root / (boost::format("%d.%d.chunk") % x % y).str()); +} + +// TODO: Handle big-endian platforms +void TerrainLoader::save_chunk(float *chunk, int64_t x, int64_t y, unsigned int width, unsigned int height) { + fs::path p = root / (boost::format("%d.%d.chunk") % x % y).str(); + fs::ofstream os(p, std::ios::out | std::ios::binary); + + uint64_t w = width; + uint64_t h = height; + os.write((const char*)&w, sizeof(w)); + os.write((const char*)&h, sizeof(h)); + + 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(int64_t x, int64_t y, unsigned int width, unsigned int height) { + fs::path p = root / (boost::format("%d.%d.chunk") % x % y).str(); + 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)); + + // 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]; + is.read((char*)chunk, width*height*sizeof(float)); + is.close(); + + return chunk; +} |