aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/api/Font.cpp
blob: 8db4b23bdfc06e886320b05fad1562b18ad71952 (plain)
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
#include <SDL2/SDL_ttf.h>

#include "facade/SDLContext.h"
#include "util/Log.h"

#include "Asset.h"
#include "Font.h"

using namespace crepe;
using namespace std;

Font::Font(const Asset &src, int size) : font_size(size) {
    dbg_trace();
    this->load(src, size);
}

Font::~Font() {
    dbg_trace();
    this->font.reset();
}

void Font::load(const Asset &res, int size) {
    SDLContext &ctx = SDLContext::get_instance();
    // Open the font using SDL's TTF_OpenFontRW, which supports loading from memory
    SDL_RWops *rw_ops = SDL_RWFromFile(res.get_path().c_str(), "rb");
    if (!rw_ops) {
        // dbg_log("Failed to create RWops for font: %s", SDL_GetError());
        return;
    }

    TTF_Font *loaded_font = TTF_OpenFontRW(rw_ops, 1, size); // 1 indicates SDL should free the RWops
    if (!loaded_font) {
        // dbg_log("Failed to load font from asset: %s", TTF_GetError());
        return;
    }

    // Wrap the TTF_Font with a unique_ptr for automatic cleanup
    this->font = unique_ptr<TTF_Font, function<void(TTF_Font *)>>(loaded_font, [](TTF_Font *f) {
        if (f) TTF_CloseFont(f);
    });
}

int Font::get_size() const {
    return this->font_size;
}