diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Script.h | 3 | ||||
-rw-r--r-- | src/crepe/api/Text.h | 2 | ||||
-rw-r--r-- | src/crepe/facade/Font.cpp | 16 | ||||
-rw-r--r-- | src/crepe/facade/Font.h | 4 | ||||
-rw-r--r-- | src/crepe/facade/FontFacade.cpp | 3 | ||||
-rw-r--r-- | src/example/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/example/FontExample.cpp | 52 | ||||
-rw-r--r-- | src/example/loadfont.cpp | 19 |
8 files changed, 76 insertions, 24 deletions
diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 668e5d1..a24e32e 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -156,6 +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 @@ -190,7 +191,7 @@ private: //! Reference to parent component OptionalRef<bool> active; //! Mediator reference - OptionalRef<Mediator> mediator; + //! \} private: 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/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index a9f1633..8c16563 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,3 +1,4 @@ +#include "../api/Asset.h" #include "../api/Config.h" #include "util/Log.h" #include <iostream> @@ -11,17 +12,12 @@ Font::Font(const Asset & src, Mediator & mediator) : Resource(src, mediator) { dbg_trace(); Config & config = Config::get_instance(); - const std::string & FONT_PATH = src.get_path(); - cout << FONT_PATH.c_str() << endl; - TTF_Font * font = TTF_OpenFont(FONT_PATH.c_str(), config.font.size); - if (font == NULL) + const std::string FONT_PATH = src.get_path(); + 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); }}; -} - -Font::~Font(){ - dbg_trace(); - this->font.reset(); + } + this->font = {loaded_font, [](TTF_Font * font) { TTF_CloseFont(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 e9f5fa1..91c59c0 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -1,13 +1,14 @@ #pragma once + #include <SDL2/SDL_ttf.h> #include <memory> #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 708b2ff..aa9d00c 100644 --- a/src/crepe/facade/FontFacade.cpp +++ b/src/crepe/facade/FontFacade.cpp @@ -1,4 +1,3 @@ -#include <SDL2/SDL_ttf.h> #include <fontconfig/fontconfig.h> #include <stdexcept> @@ -43,7 +42,7 @@ 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)); + string font_file_path = reinterpret_cast<const char *>(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..3f5af48 --- /dev/null +++ b/src/example/FontExample.cpp @@ -0,0 +1,52 @@ +#include <SDL2/SDL_ttf.h> +#include <chrono> +#include <crepe/api/Camera.h> +#include <crepe/api/GameObject.h> +#include <crepe/api/LoopManager.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/api/Text.h> +#include <crepe/facade/Font.h> +#include <crepe/facade/SDLContext.h> +#include <crepe/manager/EventManager.h> +#include <crepe/manager/Mediator.h> +#include <crepe/manager/ResourceManager.h> +#include <exception> +#include <iostream> +#include <memory> +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<seconds>(now - start_time).count(); + + if (elapsed >= 5) { + Mediator & med = mediator; + EventManager & event_mgr = med.event_manager; + event_mgr.trigger_event<ShutDownEvent>(); + } + } +}; +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<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() { + LoopManager engine; + engine.add_scene<TestScene>(); + engine.start(); + + return 0; +} diff --git a/src/example/loadfont.cpp b/src/example/loadfont.cpp index 35afdf7..ce287b4 100644 --- a/src/example/loadfont.cpp +++ b/src/example/loadfont.cpp @@ -1,5 +1,5 @@ -#include <crepe/api/Text.h> #include <SDL2/SDL_ttf.h> +#include <crepe/api/Text.h> #include <crepe/facade/Font.h> #include <crepe/facade/SDLContext.h> #include <crepe/manager/Mediator.h> @@ -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 @@ -24,14 +25,14 @@ int main() { = 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; - } + 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; } |