From 55f4aaf179cf723c5b703b0c4d848bf059ae190a Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 19:08:22 +0100 Subject: font family implemented in the text component --- src/crepe/facade/FontFacade.cpp | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/crepe/facade/FontFacade.cpp (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp new file mode 100644 index 0000000..12e10ed --- /dev/null +++ b/src/crepe/facade/FontFacade.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +#include "FontFacade.h" + +using namespace crepe; +using namespace std; + +Asset FontFacade::get_font_asset(const string font_family) { + if (!FcInit()) { + throw 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 == NULL) { + throw runtime_error("Failed to create font pattern."); + } + + // Default configuration + FcConfig * config = FcConfigGetCurrent(); + if (config == NULL) { + FcPatternDestroy(pattern); + throw runtime_error("Failed to get current Fontconfig configuration."); + } + + // Match the font pattern + FcResult result; + FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); + FcPatternDestroy(pattern); + + if (matched_pattern == NULL) { + FcPatternDestroy(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) { + 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(file_path)); + FcPatternDestroy(matched_pattern); + FcFini(); + return Asset(font_file_path); +} -- cgit v1.2.3 From 56342ffab9daade7802b015ac83b9c47a8b9b18b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 19:36:40 +0100 Subject: make format --- src/crepe/api/Text.cpp | 6 ++---- src/crepe/api/Text.h | 2 +- src/crepe/facade/FontFacade.cpp | 2 +- src/crepe/facade/FontFacade.h | 2 +- src/example/loadfont.cpp | 5 +++-- 5 files changed, 8 insertions(+), 9 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index e6db140..5b2befe 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -2,13 +2,11 @@ #include "Text.h" - 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) : UIObject(id, dimensions, offset), text(text), - data(data), - font(FontFacade::get_font_asset(font_family)) { -} + data(data), + font(FontFacade::get_font_asset(font_family)) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 13b4375..fbb1ad6 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -4,8 +4,8 @@ #include "../Component.h" -#include "Color.h" #include "Asset.h" +#include "Color.h" #include "UIObject.h" namespace crepe { diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 12e10ed..708b2ff 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include "FontFacade.h" diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 1b835d4..0e6b7da 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -22,7 +22,7 @@ public: * \param font_family Name of the font family name. * \return Asset with filepath to the font. */ - static Asset get_font_asset(const std::string font_family); + static Asset get_font_asset(const std::string font_family); }; } // namespace crepe diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 0ea6d86..508197a 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -17,8 +17,9 @@ int main() { 1, vec2(100, 100), vec2(0, 0), "test test", "OpenSymbol", Text::Data{}); std::cout << "Path: " << label->font.get_path() << std::endl; - std::unique_ptr label2 = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "test test", "fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::unique_ptr label2 + = std::make_unique(1, vec2(100, 100), vec2(0, 0), "test test", + "fsaafdafsdafsdafsdasfdds", Text::Data{}); std::cout << "Path: " << label2->font.get_path() << std::endl; } catch (const std::exception & e) { std::cout << "Standard exception thrown: " << e.what() << std::endl; -- cgit v1.2.3 From 58c57578cae755422c35acfbbdf9ad399b4fd826 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 21:49:15 +0100 Subject: very illegal commit do not push --- src/crepe/api/Script.h | 3 ++- src/crepe/facade/FontFacade.cpp | 4 ++-- src/example/CMakeLists.txt | 1 + src/example/FontExample.cpp | 51 +++++++++++++++++++++++++++++++++++++++++ src/example/loadfont.cpp | 2 +- 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/example/FontExample.cpp (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 668e5d1..ee45b8d 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,6 +156,7 @@ private: void subscribe_internal(const EventHandler & callback, event_channel_t channel); protected: + OptionalRef mediator; // NOTE: This must be the only constructor on Script, see "Late references" below Script() = default; //! Only \c BehaviorScript instantiates Script @@ -190,7 +191,7 @@ private: //! Reference to parent component OptionalRef active; //! Mediator reference - OptionalRef mediator; + //! \} private: diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 708b2ff..a63b022 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -16,7 +16,7 @@ Asset FontFacade::get_font_asset(const string font_family) { if (pattern == NULL) { throw runtime_error("Failed to create font pattern."); } - + // Default configuration FcConfig * config = FcConfigGetCurrent(); if (config == NULL) { @@ -43,7 +43,7 @@ Asset FontFacade::get_font_asset(const string font_family) { } // Convert the file path to a string - string font_file_path(reinterpret_cast(file_path)); + string font_file_path = reinterpret_cast(file_path); FcPatternDestroy(matched_pattern); FcFini(); return Asset(font_file_path); diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index 0e2d5e2..f62414e 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -20,4 +20,5 @@ add_example(rendering_particle) add_example(game) add_example(button) add_example(loadfont) +add_example(FontExample) add_example(AITest) diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp new file mode 100644 index 0000000..620cf8e --- /dev/null +++ b/src/example/FontExample.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace crepe; +using namespace std; +using namespace std::chrono; +class TestScript : public Script{ + public: + steady_clock::time_point start_time; + virtual void init() override{ + start_time = steady_clock::now(); + } + virtual void update() override{ + auto now = steady_clock::now(); + auto elapsed = duration_cast(now - start_time).count(); + + if (elapsed >= 1) { + Mediator& med = mediator; + EventManager& event_mgr = med.event_manager; + event_mgr.trigger_event(); + } + } +}; +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(vec2(100, 100), vec2(0, 0), "test test", + "OpenSymbol", Text::Data{}); + text_object.add_component(); + + } + std::string get_name(){ return "hey";} +}; +int main() { + + + + return 0; +} diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index a77e457..f863053 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -13,7 +13,7 @@ int main() { // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; - ComponentManager component_manager{mediator}; + // ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text -- cgit v1.2.3 From fce10251d772af129531896965a908dc6d881c4b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 23:00:13 +0100 Subject: small changes --- src/crepe/facade/Font.cpp | 1 + src/crepe/facade/Font.h | 4 +++- src/crepe/facade/FontFacade.cpp | 1 - 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 333e500..74dfe18 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,4 +1,5 @@ #include "../api/Config.h" +#include "../api/Asset.h" #include "Font.h" diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index e93bfe9..3ff156f 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,13 +1,14 @@ #pragma once + #include #include #include "../Resource.h" -#include "../api/Asset.h" #include "../api/Config.h" namespace crepe { +class Asset; /** * \brief Resource for managing font creation and destruction * @@ -16,6 +17,7 @@ namespace crepe { * when this object is destroyed. */ class Font : public Resource { + public: /** * \param src The Asset containing the font file path and metadata to load the font. diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index a63b022..4a991c6 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,3 @@ -#include #include #include -- cgit v1.2.3 From d63eb7302d05fbe9b4c044ece3444e8ac4e56e02 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 23:00:33 +0100 Subject: make format --- src/crepe/api/Script.h | 2 +- src/crepe/facade/Font.cpp | 2 +- src/crepe/facade/FontFacade.cpp | 2 +- src/example/FontExample.cpp | 52 +++++++++++++++++++---------------------- src/example/loadfont.cpp | 18 +++++++------- 5 files changed, 36 insertions(+), 40 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index ee45b8d..a24e32e 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -191,7 +191,7 @@ private: //! Reference to parent component OptionalRef active; //! Mediator reference - + //! \} private: diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 74dfe18..d419974 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,5 +1,5 @@ -#include "../api/Config.h" #include "../api/Asset.h" +#include "../api/Config.h" #include "Font.h" diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 4a991c6..aa9d00c 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -15,7 +15,7 @@ Asset FontFacade::get_font_asset(const string font_family) { if (pattern == NULL) { throw runtime_error("Failed to create font pattern."); } - + // Default configuration FcConfig * config = FcConfigGetCurrent(); if (config == NULL) { diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index c2f21c4..3f5af48 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -1,49 +1,45 @@ -#include #include +#include +#include +#include +#include +#include +#include +#include #include #include +#include #include #include -#include -#include -#include -#include -#include -#include #include #include #include -#include using namespace crepe; using namespace std; using namespace std::chrono; -class TestScript : public Script{ - public: +class TestScript : public Script { +public: steady_clock::time_point start_time; - virtual void init() override{ - start_time = steady_clock::now(); - } - virtual void update() override{ + virtual void init() override { start_time = steady_clock::now(); } + virtual void update() override { auto now = steady_clock::now(); - auto elapsed = duration_cast(now - start_time).count(); + auto elapsed = duration_cast(now - start_time).count(); - if (elapsed >= 5) { - Mediator& med = mediator; - EventManager& event_mgr = med.event_manager; + if (elapsed >= 5) { + Mediator & med = mediator; + EventManager & event_mgr = med.event_manager; event_mgr.trigger_event(); - } + } } }; -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(vec2(100, 100), vec2(0, 0), "test test", - "Noto Sans", Text::Data{}); +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(vec2(100, 100), vec2(0, 0), "test test", "Noto Sans", + Text::Data{}); text_object.add_component().set_script(); - text_object.add_component(ivec2{300,300},vec2{100,100},Camera::Data{ - }); - + text_object.add_component(ivec2{300, 300}, vec2{100, 100}, Camera::Data{}); } std::string get_name() const override { return "hey"; } }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index f863053..ce287b4 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include #include #include @@ -25,14 +25,14 @@ int main() { = std::make_unique(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(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; - } + ResourceManager & resource_mgr = mediator.resource_manager; + const Font & res = resource_manager.get(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; } -- cgit v1.2.3 From 3b5b5258b0f46a3492a7fd777908dfb01e15417b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 10:48:43 +0100 Subject: code not working --- src/crepe/facade/Font.cpp | 4 +++- src/crepe/facade/Font.h | 7 +++++++ src/crepe/facade/FontFacade.cpp | 6 +++--- src/crepe/facade/FontFacade.h | 2 +- src/crepe/system/RenderSystem.cpp | 10 ++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index d419974..5af943d 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,3 +1,5 @@ +#include + #include "../api/Asset.h" #include "../api/Config.h" @@ -15,7 +17,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 3ff156f..16f8cb6 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -24,7 +24,14 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); + Font(const Font &) = delete; + Font &operator=(const Font &) = delete; + // Default move constructor and move assignment operator + Font(Font &&) noexcept = delete; + Font &operator=(Font &&) noexcept = delete; + + ~Font() = default; /** * \brief Gets the underlying TTF_Font resource. * diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index aa9d00c..d447b6d 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -6,7 +6,7 @@ using namespace crepe; using namespace std; -Asset FontFacade::get_font_asset(const string font_family) { +Asset FontFacade::get_font_asset(const string& font_family) { if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } @@ -19,7 +19,7 @@ Asset FontFacade::get_font_asset(const string font_family) { // Default configuration FcConfig * config = FcConfigGetCurrent(); if (config == NULL) { - FcPatternDestroy(pattern); + // FcPatternDestroy(pattern); throw runtime_error("Failed to get current Fontconfig configuration."); } @@ -37,7 +37,7 @@ Asset FontFacade::get_font_asset(const string font_family) { FcChar8 * file_path = nullptr; if (FcPatternGetString(matched_pattern, FC_FILE, 0, &file_path) != FcResultMatch || file_path == NULL) { - FcPatternDestroy(matched_pattern); + // FcPatternDestroy(matched_pattern); throw runtime_error("Failed to get font file path."); } diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 0e6b7da..fc200d6 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -22,7 +22,7 @@ public: * \param font_family Name of the font family name. * \return Asset with filepath to the font. */ - static Asset get_font_asset(const std::string font_family); + static Asset get_font_asset(const std::string& font_family); }; } // namespace crepe diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index afd9548..5aa00b5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -8,6 +8,8 @@ #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" @@ -120,8 +122,13 @@ void RenderSystem::render() { this->update_camera(); RefVector sprites = mgr.get_components_by_type(); + ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); + RefVector texts = mgr.get_components_by_type(); + for(const Text& text : texts){ + const Font & res = resource_manager.get(text.font); + } for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; const Transform & transform @@ -132,5 +139,8 @@ void RenderSystem::render() { if (rendered_particles) continue; this->render_normal(sprite, transform); + + + } } -- cgit v1.2.3 From b99f5fc0f52fdd4ec96be844e643060503a8860b Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:22:34 +0100 Subject: text now working with optional --- src/crepe/api/Asset.h | 2 +- src/crepe/api/LoopManager.h | 7 ++++--- src/crepe/api/Script.h | 5 +++-- src/crepe/api/Text.cpp | 8 +++++--- src/crepe/api/Text.h | 15 +++++++++++---- src/crepe/facade/Font.cpp | 3 +-- src/crepe/facade/Font.h | 10 +--------- src/crepe/facade/FontFacade.cpp | 11 ++++++++--- src/crepe/facade/FontFacade.h | 12 +++++++++--- src/crepe/facade/SDLContext.cpp | 5 +++++ src/crepe/facade/SDLContext.h | 15 +++++++++++++++ src/crepe/system/RenderSystem.cpp | 28 +++++++++++++++++++++++++--- src/crepe/system/RenderSystem.h | 10 ++++++++-- src/example/FontExample.cpp | 6 ++++-- src/example/loadfont.cpp | 31 ++++++++++++++++++------------- 15 files changed, 118 insertions(+), 50 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') 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 & callback, event_channel_t channel); protected: - OptionalRef 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 active; //! Mediator reference - + OptionalRef mediator; //! \} private: diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 5b2befe..0624c98 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -4,9 +4,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 font) : UIObject(id, dimensions, offset), text(text), data(data), - font(FontFacade::get_font_asset(font_family)) {} + 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 +#include #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 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 font; //! Data instance Data data; + }; } // namespace crepe diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 5af943d..558641f 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -9,8 +9,7 @@ using namespace std; using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) - : Resource(src, mediator), - font(nullptr, TTF_CloseFont) { + : Resource(src, mediator){ Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); diff --git a/src/crepe/facade/Font.h b/src/crepe/facade/Font.h index 16f8cb6..b208d96 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -24,14 +24,6 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); - Font(const Font &) = delete; - Font &operator=(const Font &) = delete; - - // Default move constructor and move assignment operator - Font(Font &&) noexcept = delete; - Font &operator=(Font &&) noexcept = delete; - - ~Font() = default; /** * \brief Gets the underlying TTF_Font resource. * @@ -44,7 +36,7 @@ public: private: //! The SDL_ttf font object with custom deleter. - std::unique_ptr> font; + std::unique_ptr> font = nullptr; }; } // namespace crepe diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index d447b6d..0a8ba5f 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,5 @@ #include +#include #include #include "FontFacade.h" @@ -6,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(font_family.c_str())); if (pattern == NULL) { @@ -32,7 +39,6 @@ Asset FontFacade::get_font_asset(const string& font_family) { FcPatternDestroy(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 @@ -44,6 +50,5 @@ 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(); return Asset(font_file_path); } diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index fc200d6..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 d1d109c..8ffaad9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -434,7 +434,12 @@ std::vector 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 efdd6fe..ffa3cc0 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; @@ -239,6 +240,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 5aa00b5..18f6393 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -22,6 +23,7 @@ using namespace crepe; using namespace std; + void RenderSystem::clear_screen() { SDLContext & ctx = this->mediator.sdl_context; ctx.clear_screen(); @@ -124,9 +126,11 @@ void RenderSystem::render() { RefVector sprites = mgr.get_components_by_type(); ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); - RefVector texts = mgr.get_components_by_type(); - for(const Text& text : texts){ - const Font & res = resource_manager.get(text.font); + RefVector text_components = mgr.get_components_by_type(); + for(Text& text : text_components){ + const Transform & transform + = mgr.get_components_by_id(text.game_object_id).front().get(); + this->render_text(text,transform); } for (const Sprite & sprite : sorted_sprites) { @@ -143,4 +147,22 @@ void RenderSystem::render() { } + } +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_asset); + } +} + + + diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index fc7b46e..56a0553 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. * @@ -50,7 +50,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 #include #include +#include #include #include #include @@ -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(vec2(100, 100), vec2(0, 0), "test test", "Noto Sans", - Text::Data{}); + text_object.add_component(vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}); text_object.add_component().set_script(); text_object.add_component(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(); engine.start(); diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index ce287b4..9d59afc 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -2,10 +2,12 @@ #include #include #include +#include #include #include #include #include +#include #include using namespace crepe; int main() { @@ -18,21 +20,24 @@ int main() { try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( - 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 label2 - = std::make_unique(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(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(1, vec2(100, 100), vec2(0, 0),"fsaafdafsdafsdafsdasfdds", Text::Data{}); + Asset asset = Asset("test test"); + label->font = std::make_optional(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(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; } -- cgit v1.2.3 From 707db8c94b6bb3921105f40658aab13511d8df07 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:29:02 +0100 Subject: removed iostream --- src/crepe/facade/FontFacade.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 0a8ba5f..08ff31c 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,5 +1,4 @@ #include -#include #include #include "FontFacade.h" -- cgit v1.2.3 From 69ca7fdd738fd4ed98aefc07bab5a43486a55619 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 13:34:19 +0100 Subject: final changes --- src/crepe/api/LoopManager.h | 3 +-- src/crepe/api/Script.h | 1 - src/crepe/api/Text.cpp | 8 ++++---- src/crepe/api/Text.h | 11 +++++------ src/crepe/facade/Font.cpp | 3 +-- src/crepe/facade/FontFacade.cpp | 10 ++++------ src/crepe/facade/FontFacade.h | 10 +++++----- src/crepe/facade/SDLContext.cpp | 2 +- src/crepe/facade/SDLContext.h | 6 ++++-- src/crepe/system/RenderSystem.cpp | 35 ++++++++++++++--------------------- src/example/FontExample.cpp | 5 +++-- src/example/loadfont.cpp | 10 +++++----- 12 files changed, 47 insertions(+), 57 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index 1725810..1d23cbf 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -71,7 +71,7 @@ private: private: //! Global context Mediator mediator; - + //! SDLContext instance SDLContext sdl_context{mediator}; //! Component manager instance @@ -86,7 +86,6 @@ private: ResourceManager resource_manager{mediator}; //! Save manager instance SaveManager save_manager{mediator}; - private: /** diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 4503525..8bca38a 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,7 +156,6 @@ private: void subscribe_internal(const EventHandler & callback, event_channel_t channel); protected: - // NOTE: This must be the only constructor on Script, see "Late references" below Script() = default; //! Only \c BehaviorScript instantiates Script diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 0624c98..58dc6c6 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -4,11 +4,11 @@ using namespace crepe; -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 font) +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 font) : UIObject(id, dimensions, offset), text(text), data(data), font_family(font_family), - font(font) { -} + font(font) {} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index ab72bc0..92cca18 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -1,7 +1,7 @@ #pragma once +#include #include -#include #include "../Component.h" @@ -50,10 +50,10 @@ public: * \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 & font_family, const Data & data, - const std::string & text = "", std::optional font = std::nullopt); - + 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 font = std::nullopt); + //! Label text. std::string text = ""; //! font family name @@ -62,7 +62,6 @@ public: std::optional font; //! Data instance Data data; - }; } // namespace crepe diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 558641f..f202c05 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -8,8 +8,7 @@ using namespace std; using namespace crepe; -Font::Font(const Asset & src, Mediator & mediator) - : Resource(src, mediator){ +Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); TTF_Font * loaded_font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 08ff31c..7edfeb8 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -6,16 +6,14 @@ using namespace crepe; using namespace std; -FontFacade::FontFacade(){ +FontFacade::FontFacade() { if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } } -FontFacade::~FontFacade(){ - FcFini(); -} -Asset FontFacade::get_font_asset(const string& font_family) { - +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(font_family.c_str())); if (pattern == NULL) { diff --git a/src/crepe/facade/FontFacade.h b/src/crepe/facade/FontFacade.h index 2e08f3f..9761070 100644 --- a/src/crepe/facade/FontFacade.h +++ b/src/crepe/facade/FontFacade.h @@ -15,10 +15,10 @@ 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; + 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. @@ -28,7 +28,7 @@ public: * \param font_family Name of the font family name. * \return Asset with filepath to the corresponding font. */ - 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 8ffaad9..c88687e 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -440,6 +440,6 @@ void SDLContext::set_color_texture(const Texture & texture, const Color & color) SDL_SetTextureAlphaMod(texture.get_img(), color.a); } -Asset SDLContext::get_font_from_name(const std::string& font_family){ +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 e348ff6..6f6eddb 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -241,9 +241,11 @@ private: * - this is defined in this class because get_events() needs this information aswell */ CameraAuxiliaryData cam_aux_data; -private: + +private: //! instance of the font_facade FontFacade font_facade{}; + public: /** * \brief Function to Get asset from font_family @@ -254,7 +256,7 @@ public: * * \return asset with the font style absolute path */ - Asset get_font_from_name(const std::string& font_family); + 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 5fc98a9..a03c636 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -10,8 +10,8 @@ #include "../api/ParticleEmitter.h" #include "../api/Sprite.h" #include "../api/Text.h" -#include "../facade/Font.h" #include "../api/Transform.h" +#include "../facade/Font.h" #include "../facade/SDLContext.h" #include "../facade/Texture.h" #include "../manager/ComponentManager.h" @@ -23,7 +23,6 @@ using namespace crepe; using namespace std; - void RenderSystem::clear_screen() { SDLContext & ctx = this->mediator.sdl_context; ctx.clear_screen(); @@ -126,12 +125,11 @@ void RenderSystem::render() { RefVector sprites = mgr.get_components_by_type(); ResourceManager & resource_manager = this->mediator.resource_manager; RefVector sorted_sprites = this->sort(sprites); - RefVector text_components = mgr.get_components_by_type(); - for(Text& text : text_components){ + RefVector text_components = mgr.get_components_by_type(); + for (Text & text : text_components) { const Transform & transform = mgr.get_components_by_id(text.game_object_id).front().get(); - this->render_text(text,transform); - + this->render_text(text, transform); } for (const Sprite & sprite : sorted_sprites) { if (!sprite.active) continue; @@ -143,25 +141,20 @@ 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; + SDLContext & ctx = this->mediator.sdl_context; - if (!text.font.has_value()) { - text.font = ctx.get_font_from_name(text.font_family); - } + if (!text.font.has_value()) { + text.font = ctx.get_font_from_name(text.font_family); + } - ResourceManager & resource_manager = this->mediator.resource_manager; + ResourceManager & resource_manager = this->mediator.resource_manager; - if (!text.font.has_value()) {return;} - const Asset& font_asset = text.font.value(); - const Font & res = resource_manager.get(font_asset); + if (!text.font.has_value()) { + return; + } + const Asset & font_asset = text.font.value(); + const Font & res = resource_manager.get(font_asset); } - - - diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index 7b2dadb..6a334b1 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -11,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -37,7 +37,8 @@ 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(vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}); + text_object.add_component(vec2(100, 100), vec2(0, 0), "OpenSymbol", + Text::Data{}); text_object.add_component().set_script(); text_object.add_component(ivec2{300, 300}, vec2{100, 100}, Camera::Data{}); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 36d00dd..ed67ffa 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,14 +1,14 @@ #include +#include #include #include #include -#include #include #include #include #include -#include #include +#include using namespace crepe; int main() { @@ -20,11 +20,11 @@ int main() { try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{},"test text", Asset("")); + 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 label2 - = std::make_unique(1, vec2(100, 100), vec2(0, 0),"fsaafdafsdafsdafsdasfdds", Text::Data{}); + std::unique_ptr label2 = std::make_unique( + 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; -- cgit v1.2.3 From e1e7ead5df44383c78c8da65fc5916be13d111b6 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 15:14:32 +0100 Subject: feedback changes --- src/crepe/api/Text.cpp | 6 ++---- src/crepe/api/Text.h | 3 +-- src/crepe/facade/FontFacade.cpp | 48 ++++++++++++++++++++++------------------- src/example/loadfont.cpp | 7 ++++-- 4 files changed, 34 insertions(+), 30 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 58dc6c6..6c632f3 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -5,10 +5,8 @@ using namespace crepe; 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 font) + const std::string & font_family, const Data & data, const std::string & text) : UIObject(id, dimensions, offset), text(text), data(data), - font_family(font_family), - font(font) {} + font_family(font_family){} diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 92cca18..c30dc80 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -51,8 +51,7 @@ public: * \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 & font_family, const Data & data, const std::string & text = "", - std::optional font = std::nullopt); + const std::string & font_family, const Data & data, const std::string & text = ""); //! Label text. std::string text = ""; diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 7edfeb8..5382f1a 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,51 +1,55 @@ #include #include +#include +#include +#include #include "FontFacade.h" -using namespace crepe; using namespace std; +using namespace crepe; FontFacade::FontFacade() { if (!FcInit()) { throw runtime_error("Failed to initialize Fontconfig."); } } + FontFacade::~FontFacade() { FcFini(); } -Asset FontFacade::get_font_asset(const string & font_family) { +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) { + FcPattern* raw_pattern = FcNameParse(reinterpret_cast(font_family.c_str())); + if (!raw_pattern) { throw runtime_error("Failed to create font pattern."); } - // Default configuration - FcConfig * config = FcConfigGetCurrent(); - if (config == NULL) { - // FcPatternDestroy(pattern); + std::unique_ptr> pattern( + raw_pattern, + [](FcPattern* p) { FcPatternDestroy(p); } + ); + + FcConfig* config = FcConfigGetCurrent(); + if (!config) { throw runtime_error("Failed to get current Fontconfig configuration."); } - // Match the font pattern FcResult result; - FcPattern * matched_pattern = FcFontMatch(config, pattern, &result); - FcPatternDestroy(pattern); - - if (matched_pattern == NULL) { - FcPatternDestroy(matched_pattern); + FcPattern* raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); + if (!raw_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) { - // FcPatternDestroy(matched_pattern); + + std::unique_ptr> matched_pattern( + raw_matched_pattern, + [](FcPattern* p) { FcPatternDestroy(p); } + ); + + FcChar8* file_path = nullptr; + if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { throw runtime_error("Failed to get font file path."); } - // Convert the file path to a string - string font_file_path = reinterpret_cast(file_path); - FcPatternDestroy(matched_pattern); + string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 78b3adb..788fac4 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -14,15 +15,17 @@ int main() { // SDLFontContext font_facade; Mediator mediator; + FontFacade font_facade{}; SDLContext sdl_context{mediator}; // ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text std::unique_ptr label = std::make_unique( - 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text", Asset("")); + 1, vec2(100, 100), vec2(0, 0), "OpenSymbol", Text::Data{}, "test text"); // std::cout << "Path: " << label->font.get_path() << std::endl; - + Asset asset1 = font_facade.get_font_asset("OpenSymbol"); + std::cout << asset1.get_path() << std::endl; std::unique_ptr label2 = std::make_unique( 1, vec2(100, 100), vec2(0, 0), "fsaafdafsdafsdafsdasfdds", Text::Data{}); Asset asset = Asset("test test"); -- cgit v1.2.3 From 2772bb158f10fc051d35006041303f2e5418e817 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Tue, 17 Dec 2024 15:15:12 +0100 Subject: make format --- src/crepe/api/Text.cpp | 2 +- src/crepe/facade/FontFacade.cpp | 34 ++++++++++++++++------------------ src/example/loadfont.cpp | 2 +- 3 files changed, 18 insertions(+), 20 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 6c632f3..54a4370 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -9,4 +9,4 @@ Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, : UIObject(id, dimensions, offset), text(text), data(data), - font_family(font_family){} + font_family(font_family) {} diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 5382f1a..5db06d2 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,7 +1,7 @@ #include -#include -#include #include +#include +#include #include #include "FontFacade.h" @@ -17,39 +17,37 @@ FontFacade::FontFacade() { FontFacade::~FontFacade() { FcFini(); } -Asset FontFacade::get_font_asset(const string& font_family) { - // Create a pattern to search for the font family - FcPattern* raw_pattern = FcNameParse(reinterpret_cast(font_family.c_str())); +Asset FontFacade::get_font_asset(const string & font_family) { + + FcPattern * raw_pattern + = FcNameParse(reinterpret_cast(font_family.c_str())); if (!raw_pattern) { throw runtime_error("Failed to create font pattern."); } - std::unique_ptr> pattern( - raw_pattern, - [](FcPattern* p) { FcPatternDestroy(p); } - ); + std::unique_ptr> pattern( + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); - FcConfig* config = FcConfigGetCurrent(); + FcConfig * config = FcConfigGetCurrent(); if (!config) { throw runtime_error("Failed to get current Fontconfig configuration."); } FcResult result; - FcPattern* raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); + FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); if (!raw_matched_pattern) { throw runtime_error("No matching font found."); } - std::unique_ptr> matched_pattern( - raw_matched_pattern, - [](FcPattern* p) { FcPatternDestroy(p); } - ); + std::unique_ptr> matched_pattern( + raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); - FcChar8* file_path = nullptr; - if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch || !file_path) { + FcChar8 * file_path = nullptr; + if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch + || !file_path) { throw runtime_error("Failed to get font file path."); } - string font_file_path = reinterpret_cast(file_path); + string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 788fac4..e459332 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -2,8 +2,8 @@ #include #include #include -#include #include +#include #include #include #include -- cgit v1.2.3 From b4335bd670cb7f3f622e6ab701123cfbabd15d37 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 15:32:57 +0100 Subject: fix tests + nitpick #77 --- src/crepe/facade/FontFacade.cpp | 32 +++++++++++++------------------- src/test/InputTest.cpp | 7 ++++++- 2 files changed, 19 insertions(+), 20 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index 5db06d2..d47095f 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -10,44 +10,38 @@ using namespace std; using namespace crepe; FontFacade::FontFacade() { - if (!FcInit()) { + if (!FcInit()) throw runtime_error("Failed to initialize Fontconfig."); - } } FontFacade::~FontFacade() { FcFini(); } Asset FontFacade::get_font_asset(const string & font_family) { - FcPattern * raw_pattern = FcNameParse(reinterpret_cast(font_family.c_str())); - if (!raw_pattern) { - throw runtime_error("Failed to create font pattern."); - } + if (raw_pattern == NULL) throw runtime_error("Failed to create font pattern."); - std::unique_ptr> pattern( - raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); + unique_ptr> pattern{ + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); } + }; FcConfig * config = FcConfigGetCurrent(); - if (!config) { - throw runtime_error("Failed to get current Fontconfig configuration."); - } + if (config == NULL) throw runtime_error("Failed to get current Fontconfig configuration."); FcResult result; FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); - if (!raw_matched_pattern) { - throw runtime_error("No matching font found."); - } + if (raw_matched_pattern == NULL) throw runtime_error("No matching font found."); - std::unique_ptr> matched_pattern( - raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }); + unique_ptr> matched_pattern = { + raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); } + }; FcChar8 * file_path = nullptr; - if (FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path) != FcResultMatch - || !file_path) { + FcResult res = FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path); + if (res != FcResultMatch || file_path == NULL) throw runtime_error("Failed to get font file path."); - } string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } + diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index d893276..2d844d4 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -1,7 +1,11 @@ -#include "system/RenderSystem.h" #include + +#include +#include + #define protected public #define private public + #include "api/KeyCodes.h" #include "manager/ComponentManager.h" #include "manager/EventManager.h" @@ -29,6 +33,7 @@ public: SDLContext sdl_context{mediator}; InputSystem input_system{mediator}; + ResourceManager resman{mediator}; RenderSystem render{mediator}; EventManager event_manager{mediator}; //GameObject camera; -- cgit v1.2.3 From a8ccf7fe8662086bb223aa4eafd0f85e717d16cf Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 17 Dec 2024 15:33:31 +0100 Subject: `make format` --- mwe/events/include/event.h | 2 +- src/crepe/facade/FontFacade.cpp | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) (limited to 'src/crepe/facade/FontFacade.cpp') diff --git a/mwe/events/include/event.h b/mwe/events/include/event.h index e1b220b..ee1bf52 100644 --- a/mwe/events/include/event.h +++ b/mwe/events/include/event.h @@ -148,7 +148,7 @@ private: }; class ShutDownEvent : public Event { public: - ShutDownEvent() : Event("ShutDownEvent"){}; + ShutDownEvent() : Event("ShutDownEvent") {}; REGISTER_EVENT_TYPE(ShutDownEvent) diff --git a/src/crepe/facade/FontFacade.cpp b/src/crepe/facade/FontFacade.cpp index d47095f..87f95ab 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -10,8 +10,7 @@ using namespace std; using namespace crepe; FontFacade::FontFacade() { - if (!FcInit()) - throw runtime_error("Failed to initialize Fontconfig."); + if (!FcInit()) throw runtime_error("Failed to initialize Fontconfig."); } FontFacade::~FontFacade() { FcFini(); } @@ -22,8 +21,7 @@ Asset FontFacade::get_font_asset(const string & font_family) { if (raw_pattern == NULL) throw runtime_error("Failed to create font pattern."); unique_ptr> pattern{ - raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); } - }; + raw_pattern, [](FcPattern * p) { FcPatternDestroy(p); }}; FcConfig * config = FcConfigGetCurrent(); if (config == NULL) throw runtime_error("Failed to get current Fontconfig configuration."); @@ -32,9 +30,8 @@ Asset FontFacade::get_font_asset(const string & font_family) { FcPattern * raw_matched_pattern = FcFontMatch(config, pattern.get(), &result); if (raw_matched_pattern == NULL) throw runtime_error("No matching font found."); - unique_ptr> matched_pattern = { - raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); } - }; + unique_ptr> matched_pattern + = {raw_matched_pattern, [](FcPattern * p) { FcPatternDestroy(p); }}; FcChar8 * file_path = nullptr; FcResult res = FcPatternGetString(matched_pattern.get(), FC_FILE, 0, &file_path); @@ -44,4 +41,3 @@ Asset FontFacade::get_font_asset(const string & font_family) { string font_file_path = reinterpret_cast(file_path); return Asset(font_file_path); } - -- cgit v1.2.3