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/facade/SDLContext.cpp | 38 ++++++++++++++++++++------------------ src/crepe/facade/SDLContext.h | 19 +++++++++---------- 2 files changed, 29 insertions(+), 28 deletions(-) (limited to 'src/crepe/facade') diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index 82c8c50..ac21d15 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -21,10 +21,10 @@ #include "../api/Sprite.h" #include "../api/Texture.h" #include "../util/Log.h" - -#include "SDLContext.h" #include "manager/Manager.h" #include "manager/Mediator.h" + +#include "SDLContext.h" #include "types.h" using namespace crepe; @@ -219,25 +219,26 @@ void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); } -SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { - return SDL_Rect{ - .x = sprite.mask.x, - .y = sprite.mask.y, - .w = sprite.mask.w, - .h = sprite.mask.h, - }; +SDL_Rect * SDLContext::get_src_rect(const Sprite & sprite) { + if (sprite.mask.w == 0 && sprite.mask.h == 0) return NULL; + + this->mask.x = sprite.mask.x; + this->mask.y = sprite.mask.y; + this->mask.w = sprite.mask.w; + this->mask.h = sprite.mask.h; + return &this->mask; } SDL_FRect SDLContext::get_dst_rect(const DestinationRectangleData & ctx) const { const Sprite::Data & data = ctx.sprite.data; - vec2 size = {data.size.x , data.size.y}; + vec2 size = {data.size.x, data.size.y}; if (data.size.x == 0 && data.size.y != 0) { - size.x = data.size.y * ctx.sprite.aspect_ratio; + size.x = data.size.y * ctx.texture.get_ratio(); } if (data.size.y == 0 && data.size.x != 0) { - size.y = data.size.x / ctx.sprite.aspect_ratio; + size.y = data.size.x / ctx.texture.get_ratio(); } const CameraValues & cam = ctx.cam; @@ -263,9 +264,10 @@ void SDLContext::draw(const RenderContext & ctx) { = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * data.flip.flip_x) | (SDL_FLIP_VERTICAL * data.flip.flip_y)); - SDL_Rect srcrect = this->get_src_rect(ctx.sprite); + SDL_Rect * srcrect = this->get_src_rect(ctx.sprite); SDL_FRect dstrect = this->get_dst_rect(SDLContext::DestinationRectangleData{ .sprite = ctx.sprite, + .texture = ctx.texture, .cam = ctx.cam, .pos = ctx.pos, .img_scale = ctx.scale, @@ -275,8 +277,8 @@ void SDLContext::draw(const RenderContext & ctx) { double angle = ctx.angle + data.angle_offset; this->set_color_texture(ctx.texture, ctx.sprite.data.color); - int error = SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.texture.get(), - &srcrect, &dstrect, angle, NULL, render_flip); + SDL_RenderCopyExF(this->game_renderer.get(), ctx.texture.get_img(), srcrect, &dstrect, + angle, NULL, render_flip); } SDLContext::CameraValues SDLContext::set_camera(const Camera & cam) { @@ -366,7 +368,7 @@ SDLContext::texture_from_path(const std::string & path) { ivec2 SDLContext::get_size(const Texture & ctx) { ivec2 size; - SDL_QueryTexture(ctx.texture.get(), NULL, NULL, &size.x, &size.y); + SDL_QueryTexture(ctx.get_img(), NULL, NULL, &size.x, &size.y); return size; } @@ -433,6 +435,6 @@ std::vector SDLContext::get_events() { return event_list; } void SDLContext::set_color_texture(const Texture & texture, const Color & color) { - SDL_SetTextureColorMod(texture.texture.get(), color.r, color.g, color.b); - SDL_SetTextureAlphaMod(texture.texture.get(), color.a); + SDL_SetTextureColorMod(texture.get_img(), color.r, color.g, color.b); + SDL_SetTextureAlphaMod(texture.get_img(), color.a); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 9676940..36e6e97 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -15,14 +15,14 @@ #include "api/KeyCodes.h" #include "api/Sprite.h" #include "api/Transform.h" - #include "manager/Manager.h" -#include "manager/Mediator.h" + #include "types.h" namespace crepe { class Texture; +class Mediator; /** * \class SDLContext @@ -165,7 +165,6 @@ public: */ void delay(int ms) const; - public: /** * \brief Loads a texture from a file path. @@ -177,7 +176,7 @@ public: /** * \brief Gets the size of a texture. * \param texture Reference to the Texture object. - * \return Width and height of the texture as an integer. + * \return Width and height of the texture as an integer in pixels. */ ivec2 get_size(const Texture & ctx); @@ -197,6 +196,7 @@ public: /** * \brief sets the background of the camera (will be adjusted in future PR) * \param camera Reference to the Camera object. + * \return camera data the component cannot store */ CameraValues set_camera(const Camera & camera); @@ -204,6 +204,7 @@ public: //! the data needed to construct a sdl dst rectangle struct DestinationRectangleData { const Sprite & sprite; + const Texture & texture; const CameraValues & cam; const vec2 & pos; const double & img_scale; @@ -214,16 +215,12 @@ public: * \param sprite Reference to the sprite to calculate the rectangle * \return sdl rectangle to draw a src image */ - SDL_Rect get_src_rect(const Sprite & sprite) const; + SDL_Rect * get_src_rect(const Sprite & sprite); /** * \brief calculates the sqaure size of the image for destination * - * \param sprite Reference to the sprite to calculate rectangle - * \param pos the pos in world units - * \param cam the camera of the current scene - * \param cam_pos the current postion of the camera - * \param img_scale the image multiplier for increasing img size + * \param data needed to calculate a destination rectangle * \return sdl rectangle to draw a dst image to draw on the screen */ SDL_FRect get_dst_rect(const DestinationRectangleData & data) const; @@ -242,6 +239,8 @@ private: //! renderer for the crepe engine std::unique_ptr> game_renderer; + SDL_Rect mask = {}; + //! black bars rectangle to draw SDL_FRect black_bars[2] = {}; }; -- cgit v1.2.3