From c396ae5f78222a7c3547ae5e2ce719ae143acb66 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Sun, 1 Dec 2024 21:14:35 +0100 Subject: start of text component --- src/crepe/api/Font.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/crepe/api/Font.cpp (limited to 'src/crepe/api/Font.cpp') diff --git a/src/crepe/api/Font.cpp b/src/crepe/api/Font.cpp new file mode 100644 index 0000000..8db4b23 --- /dev/null +++ b/src/crepe/api/Font.cpp @@ -0,0 +1,45 @@ +#include + +#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>(loaded_font, [](TTF_Font *f) { + if (f) TTF_CloseFont(f); + }); +} + +int Font::get_size() const { + return this->font_size; +} -- cgit v1.2.3