From e37a174979059a97c342c51b741f4f89609830c4 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 16 May 2021 20:25:15 +0200 Subject: Use vertex buffer for rendering text --- TODO | 2 +- include/Body.hpp | 1 + include/Text.hpp | 2 +- src/Body.cpp | 15 ++++++++++++--- src/Text.cpp | 38 +++++++++++++++++++++++--------------- 5 files changed, 38 insertions(+), 20 deletions(-) diff --git a/TODO b/TODO index dcd998c..af24b76 100644 --- a/TODO +++ b/TODO @@ -13,7 +13,7 @@ Make code blocks on matrix and 4chan use monospace and have a background of a di Allow deleting watch history with delete key (and show confirmation). Add navigation to nyaa.si submitter torrents. Create a large texture and add downloaded images to it. This will save memory usage because sfml has to use power of two textures (and so does opengl internally) for textures, so if you have multiple textures they will use more memory than one large texture with the same texture data. -Use fallback cjk font for regular sf::Text as well (search, tabs, chapter name when viewing a page, path in file-manager, etc). +Use fallback cjk font for regular sf::Text as well (search, tabs, chapter name when viewing a page, path in file-manager, etc). That should also result in better performance on pinephone. Fix some japanese fonts not rendering (half width alphanumeric?). Extract thumbnail from images that are being downloaded, while its downloading and show that while the full image is downloading (upscaled, or with blurhash). Add setting to disable sending typing events to the server (matrix). diff --git a/include/Body.hpp b/include/Body.hpp index b5ca210..3106cc3 100644 --- a/include/Body.hpp +++ b/include/Body.hpp @@ -317,5 +317,6 @@ namespace QuickMedia { std::shared_ptr clicked_body_item = nullptr; RoundedRectangle item_background; RoundedRectangle reaction_background; + bool render_selected_item_bg = true; }; } \ No newline at end of file diff --git a/include/Text.hpp b/include/Text.hpp index fbd850a..989edbb 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -147,7 +147,7 @@ namespace QuickMedia // 2: CJK // 3: Emoji std::array vertices; - //sf::VertexBuffer vertex_buffers[3]; + std::array vertex_buffers; float maxWidth; sf::Vector2f position; sf::Color color; diff --git a/src/Body.cpp b/src/Body.cpp index 803cece..ffafb90 100644 --- a/src/Body.cpp +++ b/src/Body.cpp @@ -141,6 +141,7 @@ namespace QuickMedia { thumbnail_max_size.y = 141; sf::Vector2f loading_icon_size(loading_icon.getTexture()->getSize().x, loading_icon.getTexture()->getSize().y); loading_icon.setOrigin(loading_icon_size.x * 0.5f, loading_icon_size.y * 0.5f); + render_selected_item_bg = !is_touch_enabled(); } Body::~Body() { @@ -398,31 +399,37 @@ namespace QuickMedia { bool Body::on_event(const sf::RenderWindow &window, const sf::Event &event, bool keyboard_navigation) { if(keyboard_navigation && event.type == sf::Event::KeyPressed && !event.key.alt) { if(event.key.code == sf::Keyboard::Up || (event.key.control && event.key.code == sf::Keyboard::K)) { + render_selected_item_bg = true; bool top_reached = select_previous_item(); if(!top_reached && on_top_reached) on_top_reached(); return true; } else if(event.key.code == sf::Keyboard::Down || (event.key.control && event.key.code == sf::Keyboard::J)) { + render_selected_item_bg = true; bool bottom_reached = select_next_item(); if(!bottom_reached && on_bottom_reached) on_bottom_reached(); return true; } else if(event.key.code == sf::Keyboard::Home) { + render_selected_item_bg = true; select_first_item(); if(on_top_reached) on_top_reached(); return true; } else if(event.key.code == sf::Keyboard::End) { + render_selected_item_bg = true; select_last_item(); if(on_bottom_reached) on_bottom_reached(); return true; } else if(event.key.code == sf::Keyboard::PageUp) { + render_selected_item_bg = true; bool top_reached = select_previous_page(); if(!top_reached && on_top_reached) on_top_reached(); return true; } else if(event.key.code == sf::Keyboard::PageDown) { + render_selected_item_bg = true; bool bottom_reached = select_next_page(); if(!bottom_reached && on_bottom_reached) on_bottom_reached(); @@ -445,6 +452,7 @@ namespace QuickMedia { mouse_click_pos = mouse_pos; mouse_press_pixels_moved_abs = 0.0; has_scrolled_with_input = true; + render_selected_item_bg = false; return true; } else if(event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left && mouse_left_pressed) { mouse_left_pressed = false; @@ -456,6 +464,7 @@ namespace QuickMedia { mouse_press_pixels_moved_abs += std::sqrt(mouse_pos_diff.x*mouse_pos_diff.x + mouse_pos_diff.y*mouse_pos_diff.y); mouse_pos_raw.x = event.mouseMove.x; mouse_pos_raw.y = event.mouseMove.y; + render_selected_item_bg = false; return true; } @@ -1093,7 +1102,7 @@ namespace QuickMedia { void Body::draw_item(sf::RenderWindow &window, BodyItem *item, const sf::Vector2f &pos, const sf::Vector2f &size, const float item_height, const int item_index, const Json::Value &content_progress, bool include_embedded_item, bool merge_with_previous) { sf::Vector2i thumbnail_size = get_item_thumbnail_size(item); std::shared_ptr item_thumbnail; - if(draw_thumbnails && !item->thumbnail_url.empty()) + if(draw_thumbnails && !merge_with_previous && !item->thumbnail_url.empty()) item_thumbnail = AsyncImageLoader::get_instance().get_thumbnail(item->thumbnail_url, item->thumbnail_is_local, thumbnail_size); if(body_item_render_callback && include_embedded_item) @@ -1116,7 +1125,7 @@ namespace QuickMedia { //item_separator.setPosition(item_pos + sf::Vector2f(10.0f, std::floor(item_height + spacing_y * 0.5f))); //window.draw(item_separator); - if(item_index == selected_item) { + if(render_selected_item_bg && item_index == selected_item) { item_background.set_position(item_pos); item_background.set_size(sf::Vector2f(size.x, item_height)); item_background.draw(window); @@ -1134,7 +1143,7 @@ namespace QuickMedia { auto image_scale = get_ratio(image_size_f, new_image_size); image.setScale(image_scale); image.setPosition(item_pos + sf::Vector2f(image_padding_x, padding_y)); - if(thumbnail_mask_shader && item->thumbnail_mask_type == ThumbnailMaskType::CIRCLE) { + if(!is_touch_enabled() && thumbnail_mask_shader && item->thumbnail_mask_type == ThumbnailMaskType::CIRCLE) { thumbnail_mask_shader->setUniform("resolution", new_image_size); window.draw(image, thumbnail_mask_shader); } else { diff --git a/src/Text.cpp b/src/Text.cpp index 2940520..23dfb5b 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -49,10 +49,9 @@ namespace QuickMedia vertices[0].setPrimitiveType(sf::PrimitiveType::Triangles); vertices[1].setPrimitiveType(sf::PrimitiveType::Triangles); vertices[2].setPrimitiveType(sf::PrimitiveType::Triangles); - /*for(int i = 0; i < 3; ++i) { + for(int i = 0; i < 3; ++i) { vertex_buffers[i] = sf::VertexBuffer(sf::PrimitiveType::Triangles, sf::VertexBuffer::Static); - vertex_buffers[i].create(0); - }*/ + } setString(std::move(_str)); } @@ -346,9 +345,10 @@ namespace QuickMedia dirty = false; vertices_linear.clear(); - vertices[0].clear(); - vertices[1].clear(); - vertices[2].clear(); + for(int i = 0; i < 3; ++i) { + vertices[i].clear(); + vertices[i].resize(0); + } boundingBox = sf::FloatRect(); sf::Font *latin_font; @@ -584,13 +584,21 @@ namespace QuickMedia } boundingBox.height = num_lines * line_height; - /*for(int i = 0; i < 3; ++i) { + // TODO: Clear |vertices| somehow even with editable text + for(int i = 0; i < 3; ++i) { + vertex_buffers[i].create(vertices[i].getVertexCount()); vertex_buffers[i].update(&vertices[i][0], vertices[i].getVertexCount(), 0); - }*/ + } //url_ranges.clear(); - if(!editable) + if(!editable) { + for(int i = 0; i < 3; ++i) { + vertices[i].clear(); + vertices[i].resize(0); + } vertices_linear.clear(); + vertices_linear.resize(0); + } } void Text::updateCaret() @@ -895,23 +903,23 @@ namespace QuickMedia const FontLoader::FontType font_types[] = { latin_font_type, FontLoader::FontType::CJK }; for(size_t i = 0; i < 2; ++i) { - if(vertices[i].getVertexCount() == 0) + if(vertex_buffers[i].getVertexCount() == 0) continue; sf::Font *font = FontLoader::get_font(font_types[i]); sf::RenderStates states; states.transform.translate(pos); states.texture = &font->getTexture(characterSize); - //target.draw(vertex_buffers[i], states); - target.draw(vertices[i], states); + target.draw(vertex_buffers[i], states); + //target.draw(vertices[i], states); } - if(vertices[2].getVertexCount() > 0) { + if(vertex_buffers[2].getVertexCount() > 0) { sf::RenderStates states; states.transform.translate(pos); states.texture = TextureLoader::get_texture("images/emoji.png"); - //target.draw(vertex_buffers[2], states); - target.draw(vertices[2], states); + target.draw(vertex_buffers[2], states); + //target.draw(vertices[2], states); } if(!editable) return true; -- cgit v1.2.3