4#include <SDL3_image/SDL_image.h>
5#include <SDL3_ttf/SDL_ttf.h>
43 template <
typename K> std::size_t
operator()(
const K& key)
const
46 std::size_t seed = std::hash<const TTF_Font*> { }(v.
font);
56 template <
typename A,
typename B>
bool operator()(
const A& a,
const B& b)
const
58 return view(a) == view(b);
70 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Error", message.c_str(),
nullptr);
77 SDL_GetTextureSize(tex.get(), &size.
x, &size.
y);
83 if (font ==
nullptr) {
87 const uint32_t font_generation = TTF_GetFontGeneration(font.get());
88 if (
const auto* cached
89 =
text_size_cache.find(TextSizeKeyView { font.get(), text, font_generation })) {
95 TTF_Text* ttf_text = text.empty()
96 ? TTF_CreateText(
nullptr, font.get(),
"", 0)
97 : TTF_CreateText(
nullptr, font.get(), text.data(), text.size());
99 TTF_GetTextSize(ttf_text, &size.
x, &size.
y);
100 TTF_DestroyText(ttf_text);
102 text_size_cache.insert({ font, std::string(text), font_generation }, size);
108 if (font ==
nullptr) {
112 return TTF_GetFontHeight(font.get());
122 if (!SDL_OpenURL(url.c_str())) {
T y
The y component of the vector.
T x
The x component of the vector.
A hash map with a size limit. When full, the least recently used half is dropped, so entries in use e...
Structured logging system.
Bounded cache that drops the least recently used entries. Private to the library.
TextSizeKeyView view(const TextSizeCacheKey &key)
asw::detail::LruCache< TextSizeCacheKey, asw::Vec2< int >, TextSizeKeyHash, TextSizeKeyEqual > text_size_cache(TEXT_SIZE_CACHE_LIMIT)
constexpr std::size_t TEXT_SIZE_CACHE_LIMIT
void hash_combine(std::size_t &seed, std::size_t hash)
Combine a hash into a seed, as boost::hash_combine does.
void error(const std::string &message)
Log an error message.
void warn(const std::string &message)
Log a warning message.
int get_font_height(const asw::Font &font)
Get the height of a line of text in a font.
void clear_text_size_cache()
Clear cached text metrics.
asw::Vec2< int > get_text_size(const asw::Font &font, std::string_view text)
Get text size.
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
bool open_url(const std::string &url)
Open a web page in the player's browser, e.g. a login or store page. Web builds open it in a new tab.
void abort_on_error(const std::string &message)
Abort program and display error message.
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
bool operator()(const A &a, const B &b) const
std::size_t operator()(const K &key) const
bool operator==(const TextSizeKeyView &) const =default
General utility functions.