Index: source/graphics/MapGenerator.h =================================================================== --- source/graphics/MapGenerator.h +++ source/graphics/MapGenerator.h @@ -23,6 +23,7 @@ #include "ps/TemplateLoader.h" #include "scriptinterface/StructuredClone.h" +#include #include #include #include @@ -57,7 +58,7 @@ * * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error */ - int GetProgress(); + int GetProgress() const; /** * Get random map data, according to this format: @@ -100,7 +101,7 @@ * * @return Progress percentage 1-100 if active, 0 when finished, or -1 on error */ - int GetProgress(); + int GetProgress() const; /** * Get random map data, according to this format: @@ -199,7 +200,7 @@ /** * Current map generation progress. */ - int m_Progress; + std::atomic m_Progress; /** * Provides the script context. Index: source/graphics/MapGenerator.cpp =================================================================== --- source/graphics/MapGenerator.cpp +++ source/graphics/MapGenerator.cpp @@ -97,7 +97,6 @@ if (!Run() || m_Progress > 0) { // Don't leave progress in an unknown state, if generator failed, set it to -1 - std::lock_guard lock(m_WorkerMutex); m_Progress = -1; } @@ -210,9 +209,8 @@ #undef REGISTER_MAPGEN_FUNC #undef REGISTER_MAPGEN_FUNC_NAME -int CMapGeneratorWorker::GetProgress() +int CMapGeneratorWorker::GetProgress() const { - std::lock_guard lock(m_WorkerMutex); return m_Progress; } @@ -237,13 +235,15 @@ void CMapGeneratorWorker::SetProgress(int progress) { - // Copy data - std::lock_guard lock(m_WorkerMutex); + const int currentProgress = m_Progress; + if (progress < currentProgress) + { + LOGWARNING("The random map script tried to reduce the loading progress from %d to %d", currentProgress, progress); + return; + } - if (progress >= m_Progress) - m_Progress = progress; - else - LOGWARNING("The random map script tried to reduce the loading progress from %d to %d", m_Progress, progress); + const int previousProgress = m_Progress.exchange(progress); + ENSURE(previousProgress <= progress); } CParamNode CMapGeneratorWorker::GetTemplate(const std::string& templateName) @@ -413,7 +413,7 @@ m_Worker->Initialize(scriptFile, settings); } -int CMapGenerator::GetProgress() +int CMapGenerator::GetProgress() const { return m_Worker->GetProgress(); } Index: source/graphics/MapReader.cpp =================================================================== --- source/graphics/MapReader.cpp +++ source/graphics/MapReader.cpp @@ -29,7 +29,6 @@ #include "graphics/TerrainTextureEntry.h" #include "graphics/TerrainTextureManager.h" #include "lib/timer.h" -#include "lib/external_libraries/libsdl.h" #include "maths/MathUtil.h" #include "ps/CLogger.h" #include "ps/Loader.h" @@ -1313,14 +1312,6 @@ m_MapData.init(rq.cx, data); } } - else - { - // Still working - - // Sleep for a while, slowing down the rendering thread - // to allow more CPU for the map generator thread - SDL_Delay(100); - } // return progress return progress;