From 424b02609fa34175a4e2aadb95e68b3c9c8dc93c Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 21 Apr 2018 04:17:24 +0200 Subject: Fix emoji async download drawing --- src/Cache.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/Cache.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index 7e3272a..ba57d4c 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -71,6 +71,7 @@ namespace dchat if(it->process->try_get_exit_status(exitStatus)) { bool failed = exitStatus != 0; + ImageByUrlResult &imageByUrlResult = imageUrlCache[it->url]; if(!failed) { @@ -80,10 +81,10 @@ namespace dchat try { - const sf::Texture *texture = ResourceCache::getTexture(filepath.string()); - ImageByUrlResult &imageByUrlResult = imageUrlCache[it->url]; + sf::Texture *texture = ResourceCache::getTexture(filepath.string()); imageByUrlResult.texture = texture; imageByUrlResult.type = ImageByUrlResult::Type::CACHED; + printf("Image downloaded from url: %s, texture: %u\n", it->url.c_str(), texture); } catch(FailedToLoadResourceException &e) { @@ -94,7 +95,7 @@ namespace dchat if(failed) { - imageUrlCache[it->url].type = ImageByUrlResult::Type::FAILED_DOWNLOAD; + imageByUrlResult.type = ImageByUrlResult::Type::FAILED_DOWNLOAD; fprintf(stderr, "Image download failed for url: %s\n", it->url.c_str()); } @@ -107,6 +108,8 @@ namespace dchat while(imageDownloadProcesses.empty()) this_thread::sleep_for(chrono::milliseconds(20)); + + this_thread::sleep_for(chrono::milliseconds(20)); } }); downloadWaitThread.detach(); @@ -114,6 +117,7 @@ namespace dchat const ImageByUrlResult Cache::getImageByUrl(const string &url, int downloadLimitBytes) { + lock_guard lock(imageDownloadMutex); auto it = imageUrlCache.find(url); if(it != imageUrlCache.end()) return it->second; @@ -128,10 +132,10 @@ namespace dchat { try { - const sf::Texture *texture = ResourceCache::getTexture(filepath.string()); - lock_guard lock(imageDownloadMutex); + sf::Texture *texture = ResourceCache::getTexture(filepath.string()); ImageByUrlResult result { texture, ImageByUrlResult::Type::CACHED }; imageUrlCache[url] = result; + printf("Loading image from file cache: %s\n", url.c_str()); return result; } catch(FailedToLoadResourceException &e) @@ -140,7 +144,6 @@ namespace dchat } } - lock_guard lock(imageDownloadMutex); ImageByUrlResult result { nullptr, ImageByUrlResult::Type::DOWNLOADING }; imageUrlCache[url] = result; -- cgit v1.2.3