ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
util.cpp
Go to the documentation of this file.
2
3#include <SDL3/SDL.h>
4#include <SDL3_image/SDL_image.h>
5#include <SDL3_ttf/SDL_ttf.h>
6#include <string_view>
7
8#include "./asw/modules/log.h"
9#include "./lru_cache.h"
10
11namespace {
13 // Holds the font, so its address is not reused while cached
15 std::string text;
16
17 // Changes when the font's size, style or hinting change
19};
20
21// What a lookup compares, so a lookup does not copy the text
23 const TTF_Font* font;
24 std::string_view text;
26
27 bool operator==(const TextSizeKeyView&) const = default;
28};
29
31{
32 return { key.font.get(), key.text, key.font_generation };
33}
34
36{
37 return key;
38}
39
41 using is_transparent = void;
42
43 template <typename K> std::size_t operator()(const K& key) const
44 {
45 const TextSizeKeyView v = view(key);
46 std::size_t seed = std::hash<const TTF_Font*> { }(v.font);
47 asw::detail::hash_combine(seed, std::hash<std::string_view> { }(v.text));
48 asw::detail::hash_combine(seed, std::hash<uint32_t> { }(v.font_generation));
49 return seed;
50 }
51};
52
54 using is_transparent = void;
55
56 template <typename A, typename B> bool operator()(const A& a, const B& b) const
57 {
58 return view(a) == view(b);
59 }
60};
61
62constexpr std::size_t TEXT_SIZE_CACHE_LIMIT = 512;
65} // namespace
66
67void asw::util::abort_on_error(const std::string& message)
68{
69 asw::log::error(message);
70 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message.c_str(), nullptr);
71 exit(-1);
72}
73
75{
77 SDL_GetTextureSize(tex.get(), &size.x, &size.y);
78 return size;
79}
80
81asw::Vec2<int> asw::util::get_text_size(const asw::Font& font, std::string_view text)
82{
83 if (font == nullptr) {
84 return {};
85 }
86
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 })) {
90 return *cached;
91 }
92
93 // Length given, so the text does not need to end in a null. A length of
94 // 0 means null terminated to SDL_ttf, so empty text is passed as "".
95 TTF_Text* ttf_text = text.empty()
96 ? TTF_CreateText(nullptr, font.get(), "", 0)
97 : TTF_CreateText(nullptr, font.get(), text.data(), text.size());
98 asw::Vec2<int> size;
99 TTF_GetTextSize(ttf_text, &size.x, &size.y);
100 TTF_DestroyText(ttf_text);
101
102 text_size_cache.insert({ font, std::string(text), font_generation }, size);
103 return size;
104}
105
107{
108 if (font == nullptr) {
109 return 0;
110 }
111
112 return TTF_GetFontHeight(font.get());
113}
114
119
120bool asw::util::open_url(const std::string& url)
121{
122 if (!SDL_OpenURL(url.c_str())) {
123 asw::log::warn("Could not open " + url + ": " + SDL_GetError());
124 return false;
125 }
126 return true;
127}
A 2D vector in space.
Definition geometry.h:27
T y
The y component of the vector.
Definition geometry.h:280
T x
The x component of the vector.
Definition geometry.h:277
A hash map with a size limit. When full, the least recently used half is dropped, so entries in use e...
Definition lru_cache.h:23
Structured logging system.
Bounded cache that drops the least recently used entries. Private to the library.
TextSizeKeyView view(const TextSizeCacheKey &key)
Definition util.cpp:30
asw::detail::LruCache< TextSizeCacheKey, asw::Vec2< int >, TextSizeKeyHash, TextSizeKeyEqual > text_size_cache(TEXT_SIZE_CACHE_LIMIT)
constexpr std::size_t TEXT_SIZE_CACHE_LIMIT
Definition util.cpp:62
void hash_combine(std::size_t &seed, std::size_t hash)
Combine a hash into a seed, as boost::hash_combine does.
Definition lru_cache.h:103
void error(const std::string &message)
Log an error message.
Definition log.cpp:196
void warn(const std::string &message)
Log a warning message.
Definition log.cpp:191
int get_font_height(const asw::Font &font)
Get the height of a line of text in a font.
Definition util.cpp:106
void clear_text_size_cache()
Clear cached text metrics.
Definition util.cpp:115
asw::Vec2< int > get_text_size(const asw::Font &font, std::string_view text)
Get text size.
Definition util.cpp:81
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
Definition util.cpp:74
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.
Definition util.cpp:120
void abort_on_error(const std::string &message)
Abort program and display error message.
Definition util.cpp:67
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:79
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:76
bool operator()(const A &a, const B &b) const
Definition util.cpp:56
std::size_t operator()(const K &key) const
Definition util.cpp:43
bool operator==(const TextSizeKeyView &) const =default
General utility functions.