From 7e2f4b4f7bcc6ffec4e8951fe8e8d5becebcbef0 Mon Sep 17 00:00:00 2001 From: Matus Novak Date: Sun, 25 Oct 2015 22:11:43 +0000 Subject: [PATCH] v1.4.0 - Changed std::atomic to volatile --- release/defaults.json | 22 +++++++-------- source/window.cpp | 17 +++++------ source/window.hpp | 2 +- source/windowExport.cpp | 62 ++++++++++++++++++++++------------------- 4 files changed, 54 insertions(+), 49 deletions(-) diff --git a/release/defaults.json b/release/defaults.json index 8787055..595ea57 100644 --- a/release/defaults.json +++ b/release/defaults.json @@ -1,13 +1,13 @@ { - "bumps-or-normals": false, - "export-attachments": false, - "num-of-threads": -1, - "output-folder": "", - "starmade-folder": "", - "texture-format": -1, - "texture-size": -1, - "use-alpha": false, - "use-diffuse": false, - "use-emissive": false, - "uv-maps": 0 + "bumps-or-normals": true, + "export-attachments": true, + "num-of-threads": 7, + "output-folder": "C:\\", + "starmade-folder": "E:\\Program Files (x86)\\StarMade\\StarMade", + "texture-format": 0, + "texture-size": 2, + "use-alpha": true, + "use-diffuse": true, + "use-emissive": true, + "uv-maps": 2 } \ No newline at end of file diff --git a/source/window.cpp b/source/window.cpp index 108e18e..13cf5c9 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -12,7 +12,7 @@ sm2obj::window::window(){ ///============================================================================= sm2obj::window::~window(){ - exportThreadStop.store(true); + exportThreadStop = true; exportThread.join(); } @@ -91,7 +91,7 @@ void sm2obj::window::setup(){ widgetFrames[6].push_back(radioMatNoBumps = this->addRadio(360, 160, 100, 20, L"None")); widgetFrames[6].push_back(this->addLabel(10, 180, windowSize.x-20, 80, L"Diffuse - The color of the material\nEmissive - Light generated from the material (self illuminated blocks)\nAlpha - The transparency of the material\nBumps/Normals - The detailness of the material")); - + // Frame 07 - System options widgetFrames[7].push_back(this->addLabel(windowSize.x/2-70, windowSize.y/2, 200, 20, L"How many threads to use?")); widgetFrames[7].push_back(comboNumOfThreads = this->addCombo(windowSize.x/2-50, windowSize.y/2+20, 100, 20)); @@ -235,14 +235,14 @@ bool sm2obj::window::checkoutBlueprint(){ ///============================================================================= bool sm2obj::window::checkoutOutputFolder(){ outputFolder = ffw::wstrToAnsi(textInputOutputFolder->getValue()); - + std::fstream test(outputFolder + "\\delete_me.txt", std::ios::trunc | std::ios::out); if(!test){ ffw::logError() << "Please, select correct output folder before continuing!"; ffw::showModalError(this, L"Error!", L"Please, select correct output folder before continuing!"); return false; } - + system(std::string("DEL \"" + outputFolder + "\\delete_me.txt\"").c_str()); return true; } @@ -364,7 +364,7 @@ void sm2obj::window::nextFrame(){ if(!checkoutSystemSettings())return; buttonNext->disable(); } - + if(currentFrame < FRAMES_TOTAL-1){ buttonPrev->enable(); for(const auto& w : widgetFrames[currentFrame]){ @@ -387,7 +387,7 @@ void sm2obj::window::prevFrame(){ if(currentFrame == frame::exportType){ buttonNext->enable(); } - + if(currentFrame > 0){ buttonNext->enable(); for(const auto& w : widgetFrames[currentFrame]){ @@ -419,7 +419,7 @@ void sm2obj::window::hideAll(){ ///============================================================================= void sm2obj::window::windowCloseEvent(){ - exportThreadStop.store(true); + exportThreadStop = true; exportThread.join(); saveUserDefaults(); @@ -530,6 +530,7 @@ void sm2obj::window::widgetEvent(const ffw::uiWidget* Widget){ // Start export if(Widget == buttonObjectPlusTextures || Widget == buttonTexturesOnly || Widget == buttonObjectOnly){ nextFrame(); + exportThreadStop = false; exportThread.start(NULL); buttonPrev->disable(); buttonNext->disable(); @@ -541,7 +542,7 @@ void sm2obj::window::widgetEvent(const ffw::uiWidget* Widget){ this->shouldClose(true); } else { // Wait for thread - exportThreadStop.store(true); + exportThreadStop = true; exportThread.join(); ffw::showModalWarning(this, L"Warning!", L"Export cancelled by user! The exported object/textures may be corrupted!"); this->shouldClose(true); diff --git a/source/window.hpp b/source/window.hpp index 4efcceb..6c44656 100644 --- a/source/window.hpp +++ b/source/window.hpp @@ -127,7 +127,7 @@ namespace sm2obj { int currentFrame; sm2obj::defaults userDefaults; - std::atomic_bool exportThreadStop; + volatile bool exportThreadStop; ffw::thread exportThread; ffw::mutex exportThreadMutex; void* exportThreadFunc(void* Data); diff --git a/source/windowExport.cpp b/source/windowExport.cpp index cdd9000..a916d04 100644 --- a/source/windowExport.cpp +++ b/source/windowExport.cpp @@ -29,8 +29,8 @@ struct progressStruct { static bool recursiveMetaLoader(sm2obj::entityInfoStruct* Output, const std::string& BlueprintFolder, const std::string& BlueprintName, const std::string& SubFolder); static void recursiveMetaDump(sm2obj::entityInfoStruct* Output, int Indent); -static bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& BlueprintFolder, const std::string& OutputTemp, int& FileIndex, progressStruct& Progress, std::atomic_bool& Cancel); -static void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, std::vector& Threads, progressStruct& Progress, const std::vector& BlockInfo, std::atomic_bool& Cancel); +static bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& BlueprintFolder, const std::string& OutputTemp, int& FileIndex, progressStruct& Progress, volatile bool& Cancel); +static void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, std::vector& Threads, progressStruct& Progress, const std::vector& BlockInfo, volatile bool& Cancel); static void* processChunk(void* ThreadInfoPtr); static void generateEntityQueue(sm2obj::entityInfoStruct* Input, std::vector* Output); static bool mergeVertices(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, ffw::file* Output, bool SplitTextures, bool ExportUvMaps); @@ -158,11 +158,15 @@ void* sm2obj::window::exportThreadFunc(void* Data){ labelBlockConfig->setValue(L"Block config: 0/2"); progressBlockConfig->setValue(0); + std::cout << "checking... " << exportThreadStop << std::endl; + // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } + std::cout << "loading..." << std::endl; + // Load block types if(!loadBlockTypes(blockTypes, blockTypesPath)){ ffw::showModalError(this, L"Error!", L"Failed to open block type properties: " + ffw::utf8ToWstr(blockTypesPath)); @@ -176,7 +180,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ progressBlockConfig->setValue(50); // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -189,7 +193,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -216,7 +220,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ progressReadingMeta->setValue(100); // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -241,7 +245,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -266,7 +270,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ threads.clear(); // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -313,7 +317,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ delete chunkRawBuffer; // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -339,7 +343,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ ffw::logSuccess() << "Creating material file..."; // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -348,7 +352,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ ffw::logDebug() << "Exporting altas materials"; ffw::logDebug() << "Creating 3 materials..."; createMaterialAtlas(&mtlOutput, true, exportDiffuse, exportAlpha, exportNormals, exportEmissive, textureExtension); - + } else if(exportMaterials){ ffw::logDebug() << "Exporting block materials"; std::vector materialList; @@ -380,7 +384,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } } @@ -411,7 +415,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -425,7 +429,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -439,7 +443,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -450,7 +454,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ guiProgress.status++; guiProgress.setProgress("Exporting textures"); - + } else if(exportNormals){ exportNormalAtlas(textureFolder + "\\t000_NRM.png", textureSize, textureOutputFolder + "\\Atlas_0_bump." + textureExtension); exportNormalAtlas(textureFolder + "\\t001_NRM.png", textureSize, textureOutputFolder + "\\Atlas_1_bump." + textureExtension); @@ -468,7 +472,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -479,7 +483,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ } // Check for cancelation - if(exportThreadStop.load()){ + if(exportThreadStop){ return NULL; } @@ -487,7 +491,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ exportTileBump(blockInfo, textureFolder, textureSize, textureOutputFolder, textureExtension); guiProgress.status++; guiProgress.setProgress("Exporting textures"); - + } else if(exportNormals){ exportTileNormals(blockInfo, textureFolder, textureSize, textureOutputFolder, textureExtension); guiProgress.status++; @@ -515,7 +519,7 @@ void* sm2obj::window::exportThreadFunc(void* Data){ ///============================================================================= bool recursiveMetaLoader(sm2obj::entityInfoStruct* Output, const std::string& BlueprintFolder, const std::string& BlueprintName, const std::string& SubFolder){ std::string path = BlueprintFolder + SubFolder + "\\meta.smbpm"; - + if(!loadMeta(Output, path, BlueprintName)){ ffw::logError() << "Failed to open meta file from: " << path; return false; @@ -557,7 +561,7 @@ int getTotalEntities(const sm2obj::entityInfoStruct* Input){ } ///============================================================================= -bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& BlueprintFolder, const std::string& OutputTemp, int& FileIndex, progressStruct& Progress, std::atomic_bool& Cancel){ +bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& BlueprintFolder, const std::string& OutputTemp, int& FileIndex, progressStruct& Progress, volatile bool& Cancel){ std::vector allFiles; // Open target directory @@ -672,8 +676,8 @@ bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& Bl Progress.setProgress("Loading chunks"); for(auto& item : Input->attachments){ - if(Cancel.load())return true; - + if(Cancel)return true; + if(!recursiveChunkLoader(&item, BlueprintFolder, OutputTemp, FileIndex, Progress, Cancel))return false; } @@ -682,7 +686,7 @@ bool recursiveChunkLoader(sm2obj::entityInfoStruct* Input, const std::string& Bl ///============================================================================= -void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, std::vector& Threads, progressStruct& Progress, const std::vector& BlockInfo, std::atomic_bool& Cancel){ +void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& InputOutputTemp, std::vector& Threads, progressStruct& Progress, const std::vector& BlockInfo, volatile bool& Cancel){ ffw::logDebug() << "Exporting: " << Input->name << " from: " << Input->path; // export chunks one by one @@ -717,14 +721,14 @@ void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& In found = true; break; } - if(Cancel.load())break; + if(Cancel)break; } - if(Cancel.load())break; + if(Cancel)break; // An empty thread was not found, wait and then try again if(found)break; ffw::usleep(100000); } - if(Cancel.load())break; + if(Cancel)break; } ffw::logDebug() << "Waiting for threads..."; @@ -734,7 +738,7 @@ void recursiveChunkExport(sm2obj::entityInfoStruct* Input, const std::string& In } for(auto& item : Input->attachments){ - if(Cancel.load())break; + if(Cancel)break; recursiveChunkExport(&item, InputOutputTemp, Threads, Progress, BlockInfo, Cancel); } } @@ -968,4 +972,4 @@ bool mergeVertices(sm2obj::entityInfoStruct* Input, const std::string& InputOutp } return true; -} \ No newline at end of file +}