From 33cd5566909ac089cdf56db38a3d1daf0cb7dd10 Mon Sep 17 00:00:00 2001 From: heavydemon21 Date: Mon, 9 Dec 2024 15:35:36 +0100 Subject: fixed the aspect ratio and removed the ratio from sprite and give it to texture. however the problem still lies with if animator is given the aspect ratio is then off --- src/crepe/api/Animator.cpp | 13 +++++-------- src/crepe/api/Animator.h | 6 ++++-- src/crepe/api/Sprite.cpp | 6 +----- src/crepe/api/Sprite.h | 14 ++------------ src/crepe/api/Texture.cpp | 18 +++++++++++++++--- src/crepe/api/Texture.h | 34 +++++++++++++++++++++++++--------- 6 files changed, 52 insertions(+), 39 deletions(-) (limited to 'src/crepe/api') diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index b8a91dc..ad1778d 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -7,8 +7,9 @@ using namespace crepe; -Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, - unsigned int max_col, const Animator::Data & data) +Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, + unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, + const Animator::Data & data) : Component(id), spritesheet(spritesheet), max_rows(max_row), @@ -16,14 +17,10 @@ Animator::Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_r data(data) { dbg_trace(); - this->spritesheet.mask.h /= this->max_columns; - this->spritesheet.mask.w /= this->max_rows; + this->spritesheet.mask.h = this->max_columns * pixel_frame_y; + this->spritesheet.mask.w /= this->max_rows * pixel_frame_x; this->spritesheet.mask.x = this->data.row * this->spritesheet.mask.w; this->spritesheet.mask.y = this->data.col * this->spritesheet.mask.h; - - // need to do this for to get the aspect ratio for a single clipping in the spritesheet - this->spritesheet.aspect_ratio - = static_cast(this->spritesheet.mask.w) / this->spritesheet.mask.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Animator.h b/src/crepe/api/Animator.h index 7c850b8..8ceddad 100644 --- a/src/crepe/api/Animator.h +++ b/src/crepe/api/Animator.h @@ -82,8 +82,9 @@ public: * This constructor sets up the Animator with the given parameters, and initializes the * animation system. */ - Animator(game_object_id_t id, Sprite & spritesheet, unsigned int max_row, - unsigned int max_col, const Animator::Data & data); + Animator(game_object_id_t id, Sprite & spritesheet, unsigned int pixel_frame_x, + unsigned int pixel_frame_y, unsigned int max_row, unsigned int max_col, + const Animator::Data & data); ~Animator(); // dbg_trace public: @@ -96,6 +97,7 @@ public: private: //! A reference to the Sprite sheet containing. Sprite & spritesheet; + //! Uses the spritesheet friend AnimatorSystem; }; diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index 4cf214c..ba684ba 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -10,16 +10,12 @@ using namespace std; using namespace crepe; -Sprite::Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Sprite::Data & data) +Sprite::Sprite(game_object_id_t id, const Asset & texture, const Sprite::Data & data) : Component(id), source(texture), data(data) { dbg_trace(); - - this->mask.w = size.x; - this->mask.h = size.y; - this->aspect_ratio = static_cast(this->mask.w) / this->mask.h; } Sprite::~Sprite() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 9ef9f03..7e9812d 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,10 +1,9 @@ #pragma once #include "../Component.h" +#include "api/Asset.h" #include "Color.h" -#include "Texture.h" -#include "api/Asset.h" #include "types.h" namespace crepe { @@ -75,7 +74,7 @@ public: * \param texture asset of the image * \param ctx all the sprite data */ - Sprite(game_object_id_t id, const Asset & texture, const ivec2 & size, const Data & data); + Sprite(game_object_id_t id, const Asset & texture, const Data & data); ~Sprite(); //! Texture used for the sprite @@ -84,15 +83,6 @@ public: Data data; private: - /** - * \brief ratio of the img - * - * - This will multiply one of \c size variable if it is 0. - * - Will be adjusted if \c Animator component is added to an GameObject that is why this - * value cannot be const. - */ - float aspect_ratio; - //! Reads the mask of sprite friend class SDLContext; diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 2ac8606..b0863cb 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,23 +1,35 @@ #include "../util/Log.h" +#include "manager/Mediator.h" +#include "facade/SDLContext.h" #include "Asset.h" #include "Resource.h" #include "Texture.h" -#include "facade/SDLContext.h" -#include "manager/Mediator.h" #include "types.h" using namespace crepe; using namespace std; -Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator) { +Texture::Texture(const Asset & src, Mediator & mediator) : Resource(src, mediator){ dbg_trace(); SDLContext & ctx = mediator.sdl_context; this->texture = ctx.texture_from_path(src.get_path()); this->size = ctx.get_size(*this); + this->aspect_ratio = static_cast(this->size.x) / this->size.y; } Texture::~Texture() { dbg_trace(); this->texture.reset(); } + +const ivec2 & Texture::get_size() const noexcept{ + return this->size; +} +const float & Texture::get_ratio() const noexcept{ + return this->aspect_ratio; +} + +SDL_Texture * Texture::get_img() const noexcept{ + return this->texture.get(); +} diff --git a/src/crepe/api/Texture.h b/src/crepe/api/Texture.h index 4eb1058..c33d9e5 100644 --- a/src/crepe/api/Texture.h +++ b/src/crepe/api/Texture.h @@ -4,7 +4,6 @@ // headers won't be bundled with crepe. Why is this facade in the API namespace? #include -#include #include #include "Asset.h" @@ -13,8 +12,6 @@ namespace crepe { -class SDLContext; -class Animator; class Mediator; /** @@ -30,14 +27,36 @@ public: /** * \brief Constructs a Texture from an Asset resource. * \param src Asset with texture data to load. + * \param mediator use the SDLContext reference to load the image */ Texture(const Asset & src, Mediator & mediator); /** - * \brief Destroys the Texture instance, freeing associated resources. + * \brief Destroys the Texture instance */ ~Texture(); + /** + * \brief get width and height of image in pixels + * \return pixel size width and height + * + */ + const ivec2 & get_size() const noexcept; + + /** + * \brief aspect_ratio of image + * \return ratio + * + */ + const float & get_ratio() const noexcept; + + /** + * \brief get the image texture + * \return SDL_Texture + * + */ + SDL_Texture * get_img() const noexcept; + private: //! The texture of the class from the library std::unique_ptr> texture; @@ -45,11 +64,8 @@ private: // texture size in pixel ivec2 size; - //! Grants SDLContext access to private members. - friend class SDLContext; - - //! Grants Animator access to private members. - friend class Animator; + //! ratio of image + float aspect_ratio; }; } // namespace crepe -- cgit v1.2.3