From c53b7e5d38c24bce7ba55abf3060ffd012783086 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Mon, 27 Oct 2014 23:58:09 +0100 Subject: QtBiomeVisualiser: Switched caching to entire regions. This speeds up the rendering preparation for small zooms. --- Tools/QtBiomeVisualiser/BiomeView.cpp | 42 +++++--- Tools/QtBiomeVisualiser/BiomeView.h | 8 +- Tools/QtBiomeVisualiser/ChunkCache.cpp | 126 ----------------------- Tools/QtBiomeVisualiser/ChunkCache.h | 72 -------------- Tools/QtBiomeVisualiser/ChunkLoader.cpp | 29 ------ Tools/QtBiomeVisualiser/ChunkLoader.h | 45 --------- Tools/QtBiomeVisualiser/ChunkSource.cpp | 10 +- Tools/QtBiomeVisualiser/ChunkSource.h | 6 +- Tools/QtBiomeVisualiser/MainWindow.cpp | 2 +- Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro | 18 ++-- Tools/QtBiomeVisualiser/Region.cpp | 72 ++++++++++++++ Tools/QtBiomeVisualiser/Region.h | 44 ++++++++ Tools/QtBiomeVisualiser/RegionCache.cpp | 138 ++++++++++++++++++++++++++ Tools/QtBiomeVisualiser/RegionCache.h | 73 ++++++++++++++ Tools/QtBiomeVisualiser/RegionLoader.cpp | 37 +++++++ Tools/QtBiomeVisualiser/RegionLoader.h | 50 ++++++++++ 16 files changed, 466 insertions(+), 306 deletions(-) delete mode 100644 Tools/QtBiomeVisualiser/ChunkCache.cpp delete mode 100644 Tools/QtBiomeVisualiser/ChunkCache.h delete mode 100644 Tools/QtBiomeVisualiser/ChunkLoader.cpp delete mode 100644 Tools/QtBiomeVisualiser/ChunkLoader.h create mode 100644 Tools/QtBiomeVisualiser/Region.cpp create mode 100644 Tools/QtBiomeVisualiser/Region.h create mode 100644 Tools/QtBiomeVisualiser/RegionCache.cpp create mode 100644 Tools/QtBiomeVisualiser/RegionCache.h create mode 100644 Tools/QtBiomeVisualiser/RegionLoader.cpp create mode 100644 Tools/QtBiomeVisualiser/RegionLoader.h (limited to 'Tools') diff --git a/Tools/QtBiomeVisualiser/BiomeView.cpp b/Tools/QtBiomeVisualiser/BiomeView.cpp index b44b935d7..fef2b0afd 100644 --- a/Tools/QtBiomeVisualiser/BiomeView.cpp +++ b/Tools/QtBiomeVisualiser/BiomeView.cpp @@ -1,8 +1,8 @@ #include "Globals.h" #include "BiomeView.h" -#include "QtChunk.h" #include #include +#include "Region.h" @@ -40,7 +40,7 @@ BiomeView::BiomeView(QWidget * parent) : redraw(); // Add a chunk-update callback mechanism: - connect(&m_Cache, SIGNAL(chunkAvailable(int, int)), this, SLOT(chunkAvailable(int, int))); + connect(&m_Cache, SIGNAL(regionAvailable(int, int)), this, SLOT(regionAvailable(int, int))); // Allow mouse and keyboard interaction: setFocusPolicy(Qt::StrongFocus); @@ -143,9 +143,15 @@ void BiomeView::redraw() -void BiomeView::chunkAvailable(int a_ChunkX, int a_ChunkZ) +void BiomeView::regionAvailable(int a_RegionX, int a_RegionZ) { - drawChunk(a_ChunkX, a_ChunkZ); + for (int z = 0; z < 32; z++) + { + for (int x = 0; x < 32; x++) + { + drawChunk(a_RegionX * 32 + x, a_RegionZ * 32 + z); + } + } update(); } @@ -175,8 +181,11 @@ void BiomeView::drawChunk(int a_ChunkX, int a_ChunkZ) return; } - //fetch the chunk: - ChunkPtr chunk = m_Cache.fetch(a_ChunkX, a_ChunkZ); + // Fetch the region: + int regionX; + int regionZ; + Region::chunkToRegion(a_ChunkX, a_ChunkZ, regionX, regionZ); + RegionPtr region = m_Cache.fetch(regionX, regionZ); // Figure out where on the screen this chunk should be drawn: // first find the center chunk @@ -228,9 +237,15 @@ void BiomeView::drawChunk(int a_ChunkX, int a_ChunkZ) // If the chunk is valid, use its data; otherwise use the empty placeholder: const uchar * src = m_EmptyChunkImage; - if (chunk.get() != nullptr) + if (region.get() != nullptr) { - src = chunk->getImage(); + int relChunkX = a_ChunkX - regionX * 32; + int relChunkZ = a_ChunkZ - regionZ * 32; + Chunk & chunk = region->getRelChunk(relChunkX, relChunkZ); + if (chunk.isValid()) + { + src = chunk.getImage(); + } } // Blit or scale-blit the image: @@ -317,11 +332,12 @@ void BiomeView::mouseMoveEvent(QMouseEvent * a_Event) // Update the status bar info text: int blockX = floor((a_Event->x() - width() / 2) / m_Zoom + m_X); int blockZ = floor((a_Event->y() - height() / 2) / m_Zoom + m_Z); - int chunkX, chunkZ; - int relX = blockX, relY, relZ = blockZ; - cChunkDef::AbsoluteToRelative(relX, relY, relZ, chunkX, chunkZ); - auto chunk = m_Cache.fetch(chunkX, chunkZ); - int biome = (chunk.get() != nullptr) ? chunk->getBiome(relX, relZ) : biInvalidBiome; + int regionX, regionZ; + Region::blockToRegion(blockX, blockZ, regionX, regionZ); + int relX = blockX - regionX * 512; + int relZ = blockZ - regionZ * 512; + auto region = m_Cache.fetch(regionX, regionZ); + int biome = (region.get() != nullptr) ? region->getRelBiome(relX, relZ) : biInvalidBiome; emit hoverChanged(blockX, blockZ, biome); } diff --git a/Tools/QtBiomeVisualiser/BiomeView.h b/Tools/QtBiomeVisualiser/BiomeView.h index 40d8b96ae..9901b8f24 100644 --- a/Tools/QtBiomeVisualiser/BiomeView.h +++ b/Tools/QtBiomeVisualiser/BiomeView.h @@ -2,7 +2,7 @@ #include #include -#include "ChunkCache.h" +#include "RegionCache.h" #include "ChunkSource.h" @@ -51,8 +51,8 @@ public slots: /** Redraw the entire widget area. */ void redraw(); - /** A specified chunk has become available, redraw it. */ - void chunkAvailable(int a_ChunkX, int a_ChunkZ); + /** A specified region has become available, redraw it. */ + void regionAvailable(int a_RegionX, int a_RegionZ); /** Reloads the current chunk source and redraws the entire workspace. */ void reload(); @@ -62,7 +62,7 @@ protected: double m_Zoom; /** Cache for the loaded chunk data. */ - ChunkCache m_Cache; + RegionCache m_Cache; /** The entire view's contents in an offscreen image. */ QImage m_Image; diff --git a/Tools/QtBiomeVisualiser/ChunkCache.cpp b/Tools/QtBiomeVisualiser/ChunkCache.cpp deleted file mode 100644 index 05c267d30..000000000 --- a/Tools/QtBiomeVisualiser/ChunkCache.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#include "Globals.h" -#include "ChunkCache.h" -#include -#include -#include "ChunkSource.h" -#include "ChunkLoader.h" - - - - - -ChunkCache::ChunkCache(QObject * parent) : - super(parent) -{ - m_Cache.setMaxCost(1024 * 1024 * 1024); // 1 GiB of memory for the cache -} - - - - - -ChunkPtr ChunkCache::fetch(int a_ChunkX, int a_ChunkZ) -{ - // Retrieve from the cache: - quint32 hash = getChunkHash(a_ChunkX, a_ChunkZ); - ChunkPtr * res; - { - QMutexLocker lock(&m_Mtx); - res = m_Cache[hash]; - // If succesful and chunk loaded, return the retrieved value: - if ((res != nullptr) && (*res)->isValid()) - { - return *res; - } - } - - // If the chunk is in cache but not valid, it means it has been already queued for rendering, do nothing now: - if (res != nullptr) - { - return ChunkPtr(nullptr); - } - - // There's no such item in the cache, create it now: - res = new ChunkPtr(new Chunk); - if (res == nullptr) - { - return ChunkPtr(nullptr); - } - { - QMutexLocker lock(&m_Mtx); - m_Cache.insert(hash, res, sizeof(Chunk)); - } - - // Queue the chunk for rendering: - queueChunkRender(a_ChunkX, a_ChunkZ, *res); - - // Return failure, the chunk is not yet rendered: - return ChunkPtr(nullptr); -} - - - - - -void ChunkCache::setChunkSource(std::shared_ptr a_ChunkSource) -{ - // Replace the chunk source: - m_ChunkSource = a_ChunkSource; - - // Clear the cache: - QMutexLocker lock(&m_Mtx); - m_Cache.clear(); -} - - - - - -void ChunkCache::reload() -{ - assert(m_ChunkSource.get() != nullptr); - - // Reload the chunk source: - m_ChunkSource->reload(); - - // Clear the cache: - QMutexLocker lock(&m_Mtx); - m_Cache.clear(); -} - - - - - -void ChunkCache::gotChunk(int a_ChunkX, int a_ChunkZ) -{ - emit chunkAvailable(a_ChunkX, a_ChunkZ); -} - - - - - -quint32 ChunkCache::getChunkHash(int a_ChunkX, int a_ChunkZ) -{ - // Simply join the two coords into a single int - // The coords will never be larger than 16-bits, so we can do this safely - return (((static_cast(a_ChunkX) & 0xffff) << 16) | (static_cast(a_ChunkZ) & 0xffff)); -} - - - - - -void ChunkCache::queueChunkRender(int a_ChunkX, int a_ChunkZ, ChunkPtr & a_Chunk) -{ - // Create a new loader task: - ChunkLoader * loader = new ChunkLoader(a_ChunkX, a_ChunkZ, a_Chunk, m_ChunkSource); - connect(loader, SIGNAL(loaded(int, int)), this, SLOT(gotChunk(int, int))); - - QThreadPool::globalInstance()->start(loader); -} - - - - diff --git a/Tools/QtBiomeVisualiser/ChunkCache.h b/Tools/QtBiomeVisualiser/ChunkCache.h deleted file mode 100644 index 8d198f02f..000000000 --- a/Tools/QtBiomeVisualiser/ChunkCache.h +++ /dev/null @@ -1,72 +0,0 @@ -#pragma once - -#include -#include -#include -#include - - - - - -class Chunk; -typedef std::shared_ptr ChunkPtr; - -class ChunkSource; - - - - - -/** Caches chunk data for reuse */ -class ChunkCache : - public QObject -{ - typedef QObject super; - Q_OBJECT - -public: - explicit ChunkCache(QObject * parent = NULL); - - /** Retrieves the specified chunk from the cache. - Only returns valid chunks; if the chunk is invalid, queues it for rendering and returns an empty ptr. */ - ChunkPtr fetch(int a_ChunkX, int a_ChunkZ); - - /** 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 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 chunkAvailable(int a_ChunkX, int a_ChunkZ); - -protected slots: - void gotChunk(int a_ChunkX, int a_ChunkZ); - -protected: - /** The cache of the chunks */ - QCache m_Cache; - - /** Locks te cache against multithreaded access */ - QMutex m_Mtx; - - /** The source used to get the biome data. */ - std::shared_ptr m_ChunkSource; - - - /** Returns the hash used by the chunk in the cache */ - quint32 getChunkHash(int a_ChunkX, int a_ChunkZ); - - /** Queues the specified chunk for rendering by m_ChunkSource. */ - void queueChunkRender(int a_ChunkX, int a_ChunkZ, ChunkPtr & a_Chunk); -}; - - - - - diff --git a/Tools/QtBiomeVisualiser/ChunkLoader.cpp b/Tools/QtBiomeVisualiser/ChunkLoader.cpp deleted file mode 100644 index 3d0123b23..000000000 --- a/Tools/QtBiomeVisualiser/ChunkLoader.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "Globals.h" -#include "ChunkLoader.h" -#include "ChunkSource.h" - - - - - -ChunkLoader::ChunkLoader(int a_ChunkX, int a_ChunkZ, ChunkPtr a_Chunk, ChunkSourcePtr a_ChunkSource) : - m_ChunkX(a_ChunkX), - m_ChunkZ(a_ChunkZ), - m_Chunk(a_Chunk), - m_ChunkSource(a_ChunkSource) -{ -} - - - - - -void ChunkLoader::run() -{ - m_ChunkSource->getChunkBiomes(m_ChunkX, m_ChunkZ, m_Chunk); - emit loaded(m_ChunkX, m_ChunkZ); -} - - - - diff --git a/Tools/QtBiomeVisualiser/ChunkLoader.h b/Tools/QtBiomeVisualiser/ChunkLoader.h deleted file mode 100644 index 4d026a45e..000000000 --- a/Tools/QtBiomeVisualiser/ChunkLoader.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once - -#include -#include -#include - - - - -// fwd: -class Chunk; -typedef std::shared_ptr ChunkPtr; - -class ChunkSource; -typedef std::shared_ptr ChunkSourcePtr; - - - - - -class ChunkLoader : - public QObject, - public QRunnable -{ - Q_OBJECT - -public: - ChunkLoader(int a_ChunkX, int a_ChunkZ, ChunkPtr a_Chunk, ChunkSourcePtr a_ChunkSource); - virtual ~ChunkLoader() {} - -signals: - void loaded(int a_ChunkX, int a_ChunkZ); - -protected: - virtual void run() override; - -private: - int m_ChunkX, m_ChunkZ; - ChunkPtr m_Chunk; - ChunkSourcePtr m_ChunkSource; -}; - - - - diff --git a/Tools/QtBiomeVisualiser/ChunkSource.cpp b/Tools/QtBiomeVisualiser/ChunkSource.cpp index c5cde1c3b..c6d50b2e0 100644 --- a/Tools/QtBiomeVisualiser/ChunkSource.cpp +++ b/Tools/QtBiomeVisualiser/ChunkSource.cpp @@ -24,14 +24,14 @@ BioGenSource::BioGenSource(cIniFilePtr a_IniFile) : -void BioGenSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) +void BioGenSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk) { cChunkDef::BiomeMap biomes; { QMutexLocker lock(&m_Mtx); m_BiomeGen->GenBiomes(a_ChunkX, a_ChunkZ, biomes); } - a_DestChunk->setBiomes(biomes); + a_DestChunk.setBiomes(biomes); } @@ -160,7 +160,7 @@ AnvilSource::AnvilSource(QString a_WorldRegionFolder) : -void AnvilSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) +void AnvilSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk) { // Load the compressed data: AString compressedChunkData = getCompressedChunkData(a_ChunkX, a_ChunkZ); @@ -200,7 +200,7 @@ void AnvilSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChun { biomeMap[i] = (EMCSBiome)GetBEInt(beBiomes + 4 * i); } - a_DestChunk->setBiomes(biomeMap); + a_DestChunk.setBiomes(biomeMap); return; } @@ -216,7 +216,7 @@ void AnvilSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChun { biomeMap[i] = EMCSBiome(vanillaBiomes[i]); } - a_DestChunk->setBiomes(biomeMap); + a_DestChunk.setBiomes(biomeMap); } diff --git a/Tools/QtBiomeVisualiser/ChunkSource.h b/Tools/QtBiomeVisualiser/ChunkSource.h index 5332c5d3f..ed2f457e9 100644 --- a/Tools/QtBiomeVisualiser/ChunkSource.h +++ b/Tools/QtBiomeVisualiser/ChunkSource.h @@ -26,7 +26,7 @@ public: /** Fills the a_DestChunk with the biomes for the specified coords. It is expected to be thread-safe and re-entrant. Usually QThread::idealThreadCount() threads are used. */ - virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) = 0; + virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk) = 0; /** Forces a fresh reload of the source. Useful mainly for the generator, whose underlying definition file may have been changed. */ virtual void reload() = 0; @@ -45,7 +45,7 @@ public: BioGenSource(cIniFilePtr a_IniFile); // ChunkSource overrides: - virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override; + virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk) override; virtual void reload(void) override; protected: @@ -70,7 +70,7 @@ public: AnvilSource(QString a_WorldRegionFolder); // ChunkSource overrides: - virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override; + virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk) override; virtual void reload() override; protected: diff --git a/Tools/QtBiomeVisualiser/MainWindow.cpp b/Tools/QtBiomeVisualiser/MainWindow.cpp index 7853d768e..dc26a32c7 100644 --- a/Tools/QtBiomeVisualiser/MainWindow.cpp +++ b/Tools/QtBiomeVisualiser/MainWindow.cpp @@ -8,8 +8,8 @@ #include #include #include -#include "src/IniFile.h" #include "ChunkSource.h" +#include "src/IniFile.h" #include "src/Generating/BioGen.h" #include "src/StringCompression.h" #include "src/WorldStorage/FastNBT.h" diff --git a/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro b/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro index f3a5255fb..4cff9c90f 100644 --- a/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro +++ b/Tools/QtBiomeVisualiser/QtBiomeVisualiser.pro @@ -26,9 +26,6 @@ SOURCES +=\ ../../src/OSSupport/CriticalSection.cpp \ ../../src/OSSupport/IsThread.cpp \ ../../src/BiomeDef.cpp \ - ChunkCache.cpp \ - ChunkSource.cpp \ - ChunkLoader.cpp \ ../../src/StringCompression.cpp \ ../../src/WorldStorage/FastNBT.cpp \ ../../lib/zlib/adler32.c \ @@ -48,7 +45,11 @@ SOURCES +=\ ../../lib/zlib/zutil.c \ GeneratorSetup.cpp \ QtBiomeVisualiser.cpp \ - QtChunk.cpp + QtChunk.cpp \ + RegionCache.cpp \ + Region.cpp \ + ChunkSource.cpp \ + RegionLoader.cpp HEADERS += MainWindow.h \ Globals.h \ @@ -64,9 +65,6 @@ HEADERS += MainWindow.h \ ../../src/OSSupport/CriticalSection.h \ ../../src/OSSupport/IsThread.h \ ../../src/BiomeDef.h \ - ChunkCache.h \ - ChunkSource.h \ - ChunkLoader.h \ ../../src/StringCompression.h \ ../../src/WorldStorage/FastNBT.h \ ../../lib/zlib/crc32.h \ @@ -81,7 +79,11 @@ HEADERS += MainWindow.h \ ../../lib/zlib/zlib.h \ ../../lib/zlib/zutil.h \ GeneratorSetup.h \ - QtChunk.h + QtChunk.h \ + RegionCache.h \ + Region.h \ + ChunkSource.h \ + RegionLoader.h INCLUDEPATH += $$_PRO_FILE_PWD_ \ $$_PRO_FILE_PWD_/../../lib \ diff --git a/Tools/QtBiomeVisualiser/Region.cpp b/Tools/QtBiomeVisualiser/Region.cpp new file mode 100644 index 000000000..d8a0a2f76 --- /dev/null +++ b/Tools/QtBiomeVisualiser/Region.cpp @@ -0,0 +1,72 @@ + +#include "Globals.h" +#include "Region.h" + + + + + +Region::Region() +{ +} + + + + + +Chunk & Region::getRelChunk(int a_RelChunkX, int a_RelChunkZ) +{ + ASSERT(a_RelChunkX >= 0); + ASSERT(a_RelChunkZ >= 0); + ASSERT(a_RelChunkX < 32); + ASSERT(a_RelChunkZ < 32); + + return m_Chunks[a_RelChunkX + a_RelChunkZ * 32]; +} + + + + + +int Region::getRelBiome(int a_RelBlockX, int a_RelBlockZ) +{ + ASSERT(a_RelBlockX >= 0); + ASSERT(a_RelBlockZ >= 0); + ASSERT(a_RelBlockX < 512); + ASSERT(a_RelBlockZ < 512); + + int chunkX = a_RelBlockX / 16; + int chunkZ = a_RelBlockZ / 16; + Chunk & chunk = m_Chunks[chunkX + 32 * chunkZ]; + if (chunk.isValid()) + { + return chunk.getBiome(a_RelBlockX - 16 * chunkX, a_RelBlockZ - 16 * chunkZ); + } + else + { + return biInvalidBiome; + } +} + + + + +void Region::blockToRegion(int a_BlockX, int a_BlockZ, int & a_RegionX, int & a_RegionZ) +{ + a_RegionX = static_cast(std::floor(static_cast(a_BlockX) / 512)); + a_RegionZ = static_cast(std::floor(static_cast(a_BlockZ) / 512)); +} + + + + + +void Region::chunkToRegion(int a_ChunkX, int a_ChunkZ, int & a_RegionX, int & a_RegionZ) +{ + a_RegionX = static_cast(std::floor(static_cast(a_ChunkX) / 32)); + a_RegionZ = static_cast(std::floor(static_cast(a_ChunkZ) / 32)); +} + + + + diff --git a/Tools/QtBiomeVisualiser/Region.h b/Tools/QtBiomeVisualiser/Region.h new file mode 100644 index 000000000..f1bef0c2d --- /dev/null +++ b/Tools/QtBiomeVisualiser/Region.h @@ -0,0 +1,44 @@ +#pragma once + +#include "QtChunk.h" + + + + + +class Region +{ +public: + Region(); + + /** Retrieves the chunk with the specified relative coords. */ + Chunk & getRelChunk(int a_RelChunkX, int a_RelChunkZ); + + /** Returns true iff the chunk data for all chunks has been loaded. + This doesn't mean that all the chunks are valid, only that the entire region has been processed and should + be displayed. */ + bool isValid(void) const { return m_IsValid; } + + /** Returns the biome in the block coords relative to this region. + Returns biInvalidBiome if the underlying chunk is not valid. */ + int getRelBiome(int a_RelBlockX, int a_RelBlockZ); + + /** Converts block coordinates into region coordinates. */ + static void blockToRegion(int a_BlockX, int a_BlockZ, int & a_RegionX, int & a_RegionZ); + + /** Converts chunk coordinates into region coordinates. */ + static void chunkToRegion(int a_ChunkX, int a_ChunkZ, int & a_RegionX, int & a_RegionZ); + +protected: + + Chunk m_Chunks[32 * 32]; + + /** True iff the data for all the chunks has been loaded. + This doesn't mean that all the chunks are valid, only that the entire region has been processed and should + be displayed. */ + bool m_IsValid; +}; + + + + diff --git a/Tools/QtBiomeVisualiser/RegionCache.cpp b/Tools/QtBiomeVisualiser/RegionCache.cpp new file mode 100644 index 000000000..e46fd222a --- /dev/null +++ b/Tools/QtBiomeVisualiser/RegionCache.cpp @@ -0,0 +1,138 @@ +#include "Globals.h" +#include "RegionCache.h" +#include +#include +#include "ChunkSource.h" +#include "RegionLoader.h" +#include "Region.h" + + + + + +RegionCache::RegionCache(QObject * parent) : + super(parent) +{ + m_Cache.setMaxCost(1024 * 1024 * 1024); // 1 GiB of memory for the cache +} + + + + + +RegionPtr RegionCache::fetch(int a_RegionX, int a_RegionZ) +{ + // Retrieve from the cache: + quint32 hash = getRegionHash(a_RegionX, a_RegionZ); + RegionPtr * res; + { + QMutexLocker lock(&m_Mtx); + res = m_Cache[hash]; + // If succesful and region loaded, return the retrieved value: + if ((res != nullptr) && (*res)->isValid()) + { + return *res; + } + } + + // If the region is in cache but not valid, it means it has been already queued for rendering, do nothing now: + if (res != nullptr) + { + return RegionPtr(nullptr); + } + + // There's no such item in the cache, create it now: + try + { + res = new RegionPtr(new Region); + } + catch (const std::bad_alloc &) + { + /* Allocation failed (32-bit process hit the 2 GiB barrier?) + This may happen even with the cache set to 1 GiB, because it contains shared ptrs and so they may be + held by another place in the code even when they are removed from cache. + */ + return RegionPtr(nullptr); + } + if (res == nullptr) + { + return RegionPtr(nullptr); + } + { + QMutexLocker lock(&m_Mtx); + m_Cache.insert(hash, res, sizeof(Region)); + } + + // Queue the region for rendering: + queueRegionRender(a_RegionX, a_RegionZ, *res); + + // Return failure, the region is not yet rendered: + return RegionPtr(nullptr); +} + + + + + +void RegionCache::setChunkSource(std::shared_ptr a_ChunkSource) +{ + // Replace the chunk source: + m_ChunkSource = a_ChunkSource; + + // Clear the cache: + QMutexLocker lock(&m_Mtx); + m_Cache.clear(); +} + + + + + +void RegionCache::reload() +{ + assert(m_ChunkSource.get() != nullptr); + + // Reload the chunk source: + m_ChunkSource->reload(); + + // Clear the cache: + QMutexLocker lock(&m_Mtx); + m_Cache.clear(); +} + + + + + +void RegionCache::gotRegion(int a_RegionX, int a_RegionZ) +{ + emit regionAvailable(a_RegionX, a_RegionZ); +} + + + + + +quint32 RegionCache::getRegionHash(int a_RegionX, int a_RegionZ) +{ + // Simply join the two coords into a single int + // The coords will never be larger than 16-bits, so we can do this safely + return (((static_cast(a_RegionX) & 0xffff) << 16) | (static_cast(a_RegionZ) & 0xffff)); +} + + + + + +void RegionCache::queueRegionRender(int a_RegionX, int a_RegionZ, RegionPtr & a_Region) +{ + // Create a new loader task: + RegionLoader * loader = new RegionLoader(a_RegionX, a_RegionZ, a_Region, m_ChunkSource); + connect(loader, SIGNAL(loaded(int, int)), this, SLOT(gotRegion(int, int))); + + QThreadPool::globalInstance()->start(loader); +} + + + + diff --git a/Tools/QtBiomeVisualiser/RegionCache.h b/Tools/QtBiomeVisualiser/RegionCache.h new file mode 100644 index 000000000..c343e4ba9 --- /dev/null +++ b/Tools/QtBiomeVisualiser/RegionCache.h @@ -0,0 +1,73 @@ +#pragma once + +#include +#include +#include +#include + + + + + +// fwd: +class Region; +typedef std::shared_ptr 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 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 m_Cache; + + /** Locks the cache against multithreaded access */ + QMutex m_Mtx; + + /** The source used to get the biome data. */ + std::shared_ptr 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); +}; + + + + + diff --git a/Tools/QtBiomeVisualiser/RegionLoader.cpp b/Tools/QtBiomeVisualiser/RegionLoader.cpp new file mode 100644 index 000000000..8c298da1e --- /dev/null +++ b/Tools/QtBiomeVisualiser/RegionLoader.cpp @@ -0,0 +1,37 @@ +#include "Globals.h" +#include "RegionLoader.h" +#include "ChunkSource.h" +#include "Region.h" + + + + + +RegionLoader::RegionLoader(int a_RegionX, int a_RegionZ, RegionPtr a_Region, ChunkSourcePtr a_ChunkSource) : + m_RegionX(a_RegionX), + m_RegionZ(a_RegionZ), + m_Region(a_Region), + m_ChunkSource(a_ChunkSource) +{ +} + + + + + +void RegionLoader::run() +{ + // Load all the chunks in this region: + for (int z = 0; z < 32; z++) + { + for (int x = 0; x < 32; x++) + { + m_ChunkSource->getChunkBiomes(m_RegionX * 32 + x, m_RegionZ * 32 + z, m_Region->getRelChunk(x, z)); + } + } + emit loaded(m_RegionX, m_RegionZ); +} + + + + diff --git a/Tools/QtBiomeVisualiser/RegionLoader.h b/Tools/QtBiomeVisualiser/RegionLoader.h new file mode 100644 index 000000000..5a993f5ca --- /dev/null +++ b/Tools/QtBiomeVisualiser/RegionLoader.h @@ -0,0 +1,50 @@ +#pragma once + +#include +#include +#include + + + + +// fwd: +class Region; +typedef std::shared_ptr RegionPtr; + +class ChunkSource; +typedef std::shared_ptr ChunkSourcePtr; + + + + + +class RegionLoader : + public QObject, + public QRunnable +{ + Q_OBJECT + +public: + RegionLoader(int a_RegionX, int a_RegionZ, RegionPtr a_Region, ChunkSourcePtr a_ChunkSource); + virtual ~RegionLoader() {} + +signals: + void loaded(int a_RegionX, int a_RegionZ); + +protected: + virtual void run() override; + +private: + /** Coords of the region to be loaded. */ + int m_RegionX, m_RegionZ; + + /** The region to be loaded. */ + RegionPtr m_Region; + + /** The chunk source to be used for individual chunks within the region. */ + ChunkSourcePtr m_ChunkSource; +}; + + + + -- cgit v1.2.3 From 53ff5c527c154348608f9ab9559bf67064c8ed2a Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 28 Oct 2014 11:23:54 +0100 Subject: QtBiomeVisualiser: Attempt at fixing Linux compilation. --- Tools/QtBiomeVisualiser/ChunkSource.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Tools') diff --git a/Tools/QtBiomeVisualiser/ChunkSource.cpp b/Tools/QtBiomeVisualiser/ChunkSource.cpp index c6d50b2e0..d9660b886 100644 --- a/Tools/QtBiomeVisualiser/ChunkSource.cpp +++ b/Tools/QtBiomeVisualiser/ChunkSource.cpp @@ -260,7 +260,7 @@ AnvilSource::AnvilFilePtr AnvilSource::getAnvilFile(int a_ChunkX, int a_ChunkZ) // Search the cache for the file: QMutexLocker lock(&m_Mtx); - for (auto itr = m_Files.cbegin(), end = m_Files.cend(); itr != end; ++itr) + for (auto itr = m_Files.begin(), end = m_Files.end(); itr != end; ++itr) { if (((*itr)->m_RegionX == RegionX) && ((*itr)->m_RegionZ == RegionZ)) { -- cgit v1.2.3 From 3f3a2a6755460e4f0d976deee9f067969edbfdd2 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 28 Oct 2014 20:49:29 +0100 Subject: QtBiomeVisualiser: Fixed random regions not rendered. --- Tools/QtBiomeVisualiser/Region.h | 2 ++ Tools/QtBiomeVisualiser/RegionLoader.cpp | 2 ++ 2 files changed, 4 insertions(+) (limited to 'Tools') diff --git a/Tools/QtBiomeVisualiser/Region.h b/Tools/QtBiomeVisualiser/Region.h index f1bef0c2d..863c0ac02 100644 --- a/Tools/QtBiomeVisualiser/Region.h +++ b/Tools/QtBiomeVisualiser/Region.h @@ -30,6 +30,8 @@ public: static void chunkToRegion(int a_ChunkX, int a_ChunkZ, int & a_RegionX, int & a_RegionZ); protected: + friend class RegionLoader; + Chunk m_Chunks[32 * 32]; diff --git a/Tools/QtBiomeVisualiser/RegionLoader.cpp b/Tools/QtBiomeVisualiser/RegionLoader.cpp index 8c298da1e..771c1bbac 100644 --- a/Tools/QtBiomeVisualiser/RegionLoader.cpp +++ b/Tools/QtBiomeVisualiser/RegionLoader.cpp @@ -29,6 +29,8 @@ void RegionLoader::run() m_ChunkSource->getChunkBiomes(m_RegionX * 32 + x, m_RegionZ * 32 + z, m_Region->getRelChunk(x, z)); } } + m_Region->m_IsValid = true; + emit loaded(m_RegionX, m_RegionZ); } -- cgit v1.2.3 From bea2edb2a9b3b3db6fd734f4e53a4d60eebc3272 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 28 Oct 2014 20:52:04 +0100 Subject: QtBiomeVisualiser: Reduced memory usage. --- Tools/QtBiomeVisualiser/BiomeView.cpp | 158 +++++++++++++++++++++++++++++---- Tools/QtBiomeVisualiser/BiomeView.h | 3 + Tools/QtBiomeVisualiser/QtChunk.cpp | 159 ++-------------------------------- Tools/QtBiomeVisualiser/QtChunk.h | 18 ++-- 4 files changed, 156 insertions(+), 182 deletions(-) (limited to 'Tools') diff --git a/Tools/QtBiomeVisualiser/BiomeView.cpp b/Tools/QtBiomeVisualiser/BiomeView.cpp index fef2b0afd..c77b39482 100644 --- a/Tools/QtBiomeVisualiser/BiomeView.cpp +++ b/Tools/QtBiomeVisualiser/BiomeView.cpp @@ -14,6 +14,116 @@ static const int DELTA_STEP = 120; // The normal per-notch wheel delta +/** Map for converting biome values to colors. Initialized from biomeColors[]. */ +static uchar biomeToColor[256 * 4]; + +/** Map for converting biome values to colors. Used to initialize biomeToColor[].*/ +static struct +{ + EMCSBiome m_Biome; + uchar m_Color[3]; +} biomeColors[] = +{ + { biOcean, { 0x00, 0x00, 0x70 }, }, + { biPlains, { 0x8d, 0xb3, 0x60 }, }, + { biDesert, { 0xfa, 0x94, 0x18 }, }, + { biExtremeHills, { 0x60, 0x60, 0x60 }, }, + { biForest, { 0x05, 0x66, 0x21 }, }, + { biTaiga, { 0x0b, 0x66, 0x59 }, }, + { biSwampland, { 0x2f, 0xff, 0xda }, }, + { biRiver, { 0x30, 0x30, 0xaf }, }, + { biHell, { 0x7f, 0x00, 0x00 }, }, + { biSky, { 0x00, 0x7f, 0xff }, }, + { biFrozenOcean, { 0xa0, 0xa0, 0xdf }, }, + { biFrozenRiver, { 0xa0, 0xa0, 0xff }, }, + { biIcePlains, { 0xff, 0xff, 0xff }, }, + { biIceMountains, { 0xa0, 0xa0, 0xa0 }, }, + { biMushroomIsland, { 0xff, 0x00, 0xff }, }, + { biMushroomShore, { 0xa0, 0x00, 0xff }, }, + { biBeach, { 0xfa, 0xde, 0x55 }, }, + { biDesertHills, { 0xd2, 0x5f, 0x12 }, }, + { biForestHills, { 0x22, 0x55, 0x1c }, }, + { biTaigaHills, { 0x16, 0x39, 0x33 }, }, + { biExtremeHillsEdge, { 0x7f, 0x8f, 0x7f }, }, + { biJungle, { 0x53, 0x7b, 0x09 }, }, + { biJungleHills, { 0x2c, 0x42, 0x05 }, }, + + { biJungleEdge, { 0x62, 0x8b, 0x17 }, }, + { biDeepOcean, { 0x00, 0x00, 0x30 }, }, + { biStoneBeach, { 0xa2, 0xa2, 0x84 }, }, + { biColdBeach, { 0xfa, 0xf0, 0xc0 }, }, + { biBirchForest, { 0x30, 0x74, 0x44 }, }, + { biBirchForestHills, { 0x1f, 0x5f, 0x32 }, }, + { biRoofedForest, { 0x40, 0x51, 0x1a }, }, + { biColdTaiga, { 0x31, 0x55, 0x4a }, }, + { biColdTaigaHills, { 0x59, 0x7d, 0x72 }, }, + { biMegaTaiga, { 0x59, 0x66, 0x51 }, }, + { biMegaTaigaHills, { 0x59, 0x66, 0x59 }, }, + { biExtremeHillsPlus, { 0x50, 0x70, 0x50 }, }, + { biSavanna, { 0xbd, 0xb2, 0x5f }, }, + { biSavannaPlateau, { 0xa7, 0x9d, 0x64 }, }, + { biMesa, { 0xd9, 0x45, 0x15 }, }, + { biMesaPlateauF, { 0xb0, 0x97, 0x65 }, }, + { biMesaPlateau, { 0xca, 0x8c, 0x65 }, }, + + // M variants: + { biSunflowerPlains, { 0xb5, 0xdb, 0x88 }, }, + { biDesertM, { 0xff, 0xbc, 0x40 }, }, + { biExtremeHillsM, { 0x88, 0x88, 0x88 }, }, + { biFlowerForest, { 0x2d, 0x8e, 0x49 }, }, + { biTaigaM, { 0x33, 0x8e, 0x81 }, }, + { biSwamplandM, { 0x07, 0xf9, 0xb2 }, }, + { biIcePlainsSpikes, { 0xb4, 0xdc, 0xdc }, }, + { biJungleM, { 0x7b, 0xa3, 0x31 }, }, + { biJungleEdgeM, { 0x62, 0x8b, 0x17 }, }, + { biBirchForestM, { 0x58, 0x9c, 0x6c }, }, + { biBirchForestHillsM, { 0x47, 0x87, 0x5a }, }, + { biRoofedForestM, { 0x68, 0x79, 0x42 }, }, + { biColdTaigaM, { 0x24, 0x3f, 0x36 }, }, + { biMegaSpruceTaiga, { 0x45, 0x4f, 0x3e }, }, + { biMegaSpruceTaigaHills, { 0x45, 0x4f, 0x4e }, }, + { biExtremeHillsPlusM, { 0x78, 0x98, 0x78 }, }, + { biSavannaM, { 0xe5, 0xda, 0x87 }, }, + { biSavannaPlateauM, { 0xa7, 0x9d, 0x74 }, }, + { biMesaBryce, { 0xff, 0x6d, 0x3d }, }, + { biMesaPlateauFM, { 0xd8, 0xbf, 0x8d }, }, + { biMesaPlateauM, { 0xf2, 0xb4, 0x8d }, }, +} ; + + + + + +static class BiomeColorsInitializer +{ +public: + BiomeColorsInitializer(void) + { + // Reset all colors to gray: + for (size_t i = 0; i < ARRAYCOUNT(biomeToColor); i++) + { + biomeToColor[i] = 0x7f; + } + + // Set known biomes to their colors: + for (size_t i = 0; i < ARRAYCOUNT(biomeColors); i++) + { + uchar * color = &biomeToColor[4 * biomeColors[i].m_Biome]; + color[0] = biomeColors[i].m_Color[2]; + color[1] = biomeColors[i].m_Color[1]; + color[2] = biomeColors[i].m_Color[0]; + color[3] = 0xff; + } + } +} biomeColorInitializer; + + + + + +//////////////////////////////////////////////////////////////////////////////// +// BiomeView: + BiomeView::BiomeView(QWidget * parent) : super(parent), m_X(0), @@ -203,11 +313,10 @@ void BiomeView::drawChunk(int a_ChunkX, int a_ChunkZ) centerx += (a_ChunkX - centerchunkx) * chunksize; centery += (a_ChunkZ - centerchunkz) * chunksize; - int srcoffset = 0; uchar * bits = m_Image.bits(); int imgstride = m_Image.bytesPerLine(); - int skipx = 0,skipy = 0; + int skipx = 0, skipy = 0; int blockwidth = chunksize, blockheight = chunksize; // now if we're off the screen we need to crop if (centerx < 0) @@ -236,7 +345,7 @@ void BiomeView::drawChunk(int a_ChunkX, int a_ChunkZ) int imgoffset = centerx * 4 + centery * imgstride; // If the chunk is valid, use its data; otherwise use the empty placeholder: - const uchar * src = m_EmptyChunkImage; + const short * src = m_EmptyChunkBiomes; if (region.get() != nullptr) { int relChunkX = a_ChunkX - regionX * 32; @@ -244,27 +353,44 @@ void BiomeView::drawChunk(int a_ChunkX, int a_ChunkZ) Chunk & chunk = region->getRelChunk(relChunkX, relChunkZ); if (chunk.isValid()) { - src = chunk.getImage(); + src = chunk.getBiomes(); } } - // Blit or scale-blit the image: + // Scale-blit the image: for (int z = skipy; z < blockheight; z++, imgoffset += imgstride) { - srcoffset = floor((double)z / m_Zoom) * 16 * 4; - if (m_Zoom == 1.0) + size_t srcoffset = static_cast(std::floor((double)z / m_Zoom)) * 16; + int imgxoffset = imgoffset; + for (int x = skipx; x < blockwidth; x++) { - memcpy(bits + imgoffset, src + srcoffset + skipx * 4, (blockwidth - skipx) * 4); - } - else - { - int xofs = 0; - for (int x = skipx; x < blockwidth; x++, xofs +=4) + short biome = src[srcoffset + static_cast(std::floor((double)x / m_Zoom))]; + const uchar * color; + if (biome < 0) { - memcpy(bits + imgoffset + xofs, src + srcoffset + (int)floor((double)x / m_Zoom) * 4, 4); + static const uchar emptyBiome1[] = { 0x44, 0x44, 0x44, 0xff }; + static const uchar emptyBiome2[] = { 0x88, 0x88, 0x88, 0xff }; + color = ((x & 8) ^ (z & 8)) ? emptyBiome1 : emptyBiome2; } - } - } + else + { + if (biome * 4 >= ARRAYCOUNT(biomeToColor)) + { + static const uchar errorImage[] = { 0xff, 0x00, 0x00, 0xff }; + color = errorImage; + } + else + { + color = biomeToColor + biome * 4; + } + } + bits[imgxoffset] = color[0]; + bits[imgxoffset + 1] = color[1]; + bits[imgxoffset + 2] = color[2]; + bits[imgxoffset + 3] = color[3]; + imgxoffset += 4; + } // for x + } // for z } diff --git a/Tools/QtBiomeVisualiser/BiomeView.h b/Tools/QtBiomeVisualiser/BiomeView.h index 9901b8f24..cd9c7ead9 100644 --- a/Tools/QtBiomeVisualiser/BiomeView.h +++ b/Tools/QtBiomeVisualiser/BiomeView.h @@ -79,6 +79,9 @@ protected: /** Data used for rendering a chunk that hasn't been loaded yet */ uchar m_EmptyChunkImage[16 * 16 * 4]; + /** Data placeholder for chunks that aren't valid. */ + short m_EmptyChunkBiomes[16 * 16]; + /** Draws the specified chunk into m_Image */ void drawChunk(int a_ChunkX, int a_ChunkZ); diff --git a/Tools/QtBiomeVisualiser/QtChunk.cpp b/Tools/QtBiomeVisualiser/QtChunk.cpp index 031aa3654..f201ef220 100644 --- a/Tools/QtBiomeVisualiser/QtChunk.cpp +++ b/Tools/QtBiomeVisualiser/QtChunk.cpp @@ -5,138 +5,6 @@ -/** Map for converting biome values to colors. Initialized from biomeColors[]. */ -static uchar biomeToColor[256 * 4]; - -/** Map for converting biome values to colors. Used to initialize biomeToColor[].*/ -static struct -{ - EMCSBiome m_Biome; - uchar m_Color[3]; -} biomeColors[] = -{ - { biOcean, { 0x00, 0x00, 0x70 }, }, - { biPlains, { 0x8d, 0xb3, 0x60 }, }, - { biDesert, { 0xfa, 0x94, 0x18 }, }, - { biExtremeHills, { 0x60, 0x60, 0x60 }, }, - { biForest, { 0x05, 0x66, 0x21 }, }, - { biTaiga, { 0x0b, 0x66, 0x59 }, }, - { biSwampland, { 0x2f, 0xff, 0xda }, }, - { biRiver, { 0x30, 0x30, 0xaf }, }, - { biHell, { 0x7f, 0x00, 0x00 }, }, - { biSky, { 0x00, 0x7f, 0xff }, }, - { biFrozenOcean, { 0xa0, 0xa0, 0xdf }, }, - { biFrozenRiver, { 0xa0, 0xa0, 0xff }, }, - { biIcePlains, { 0xff, 0xff, 0xff }, }, - { biIceMountains, { 0xa0, 0xa0, 0xa0 }, }, - { biMushroomIsland, { 0xff, 0x00, 0xff }, }, - { biMushroomShore, { 0xa0, 0x00, 0xff }, }, - { biBeach, { 0xfa, 0xde, 0x55 }, }, - { biDesertHills, { 0xd2, 0x5f, 0x12 }, }, - { biForestHills, { 0x22, 0x55, 0x1c }, }, - { biTaigaHills, { 0x16, 0x39, 0x33 }, }, - { biExtremeHillsEdge, { 0x7f, 0x8f, 0x7f }, }, - { biJungle, { 0x53, 0x7b, 0x09 }, }, - { biJungleHills, { 0x2c, 0x42, 0x05 }, }, - - { biJungleEdge, { 0x62, 0x8b, 0x17 }, }, - { biDeepOcean, { 0x00, 0x00, 0x30 }, }, - { biStoneBeach, { 0xa2, 0xa2, 0x84 }, }, - { biColdBeach, { 0xfa, 0xf0, 0xc0 }, }, - { biBirchForest, { 0x30, 0x74, 0x44 }, }, - { biBirchForestHills, { 0x1f, 0x5f, 0x32 }, }, - { biRoofedForest, { 0x40, 0x51, 0x1a }, }, - { biColdTaiga, { 0x31, 0x55, 0x4a }, }, - { biColdTaigaHills, { 0x59, 0x7d, 0x72 }, }, - { biMegaTaiga, { 0x59, 0x66, 0x51 }, }, - { biMegaTaigaHills, { 0x59, 0x66, 0x59 }, }, - { biExtremeHillsPlus, { 0x50, 0x70, 0x50 }, }, - { biSavanna, { 0xbd, 0xb2, 0x5f }, }, - { biSavannaPlateau, { 0xa7, 0x9d, 0x64 }, }, - { biMesa, { 0xd9, 0x45, 0x15 }, }, - { biMesaPlateauF, { 0xb0, 0x97, 0x65 }, }, - { biMesaPlateau, { 0xca, 0x8c, 0x65 }, }, - - // M variants: - { biSunflowerPlains, { 0xb5, 0xdb, 0x88 }, }, - { biDesertM, { 0xff, 0xbc, 0x40 }, }, - { biExtremeHillsM, { 0x88, 0x88, 0x88 }, }, - { biFlowerForest, { 0x2d, 0x8e, 0x49 }, }, - { biTaigaM, { 0x33, 0x8e, 0x81 }, }, - { biSwamplandM, { 0x07, 0xf9, 0xb2 }, }, - { biIcePlainsSpikes, { 0xb4, 0xdc, 0xdc }, }, - { biJungleM, { 0x7b, 0xa3, 0x31 }, }, - { biJungleEdgeM, { 0x62, 0x8b, 0x17 }, }, - { biBirchForestM, { 0x58, 0x9c, 0x6c }, }, - { biBirchForestHillsM, { 0x47, 0x87, 0x5a }, }, - { biRoofedForestM, { 0x68, 0x79, 0x42 }, }, - { biColdTaigaM, { 0x24, 0x3f, 0x36 }, }, - { biMegaSpruceTaiga, { 0x45, 0x4f, 0x3e }, }, - { biMegaSpruceTaigaHills, { 0x45, 0x4f, 0x4e }, }, - { biExtremeHillsPlusM, { 0x78, 0x98, 0x78 }, }, - { biSavannaM, { 0xe5, 0xda, 0x87 }, }, - { biSavannaPlateauM, { 0xa7, 0x9d, 0x74 }, }, - { biMesaBryce, { 0xff, 0x6d, 0x3d }, }, - { biMesaPlateauFM, { 0xd8, 0xbf, 0x8d }, }, - { biMesaPlateauM, { 0xf2, 0xb4, 0x8d }, }, -} ; - - - - - -static class BiomeColorsInitializer -{ -public: - BiomeColorsInitializer(void) - { - // Reset all colors to gray: - for (size_t i = 0; i < ARRAYCOUNT(biomeToColor); i++) - { - biomeToColor[i] = 0x7f; - } - - // Set known biomes to their colors: - for (size_t i = 0; i < ARRAYCOUNT(biomeColors); i++) - { - uchar * color = &biomeToColor[4 * biomeColors[i].m_Biome]; - color[0] = biomeColors[i].m_Color[2]; - color[1] = biomeColors[i].m_Color[1]; - color[2] = biomeColors[i].m_Color[0]; - color[3] = 0xff; - } - } -} biomeColorInitializer; - - - - - -/** Converts biomes in an array into the chunk image data. */ -static void biomesToImage(const cChunkDef::BiomeMap & a_Biomes, Chunk::Image & a_Image) -{ - // Make sure the two arrays are of the same size, compile-time. - // Note that a_Image is actually 4 items per pixel, so the array is 4 times bigger: - static const char Check1[4 * ARRAYCOUNT(a_Biomes) - ARRAYCOUNT(a_Image) + 1] = {}; - static const char Check2[ARRAYCOUNT(a_Image) - 4 * ARRAYCOUNT(a_Biomes) + 1] = {}; - - // Convert the biomes into color: - for (size_t i = 0; i < ARRAYCOUNT(a_Biomes); i++) - { - a_Image[4 * i + 0] = biomeToColor[4 * a_Biomes[i] + 0]; - a_Image[4 * i + 1] = biomeToColor[4 * a_Biomes[i] + 1]; - a_Image[4 * i + 2] = biomeToColor[4 * a_Biomes[i] + 2]; - a_Image[4 * i + 3] = biomeToColor[4 * a_Biomes[i] + 3]; - } -} - - - - - -//////////////////////////////////////////////////////////////////////////////// -// Chunk: - Chunk::Chunk() : m_IsValid(false) { @@ -146,20 +14,12 @@ Chunk::Chunk() : -const uchar * Chunk::getImage(void) const -{ - ASSERT(m_IsValid); - return m_Image; -} - - - - - void Chunk::setBiomes(const cChunkDef::BiomeMap & a_Biomes) { - memcpy(m_Biomes, a_Biomes, sizeof(m_Biomes)); - renderBiomes(); + for (size_t idx = 0; idx < ARRAYCOUNT(a_Biomes); ++idx) + { + m_Biomes[idx] = static_cast(a_Biomes[idx]); + } m_IsValid = true; } @@ -173,16 +33,7 @@ EMCSBiome Chunk::getBiome(int a_RelX, int a_RelZ) { return biInvalidBiome; } - return cChunkDef::GetBiome(m_Biomes, a_RelX, a_RelZ); -} - - - - - -void Chunk::renderBiomes() -{ - biomesToImage(m_Biomes, m_Image); + return static_cast(m_Biomes[a_RelX + 16 * a_RelZ]); } diff --git a/Tools/QtBiomeVisualiser/QtChunk.h b/Tools/QtBiomeVisualiser/QtChunk.h index 74321577a..d806d18bb 100644 --- a/Tools/QtBiomeVisualiser/QtChunk.h +++ b/Tools/QtBiomeVisualiser/QtChunk.h @@ -18,9 +18,6 @@ public: /** Returns true iff the chunk data is valid - loaded or generated. */ bool isValid(void) const { return m_IsValid; } - /** Returns the image of the chunk's biomes. Assumes that the chunk is valid. */ - const uchar * getImage(void) const; - /** Sets the biomes to m_Biomes and renders them into m_Image. */ void setBiomes(const cChunkDef::BiomeMap & a_Biomes); @@ -28,19 +25,16 @@ public: Coords must be valid inside this chunk. */ EMCSBiome getBiome(int a_RelX, int a_RelZ); + /** Returns the raw biome data for this chunk. */ + const short * getBiomes(void) const { return m_Biomes; } + protected: /** Flag that specifies if the chunk data is valid - loaded or generated. */ bool m_IsValid; - /** Cached rendered image of this chunk's biomes. Updated in render(). */ - Image m_Image; - - /** Biomes comprising the chunk, in the X + 16 * Z ordering. */ - cChunkDef::BiomeMap m_Biomes; - - - /** Renders biomes from m_Biomes into m_Image. */ - void renderBiomes(); + /** Biomes comprising the chunk, in the X + 16 * Z ordering. + Typed as short to save on memory, converted automatically when needed. */ + short m_Biomes[16 * 16]; }; typedef std::shared_ptr ChunkPtr; -- cgit v1.2.3