From a84ec50045bc92680855826fde70fb74769f8883 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 3 May 2025 12:03:45 -0500 Subject: [PATCH] Avoid font corruption by pre-enlarging font atlas --- src/game/boe.main.cpp | 4 ++++ src/game/boe.text.cpp | 10 ++++++++-- src/gfx/render_text.cpp | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/game/boe.main.cpp b/src/game/boe.main.cpp index 413468df..dddbbf7a 100644 --- a/src/game/boe.main.cpp +++ b/src/game/boe.main.cpp @@ -34,6 +34,7 @@ #include "gfx/tiling.hpp" #include "mathutil.hpp" #include "fileio/fileio.hpp" +#include "fileio/resmgr/res_font.hpp" #include "dialogxml/dialogs/strdlog.hpp" #include "dialogxml/dialogs/choicedlog.hpp" #include "dialogxml/widgets/scrollbar.hpp" @@ -1265,6 +1266,9 @@ void handle_events() { if(changed_display_mode) { changed_display_mode = false; + // Force reload fonts for possible new UI scale + ResMgr::fonts.drain(); + adjust_window_mode(); init_mini_map(); } diff --git a/src/game/boe.text.cpp b/src/game/boe.text.cpp index 6e6f123a..672070e2 100644 --- a/src/game/boe.text.cpp +++ b/src/game/boe.text.cpp @@ -1108,6 +1108,11 @@ void print_buf () { int num_lines_visible = -line_offset; location moveTo; + TextStyle line_style; + line_style.font = FONT_PLAIN; + line_style.colour = Colours::BLACK; + line_style.pointSize = 12; + while(num_lines_visible < LINES_IN_TEXT_WIN){ std::string message = text_buffer[message_idx].message; int indent = message.find_first_not_of(' '); @@ -1122,8 +1127,9 @@ void print_buf () { } moveTo = location(4, 1 + 12 * (LINES_IN_TEXT_WIN + line_offset - num_lines_total - text_buffer[message_idx].line_count)); - sf::Text text(message, *ResMgr::fonts.get("plain"), 12 * get_ui_scale()); - text.setColor(Colours::BLACK); + sf::Text text; + line_style.applyTo(text, get_ui_scale()); + text.setString(message); text.setPosition(moveTo); draw_scale_aware_text(text_area_gworld(), text); } diff --git a/src/gfx/render_text.cpp b/src/gfx/render_text.cpp index 02ee3a94..c0c3341c 100644 --- a/src/gfx/render_text.cpp +++ b/src/gfx/render_text.cpp @@ -29,8 +29,23 @@ sf::Font& get_font_rsrc(eFont font) { } void TextStyle::applyTo(sf::Text& text, double scale) const { - text.setFont(get_font_rsrc(font)); - text.setCharacterSize(pointSize * scale); + // Guarantee the font texture for the needed text size won't need to be resized in the middle of rendering + sf::Font& font_obj = get_font_rsrc(font); + int size = pointSize * scale; + sf::Texture& font_texture = const_cast(font_obj.getTexture(size)); + int min_texture_size = 128 * scale; + if(font_texture.getSize().x < min_texture_size){ + int texture_scale = 2; + while(128 * texture_scale < min_texture_size){ + texture_scale << 1; + } + if(!font_texture.create(128 * texture_scale, 128 * texture_scale)){ + throw std::string { "Failed to create large enough font texture!" }; + } + } + + text.setFont(font_obj); + text.setCharacterSize(size); int style = sf::Text::Regular; if(italic) style |= sf::Text::Italic; if(underline) style |= sf::Text::Underlined;