Cache measurement of space in TextStyle
I noticed a ton of string_length() calls on empty space strings, each one making an sf::Text object to measure. I thought caching the result would make the slow dialogs open faster but it didn't. Still might as well do it, though.
This commit is contained in:
@@ -9,12 +9,13 @@
|
||||
#include "render_text.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <set>
|
||||
#include "fileio/resmgr/res_font.hpp"
|
||||
#include "gfx/render_shapes.hpp"
|
||||
#include <utility>
|
||||
#include "winutil.hpp"
|
||||
|
||||
void TextStyle::applyTo(sf::Text& text, double scale) {
|
||||
void TextStyle::applyTo(sf::Text& text, double scale) const {
|
||||
switch(font) {
|
||||
case FONT_PLAIN:
|
||||
text.setFont(*ResMgr::fonts.get("plain"));
|
||||
@@ -319,13 +320,26 @@ std::vector<snippet_t> draw_string_sel(sf::RenderTarget& dest_window,rectangle d
|
||||
return params.snippets;
|
||||
}
|
||||
|
||||
size_t string_length(std::string str, TextStyle style, short* height){
|
||||
std::set<std::string> strings_to_cache = {" "};
|
||||
|
||||
size_t string_length(std::string str, const TextStyle& style, short* height){
|
||||
size_t total_width = 0;
|
||||
if(style.measurementCache.find(str) != style.measurementCache.end()){
|
||||
location measurement = style.measurementCache[str];
|
||||
if(height) *height = measurement.y;
|
||||
return measurement.x;
|
||||
}
|
||||
|
||||
sf::Text text;
|
||||
style.applyTo(text);
|
||||
text.setString(str);
|
||||
total_width = text.getLocalBounds().width;
|
||||
if(strings_to_cache.count(str)){
|
||||
location measurement;
|
||||
measurement.x = total_width;
|
||||
measurement.y = text.getLocalBounds().height;
|
||||
style.measurementCache[str] = measurement;
|
||||
}
|
||||
if(height) *height = text.getLocalBounds().height;
|
||||
return total_width;
|
||||
}
|
||||
|
@@ -36,7 +36,8 @@ struct TextStyle {
|
||||
int pointSize = 10, lineHeight = 10;
|
||||
sf::Color colour;
|
||||
TextStyle() : colour(sf::Color::Black) {}
|
||||
void applyTo(sf::Text& text, double scale = 1.0);
|
||||
void applyTo(sf::Text& text, double scale = 1.0) const;
|
||||
mutable std::map<std::string, location> measurementCache;
|
||||
};
|
||||
|
||||
// elements: std::make_tuple(last_line_break, last_word_break, line_width)
|
||||
@@ -63,6 +64,6 @@ void clear_scale_aware_text(sf::RenderTexture& texture);
|
||||
void win_draw_string(sf::RenderTarget& dest_window,rectangle dest_rect,std::string str,eTextMode mode,TextStyle style,bool right_align = false);
|
||||
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, TextStyle style, short* height = nullptr);
|
||||
size_t string_length(std::string str, const TextStyle& style, short* height = nullptr);
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user