summaryrefslogtreecommitdiff
path: root/terrain_loader.cpp
blob: 5a4aec707464bdf5f9f8737f72e7bb344979c489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#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;
}

std::list<Vector3> TerrainLoader::get_objects(int64_t x, int64_t y) {
	std::list<Vector3> objects;

	if(has_objects(x, y))
		objects = load_objects(x, y);

	return objects;
}

bool TerrainLoader::has_objects(int64_t x, int64_t y) {
	return fs::exists(root / (boost::format("%d.%d.objs") % x % y).str());
}

void TerrainLoader::save_objects(std::list<Vector3>& objects, int64_t x, int64_t y) {
	fs::path p = root / (boost::format("%d.%d.objs") % x % y).str();
	fs::ofstream os(p, std::ios::out | std::ios::binary);

	uint32_t count = objects.size();
	os.write((const char*)&count, sizeof(count));

	for(std::list<Vector3>::iterator it = objects.begin(); it != objects.end(); it++) {
		Vector3& obj = *it;
		os.write((const char*)&obj.x, sizeof(obj.x));
		os.write((const char*)&obj.y, sizeof(obj.y));
		os.write((const char*)&obj.z, sizeof(obj.z));
	}
	os.close();
}

std::list<Vector3> TerrainLoader::load_objects(int64_t x, int64_t y) {
	fs::path p = root / (boost::format("%d.%d.objs") % x % y).str();
	fs::ifstream is(p, std::ios::in | std::ios::binary);

	std::list<Vector3> objects;

	uint32_t count;
	is.read((char*)&count, sizeof(count));

	for(uint32_t i = 0; i < count; i++) {
		Vector3 obj;

		is.read((char*)&obj.x, sizeof(obj.x));
		is.read((char*)&obj.y, sizeof(obj.y));
		is.read((char*)&obj.z, sizeof(obj.z));

		objects.push_back(obj);
	}
	is.close();

	return objects;
}