QtBiomeVisualiser: Added multithreading.
For some reason this makes the UI less responsive.
This commit is contained in:
parent
ddf130f849
commit
d772bc032f
@ -1,6 +1,8 @@
|
|||||||
#include "Globals.h"
|
#include "Globals.h"
|
||||||
#include "ChunkSource.h"
|
#include "ChunkSource.h"
|
||||||
|
#include <QThread>
|
||||||
#include "Generating/BioGen.h"
|
#include "Generating/BioGen.h"
|
||||||
|
#include "inifile/iniFile.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -138,9 +140,12 @@ static void biomesToImage(cChunkDef::BiomeMap & a_Biomes, Chunk::Image & a_Image
|
|||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// BioGenSource:
|
// BioGenSource:
|
||||||
|
|
||||||
BioGenSource::BioGenSource(cBiomeGen * a_BiomeGen) :
|
BioGenSource::BioGenSource(QString a_WorldIniPath) :
|
||||||
m_BiomeGen(a_BiomeGen)
|
m_WorldIniPath(a_WorldIniPath),
|
||||||
|
m_WorldIni(new cIniFile),
|
||||||
|
m_Mtx(QMutex::Recursive)
|
||||||
{
|
{
|
||||||
|
reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -149,11 +154,14 @@ BioGenSource::BioGenSource(cBiomeGen * a_BiomeGen) :
|
|||||||
|
|
||||||
void BioGenSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk)
|
void BioGenSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk)
|
||||||
{
|
{
|
||||||
// TODO: To make use of multicore machines, we need multiple copies of the biomegen
|
cBiomeGenPtr biomeGen;
|
||||||
// Right now we have only one, so we can let only one thread use it (hence the mutex)
|
{
|
||||||
QMutexLocker lock(&m_Mtx);
|
QMutexLocker lock(&m_Mtx);
|
||||||
|
biomeGen = getBiomeGen();
|
||||||
|
}
|
||||||
cChunkDef::BiomeMap biomes;
|
cChunkDef::BiomeMap biomes;
|
||||||
m_BiomeGen->GenBiomes(a_ChunkX, a_ChunkZ, biomes);
|
biomeGen->GenBiomes(a_ChunkX, a_ChunkZ, biomes);
|
||||||
|
releaseBiomeGen(biomeGen);
|
||||||
Chunk::Image img;
|
Chunk::Image img;
|
||||||
biomesToImage(biomes, img);
|
biomesToImage(biomes, img);
|
||||||
a_DestChunk->setImage(img);
|
a_DestChunk->setImage(img);
|
||||||
@ -162,3 +170,57 @@ void BioGenSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChu
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void BioGenSource::reload()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_Mtx);
|
||||||
|
if (!m_WorldIni->ReadFile(m_WorldIniPath.toStdString()))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_AvailableGens.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
cBiomeGenPtr BioGenSource::getBiomeGen()
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_Mtx);
|
||||||
|
|
||||||
|
// Return a generator from the cache, if available:
|
||||||
|
if (!m_AvailableGens.empty())
|
||||||
|
{
|
||||||
|
cBiomeGenPtr res = m_AvailableGens.back();
|
||||||
|
m_AvailableGens.pop_back();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No generator in cache available, create a new one:
|
||||||
|
int seed = m_WorldIni->GetValueSetI("Seed", "Seed", 0);
|
||||||
|
bool unused = false;
|
||||||
|
return cBiomeGenPtr(cBiomeGen::CreateBiomeGen(*m_WorldIni, seed, unused));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void BioGenSource::releaseBiomeGen(cBiomeGenPtr a_BiomeGen)
|
||||||
|
{
|
||||||
|
QMutexLocker lock(&m_Mtx);
|
||||||
|
m_AvailableGens.push_back(a_BiomeGen);
|
||||||
|
|
||||||
|
// Trim the cache if there are too many gens:
|
||||||
|
int wantedNumGens = QThread::idealThreadCount();
|
||||||
|
if (m_AvailableGens.size() > (size_t)(4 * wantedNumGens))
|
||||||
|
{
|
||||||
|
m_AvailableGens.resize(wantedNumGens);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <QString>
|
||||||
#include "Chunk.h"
|
#include "Chunk.h"
|
||||||
|
|
||||||
|
|
||||||
@ -7,6 +8,8 @@
|
|||||||
|
|
||||||
// fwd:
|
// fwd:
|
||||||
class cBiomeGen;
|
class cBiomeGen;
|
||||||
|
typedef std::shared_ptr<cBiomeGen> cBiomeGenPtr;
|
||||||
|
class cIniFile;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -21,6 +24,9 @@ public:
|
|||||||
/** Fills the a_DestChunk with the biomes for the specified coords.
|
/** 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. */
|
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, ChunkPtr 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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -32,15 +38,36 @@ class BioGenSource :
|
|||||||
public ChunkSource
|
public ChunkSource
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/** Constructs a new BioGenSource based on the biome generator given.
|
/** Constructs a new BioGenSource based on the biome generator that is defined in the specified world.ini file. */
|
||||||
Takes ownership of a_BiomeGen */
|
BioGenSource(QString a_WorldIniPath);
|
||||||
BioGenSource(cBiomeGen * a_BiomeGen);
|
|
||||||
|
|
||||||
|
// ChunkSource overrides:
|
||||||
virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override;
|
virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override;
|
||||||
|
virtual void reload(void) override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::shared_ptr<cBiomeGen> m_BiomeGen;
|
/** Path to the world.ini file from which the m_WorldIni is regenerated on reload requests. */
|
||||||
|
QString m_WorldIniPath;
|
||||||
|
|
||||||
|
/** Parsed contents of the world.ini file from which the biome generators are initialized.
|
||||||
|
Locked by m_Mtx to avoid multithreaded access. */
|
||||||
|
std::unique_ptr<cIniFile> m_WorldIni;
|
||||||
|
|
||||||
|
/** List of cBiomeGen instances that are "free" - aren't doing any generating at this moment.
|
||||||
|
Locked by m_Mtx to avoid multithreaded access. */
|
||||||
|
std::vector<cBiomeGenPtr> m_AvailableGens;
|
||||||
|
|
||||||
|
/** Guards m_AvailableGens and m_WorldIni against multithreaded access. */
|
||||||
QMutex m_Mtx;
|
QMutex m_Mtx;
|
||||||
|
|
||||||
|
|
||||||
|
/** Returns a cBiomeGen that can generate a new chunk's biomes.
|
||||||
|
Uses m_AvailableGens as a cache before creating a new generator. */
|
||||||
|
cBiomeGenPtr BioGenSource::getBiomeGen();
|
||||||
|
|
||||||
|
/** Puts the specified BiomeGen back to m_AvailableGens to make it available for next getBiomeGen() request.
|
||||||
|
Truncates m_AvailableGens if there are too many instances in there. */
|
||||||
|
void releaseBiomeGen(cBiomeGenPtr a_BiomeGen);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
@ -52,7 +79,9 @@ class AnvilSource :
|
|||||||
public:
|
public:
|
||||||
// TODO
|
// TODO
|
||||||
|
|
||||||
|
// ChunkSource overrides:
|
||||||
virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override;
|
virtual void getChunkBiomes(int a_ChunkX, int a_ChunkZ, ChunkPtr a_DestChunk) override;
|
||||||
|
virtual void reload() override {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,19 +39,7 @@ MainWindow::~MainWindow()
|
|||||||
void MainWindow::generate()
|
void MainWindow::generate()
|
||||||
{
|
{
|
||||||
QString worldIni = QFileDialog::getOpenFileName(this, tr("Open world.ini"), QString(), tr("world.ini (world.ini)"));
|
QString worldIni = QFileDialog::getOpenFileName(this, tr("Open world.ini"), QString(), tr("world.ini (world.ini)"));
|
||||||
cIniFile ini;
|
m_BiomeView->setChunkSource(std::shared_ptr<BioGenSource>(new BioGenSource(worldIni)));
|
||||||
if (!ini.ReadFile(worldIni.toStdString()))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int seed = ini.GetValueSetI("Seed", "Seed", 0);
|
|
||||||
bool unused = false;
|
|
||||||
cBiomeGen * biomeGen = cBiomeGen::CreateBiomeGen(ini, seed, unused);
|
|
||||||
if (biomeGen == nullptr)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_BiomeView->setChunkSource(std::shared_ptr<BioGenSource>(new BioGenSource(biomeGen)));
|
|
||||||
m_BiomeView->redraw();
|
m_BiomeView->redraw();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user