From 3a23e41255af0de3c5c7f5d9df981c8b205e291f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 13 Dec 2024 17:42:34 +0100 Subject: add timer functionality to script --- src/crepe/system/ScriptSystem.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index d6b2ca1..4afd2ba 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -11,6 +11,7 @@ void ScriptSystem::update() { dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; + LoopTimerManager & timer = this->mediator.loop_timer; RefVector behavior_scripts = mgr.get_components_by_type(); for (BehaviorScript & behavior_script : behavior_scripts) { @@ -23,6 +24,8 @@ void ScriptSystem::update() { script->init(); script->initialized = true; } - script->update(); + + duration_t delta_time = timer.get_delta_time(); + script->update(delta_time); } } -- cgit v1.2.3 From 8abc6008880dd9ed0c16a68a126b49f0eb03caa2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 16 Dec 2024 20:52:43 +0100 Subject: fonting --- src/crepe/api/Config.h | 2 +- src/crepe/api/Text.cpp | 5 ++++- src/crepe/facade/Font.cpp | 10 ++++++++-- src/crepe/facade/Font.h | 1 + src/crepe/facade/SDLContext.cpp | 24 ++++++++++++++++++++++++ src/crepe/facade/SDLContext.h | 4 ++++ src/crepe/system/RenderSystem.cpp | 19 +++++++++++++++++++ src/crepe/system/RenderSystem.h | 2 ++ src/example/rendering_particle.cpp | 25 +++++-------------------- 9 files changed, 68 insertions(+), 24 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h index 47a81b7..c20287d 100644 --- a/src/crepe/api/Config.h +++ b/src/crepe/api/Config.h @@ -26,7 +26,7 @@ struct Config final { * * Only messages with equal or higher priority than this value will be logged. */ - Log::Level level = Log::Level::INFO; + Log::Level level = Log::Level::DEBUG; /** * \brief Colored log output * diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 5b2befe..07d1705 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,4 +1,5 @@ #include "../facade/FontFacade.h" +#include "util/Log.h" #include "Text.h" @@ -9,4 +10,6 @@ Text::Text(game_object_id_t id, const vec2 & dimensions, const vec2 & offset, : UIObject(id, dimensions, offset), text(text), data(data), - font(FontFacade::get_font_asset(font_family)) {} + font(FontFacade::get_font_asset(font_family)) { + dbg_trace(); +} diff --git a/src/crepe/facade/Font.cpp b/src/crepe/facade/Font.cpp index 1ee3de1..83e8519 100644 --- a/src/crepe/facade/Font.cpp +++ b/src/crepe/facade/Font.cpp @@ -1,4 +1,5 @@ #include "../api/Config.h" +#include "util/Log.h" #include "Font.h" @@ -6,8 +7,8 @@ using namespace std; using namespace crepe; Font::Font(const Asset & src, Mediator & mediator) - : Resource(src, mediator), - font(nullptr, TTF_CloseFont) { + : Resource(src, mediator) { + dbg_trace(); 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); @@ -16,4 +17,9 @@ Font::Font(const Asset & src, Mediator & mediator) this->font = {font, [](TTF_Font * font) { TTF_CloseFont(font); }}; } +Font::~Font(){ + dbg_trace(); + this->font.reset(); +} + 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 e93bfe9..e9f5fa1 100644 --- a/src/crepe/facade/Font.h +++ b/src/crepe/facade/Font.h @@ -22,6 +22,7 @@ public: * \param mediator The Mediator object used for managing the SDL context or related systems. */ Font(const Asset & src, Mediator & mediator); + ~Font(); /** * \brief Gets the underlying TTF_Font resource. diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index d1d109c..4e969b9 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -21,6 +21,8 @@ #include "../api/Config.h" #include "../api/Sprite.h" #include "../util/Log.h" +#include "api/Text.h" +#include "facade/Font.h" #include "manager/Mediator.h" #include "SDLContext.h" @@ -283,6 +285,28 @@ void SDLContext::draw(const RenderContext & ctx) { angle, NULL, render_flip); } + +void SDLContext::draw_text(const Text & text, const Font & font){ + SDL_Color color { + .r = text.data.text_color.r, + .g = text.data.text_color.g, + .b = text.data.text_color.b, + .a = text.data.text_color.a, + }; + SDL_Surface * font_surface = TTF_RenderText_Solid(font.get_font(), text.text.c_str(), color); + SDL_Texture * font_texture = SDL_CreateTextureFromSurface(this->game_renderer.get(), font_surface); + SDL_FreeSurface(font_surface); + + SDL_FRect dstrect { + .x = text.offset.x, + .y = text.offset.y, + .w = text.dimensions.x, + .h = text.dimensions.y, + }; + SDL_RenderCopyExF(this->game_renderer.get(), font_texture, NULL, &dstrect , 0 , NULL, SDL_FLIP_NONE); + SDL_DestroyTexture(font_texture); +} + void SDLContext::update_camera_view(const Camera & cam, const vec2 & new_pos) { const Camera::Data & cam_data = cam.data; diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index efdd6fe..3f9f9ee 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -20,6 +20,8 @@ namespace crepe { class Texture; +class Text; +class Font; class Mediator; /** @@ -183,6 +185,8 @@ public: */ void draw(const RenderContext & ctx); + void draw_text(const Text & text, const Font & font); + //! Clears the screen, preparing for a new frame. void clear_screen(); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index afd9548..7f00891 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -15,6 +15,8 @@ #include "../manager/ResourceManager.h" #include "RenderSystem.h" +#include "api/Text.h" +#include "facade/Font.h" #include "types.h" using namespace crepe; @@ -67,9 +69,26 @@ RefVector RenderSystem::sort(RefVector & objs) const { void RenderSystem::update() { this->clear_screen(); this->render(); + this->render_text(); this->present_screen(); } + +void RenderSystem::render_text(){ + SDLContext & ctx = this->mediator.sdl_context; + ComponentManager & mgr = this->mediator.component_manager; + ResourceManager & resource_manager = this->mediator.resource_manager; + + RefVector texts = mgr.get_components_by_type(); + + for (Text & text : texts) { + if (!text.active) continue; + resource_manager.get(text.font); + //ctx.draw_text(text, font); + } + +} + bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) { ComponentManager & mgr = this->mediator.component_manager; diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index fc7b46e..ae55404 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -27,6 +27,8 @@ public: void update() override; private: + + void render_text(); //! Clears the screen in preparation for rendering. void clear_screen(); diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 13e625f..3857acc 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -1,4 +1,5 @@ #include "api/Asset.h" +#include "api/Text.h" #include #include #include @@ -43,11 +44,7 @@ using namespace std; class TestScene : public Scene { public: void load_scene() { - - cout << "TestScene" << endl; - Mediator & mediator = this->mediator; - ComponentManager & mgr = mediator.component_manager; - GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 1); + GameObject game_object = new_object("", "", vec2{0, 0}, 0, 1); Color color(255, 255, 255, 255); @@ -59,29 +56,17 @@ public: .flip = Sprite::FlipSettings{false, false}, .sorting_in_layer = 2, .order_in_layer = 2, - .size = {0, 100}, + .size = {0, 0}, .angle_offset = 0, .position_offset = {0, 0}, }); - //auto & anim = game_object.add_component(test_sprite,ivec2{32, 64}, uvec2{4,1}, Animator::Data{}); - //anim.set_anim(0); - - auto & cam = game_object.add_component(ivec2{720, 1280}, vec2{400, 400}, + auto & cam = game_object.add_component(ivec2{400, 400}, vec2{400, 400}, Camera::Data{ .bg_color = Color::WHITE, }); - function on_click = [&]() { cout << "button clicked" << std::endl; }; - function on_enter = [&]() { cout << "enter" << std::endl; }; - function on_exit = [&]() { cout << "exit" << std::endl; }; - - auto & button - = game_object.add_component