aboutsummaryrefslogtreecommitdiff
path: root/include/AsyncImageLoader.hpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-05-14 16:40:49 +0200
committerdec05eba <dec05eba@protonmail.com>2021-05-14 17:00:06 +0200
commit9dfef8c22987c12a6aad47a8913e60943d8578e7 (patch)
tree5d167931ec4496d9509c03679516803b5c570315 /include/AsyncImageLoader.hpp
parentacb6ac0a04e800a79876908fd1fdb98dc7e93678 (diff)
Move thumbnail loading/unloading to AsyncImageLoader
Diffstat (limited to 'include/AsyncImageLoader.hpp')
-rw-r--r--include/AsyncImageLoader.hpp22
1 files changed, 17 insertions, 5 deletions
diff --git a/include/AsyncImageLoader.hpp b/include/AsyncImageLoader.hpp
index 0384a71..7fe7c07 100644
--- a/include/AsyncImageLoader.hpp
+++ b/include/AsyncImageLoader.hpp
@@ -9,6 +9,7 @@
#include <string>
#include <memory>
#include <thread>
+#include <unordered_map>
namespace QuickMedia {
enum class LoadingState {
@@ -19,11 +20,10 @@ namespace QuickMedia {
};
struct ThumbnailData {
- bool referenced = false;
LoadingState loading_state = LoadingState::NOT_LOADED;
sf::Texture texture;
std::unique_ptr<sf::Image> image; // Set in another thread. This should be .reset after loading it into |texture|, to save memory
- sf::Clock texture_applied_time;
+ size_t counter = 0;
};
// This function is async
@@ -34,10 +34,16 @@ namespace QuickMedia {
class AsyncImageLoader {
public:
static AsyncImageLoader& get_instance();
- // Returns false if the image loader is already loading an image. In that case, this function should be called again later.
+
+ // Never returns nullptr. Instead check the |loading_state| of the thumbnail data to see if it has finished loading.
+ // This function should be called every frame for the objects that need to display this thumbnail, otherwise it can be unloaded.
// set |resize_target_size| to {0, 0} to disable resizing.
// Note: this method is not thread-safe
- void load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, std::shared_ptr<ThumbnailData> thumbnail_data);
+ std::shared_ptr<ThumbnailData> get_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size);
+
+ // Note: this should only be called once every frame.
+ // Note: this method is not thread-safe
+ void update();
private:
AsyncImageLoader();
~AsyncImageLoader();
@@ -52,6 +58,10 @@ namespace QuickMedia {
sf::Vector2i resize_target_size;
};
+ // set |resize_target_size| to {0, 0} to disable resizing.
+ // Note: this method is not thread-safe
+ void load_thumbnail(const std::string &url, bool local, sf::Vector2i resize_target_size, std::shared_ptr<ThumbnailData> thumbnail_data);
+
// Returns -1 if all threads are busy
int get_free_load_index() const;
private:
@@ -60,5 +70,7 @@ namespace QuickMedia {
std::thread download_image_thread[NUM_IMAGE_LOAD_THREADS];
std::thread load_image_thread;
MessageQueue<ThumbnailLoadData> image_load_queue;
+ std::unordered_map<std::string, std::shared_ptr<ThumbnailData>> thumbnails;
+ size_t counter = 0;
};
-} \ No newline at end of file
+}