From 9f3af4431853288c6e1ec0ff5b8f5bb0e6379b66 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 20:58:02 +0100 Subject: added const --- src/crepe/api/Text.h | 2 +- src/example/loadfont.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index fbb1ad6..ec0bf74 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -53,7 +53,7 @@ public: //! Label text. std::string text = ""; //! Font asset variable - Asset font; + const Asset font; //! Data instance Data data; }; diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 35afdf7..a77e457 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -13,6 +13,7 @@ int main() { // SDLFontContext font_facade; Mediator mediator; SDLContext sdl_context{mediator}; + ComponentManager component_manager{mediator}; ResourceManager resource_manager{mediator}; try { // Correct way to create a unique pointer for Text -- 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 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 d33dbdff59693377d06d83225b0fe78c3168c464 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 16 Dec 2024 22:56:42 +0100 Subject: segmentation fault fixed --- src/crepe/facade/Font.cpp | 7 ++++--- src/example/FontExample.cpp | 17 +++++++++++------ 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 1ee3de1..333e500 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -10,10 +10,11 @@ Font::Font(const Asset & src, Mediator & mediator) font(nullptr, TTF_CloseFont) { Config & config = Config::get_instance(); const std::string FONT_PATH = src.get_path(); - TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); - if (font == NULL) + 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 = {font, [](TTF_Font * font) { TTF_CloseFont(font); }}; + } + this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(font); }}; } TTF_Font * Font::get_font() const { return this->font.get(); } diff --git a/src/example/FontExample.cpp b/src/example/FontExample.cpp index 620cf8e..c2f21c4 100644 --- a/src/example/FontExample.cpp +++ b/src/example/FontExample.cpp @@ -7,7 +7,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -25,7 +27,7 @@ class TestScript : public Script{ auto now = steady_clock::now(); auto elapsed = duration_cast(now - start_time).count(); - if (elapsed >= 1) { + if (elapsed >= 5) { Mediator& med = mediator; EventManager& event_mgr = med.event_manager; event_mgr.trigger_event(); @@ -37,15 +39,18 @@ class TestScene : public Scene{ 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(); + "Noto Sans", Text::Data{}); + text_object.add_component().set_script(); + text_object.add_component(ivec2{300,300},vec2{100,100},Camera::Data{ + }); } - std::string get_name(){ return "hey";} + std::string get_name() const override { return "hey"; } }; int main() { - - + LoopManager engine; + engine.add_scene(); + engine.start(); return 0; } -- 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(-) 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(-) 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