From 880d7bd1bf7016e7f5aa55e3e54f1d0dd4056107 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Thu, 1 May 2025 19:16:58 -0500 Subject: [PATCH] round final text render coordinates to int. This didn't fix the main bug I'm struggling with, but someone in SFML forums reported that float coordinates could cause graphical bugs in text rendering. --- src/gfx/render_image.cpp | 5 ++++- src/gfx/render_text.cpp | 5 ++++- src/gfx/render_text.hpp | 4 ++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/gfx/render_image.cpp b/src/gfx/render_image.cpp index 71cad846..742670be 100644 --- a/src/gfx/render_image.cpp +++ b/src/gfx/render_image.cpp @@ -152,7 +152,10 @@ static void draw_stored_scale_aware_text(sf::RenderTexture& texture, sf::RenderT sf::Vector2f position = str_to_draw.getPosition(); position = position + sf::Vector2f {0.0f+targ_rect.left, 0.0f+targ_rect.top}; position *= (float)get_ui_scale(); - str_to_draw.setPosition(position + scaled_top_left); + position += scaled_top_left; + // Rounding to whole-number positions *might* avoid unpredictable graphics bugs: + round_vec(position); + str_to_draw.setPosition(position); dest_window.draw(str_to_draw); if(!text.clip_rect.empty()) undo_clip(dest_window); diff --git a/src/gfx/render_text.cpp b/src/gfx/render_text.cpp index f1495dbf..02ee3a94 100644 --- a/src/gfx/render_text.cpp +++ b/src/gfx/render_text.cpp @@ -182,7 +182,10 @@ void draw_scale_aware_text(sf::RenderTarget& dest_window, sf::Text str_to_draw) dest_window.setView(dest_window.getDefaultView()); sf::Vector2f text_position = str_to_draw.getPosition() * (float)get_ui_scale(); - str_to_draw.setPosition(text_position + scaled_view_top_left(dest_window, scaled_view)); + text_position += scaled_view_top_left(dest_window, scaled_view); + // Rounding to whole-number positions *might* avoid unpredictable graphics bugs: + round_vec(text_position); + str_to_draw.setPosition(text_position); // Draw the text immediately dest_window.draw(str_to_draw); diff --git a/src/gfx/render_text.hpp b/src/gfx/render_text.hpp index 1b61b041..f91c0298 100644 --- a/src/gfx/render_text.hpp +++ b/src/gfx/render_text.hpp @@ -65,5 +65,9 @@ void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::stri void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style, break_info_t break_info,bool right_align = false); break_info_t calculate_line_wrapping(rectangle dest_rect, std::string str, TextStyle style); size_t string_length(std::string str, const TextStyle& style, short* height = nullptr); +inline void round_vec(sf::Vector2f& vec) { + vec.x = round(vec.x); + vec.y = round(vec.y); +} #endif