-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfontrenderer.hpp
51 lines (39 loc) · 1010 Bytes
/
fontrenderer.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef FONTRENDERER_HPP
#define FONTRENDERER_HPP
#include <SDL_ttf.h>
#include <map>
#include <string>
#include <memory>
struct FontRenderer
{
enum Align
{
Top = 0,
Left = 0,
Middle = 1,
Center = 2,
Right = 4,
Bottom = 8,
};
using TexturePtr = std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)>;
struct Text
{
TexturePtr texture;
int width, height;
size_t ttl = 1;
size_t last_use;
explicit Text(TexturePtr && tex);
};
SDL_Renderer * renderer;
std::unique_ptr<TTF_Font, decltype(&TTF_CloseFont)> font;
mutable std::map<std::string, Text> cache;
size_t generation;
explicit FontRenderer(SDL_Renderer * renderer, TTF_Font * font);
Text const & render(std::string const & what) const;
void render(SDL_Rect const & target, std::string const & what, int align = Middle | Center, SDL_Color const & color = { 0xFF, 0xFF, 0xFF, 0xFF }) const;
void collect_garbage();
int height() const {
return TTF_FontHeight(font.get());
}
};
#endif // FONTRENDERER_HPP