aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/Cache.hpp2
-rw-r--r--include/MessagePart.hpp1
-rw-r--r--include/ResourceCache.hpp2
-rw-r--r--src/Cache.cpp15
-rw-r--r--src/Channel.cpp2
-rw-r--r--src/MessageBoard.cpp11
-rw-r--r--src/MessagePart.cpp3
-rw-r--r--src/ResourceCache.cpp2
8 files changed, 24 insertions, 14 deletions
diff --git a/include/Cache.hpp b/include/Cache.hpp
index 89abe2c..59d997a 100644
--- a/include/Cache.hpp
+++ b/include/Cache.hpp
@@ -24,7 +24,7 @@ namespace dchat
};
// @texture is null if @type is DOWNLOADING or FAILED_DOWNLOAD
- const sf::Texture *texture;
+ sf::Texture *texture;
Type type;
};
diff --git a/include/MessagePart.hpp b/include/MessagePart.hpp
index cbb0f26..d0a0a03 100644
--- a/include/MessagePart.hpp
+++ b/include/MessagePart.hpp
@@ -49,5 +49,6 @@ namespace dchat
sf::Sprite sprite;
std::string url;
+ bool dirty;
};
}
diff --git a/include/ResourceCache.hpp b/include/ResourceCache.hpp
index 256e5a4..7361862 100644
--- a/include/ResourceCache.hpp
+++ b/include/ResourceCache.hpp
@@ -20,6 +20,6 @@ namespace dchat
static const sf::Font& getFont(const std::string &filepath);
// Throws FailedToLoadResourceException on failure
- static const sf::Texture* getTexture(const std::string &filepath);
+ static sf::Texture* getTexture(const std::string &filepath);
};
}
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())