diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/Font.cpp | 11 | ||||
-rw-r--r-- | src/crepe/facade/Font.h | 8 | ||||
-rw-r--r-- | src/crepe/facade/FontFacade.cpp | 12 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 2 |
4 files changed, 25 insertions, 8 deletions
diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 8c16563..47d0e39 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -2,6 +2,7 @@ #include "../api/Config.h" #include "util/Log.h" #include <iostream> +#include <string> #include "Font.h" @@ -13,11 +14,21 @@ Font::Font(const Asset & src, Mediator & mediator) dbg_trace(); Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); + + this->path = FONT_PATH; + + cout << this->path << endl; + /* TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); if (loaded_font == NULL) { throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH)); } this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(font); }}; + */ } TTF_Font * Font::get_font() const { return this->font.get(); } + +const string & Font::get_path() const noexcept{ + return this->path; +} diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 91c59c0..0b1768b 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,10 +1,11 @@ #pragma once #include <SDL2/SDL_ttf.h> +#include <functional> #include <memory> +#include <string> #include "../Resource.h" -#include "../api/Config.h" namespace crepe { @@ -24,7 +25,6 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); - ~Font(); /** * \brief Gets the underlying TTF_Font resource. @@ -36,9 +36,13 @@ public: */ TTF_Font * get_font() const; + const std::string & get_path() const noexcept; + private: //! The SDL_ttf font object with custom deleter. std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font; + + std::string path; }; } // namespace crepe diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index aa9d00c..a0464e0 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,5 @@ #include <fontconfig/fontconfig.h> +#include <iostream> #include <stdexcept> #include "FontFacade.h" @@ -12,13 +13,13 @@ Asset FontFacade::get_font_asset(const string font_family) { } // Create a pattern to search for the font family FcPattern * pattern = FcNameParse(reinterpret_cast<const FcChar8 *>(font_family.c_str())); - if (pattern == NULL) { + if (!pattern) { throw runtime_error("Failed to create font pattern."); } // Default configuration FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { + if (!config) { FcPatternDestroy(pattern); throw runtime_error("Failed to get current Fontconfig configuration."); } @@ -28,15 +29,14 @@ Asset FontFacade::get_font_asset(const string font_family) { FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); FcPatternDestroy(pattern); - if (matched_pattern == NULL) { - FcPatternDestroy(matched_pattern); + if (!matched_pattern) { throw runtime_error("No matching font found."); } // Extract the file path FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch - || file_path == NULL) { + || !file_path) { FcPatternDestroy(matched_pattern); throw runtime_error("Failed to get font file path."); } @@ -44,6 +44,6 @@ Asset FontFacade::get_font_asset(const string font_family) { // Convert the file path to a string string font_file_path = reinterpret_cast<const char *>(file_path); FcPatternDestroy(matched_pattern); - FcFini(); + cout << font_file_path << endl; return Asset(font_file_path); } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index a06940d..1befbe5 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -294,7 +294,9 @@ void SDLContext::draw_text(const Text & text, const Font & font){ .b = text.data.text_color.b, .a = text.data.text_color.a, }; + TTF_Font * ttf_font = TTF_OpenFont(font.get_path().c_str(), Config::get_instance().font.size); SDL_Surface * font_surface = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color); + TTF_CloseFont(ttf_font); SDL_Texture * font_texture = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface); SDL_FreeSurface(font_surface); |