From b2c72ee8ce282bb13b0fbeb2ddb01fdfd6ad1280 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 10 Dec 2024 12:27:59 +0100 Subject: font progress --- src/crepe/facade/SDLFontContext.cpp | 19 ++++++++------- src/crepe/facade/SDLFontContext.h | 5 +++- src/crepe/facade/font.cpp | 21 +++++++++++++++++ src/crepe/facade/font.h | 47 +++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/crepe/facade/font.cpp create mode 100644 src/crepe/facade/font.h (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/SDLFontContext.cpp b/src/crepe/facade/SDLFontContext.cpp index 6ce5efd..a4be143 100644 --- a/src/crepe/facade/SDLFontContext.cpp +++ b/src/crepe/facade/SDLFontContext.cpp @@ -1,19 +1,22 @@ -#include "SDLFontContext.h" #include +#include "SDLFontContext.h" + + using namespace crepe; using namespace std; SDLFontContext::SDLFontContext(){ - if (!FcInit()) { - throw std::runtime_error("Failed to initialize Fontconfig."); - } + } SDLFontContext::~SDLFontContext(){ - FcFini(); + } -Asset SDLFontContext::get_font_asset(const std::string & font_family) { +unique_ptr SDLFontContext::get_font_asset(const std::string & font_family) { + if (!FcInit()) { + throw std::runtime_error("Failed to initialize Fontconfig."); + } // Create a pattern to search for the font family FcPattern* pattern = FcNameParse(reinterpret_cast(font_family.c_str())); if (!pattern) { @@ -46,6 +49,6 @@ Asset SDLFontContext::get_font_asset(const std::string & font_family) { // Convert the file path to a std::string std::string font_file_path(reinterpret_cast(file_path)); FcPatternDestroy(matched_pattern); - - return Asset(font_file_path); + FcFini(); + return std::move(make_unique(font_file_path)); } diff --git a/src/crepe/facade/SDLFontContext.h b/src/crepe/facade/SDLFontContext.h index 3b2e162..cd91383 100644 --- a/src/crepe/facade/SDLFontContext.h +++ b/src/crepe/facade/SDLFontContext.h @@ -1,14 +1,17 @@ #pragma once +#include #include #include + #include "../api/Asset.h" + namespace crepe { class SDLFontContext{ public: SDLFontContext(); ~SDLFontContext(); - Asset get_font_asset(const std::string & font_family); + std::unique_ptr get_font_asset(const std::string & font_family); private: }; diff --git a/src/crepe/facade/font.cpp b/src/crepe/facade/font.cpp new file mode 100644 index 0000000..259a6cd --- /dev/null +++ b/src/crepe/facade/font.cpp @@ -0,0 +1,21 @@ +#include "font.h" +#include "../api/Config.h" +using namespace std; +using namespace crepe; + +void Font::load(unique_ptr res){ + const char* font_path = res->get_path(); + int font_size = Config::get_instance().font.font_size; + this->font = std::unique_ptr( + TTF_OpenFont(font_path, font_size), &TTF_CloseFont); + + if (!font) { + throw std::runtime_error("Failed to load font: " + std::string(TTF_GetError())); + } +} +Font::Font(const char* src){ + this->load(make_unique(src)); +} +Font::Font(std::unique_ptr res){ + this->load(std::move(res)); +} diff --git a/src/crepe/facade/font.h b/src/crepe/facade/font.h new file mode 100644 index 0000000..f5c7785 --- /dev/null +++ b/src/crepe/facade/font.h @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +#include "../api/Asset.h" + +namespace crepe { + +/** + * \brief Font resource facade + * + * This class is a wrapper around an SDL_ttf font instance, encapsulating font loading and usage. + */ +class Font { +public: + /** + * \param src The file path to the font file. + */ + Font(const char* src); + + /** + * \param res A unique pointer to an Asset holding the font resource. + */ + Font(std::unique_ptr res); + + /** + * \brief Destructor to clean up font resources. + */ + ~Font() = default; + + void draw(const vec2& pos, const vec2&) +private: + /** + * \brief Load the font from the given resource. + * + * This method is used by both constructors to load the font resource. + * + * \param res A unique pointer to an Asset holding the font resource. + */ + void load(std::unique_ptr res); + +private: + std::unique_ptr font; ///< The SDL_ttf font object with custom deleter. +}; + +} // namespace crepe -- cgit v1.2.3