summaryrefslogtreecommitdiff
path: root/terrain_cache.h
blob: e8101fa5461bccda9e6a9e64046e159baba653ff (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
#ifndef TERRAIN_CACHE_H
#define TERRAIN_CACHE_H

#include "terrain_loader.h"
#include "vector.h"

#include <boost/shared_ptr.hpp>

#include <map>
#include <cstddef>

class TerrainCache;

struct TerrainCacheObject {
	typedef boost::shared_ptr<TerrainCacheObject> p;

	TerrainCache *cache;
	float *heights;
	// FIXME: Support other object types.
	std::list<Vector3> objects;
	int64_t x, y;
	unsigned int width, height;

	TerrainCacheObject(TerrainCache *cache, int64_t x, int64_t y, unsigned int width, unsigned int height,
			float *heights = NULL, std::list<Vector3> *objects = NULL);
	virtual ~TerrainCacheObject();
};

class TerrainCache {
	friend class TerrainCacheObject;

	private:
		typedef std::pair<int64_t, int64_t> intpair;
		typedef std::map<intpair, TerrainCacheObject::p> cache_map;

		cache_map caches;
		size_t max_size;

		TerrainCacheObject::p make_object(int64_t x, int64_t y, unsigned int width, unsigned int height, float *heights = NULL);

	public:
		typedef boost::shared_ptr<TerrainCache> p;

		TerrainCache(fs::path root, size_t max_size);
		TerrainCache(TerrainLoader::p loader, size_t max_size);
		virtual ~TerrainCache();

		TerrainLoader::p tl;

		TerrainCacheObject::p get_chunk(int64_t x, int64_t y, unsigned int width, unsigned int height);
		void add_chunk(float *heights, int64_t x, int64_t y, unsigned int width, unsigned int height);
		size_t get_size();
};

#endif