From e5b57f6a0aadf4f5ca597e4ffa3e557eec87b54b Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 20 Dec 2024 15:26:51 +0100 Subject: add world space to text rendering and removedlayers on text --- src/crepe/api/Text.h | 6 ------ src/crepe/facade/SDLContext.cpp | 16 +++++++++++----- src/example/rendering_particle.cpp | 24 +++++++++++++++--------- 3 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index da40141..fb5c7e5 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -28,12 +28,6 @@ public: */ unsigned int font_size = 16; - //! Layer sorting level of the text - const int sorting_in_layer = 0; - - //! Order within the sorting text - const int order_in_layer = 0; - //! Label text color. Color text_color = Color::BLACK; }; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 64c1fe2..9c45089 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -68,7 +68,7 @@ SDLContext::SDLContext(Mediator & mediator) { throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); } - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"); + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); mediator.sdl_context = *this; } @@ -232,10 +232,16 @@ void SDLContext::draw_text(const RenderText & data) { = {tmp_font_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }}; vec2 size = text.dimensions * cam_aux_data.render_scale * data.transform.scale; - vec2 screen_pos - = (absoluut_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) - * cam_aux_data.render_scale - - size / 2 + cam_aux_data.bar_size; + vec2 screen_pos = absoluut_pos; + if (text.world_space) { + screen_pos = (screen_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) + * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; + } else { + screen_pos + = (screen_pos + (cam_aux_data.zoomed_viewport) / 2) * cam_aux_data.render_scale + - size / 2 + cam_aux_data.bar_size; + } SDL_FRect dstrect{ .x = screen_pos.x, diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 5440fdd..0a2f946 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -7,8 +7,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -27,6 +27,7 @@ public: Color color(255, 255, 255, 255); + /* Asset img{"asset/texture/square.png"}; Sprite & test_sprite = game_object.add_component( @@ -49,6 +50,7 @@ public: .position_offset = {0, -1}, .world_space = false, }); + */ auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{5, 5}, Camera::Data{ @@ -56,21 +58,25 @@ public: .postion_offset = {1000, 1000}, }); - /* - game_object.add_component(vec2{1, 1}, vec2{0, -0.5}, "ComicSansMS", - Text::Data{.text_color = Color::RED}, "test TEST"); + game_object.add_component(vec2{1, 1}, vec2{0, -1}, "ComicSansMS", + Text::Data{ + .text_color = Color::RED, + }, + "test TEST"); - game_object.add_component(vec2{1, 1}, vec2{0, 0.5}, "ComicSansMS", - Text::Data{.text_color = Color::BLACK}, "TEST test"); - */ + game_object.add_component(vec2{1, 1}, vec2{0, 1}, "Ariel", + Text::Data{ + .text_color = Color::BLACK, + }, + "TEST test").world_space = true; } string get_name() const { return "TestScene"; }; }; int main(int argc, char * argv[]) { - LoopManager engine; + Engine engine; engine.add_scene(); - engine.start(); + engine.main(); return 0; } -- cgit v1.2.3 From fd09501a1f010126bda5816f17e6e21e204e79cb Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 20 Dec 2024 15:27:15 +0100 Subject: make format --- src/example/rendering_particle.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 0a2f946..c8db499 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -64,11 +64,14 @@ public: }, "test TEST"); - game_object.add_component(vec2{1, 1}, vec2{0, 1}, "Ariel", - Text::Data{ - .text_color = Color::BLACK, - }, - "TEST test").world_space = true; + game_object + .add_component(vec2{1, 1}, vec2{0, 1}, "Ariel", + Text::Data{ + .text_color = Color::BLACK, + }, + "TEST test") + .world_space + = true; } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From 631975d4a03642271f80baeb4494dd01bda9c76e Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 20 Dec 2024 15:32:19 +0100 Subject: moved sdl hint to draw sprites and added another to text with 0 instead of 2 beacause otherwise its blurry --- src/crepe/facade/SDLContext.cpp | 3 ++- src/example/rendering_particle.cpp | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 9c45089..672d55a 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -68,7 +68,6 @@ SDLContext::SDLContext(Mediator & mediator) { throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); } - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); mediator.sdl_context = *this; } @@ -173,6 +172,7 @@ SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { } void SDLContext::draw(const RenderContext & ctx) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); const Sprite::Data & data = ctx.sprite.data; SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) @@ -203,6 +203,7 @@ void SDLContext::draw(const RenderContext & ctx) { } void SDLContext::draw_text(const RenderText & data) { + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"); const Text & text = data.text; const Font & font = data.font; diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index c8db499..5eae00d 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -27,7 +27,6 @@ public: Color color(255, 255, 255, 255); - /* Asset img{"asset/texture/square.png"}; Sprite & test_sprite = game_object.add_component( @@ -50,7 +49,6 @@ public: .position_offset = {0, -1}, .world_space = false, }); - */ auto & cam = game_object.add_component(ivec2{1280, 720}, vec2{5, 5}, Camera::Data{ -- cgit v1.2.3 From 0ba3392d8d5ef1f69bda831c8e2be2efa505b632 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Fri, 20 Dec 2024 15:32:38 +0100 Subject: make format --- src/crepe/facade/SDLContext.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 672d55a..8e2e8a2 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -68,7 +68,6 @@ SDLContext::SDLContext(Mediator & mediator) { throw runtime_error(format("SDL_ttf initialization failed: {}", TTF_GetError())); } - mediator.sdl_context = *this; } -- cgit v1.2.3 From 296c80edd6727d6808ed0c98c001a8b456f1c037 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sat, 21 Dec 2024 10:27:16 +0100 Subject: implemented feedback --- src/crepe/api/Button.h | 6 +++--- src/crepe/api/Text.h | 12 ++---------- src/crepe/api/UIObject.h | 2 -- src/crepe/facade/SDLContext.cpp | 2 +- 4 files changed, 6 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index d42527e..25709a4 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,8 +1,5 @@ #pragma once -#include - -#include "Event.h" #include "UIObject.h" namespace crepe { @@ -43,6 +40,9 @@ private: friend class InputSystem; //! Indicates whether the mouse is currently hovering over the button bool hover = false; + + //! variable indicating if transform is relative to camera(false) or world(true) + bool world_space = false; }; } // namespace crepe diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index fb5c7e5..9d8573f 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -17,16 +17,8 @@ class Text : public UIObject { public: //! Text data that does not have to be set in the constructor struct Data { - /** - * \brief fontsize for text rendering - * - * \note this is not the actual font size that is loaded in. - * - * Since SDL_TTF requires the font size when loading in the font it is not possible to switch the font size. - * The default font size that is loaded is set in the Config. - * Instead this value is used to upscale the font texture which can cause blurring or distorted text when upscaling or downscaling too much. - */ - unsigned int font_size = 16; + //! variable indicating if transform is relative to camera(false) or world(true) + bool world_space = false; //! Label text color. Color text_color = Color::BLACK; diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h index f1318ab..f7f4fba 100644 --- a/src/crepe/api/UIObject.h +++ b/src/crepe/api/UIObject.h @@ -20,8 +20,6 @@ public: vec2 dimensions; //! Position offset relative to this GameObjects Transform vec2 offset; - //! variable indicating if transform is relative to camera(false) or world(true) - bool world_space = false; }; } // namespace crepe diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 8e2e8a2..17dcd04 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -233,7 +233,7 @@ void SDLContext::draw_text(const RenderText & data) { vec2 size = text.dimensions * cam_aux_data.render_scale * data.transform.scale; vec2 screen_pos = absoluut_pos; - if (text.world_space) { + if (text.data.world_space) { screen_pos = (screen_pos - cam_aux_data.cam_pos + (cam_aux_data.zoomed_viewport) / 2) * cam_aux_data.render_scale - size / 2 + cam_aux_data.bar_size; -- cgit v1.2.3 From 15220fd01798d88a3c1acd21b6b1220c1273c1ee Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 22 Dec 2024 13:52:14 +0100 Subject: button feedback --- src/crepe/api/Button.cpp | 7 +++++-- src/crepe/api/Button.h | 14 +++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index 40153c9..c4f7f2d 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,7 +2,10 @@ namespace crepe { -Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset) - : UIObject(id, dimensions, offset) {} +Button::Button( + game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const Data & data +) + : UIObject(id, dimensions, offset), + data(data) {} } // namespace crepe diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 25709a4..f6d363a 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -17,6 +17,12 @@ namespace crepe { * */ class Button : public UIObject { +public: + struct Data { + //! variable indicating if transform is relative to camera(false) or world(true) + bool world_space = false; + }; + public: /** * \brief Constructs a Button with the specified game object ID and dimensions. @@ -24,8 +30,11 @@ public: * \param id The unique ID of the game object associated with this button. * \param dimensions The width and height of the UIObject * \param offset The offset relative this GameObjects Transform + * \param data additional data the button has */ - Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); + Button( + game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const Data & data + ); /** * \brief Get the maximum number of instances for this component * @@ -41,8 +50,7 @@ private: //! Indicates whether the mouse is currently hovering over the button bool hover = false; - //! variable indicating if transform is relative to camera(false) or world(true) - bool world_space = false; + Data data; }; } // namespace crepe -- cgit v1.2.3 From 2882e217aa5354a6befdddaa2de886f66ff40879 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 22 Dec 2024 13:55:57 +0100 Subject: fixed some includes --- src/crepe/system/InputSystem.cpp | 4 ++-- src/crepe/system/InputSystem.h | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index b4a0633..a366d34 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -2,7 +2,7 @@ #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" -#include "util/Log.h" +#include "../api/Config.h" #include "InputSystem.h" @@ -213,7 +213,7 @@ bool InputSystem::is_mouse_inside_button( const Transform & cam_transform ) { vec2 actual_pos = transform.position + button.offset; - if (!button.world_space) { + if (!button.data.world_space) { actual_pos += cam_transform.position; } vec2 half_dimensions = button.dimensions / 2; diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 37311cc..21a3dfb 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,12 +1,9 @@ #pragma once -#include "../api/Config.h" #include "../facade/EventData.h" - #include "../api/Event.h" #include "../api/Metadata.h" #include "../types.h" -#include "../util/OptionalRef.h" #include "System.h" -- cgit v1.2.3 From 76f6c4d3e36b653aec79438ae4053ea24560236c Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 22 Dec 2024 13:56:17 +0100 Subject: make format --- src/crepe/system/InputSystem.cpp | 2 +- src/crepe/system/InputSystem.h | 2 +- src/example/rendering_particle.cpp | 24 ++++++++++++++---------- 3 files changed, 16 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index a366d34..91c9c64 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -1,8 +1,8 @@ #include "../api/Button.h" +#include "../api/Config.h" #include "../facade/SDLContext.h" #include "../manager/ComponentManager.h" #include "../manager/EventManager.h" -#include "../api/Config.h" #include "InputSystem.h" diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 21a3dfb..be62367 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -1,8 +1,8 @@ #pragma once -#include "../facade/EventData.h" #include "../api/Event.h" #include "../api/Metadata.h" +#include "../facade/EventData.h" #include "../types.h" #include "System.h" diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 9093fd5..8be781a 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -62,18 +62,22 @@ public: } ); - game_object.add_component(vec2{1, 1}, vec2{0, -1}, "ComicSansMS", - Text::Data{ - .text_color = Color::RED, - }, - "test TEST"); + game_object.add_component( + vec2 {1, 1}, vec2 {0, -1}, "ComicSansMS", + Text::Data { + .text_color = Color::RED, + }, + "test TEST" + ); game_object - .add_component(vec2{1, 1}, vec2{0, 1}, "Ariel", - Text::Data{ - .text_color = Color::BLACK, - }, - "TEST test") + .add_component( + vec2 {1, 1}, vec2 {0, 1}, "Ariel", + Text::Data { + .text_color = Color::BLACK, + }, + "TEST test" + ) .world_space = true; } -- cgit v1.2.3 From a5f8b34f4701863901e89abe5b0b604fd9e553b2 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 22 Dec 2024 20:18:44 +0100 Subject: ui fixes for offset --- src/crepe/api/Button.cpp | 2 +- src/crepe/api/Button.h | 4 +++- src/crepe/api/Text.cpp | 5 +++-- src/crepe/api/Text.h | 5 +++-- src/crepe/api/UIObject.h | 2 +- 5 files changed, 11 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index c4f7f2d..8eadd89 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -3,7 +3,7 @@ namespace crepe { Button::Button( - game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const Data & data + game_object_id_t id, const vec2 & dimensions, const Data & data, const vec2 & offset ) : UIObject(id, dimensions, offset), data(data) {} diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index f6d363a..d7dc747 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,6 +1,7 @@ #pragma once #include "UIObject.h" +#include "types.h" namespace crepe { @@ -33,7 +34,8 @@ public: * \param data additional data the button has */ Button( - game_object_id_t id, const vec2 & dimensions, const vec2 & offset, const Data & data + game_object_id_t id, const vec2 & dimensions, const Data & data, + const vec2 & offset = {0, 0} ); /** * \brief Get the maximum number of instances for this component diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index b24f0ac..29e36f1 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,10 +1,11 @@ #include "Text.h" +#include "types.h" 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 + game_object_id_t id, const vec2 & dimensions, const std::string & font_family, + const Data & data, const vec2 & offset, const std::string & text ) : UIObject(id, dimensions, offset), text(text), diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 0163bbb..37d86b4 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -6,6 +6,7 @@ #include "Asset.h" #include "Color.h" #include "UIObject.h" +#include "types.h" namespace crepe { /** @@ -35,8 +36,8 @@ 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 = "" + game_object_id_t id, const vec2 & dimensions, const std::string & font_family, + const Data & data, const vec2 & offset = {0, 0}, const std::string & text = "" ); //! Label text. diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h index f7f4fba..5c1fdf7 100644 --- a/src/crepe/api/UIObject.h +++ b/src/crepe/api/UIObject.h @@ -15,7 +15,7 @@ public: * \param dimensions width and height of the UIObject * \param offset Offset relative to the GameObject Transform */ - UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset); + UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset = {0,0}); //! Width and height of the UIObject vec2 dimensions; //! Position offset relative to this GameObjects Transform -- cgit v1.2.3 From b9051627db1741fe373c08ab43170c257625073f Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 24 Dec 2024 09:49:49 +0100 Subject: feedback --- src/crepe/api/Button.h | 8 +++++--- src/crepe/api/Text.cpp | 3 ++- src/crepe/api/Text.h | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index d7dc747..e986c04 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -1,7 +1,8 @@ #pragma once +#include "../types.h" + #include "UIObject.h" -#include "types.h" namespace crepe { @@ -46,13 +47,14 @@ public: */ virtual int get_instances_max() const { return 1; } +public: + Data data; + private: //! friend relation hover variable friend class InputSystem; //! Indicates whether the mouse is currently hovering over the button bool hover = false; - - Data data; }; } // namespace crepe diff --git a/src/crepe/api/Text.cpp b/src/crepe/api/Text.cpp index 29e36f1..e5b623d 100644 --- a/src/crepe/api/Text.cpp +++ b/src/crepe/api/Text.cpp @@ -1,5 +1,6 @@ +#include "../types.h" + #include "Text.h" -#include "types.h" using namespace crepe; diff --git a/src/crepe/api/Text.h b/src/crepe/api/Text.h index 37d86b4..8b3d53e 100644 --- a/src/crepe/api/Text.h +++ b/src/crepe/api/Text.h @@ -3,10 +3,11 @@ #include #include +#include "../types.h" + #include "Asset.h" #include "Color.h" #include "UIObject.h" -#include "types.h" namespace crepe { /** -- cgit v1.2.3 From 4526efe1b6dd63aaa177f8f35194989d3910d571 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Tue, 24 Dec 2024 09:50:10 +0100 Subject: make format --- src/crepe/api/UIObject.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/api/UIObject.h b/src/crepe/api/UIObject.h index 5c1fdf7..0d9b1f7 100644 --- a/src/crepe/api/UIObject.h +++ b/src/crepe/api/UIObject.h @@ -15,7 +15,7 @@ public: * \param dimensions width and height of the UIObject * \param offset Offset relative to the GameObject Transform */ - UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset = {0,0}); + UIObject(game_object_id_t id, const vec2 & dimensions, const vec2 & offset = {0, 0}); //! Width and height of the UIObject vec2 dimensions; //! Position offset relative to this GameObjects Transform -- cgit v1.2.3 From f693119c9e102a9b51a1015168ee2a56f2309dd1 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Sun, 5 Jan 2025 16:44:25 +0100 Subject: fixed AnimatorSystem bug so that it takes delta time instead of elapsed time, the Animator api has not been changed --- src/crepe/api/Animator.h | 7 +++++++ src/crepe/system/AnimatorSystem.cpp | 23 +++++++++++++++-------- src/example/rendering_particle.cpp | 35 ++++++----------------------------- 3 files changed, 28 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 102894d..efc2f6e 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,6 +1,7 @@ #pragma once #include "../types.h" +#include "../manager/LoopTimerManager.h" #include "Component.h" #include "Sprite.h" @@ -99,6 +100,12 @@ private: //! The maximum number of rows and columns inside the spritesheet const uvec2 grid_size; + // the time elapsed from a frame duration + duration_t elapsed_time = {}; + + // frame counter + unsigned int frame = 0; + //! Uses the spritesheet friend AnimatorSystem; }; diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index e5ab2fa..1a0cde9 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,7 +1,8 @@ +#include + #include "../api/Animator.h" #include "../manager/ComponentManager.h" #include "../manager/LoopTimerManager.h" -#include #include "AnimatorSystem.h" @@ -13,27 +14,33 @@ void AnimatorSystem::frame_update() { LoopTimerManager & timer = this->mediator.loop_timer; RefVector animations = mgr.get_components_by_type(); - float elapsed_time = duration_cast>(timer.get_elapsed_time()).count(); + duration_t elapsed_time = timer.get_delta_time(); for (Animator & a : animations) { if (!a.active) continue; if (a.data.fps == 0) continue; Animator::Data & ctx = a.data; - float frame_duration = 1.0f / ctx.fps; - int last_frame = ctx.row; + a.elapsed_time += elapsed_time; + duration_t frame_duration = 1000ms / ctx.fps; + + if (a.elapsed_time <= frame_duration) continue; + + a.elapsed_time = 0ms; + a.frame++; int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; + int curr_cycle_frame = (a.frame - ctx.cycle_start) % total_frames; - int curr_frame = static_cast(elapsed_time / frame_duration) % total_frames; + ctx.row = (ctx.cycle_start + curr_cycle_frame) % a.grid_size.x; + ctx.col = curr_cycle_frame / a.grid_size.x; - ctx.row = ctx.cycle_start + curr_frame; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; - a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h); + a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; - if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) { + if (!ctx.looping && a.frame >= cycle_end) { a.active = false; } } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 8be781a..0082b91 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -27,7 +27,7 @@ public: Color color(255, 255, 255, 255); - Asset img {"asset/texture/square.png"}; + Asset img {"asset/spritesheet/pokemon_spritesheet.png"}; Sprite & test_sprite = game_object.add_component( img, @@ -36,21 +36,17 @@ public: .flip = Sprite::FlipSettings {false, false}, .sorting_in_layer = 2, .order_in_layer = 2, - .size = {1, 1}, + .size = {1, 0}, .angle_offset = 0, .position_offset = {0, 1}, .world_space = false, } ); - //auto & emitter = game_object.add_component(test_sprite, ParticleEmitter::Data{}); - Sprite & test_sprite1 = game_object.add_component( - img, - Sprite::Data { - .color = color, - .size = {1, 1}, - .position_offset = {0, -1}, - .world_space = false, + game_object.add_component( + test_sprite, ivec2 {56, 56}, uvec2 {4, 4}, + Animator::Data { + .looping = true, } ); @@ -61,25 +57,6 @@ public: .postion_offset = {1000, 1000}, } ); - - game_object.add_component( - vec2 {1, 1}, vec2 {0, -1}, "ComicSansMS", - Text::Data { - .text_color = Color::RED, - }, - "test TEST" - ); - - game_object - .add_component( - vec2 {1, 1}, vec2 {0, 1}, "Ariel", - Text::Data { - .text_color = Color::BLACK, - }, - "TEST test" - ) - .world_space - = true; } string get_name() const { return "TestScene"; }; -- cgit v1.2.3 From 92dfde9a56f5a607269e8908002a72bec85357bd Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 6 Jan 2025 10:50:01 +0100 Subject: bug fix --- src/crepe/system/InputSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 91c9c64..d9c97a3 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -216,8 +216,8 @@ bool InputSystem::is_mouse_inside_button( if (!button.data.world_space) { actual_pos += cam_transform.position; } - vec2 half_dimensions = button.dimensions / 2; - + vec2 half_dimensions = button.dimensions * transform.scale / 2; + return mouse_pos.x >= actual_pos.x - half_dimensions.x && mouse_pos.x <= actual_pos.x + half_dimensions.x && mouse_pos.y >= actual_pos.y - half_dimensions.y -- cgit v1.2.3 From cc2ffe577ad93b126bf9cac6391e678eb54a07f5 Mon Sep 17 00:00:00 2001 From: WBoerenkamps Date: Mon, 6 Jan 2025 10:52:47 +0100 Subject: make format --- src/crepe/system/InputSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index d9c97a3..be7eda6 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -217,7 +217,7 @@ bool InputSystem::is_mouse_inside_button( actual_pos += cam_transform.position; } vec2 half_dimensions = button.dimensions * transform.scale / 2; - + return mouse_pos.x >= actual_pos.x - half_dimensions.x && mouse_pos.x <= actual_pos.x + half_dimensions.x && mouse_pos.y >= actual_pos.y - half_dimensions.y -- cgit v1.2.3 From 958475050c80addf584b6a166649c337c68a879f Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 6 Jan 2025 11:26:23 +0100 Subject: make format with small adjustment system --- src/crepe/api/Animator.h | 2 +- src/crepe/system/AnimatorSystem.cpp | 9 +++++---- src/example/rendering_particle.cpp | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index efc2f6e..95539d3 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -1,7 +1,7 @@ #pragma once -#include "../types.h" #include "../manager/LoopTimerManager.h" +#include "../types.h" #include "Component.h" #include "Sprite.h" diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 1a0cde9..ec9a445 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -34,14 +34,15 @@ void AnimatorSystem::frame_update() { int total_frames = cycle_end - ctx.cycle_start; int curr_cycle_frame = (a.frame - ctx.cycle_start) % total_frames; + if (!ctx.looping && a.frame >= cycle_end) { + a.active = false; + continue; + } + ctx.row = (ctx.cycle_start + curr_cycle_frame) % a.grid_size.x; ctx.col = curr_cycle_frame / a.grid_size.x; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; - - if (!ctx.looping && a.frame >= cycle_end) { - a.active = false; - } } } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 0082b91..c28d6a0 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -46,7 +46,7 @@ public: game_object.add_component( test_sprite, ivec2 {56, 56}, uvec2 {4, 4}, Animator::Data { - .looping = true, + .looping = false, } ); -- cgit v1.2.3 From 1880d3bd97524455b72a29603b45de2da8de3f31 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 6 Jan 2025 12:05:51 +0100 Subject: hotfix used a.col which is not permitted --- src/crepe/system/AnimatorSystem.cpp | 13 ++++++------- src/example/rendering_particle.cpp | 8 ++++++-- 2 files changed, 12 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ec9a445..1a30502 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -25,23 +25,22 @@ void AnimatorSystem::frame_update() { a.elapsed_time += elapsed_time; duration_t frame_duration = 1000ms / ctx.fps; - if (a.elapsed_time <= frame_duration) continue; - - a.elapsed_time = 0ms; - a.frame++; - int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; int total_frames = cycle_end - ctx.cycle_start; int curr_cycle_frame = (a.frame - ctx.cycle_start) % total_frames; + if (a.elapsed_time >= frame_duration) { + a.elapsed_time = 0ms; + a.frame++; + } + if (!ctx.looping && a.frame >= cycle_end) { a.active = false; continue; } ctx.row = (ctx.cycle_start + curr_cycle_frame) % a.grid_size.x; - ctx.col = curr_cycle_frame / a.grid_size.x; - + //ctx.col = curr_cycle_frame / a.grid_size.x; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index c28d6a0..e6b31a7 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -43,13 +43,17 @@ public: } ); - game_object.add_component( + auto & anim = game_object.add_component( test_sprite, ivec2 {56, 56}, uvec2 {4, 4}, Animator::Data { - .looping = false, + .looping = 0, } ); + anim.set_anim(1); + anim.pause(); + anim.next_anim(); + auto & cam = game_object.add_component( ivec2 {1280, 720}, vec2 {5, 5}, Camera::Data { -- cgit v1.2.3 From db644d1cb7543faff63379ff5f1e69ea0a703f93 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 6 Jan 2025 12:36:39 +0100 Subject: animator fix -_- --- src/crepe/system/AnimatorSystem.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 1a30502..759d14c 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -26,22 +26,22 @@ void AnimatorSystem::frame_update() { duration_t frame_duration = 1000ms / ctx.fps; int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end; - int total_frames = cycle_end - ctx.cycle_start; - int curr_cycle_frame = (a.frame - ctx.cycle_start) % total_frames; - if (a.elapsed_time >= frame_duration) { a.elapsed_time = 0ms; a.frame++; + if (a.frame == cycle_end) { + a.frame = ctx.cycle_start; + if (!ctx.looping) { + a.active = false; + continue; + } + } } - if (!ctx.looping && a.frame >= cycle_end) { - a.active = false; - continue; - } - ctx.row = (ctx.cycle_start + curr_cycle_frame) % a.grid_size.x; - //ctx.col = curr_cycle_frame / a.grid_size.x; + ctx.row = ctx.cycle_start + a.frame; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; - a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; + //a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; + } } -- cgit v1.2.3 From 58542c5f986a8c96cf4313eb27c88154729fb032 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 12:37:51 +0100 Subject: Make format --- src/crepe/system/AnimatorSystem.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 759d14c..ac1fae2 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -38,10 +38,8 @@ void AnimatorSystem::frame_update() { } } - ctx.row = ctx.cycle_start + a.frame; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; //a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; - } } -- cgit v1.2.3 From eef88cd5804e88b504d940d8256ec17798da9648 Mon Sep 17 00:00:00 2001 From: Max-001 Date: Mon, 6 Jan 2025 12:38:54 +0100 Subject: Deleted comment --- src/crepe/system/AnimatorSystem.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src') diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index ac1fae2..143d5d6 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -40,6 +40,5 @@ void AnimatorSystem::frame_update() { ctx.row = ctx.cycle_start + a.frame; a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w; - //a.spritesheet.mask.y = ctx.col * a.spritesheet.mask.y; } } -- cgit v1.2.3