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 <unordered_map>
6
7#include "./asw/modules/log.h"
8
9namespace {
12 std::string text;
13
14 bool operator==(const TextSizeCacheKey&) const = default;
15};
16
18 std::size_t operator()(const TextSizeCacheKey& key) const
19 {
20 std::size_t seed = std::hash<asw::Font> {}(key.font);
21 seed ^= std::hash<std::string> {}(key.text) + 0x9e3779b9 + ((seed << 6) + (seed >> 2));
22 return seed;
23 }
24};
25
26constexpr std::size_t TEXT_SIZE_CACHE_LIMIT = 512;
27std::unordered_map<TextSizeCacheKey, asw::Vec2<int>, TextSizeCacheKeyHash> text_size_cache;
28} // namespace
29
30void asw::util::abort_on_error(const std::string& message)
31{
32 asw::log::error(message);
33 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", message.c_str(), nullptr);
34 exit(-1);
35}
36
38{
40 SDL_GetTextureSize(tex.get(), &size.x, &size.y);
41 return size;
42}
43
44asw::Vec2<int> asw::util::get_text_size(const asw::Font& font, const std::string& text)
45{
46 if (font == nullptr) {
47 return {};
48 }
49
50 const TextSizeCacheKey cache_key { font, text };
51 if (auto it = text_size_cache.find(cache_key); it != text_size_cache.end()) {
52 return it->second;
53 }
54
55 TTF_Text* ttf_text = TTF_CreateText(nullptr, font.get(), text.c_str(), 0);
56 asw::Vec2<int> size;
57 TTF_GetTextSize(ttf_text, &size.x, &size.y);
58 TTF_DestroyText(ttf_text);
59
61 // Keep the shared cache bounded while still avoiding repeated
62 // measurement work for the common stable UI strings.
63 text_size_cache.clear();
64 }
65
66 text_size_cache.emplace(cache_key, size);
67 return size;
68}
69
A 2D vector in space.
Definition geometry.h:21
T y
The y component of the vector.
Definition geometry.h:218
T x
The x component of the vector.
Definition geometry.h:215
Structured logging system.
constexpr std::size_t TEXT_SIZE_CACHE_LIMIT
Definition util.cpp:26
std::unordered_map< TextSizeCacheKey, asw::Vec2< int >, TextSizeCacheKeyHash > text_size_cache
Definition util.cpp:27
void error(const std::string &message)
Log an error message.
Definition log.cpp:178
void clear_text_size_cache()
Clear cached text metrics.
Definition util.cpp:70
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
Definition util.cpp:37
asw::Vec2< int > get_text_size(const asw::Font &font, const std::string &text)
Get text size.
Definition util.cpp:44
void abort_on_error(const std::string &message)
Abort program and display error message.
Definition util.cpp:30
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:41
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:38
std::size_t operator()(const TextSizeCacheKey &key) const
Definition util.cpp:18
bool operator==(const TextSizeCacheKey &) const =default
General utility functions.