2014-09-13 19:32:00 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QCache>
|
|
|
|
#include <QMutex>
|
2014-09-18 04:24:52 -04:00
|
|
|
#include <memory>
|
2014-09-13 19:32:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-27 18:58:09 -04:00
|
|
|
// fwd:
|
|
|
|
class Region;
|
|
|
|
typedef std::shared_ptr<Region> RegionPtr;
|
2014-09-13 19:32:00 -04:00
|
|
|
|
|
|
|
class ChunkSource;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-27 18:58:09 -04:00
|
|
|
/** Caches regions' chunk data for reuse */
|
|
|
|
class RegionCache :
|
2014-09-13 19:32:00 -04:00
|
|
|
public QObject
|
|
|
|
{
|
|
|
|
typedef QObject super;
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2014-10-27 18:58:09 -04:00
|
|
|
explicit RegionCache(QObject * parent = NULL);
|
2014-09-13 19:32:00 -04:00
|
|
|
|
2014-10-27 18:58:09 -04:00
|
|
|
/** 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);
|
2014-09-13 19:32:00 -04:00
|
|
|
|
|
|
|
/** 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. */
|
2014-09-15 11:29:34 -04:00
|
|
|
bool hasData() const { return (m_ChunkSource.get() != nullptr); }
|
|
|
|
|
|
|
|
/** Reloads the current chunk source. */
|
|
|
|
void reload();
|
2014-09-13 19:32:00 -04:00
|
|
|
|
|
|
|
signals:
|
2014-10-27 18:58:09 -04:00
|
|
|
void regionAvailable(int a_RegionX, int a_RegionZ);
|
2014-09-13 19:32:00 -04:00
|
|
|
|
|
|
|
protected slots:
|
2014-10-27 18:58:09 -04:00
|
|
|
void gotRegion(int a_RegionX, int a_RegionZ);
|
2014-09-13 19:32:00 -04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/** The cache of the chunks */
|
2014-10-27 18:58:09 -04:00
|
|
|
QCache<quint32, RegionPtr> m_Cache;
|
2014-09-13 19:32:00 -04:00
|
|
|
|
2014-10-27 18:58:09 -04:00
|
|
|
/** Locks the cache against multithreaded access */
|
2014-09-13 19:32:00 -04:00
|
|
|
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 */
|
2014-10-27 18:58:09 -04:00
|
|
|
quint32 getRegionHash(int a_RegionX, int a_RegionZ);
|
2014-09-13 19:32:00 -04:00
|
|
|
|
2014-10-27 18:58:09 -04:00
|
|
|
/** Queues the specified region for rendering by m_RegionSource. */
|
|
|
|
void queueRegionRender(int a_RegionX, int a_RegionZ, RegionPtr & a_Region);
|
2014-09-13 19:32:00 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|