From c76f65b1880f6a077aefaf8349a8bbdfc17d9226 Mon Sep 17 00:00:00 2001 From: Christophe Meneboeuf Date: Sun, 14 Feb 2021 23:09:39 +0100 Subject: [PATCH] Image modification tracking in another thread --- Rgb2Hires_PC/src/Display.cpp | 35 ++++++++++++++++++++++++----------- Rgb2Hires_PC/src/Display.h | 6 ++++++ Rgb2Hires_PC/test.png | Bin 4246 -> 4246 bytes 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Rgb2Hires_PC/src/Display.cpp b/Rgb2Hires_PC/src/Display.cpp index b235f62..e7b9ecd 100644 --- a/Rgb2Hires_PC/src/Display.cpp +++ b/Rgb2Hires_PC/src/Display.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "Picture.h" @@ -23,6 +24,13 @@ namespace RgbToHires Window::~Window() { + if (_pThread != nullptr) { + if (_pThread->joinable()) { + _stopFileSurvey.store(true); + _pThread->join(); + } + delete _pThread; + } if (_pTexture != nullptr) { SDL_DestroyTexture(_pTexture); } @@ -84,9 +92,6 @@ namespace RgbToHires void Window::display(const std::string& path, const uint8_t* hiresblob) { - bool isImgModified = false; - auto timeModified = std::filesystem::last_write_time(path); - { auto pViewport = ComputeRgbBuffer(hiresblob); SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(rgba8Bits_t) * 560); @@ -95,16 +100,27 @@ namespace RgbToHires SDL_RenderPresent(_pRenderer); } + // launch thread to survey file modifications + _pThread = new std::thread([this, path] { // freed in ~Window() + auto timeModified = std::filesystem::last_write_time(path); + while (!this->_stopFileSurvey.load()) + { + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + const bool isImgModified(timeModified != std::filesystem::last_write_time(path)); + if (isImgModified) { + timeModified = std::filesystem::last_write_time(path); + this->_isFileModified.store(isImgModified); + } + } + }); + _pThread->detach(); + // event loop SDL_Event e; while (true) { - - if (isImgModified) + if (_isFileModified.load()) { - // new modification time - timeModified = std::filesystem::last_write_time(path); - // update hires image const auto imageRgb = Magick::Image{ path }; auto imageQuantized = ImageQuantized{ imageRgb }; @@ -124,9 +140,6 @@ namespace RgbToHires { if (e.type == SDL_QUIT) { break; } } - - isImgModified = (timeModified != std::filesystem::last_write_time(path)); - } } diff --git a/Rgb2Hires_PC/src/Display.h b/Rgb2Hires_PC/src/Display.h index 1d67dff..c06a9ef 100644 --- a/Rgb2Hires_PC/src/Display.h +++ b/Rgb2Hires_PC/src/Display.h @@ -18,6 +18,8 @@ #ifndef _DISPLAY_H_ #define _DISPLAY_H_ +#include + #include "ImageQuantized.h" #include "HiRes.h" @@ -56,6 +58,10 @@ namespace RgbToHires bool init(); void sdlError(const std::string& msg); + std::thread* _pThread = nullptr; //< to survey filechange + std::atomic_bool _isFileModified = false; + std::atomic_bool _stopFileSurvey = false; + static Window* S_pInstance; static const int SCALE = 2; diff --git a/Rgb2Hires_PC/test.png b/Rgb2Hires_PC/test.png index d265caccd18a10d8b95e350f926d65bea405f46c..20c0feed398ccba9fcda84a47af8e4f32a8a3b87 100644 GIT binary patch delta 173 zcmbQHI8AYa0h==8X{A{wHkwUf;xIC{GO(~RHkf>lNfp8Q%~YvkYHpHjnw(;(n_`rj zrki4FVyJ6jYM!WTlw_G~oMvchn3QO^c?NSDlPfVQl@y9gT+2$V$}>{)Y?X`*j0|-R ejC2i6Lf{VA&0;gTo-Lo{=A~^vH-BPV!U+K9$1}YE delta 173 zcmbQHI8AYa0h}CX>%GsUkSPnJQJ%k}NDz4GfKR)69&M zbrTItOm&lz3`}&>5>w66Qq4>Z%}tUw&tOhtawSHkl0tEbYgvg^c}8lUt&)*}k)f`E dk*=Xh2;2d?S!^cPv*oiK*sz6f^Cz|?oB$+lF^&KL