From ca8171d80ceccf1538f2fef9ab2c96dc7192f9d1 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 27 Sep 2020 01:49:50 +0200 Subject: Image continuous: load as image in seperate thread instead of string, fix a bit of stuttering --- TODO | 3 ++- include/ImageViewer.hpp | 2 +- src/ImageViewer.cpp | 46 +++++++++++++++++++++------------------------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/TODO b/TODO index 41a5971..d15119e 100644 --- a/TODO +++ b/TODO @@ -32,4 +32,5 @@ Press home/end to scroll to top/bottom. Scrolling past page causes the page to jump up and down very fast because the new thumbnail is loaded. Fix this somehow. In youtube this can be fixed by setting the thumbnail image fallback size to the same size and thumbnail images, but that doesn't work for matrix with different image sizes! Add setting to disable sending typing events to the server (matrix). Support emoji (mainly for matrix), by readding Text code from dchat. Also do the same but for inline images, text editing and url colors and clicking (also clicking on inline images). -Also take code from dchat to support gifs (inline in text). \ No newline at end of file +Also take code from dchat to support gifs (inline in text). +Fix page jumping up/down in image continuous mode when scrolling past images that fail to load / scrolling fast. \ No newline at end of file diff --git a/include/ImageViewer.hpp b/include/ImageViewer.hpp index 0ca64e9..fe0b6f3 100644 --- a/include/ImageViewer.hpp +++ b/include/ImageViewer.hpp @@ -26,7 +26,7 @@ namespace QuickMedia { sf::Texture texture; sf::Sprite sprite; ImageStatus image_status; - std::unique_ptr image_data_str; + std::unique_ptr image; bool visible_on_screen; }; diff --git a/src/ImageViewer.cpp b/src/ImageViewer.cpp index 97ef7c9..416f27f 100644 --- a/src/ImageViewer.cpp +++ b/src/ImageViewer.cpp @@ -40,9 +40,9 @@ namespace QuickMedia { assert(!loading_image); loading_image = true; image_loader_thread = std::thread([this, image_data, path]() { - auto image_data_str = std::make_unique(); - if(file_get_content(path, *image_data_str) == 0) { - image_data->image_data_str = std::move(image_data_str); + auto image = std::make_unique(); + if(image->loadFromFile(path.data)) { + image_data->image = std::move(image); image_data->image_status = ImageStatus::LOADED; } else { image_data->image_status = ImageStatus::FAILED_TO_LOAD; @@ -57,18 +57,6 @@ namespace QuickMedia { return false; std::shared_ptr &page_image_data = image_data[page]; - if(page_image_data && page_image_data->image_status == ImageStatus::LOADED) { - if(page_image_data->texture.loadFromMemory(page_image_data->image_data_str->data(), page_image_data->image_data_str->size())) { - page_image_data->sprite.setTexture(page_image_data->texture, true); - page_size[page].size = get_page_size(page); - page_size[page].loaded = true; - page_image_data->image_status = ImageStatus::APPLIED_TO_TEXTURE; - } else { - page_image_data->image_status = ImageStatus::FAILED_TO_LOAD; - page_image_data->image_data_str.reset(); - } - } - const sf::Vector2 image_size = get_page_size(page); sf::Vector2 render_pos(std::floor(window_size.x * 0.5 - image_size.x * 0.5), - image_size.y * 0.5 + scroll + offset_y); if(render_pos.y + image_size.y <= 0.0 || render_pos.y >= window_size.y) { @@ -145,23 +133,15 @@ namespace QuickMedia { Path image_path = chapter_cache_dir; image_path.join(page_str); - // TODO: Make image loading asynchronous if(get_file_type(image_path) == FileType::REGULAR) { fprintf(stderr, "ImageViewer: Loaded page %d\n", 1 + page); page_image_data = std::make_shared(); page_image_data->visible_on_screen = true; - std::string image_data; - if(file_get_content(image_path, image_data) == 0) { - page_image_data->image_status = ImageStatus::WAITING; - page_image_data->texture.setSmooth(true); - } else { - show_notification("Manga", "Failed to load image for page " + page_str + ". Image filepath: " + image_path.data, Urgency::CRITICAL); - page_image_data->image_status = ImageStatus::FAILED_TO_LOAD; - } + page_image_data->image_status = ImageStatus::WAITING; + page_image_data->texture.setSmooth(true); } - } return true; @@ -273,6 +253,22 @@ namespace QuickMedia { min_page_center_dist = 9999999.0; page_closest_to_center = -1; + int page_i = 0; + for(auto &page_data : image_data) { + if(page_data && page_data->image_status == ImageStatus::LOADED) { + if(page_data->texture.loadFromImage(*page_data->image)) { + page_data->sprite.setTexture(page_data->texture, true); + page_size[page_i].size = get_page_size(page_i); + page_size[page_i].loaded = true; + page_data->image_status = ImageStatus::APPLIED_TO_TEXTURE; + } else { + page_data->image_status = ImageStatus::FAILED_TO_LOAD; + } + page_data->image.reset(); + } + ++page_i; + } + const sf::Vector2 selected_page_size = get_page_size(current_page); render_page(window, current_page, window_size.y*0.5); //if(!focused_page_rendered) -- cgit v1.2.3