Image modification tracking in another thread

This commit is contained in:
Christophe Meneboeuf 2021-02-14 23:09:39 +01:00
parent c5408f7bee
commit c76f65b188
3 changed files with 30 additions and 11 deletions

View File

@ -1,6 +1,7 @@
#include <filesystem> #include <filesystem>
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
#include <chrono>
#include <SDL.h> #include <SDL.h>
#include "Picture.h" #include "Picture.h"
@ -23,6 +24,13 @@ namespace RgbToHires
Window::~Window() Window::~Window()
{ {
if (_pThread != nullptr) {
if (_pThread->joinable()) {
_stopFileSurvey.store(true);
_pThread->join();
}
delete _pThread;
}
if (_pTexture != nullptr) { if (_pTexture != nullptr) {
SDL_DestroyTexture(_pTexture); SDL_DestroyTexture(_pTexture);
} }
@ -84,9 +92,6 @@ namespace RgbToHires
void Window::display(const std::string& path, const uint8_t* hiresblob) 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); auto pViewport = ComputeRgbBuffer(hiresblob);
SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(rgba8Bits_t) * 560); SDL_UpdateTexture(_pTexture, nullptr, pViewport->data(), sizeof(rgba8Bits_t) * 560);
@ -95,16 +100,27 @@ namespace RgbToHires
SDL_RenderPresent(_pRenderer); 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 // event loop
SDL_Event e; SDL_Event e;
while (true) while (true)
{ {
if (_isFileModified.load())
if (isImgModified)
{ {
// new modification time
timeModified = std::filesystem::last_write_time(path);
// update hires image // update hires image
const auto imageRgb = Magick::Image{ path }; const auto imageRgb = Magick::Image{ path };
auto imageQuantized = ImageQuantized{ imageRgb }; auto imageQuantized = ImageQuantized{ imageRgb };
@ -124,9 +140,6 @@ namespace RgbToHires
{ {
if (e.type == SDL_QUIT) { break; } if (e.type == SDL_QUIT) { break; }
} }
isImgModified = (timeModified != std::filesystem::last_write_time(path));
} }
} }

View File

@ -18,6 +18,8 @@
#ifndef _DISPLAY_H_ #ifndef _DISPLAY_H_
#define _DISPLAY_H_ #define _DISPLAY_H_
#include <thread>
#include "ImageQuantized.h" #include "ImageQuantized.h"
#include "HiRes.h" #include "HiRes.h"
@ -56,6 +58,10 @@ namespace RgbToHires
bool init(); bool init();
void sdlError(const std::string& msg); 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 Window* S_pInstance;
static const int SCALE = 2; static const int SCALE = 2;

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

After

Width:  |  Height:  |  Size: 4.1 KiB