Index: source/graphics/MapGenerator.h =================================================================== --- source/graphics/MapGenerator.h +++ source/graphics/MapGenerator.h @@ -23,8 +23,8 @@ #include "ps/TemplateLoader.h" #include "scriptinterface/ScriptInterface.h" +#include #include - #include #include #include @@ -59,7 +59,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: @@ -102,7 +102,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: @@ -206,7 +206,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 @@ -102,7 +102,6 @@ if (!self->Run() || self->m_Progress > 0) { // Don't leave progress in an unknown state, if generator failed, set it to -1 - std::lock_guard lock(self->m_WorkerMutex); self->m_Progress = -1; } @@ -214,9 +213,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; } @@ -241,13 +239,14 @@ void CMapGeneratorWorker::SetProgress(int progress) { - // Copy data - std::lock_guard lock(m_WorkerMutex); + if (progress < m_Progress) + { + LOGWARNING("The random map script tried to reduce the loading progress from %d to %d", previousProgress, 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) @@ -417,7 +416,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" @@ -1308,16 +1307,7 @@ 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; };