diff options
| author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 13:31:34 +0100 | 
|---|---|---|
| committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-12-17 13:31:34 +0100 | 
| commit | 24578c9afb61ae65b300dd7fb645220e133089be (patch) | |
| tree | c6451ea01a56f0bde33a803ea37169f59d0fc41d | |
| parent | bcaee968761c1d2e85c20925b237480c87da9747 (diff) | |
| parent | 707db8c94b6bb3921105f40658aab13511d8df07 (diff) | |
Merge branch 'wouter/text-component' into niels/UI
| -rw-r--r-- | src/crepe/api/Asset.h | 2 | ||||
| -rw-r--r-- | src/crepe/api/LoopManager.h | 7 | ||||
| -rw-r--r-- | src/crepe/api/Script.h | 5 | ||||
| -rw-r--r-- | src/crepe/api/Text.cpp | 8 | ||||
| -rw-r--r-- | src/crepe/api/Text.h | 15 | ||||
| -rw-r--r-- | src/crepe/facade/Font.cpp | 8 | ||||
| -rw-r--r-- | src/crepe/facade/Font.h | 5 | ||||
| -rw-r--r-- | src/crepe/facade/FontFacade.cpp | 18 | ||||
| -rw-r--r-- | src/crepe/facade/FontFacade.h | 12 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.cpp | 5 | ||||
| -rw-r--r-- | src/crepe/facade/SDLContext.h | 15 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.cpp | 31 | ||||
| -rw-r--r-- | src/crepe/system/RenderSystem.h | 10 | ||||
| -rw-r--r-- | src/example/FontExample.cpp | 6 | ||||
| -rw-r--r-- | src/example/loadfont.cpp | 31 | 
15 files changed, 129 insertions, 49 deletions
diff --git a/src/crepe/api/Asset.h b/src/crepe/api/Asset.h index bfd0ac7..b367a92 100644 --- a/src/crepe/api/Asset.h +++ b/src/crepe/api/Asset.h @@ -37,7 +37,7 @@ public:  private:  	//! path to asset -	const std::string src; +	std::string src;  private:  	/** diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 40e6b38..1725810 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -71,7 +71,9 @@ private:  private:  	//! Global context  	Mediator mediator; - +	 +	//! SDLContext instance +	SDLContext sdl_context{mediator};  	//! Component manager instance  	ComponentManager component_manager{mediator};  	//! Scene manager instance @@ -84,8 +86,7 @@ private:  	ResourceManager resource_manager{mediator};  	//! Save manager instance  	SaveManager save_manager{mediator}; -	//! SDLContext instance -	SDLContext sdl_context{mediator}; +	  private:  	/** diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index a24e32e..4503525 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,7 +156,7 @@ private:  	void subscribe_internal(const EventHandler<EventType> & callback, event_channel_t channel);  protected: -	OptionalRef<Mediator> mediator; +	  	// NOTE: This must be the only constructor on Script, see "Late references" below  	Script() = default;  	//! Only \c BehaviorScript instantiates Script @@ -186,12 +186,13 @@ private:  	 *  	 * \{  	 */ +  	//! Game object ID of game object parent BehaviorScript is attached to  	game_object_id_t game_object_id;  	//! Reference to parent component  	OptionalRef<bool> active;  	//! Mediator reference - +	OptionalRef<Mediator> mediator;  	//! \}  private: diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 07d1705..1620247 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -5,11 +5,11 @@  using namespace crepe; -Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, -		   const std::string & text, const std::string & font_family, const Data & data) +Text::Text(game_object_id_t id, const vec2 & dimensions,const vec2 & offset, const std::string & font_family, +	const Data & data, const std::string & text, std::optional<Asset> font)  	: UIObject(id, dimensions, offset),  	  text(text),  	  data(data), -	  font(FontFacade::get_font_asset(font_family)) { -	dbg_trace(); +	  font_family(font_family), +	  font(font) {  } diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index ec0bf74..ab72bc0 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -1,6 +1,7 @@  #pragma once  #include <string> +#include <optional>   #include "../Component.h" @@ -47,15 +48,21 @@ public:  	 * \param text The text to be displayed.  	 * \param font_family The font style name to be displayed.  	 * \param data Data struct containing extra text parameters. +	 * \param font Optional font asset that can be passed or left empty.  	 */ -	Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, -		 const std::string & text, const std::string & font_family, const Data & data); +	Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,  +     const std::string & font_family, const Data & data,  +     const std::string & text = "", std::optional<Asset> font = std::nullopt); +	  	//! Label text.  	std::string text = ""; -	//! Font asset variable -	const Asset font; +	//! font family name +	std::string font_family = ""; +	//! Font asset variable if this is not set, it will use the font_family to create an asset. +	std::optional<Asset> font;  	//! Data instance  	Data data; +	  };  } // namespace crepe diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 47d0e39..51e9690 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,3 +1,5 @@ +#include <SDL2/SDL_ttf.h> +  #include "../api/Asset.h"  #include "../api/Config.h"  #include "util/Log.h" @@ -10,8 +12,7 @@ using namespace std;  using namespace crepe;  Font::Font(const Asset & src, Mediator & mediator) -	: Resource(src, mediator) { -	dbg_trace(); +	: Resource(src, mediator){  	Config & config = Config::get_instance();  	const std::string FONT_PATH = src.get_path(); @@ -23,8 +24,7 @@ Font::Font(const Asset & src, Mediator & mediator)  	if (loaded_font == NULL) {  		throw runtime_error(format("Font: {} (path: {})", TTF_GetError(), FONT_PATH));  	} -	this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(font); }}; -	*/ +	this->font = {loaded_font, [](TTF_Font * close_font) { TTF_CloseFont(close_font); }};  }  TTF_Font * Font::get_font() const { return this->font.get(); } diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 0b1768b..3eff3c6 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -25,7 +25,6 @@ public:       * \param mediator The Mediator object used for managing the SDL context or related systems.       */  	Font(const Asset & src, Mediator & mediator); -  	/**       * \brief Gets the underlying TTF_Font resource.       *  @@ -40,9 +39,7 @@ public:  private:  	//! The SDL_ttf font object with custom deleter. -	std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font; - -	std::string path; +	std::unique_ptr<TTF_Font, std::function<void(TTF_Font *)>> font = nullptr;  };  } // namespace crepe diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index a0464e0..9919032 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -7,10 +7,16 @@  using namespace crepe;  using namespace std; -Asset FontFacade::get_font_asset(const string font_family) { +FontFacade::FontFacade(){  	if (!FcInit()) {  		throw runtime_error("Failed to initialize Fontconfig.");  	} +} +FontFacade::~FontFacade(){ +	FcFini(); +} +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) { @@ -19,8 +25,8 @@ Asset FontFacade::get_font_asset(const string font_family) {  	// Default configuration  	FcConfig * config = FcConfigGetCurrent(); -	if (!config) { -		FcPatternDestroy(pattern); +	if (config == NULL) { +		// FcPatternDestroy(pattern);  		throw runtime_error("Failed to get current Fontconfig configuration.");  	} @@ -32,18 +38,16 @@ Asset FontFacade::get_font_asset(const string font_family) {  	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) { -		FcPatternDestroy(matched_pattern); +		|| file_path == NULL) { +		// FcPatternDestroy(matched_pattern);  		throw runtime_error("Failed to get font file path.");  	}  	// Convert the file path to a string  	string font_file_path = reinterpret_cast<const char *>(file_path);  	FcPatternDestroy(matched_pattern); -	cout << font_file_path << endl;  	return Asset(font_file_path);  } diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 0e6b7da..2e08f3f 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -13,16 +13,22 @@ namespace crepe {   */  class FontFacade {  public: +	FontFacade(); +	~FontFacade(); +    FontFacade(const FontFacade & other) = delete; +    FontFacade & operator=(const FontFacade & other) = delete; +    FontFacade(FontFacade && other) noexcept = delete; +    FontFacade & operator=(FontFacade && other) noexcept = delete;  	/**  	 *   	 * \brief Facade function to convert a font_family into an asset.  	 *   	 * This function uses the FontConfig library to convert a font family name (Arial, Inter, Helvetica) and converts it to the font source path. -	 * This function is static so the member function can be used without create a FontFacade object. This way it can be used in a constructor as FontFacade::get_font_asset(). +	 * This function returns a default font path if the font_family name doesnt exist or cant be found  	 * \param font_family Name of the font family name. -	 * \return Asset with filepath to the font. +	 * \return Asset with filepath to the corresponding font.  	 */ -	static Asset get_font_asset(const std::string font_family); +	Asset get_font_asset(const std::string& font_family);  };  } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 1befbe5..7bd9dbc 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -463,7 +463,12 @@ std::vector<SDLContext::EventData> SDLContext::get_events() {  	}  	return event_list;  } +  void SDLContext::set_color_texture(const Texture & texture, const Color & color) {  	SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b);  	SDL_SetTextureAlphaMod(texture.get_img(), color.a);  } + +Asset SDLContext::get_font_from_name(const std::string& font_family){ +	return this->font_facade.get_font_asset(font_family); +} diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 3f9f9ee..fcf3559 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -17,6 +17,7 @@  #include "api/Transform.h"  #include "types.h" +#include "FontFacade.h"  namespace crepe {  class Texture; @@ -243,6 +244,20 @@ private:  	 * - this is defined in this class because get_events() needs this information aswell  	 */  	CameraAuxiliaryData cam_aux_data; +private:  +	//! instance of the font_facade +	FontFacade font_facade{}; +public: +	/** +	 * \brief Function to Get asset from font_family +	 *  +	 * This function uses the FontFacade function to convert a font_family to an asset. +	 *  +	 * \param font_family name of the font style that needs to be used (will return an asset with default font path of the font_family doesnt exist) +	 *  +	 * \return asset with the font style absolute path +	 */ +	Asset get_font_from_name(const std::string& font_family);  };  } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 4a70843..7d6a31b 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,12 +2,15 @@  #include <cassert>  #include <cmath>  #include <functional> +#include <optional>  #include <stdexcept>  #include <vector>  #include "../api/Camera.h"  #include "../api/ParticleEmitter.h"  #include "../api/Sprite.h" +#include "../api/Text.h" +#include "../facade/Font.h"  #include "../api/Transform.h"  #include "../facade/SDLContext.h"  #include "../facade/Texture.h" @@ -140,8 +143,15 @@ void RenderSystem::render() {  	this->update_camera();  	RefVector<Sprite> sprites = mgr.get_components_by_type<Sprite>(); +	ResourceManager & resource_manager = this->mediator.resource_manager;  	RefVector<Sprite> sorted_sprites = this->sort(sprites); +	RefVector<Text> text_components =  mgr.get_components_by_type<Text>(); +	for(Text& text : text_components){ +		const Transform & transform +			= mgr.get_components_by_id<Transform>(text.game_object_id).front().get(); +		this->render_text(text,transform); +	}  	for (const Sprite & sprite : sorted_sprites) {  		if (!sprite.active) continue;  		const Transform & transform @@ -152,5 +162,26 @@ void RenderSystem::render() {  		if (rendered_particles) continue;  		this->render_normal(sprite, transform); +		 +	 +		  	} +	  } +void RenderSystem::render_text(Text & text, const Transform & tm) { +    SDLContext & ctx = this->mediator.sdl_context; + +    // Check if font is available in text +    if (!text.font.has_value()) { +    } + +    ResourceManager & resource_manager = this->mediator.resource_manager; + +    if (text.font.has_value()) { +        const Asset& font_asset = text.font.value(); +        const Font & res = resource_manager.get<Font>(font_asset);  +	} +} + + + diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index ae55404..3476765 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -10,7 +10,7 @@ namespace crepe {  class Camera;  class Sprite;  class Transform; - +class Text;  /**   * \brief Manages rendering operations for all game objects.   * @@ -52,7 +52,13 @@ private:  	 * \return true if particles have been rendered  	 */  	bool render_particle(const Sprite & sprite, const double & scale); - +	/** +	 * \brief Renders all Text components +	 * +	 * \param text The text component to be rendered. +	 * \param tm the Transform component that holds the position,rotation and scale +	 */ +	void render_text(Text & text, const Transform & tm);  	/**  	 * \brief renders a sprite with a Transform component on the screen  	 * diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index 3f5af48..7b2dadb 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -11,6 +11,7 @@  #include <crepe/manager/EventManager.h>  #include <crepe/manager/Mediator.h>  #include <crepe/manager/ResourceManager.h> +#include <crepe/api/Config.h>  #include <exception>  #include <iostream>  #include <memory> @@ -36,14 +37,15 @@ class TestScene : public Scene {  public:  	void load_scene() override {  		GameObject text_object = this->new_object("test", "test", vec2{0, 0}, 0, 1); -		text_object.add_component<Text>(vec2(100, 100), vec2(0, 0), "test test", "Noto Sans", -										Text::Data{}); +		text_object.add_component<Text>(vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{});  		text_object.add_component<BehaviorScript>().set_script<TestScript>();  		text_object.add_component<Camera>(ivec2{300, 300}, vec2{100, 100}, Camera::Data{});  	}  	std::string get_name() const override { return "hey"; }  };  int main() { +	// Config& config = Config::get_instance(); +	// config.log.level = Log::Level::TRACE;  	LoopManager engine;  	engine.add_scene<TestScene>();  	engine.start(); diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index ce287b4..36d00dd 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -2,10 +2,12 @@  #include <crepe/api/Text.h>  #include <crepe/facade/Font.h>  #include <crepe/facade/SDLContext.h> +#include <crepe/api/Asset.h>  #include <crepe/manager/Mediator.h>  #include <crepe/manager/ResourceManager.h>  #include <exception>  #include <iostream> +#include <optional>  #include <memory>  using namespace crepe;  int main() { @@ -18,21 +20,24 @@ int main() {  	try {  		// Correct way to create a unique pointer for Text  		std::unique_ptr<Text> label = std::make_unique<Text>( -			1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); -		std::cout << "Path: " << label->font.get_path() << std::endl; +			1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{},"test text", Asset("")); +		// std::cout << "Path: " << label->font.get_path() << std::endl;  		std::unique_ptr<Text> label2 -			= std::make_unique<Text>(1, vec2(100, 100), vec2(0, 0), "test test", -									 "fsaafdafsdafsdafsdasfdds", Text::Data{}); -		std::cout << "Path: " << label2->font.get_path() << std::endl; -		ResourceManager & resource_mgr = mediator.resource_manager; -		const Font & res = resource_manager.get<Font>(label->font); -		TTF_Font * test_font = res.get_font(); -		if (test_font == NULL) { -			std::cout << "error with font" << std::endl; -		} else { -			std::cout << "correct font retrieved" << std::endl; -		} +			= std::make_unique<Text>(1, vec2(100, 100), vec2(0, 0),"fsaafdafsdafsdafsdasfdds", Text::Data{}); +		Asset asset = Asset("test test"); +		label->font = asset; +		std::cout << label->font.value().get_path() << std::endl; +		// label2->font = std::make_optional(asset); +		// std::cout << "Path: " << label2->font.get_path() << std::endl; +		// ResourceManager & resource_mgr = mediator.resource_manager; +		// const Font & res = resource_manager.get<Font>(label->font); +		// TTF_Font * test_font = res.get_font(); +		// if (test_font == NULL) { +		// 	std::cout << "error with font" << std::endl; +		// } else { +		// 	std::cout << "correct font retrieved" << std::endl; +		// }  	} catch (const std::exception & e) {  		std::cout << "Standard exception thrown: " << e.what() << std::endl;  	}  |