From bcaee968761c1d2e85c20925b237480c87da9747 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 17 Dec 2024 13:29:58 +0100 Subject: tmp fix --- src/crepe/facade/Font.cpp | 11 +++++++++++ src/crepe/facade/Font.h | 8 ++++++-- src/crepe/facade/FontFacade.cpp | 12 ++++++------ src/crepe/facade/SDLContext.cpp | 2 ++ src/crepe/system/RenderSystem.cpp | 5 +++-- src/example/rendering_particle.cpp | 7 ++++--- 6 files changed, 32 insertions(+), 13 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 +#include #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 +#include #include +#include #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> 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 +#include #include #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(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(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); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 9611a9e..4a70843 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -13,15 +13,16 @@ #include "../facade/Texture.h" #include "../manager/ComponentManager.h" #include "../manager/ResourceManager.h" - -#include "RenderSystem.h" #include "api/Text.h" #include "facade/Font.h" + +#include "RenderSystem.h" #include "types.h" using namespace crepe; using namespace std; + void RenderSystem::clear_screen() { SDLContext & ctx = this->mediator.sdl_context; ctx.clear_screen(); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 21ed7fc..b261f52 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,3 +1,5 @@ + + #include "api/Asset.h" #include "api/Text.h" #include @@ -14,7 +16,6 @@ #include #include #include -#include using namespace crepe; using namespace std; @@ -65,8 +66,8 @@ public: Camera::Data{ .bg_color = Color::WHITE, }); - - game_object.add_component(vec2{200,200}, vec2{0,0}, "test test", "", Text::Data{}); + game_object.add_component(vec2{200, 200}, vec2{0, 0}, "test test", "Ariel.ttf", + Text::Data{}); } string get_name() const { return "TestScene"; }; -- cgit v1.2.3