summaryrefslogtreecommitdiffstats
path: root/Tools/QtBiomeVisualiser/RegionCache.h
blob: fb76b93f06f51cc22c85486217d66884aa5bf3f9 (plain) (blame)
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
#pragma once

#include <QObject>
#include <QCache>
#include <QMutex>
#include <memory>





// fwd:
class Region;
typedef std::shared_ptr<Region> RegionPtr;

class ChunkSource;





/** Caches regions' chunk data for reuse */
class RegionCache : public QObject
{
	typedef QObject super;
	Q_OBJECT

  public:
	explicit RegionCache(QObject * parent = NULL);

	/** Retrieves the specified region from the cache.
	Only returns valid regions; if the region is invalid, queues it for rendering and returns an empty ptr. */
	RegionPtr fetch(int a_RegionX, int a_RegionZ);

	/** Replaces the chunk source used by the biome view to get the chunk biome data.
	The cache is then invalidated. */
	void setChunkSource(std::shared_ptr<ChunkSource> a_ChunkSource);

	/** Returns true iff the chunk source has been initialized. */
	bool hasData() const { return (m_ChunkSource.get() != nullptr); }

	/** Reloads the current chunk source. */
	void reload();

  signals:
	void regionAvailable(int a_RegionX, int a_RegionZ);

  protected slots:
	void gotRegion(int a_RegionX, int a_RegionZ);

  protected:
	/** The cache of the chunks */
	QCache<quint32, RegionPtr> m_Cache;

	/** Locks the cache against multithreaded access */
	QMutex m_Mtx;

	/** The source used to get the biome data. */
	std::shared_ptr<ChunkSource> m_ChunkSource;


	/** Returns the hash used by the chunk in the cache */
	quint32 getRegionHash(int a_RegionX, int a_RegionZ);

	/** Queues the specified region for rendering by m_RegionSource. */
	void queueRegionRender(int a_RegionX, int a_RegionZ, RegionPtr & a_Region);
};