forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunkcache.h
62 lines (51 loc) · 1.81 KB
/
chunkcache.h
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
/** Copyright (c) 2013, Sean Kasun */
#ifndef CHUNKCACHE_H_
#define CHUNKCACHE_H_
#include <QObject>
#include <QCache>
#include "./chunk.h"
// ChunkID is the key used to identify entries in the Cache
// Chunks are identified by their coordinates (CX,CZ) but a single key is needed to access a map like structure
class ChunkID {
public:
ChunkID(int cx, int cz);
bool operator==(const ChunkID &) const;
friend uint qHash(const ChunkID &);
protected:
int cx, cz;
};
class ChunkCache : public QObject {
Q_OBJECT
public:
// singleton: access to global usable instance
static ChunkCache &Instance();
private:
// singleton: prevent access to constructor and copyconstructor
ChunkCache();
~ChunkCache();
ChunkCache(const ChunkCache &);
ChunkCache &operator=(const ChunkCache &);
public:
void clear();
void setPath(QString path);
QString getPath() const;
QSharedPointer<Chunk> fetch(int cx, int cz); // fetch Chunk and load when not found
QSharedPointer<Chunk> fetchCached(int cx, int cz); // fetch Chunk only if cached
int getCost() const;
int getMaxCost() const;
signals:
void chunkLoaded(int cx, int cz);
void structureFound(QSharedPointer<GeneratedStructure> structure);
public slots:
void adaptCacheToWindow(int wx, int wy);
private slots:
void gotChunk(int cx, int cz);
void routeStructure(QSharedPointer<GeneratedStructure> structure);
private:
QString path; // path to folder with region files
QCache<ChunkID, QSharedPointer<Chunk>> cache; // real Cache
QMutex mutex; // Mutex for accessing the Cache
int maxcache; // number of Chunks that fit into Cache
QThreadPool loaderThreadPool; // extra thread pool for loading
};
#endif // CHUNKCACHE_H_