ttf.cpp

1
2
3
#include <planet/sdl/renderer.hpp>
#include <planet/sdl/ttf.hpp>
#include <planet/ui/scale.hpp>

planet::sdl::ttf

 9
10
11
12
planet::sdl::ttf::ttf(init &) { TTF_Init(); }


planet::sdl::ttf::~ttf() { TTF_Quit(); }

planet::sdl::font

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
52
53
54
55
planet::sdl::font::font(
        asset_manager const &am,
        char const *const filename,
        std::size_t const pixels,
        SDL_Color const c,
        felspar::source_location const &loc)
: font_data{am, filename, loc},
  pf{TTF_OpenFontRW(font_data.get(), false, pixels)},
  colour{c},
  space{measure(" ")},
  em{measure("m")} {
    if (not pf.get()) {
        throw felspar::stdexcept::runtime_error{
                "TTF_OpenFontRW returned nullptr"};
    }
}


planet::affine::extents2d planet::sdl::font::measure(char const *const t) const {
    int w{}, h{};
    drawing_worked(TTF_SizeUTF8(pf.get(), t, &w, &h));
    return {static_cast<float>(w), static_cast<float>(h)};
}


planet::sdl::surface planet::sdl::font::render(
        char const *text, SDL_Color c, ui::scale const fit) const {
    return {TTF_RenderUTF8_Blended(pf.get(), text, c), fit};
}


planet::sdl::surface planet::sdl::font::render(
        char const *text,
        SDL_Color const c,
        std::uint32_t const width,
        ui::scale const fit) const {
    return {TTF_RenderUTF8_Blended_Wrapped(pf.get(), text, c, width), fit};
}