diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-04-21 04:17:24 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2018-04-21 04:17:29 +0200 |
commit | 424b02609fa34175a4e2aadb95e68b3c9c8dc93c (patch) | |
tree | c6312f04ea70a483fec9e76dcef8ae3ffa5e6424 /src | |
parent | 09a8ade6becca2a71f45ff0db5f4bf6d64afb212 (diff) |
Fix emoji async download drawing
Diffstat (limited to 'src')
-rw-r--r-- | src/Cache.cpp | 15 | ||||
-rw-r--r-- | src/Channel.cpp | 2 | ||||
-rw-r--r-- | src/MessageBoard.cpp | 11 | ||||
-rw-r--r-- | src/MessagePart.cpp | 3 | ||||
-rw-r--r-- | src/ResourceCache.cpp | 2 |
5 files changed, 21 insertions, 12 deletions
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<mutex> 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<mutex> 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<mutex> lock(imageDownloadMutex); ImageByUrlResult result { nullptr, ImageByUrlResult::Type::DOWNLOADING }; imageUrlCache[url] = result; diff --git a/src/Channel.cpp b/src/Channel.cpp index 0fc7ec5..664e395 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -12,7 +12,7 @@ namespace dchat { Message *message = new Message(&localOfflineUser); message->addText(u8"hello, worldåäö1!"); - message->addImage("https://discordemoji.com/assets/emoji/think_fish.png"); + message->addImage("https://discordemoji.com/assets/emoji/playtime.png"); messageBoard.addMessage(message); } diff --git a/src/MessageBoard.cpp b/src/MessageBoard.cpp index acb7be1..575ae5f 100644 --- a/src/MessageBoard.cpp +++ b/src/MessageBoard.cpp @@ -143,6 +143,9 @@ namespace dchat case MessagePart::Type::EMOJI: { MessagePartEmoji *messagePartEmoji = static_cast<MessagePartEmoji*>(messagePart); + // Emoji is dirty when it's created, but render target can become dirty after emoji has been added, so we need to set emoji as dirty then + if(dirty) + messagePartEmoji->dirty = true; auto imageByUrlResult = cache.getImageByUrl(messagePartEmoji->url, 1024 * 512); position.x += 5.0f; if(imageByUrlResult.texture) @@ -152,8 +155,11 @@ namespace dchat sf::Vector2f spriteSize(MessagePartEmoji::getHeightScaled(), MessagePartEmoji::getHeightScaled()); messagePartEmoji->sprite.setScale(spriteSize.x / (float)imageByUrlResult.texture->getSize().x, spriteSize.y / (float)imageByUrlResult.texture->getSize().y); messagePartEmoji->sprite.setPosition(floor(position.x), floor(position.y + MessagePart::getSizeScaled() * 0.5f - MessagePartEmoji::getHeightScaled() * 0.5f)); - if(dirty) + if(messagePartEmoji->dirty) + { + messagePartEmoji->dirty = false; renderTarget->draw(messagePartEmoji->sprite); + } } else { @@ -161,8 +167,7 @@ namespace dchat sf::RectangleShape emojiDownloadRect(sf::Vector2f(MessagePartEmoji::getHeightScaled(), MessagePartEmoji::getHeightScaled())); emojiDownloadRect.setPosition(floor(position.x), floor(position.y + MessagePart::getSizeScaled() * 0.5f - MessagePartEmoji::getHeightScaled() * 0.5f)); emojiDownloadRect.setFillColor(sf::Color::White); - if(dirty) - renderTarget->draw(emojiDownloadRect); + window.draw(emojiDownloadRect); } position.x += MessagePartEmoji::getHeightScaled() + 5.0f; break; diff --git a/src/MessagePart.cpp b/src/MessagePart.cpp index dcef3f3..1bde138 100644 --- a/src/MessagePart.cpp +++ b/src/MessagePart.cpp @@ -37,7 +37,8 @@ namespace dchat MessagePartEmoji::MessagePartEmoji(const string &_url) : MessagePart(Type::EMOJI), - url(_url) + url(_url), + dirty(true) { } diff --git a/src/ResourceCache.cpp b/src/ResourceCache.cpp index 4bdd75d..ab583bb 100644 --- a/src/ResourceCache.cpp +++ b/src/ResourceCache.cpp @@ -27,7 +27,7 @@ namespace dchat return *font; } - const sf::Texture* ResourceCache::getTexture(const string &filepath) + sf::Texture* ResourceCache::getTexture(const string &filepath) { auto it = textures.find(filepath); if(it != textures.end()) |