diff options
| author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 13:29:58 +0100 | 
|---|---|---|
| committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 13:29:58 +0100 | 
| commit | bcaee968761c1d2e85c20925b237480c87da9747 (patch) | |
| tree | f91ab2c2396a6365eb0696902e63975dc18b5259 | |
| parent | bfc07676707eae2c0161c6b86ccdd1583d96f71b (diff) | |
tmp fix
| -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 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.cpp | 5 | ||||
| -rw-r--r-- | 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 <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); 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 <crepe/Component.h> @@ -14,7 +16,6 @@  #include <crepe/manager/ComponentManager.h>  #include <crepe/manager/Mediator.h>  #include <crepe/types.h> -#include <iostream>  using namespace crepe;  using namespace std; @@ -65,8 +66,8 @@ public:  													   Camera::Data{  														   .bg_color = Color::WHITE,  													   }); - -		game_object.add_component<Text>(vec2{200,200}, vec2{0,0}, "test test", "", Text::Data{}); +		game_object.add_component<Text>(vec2{200, 200}, vec2{0, 0}, "test test", "Ariel.ttf", +										Text::Data{});  	}  	string get_name() const { return "TestScene"; };  |