diff options
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Animator.cpp | 5 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 10 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 25 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 6 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 4 |
5 files changed, 30 insertions, 20 deletions
diff --git a/src/crepe/api/Animator.cpp b/src/crepe/api/Animator.cpp index d206428..178b165 100644 --- a/src/crepe/api/Animator.cpp +++ b/src/crepe/api/Animator.cpp @@ -18,7 +18,10 @@ Animator::Animator(game_object_id_t id, Sprite & ss, int row, int col, int col_a animator_rect.h /= col; animator_rect.w /= row; animator_rect.x = 0; - animator_rect.y = (col_animator - 1) * animator_rect.h; + animator_rect.y = col_animator * animator_rect.h; this->active = false; + + // need to do this for to get the aspect ratio for a single clipping in the spritesheet + this->spritesheet.aspect_ratio = (double)animator_rect.w / (double)animator_rect.h; } Animator::~Animator() { dbg_trace(); } diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 66599c9..89f9121 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -68,7 +68,13 @@ public: //! height in world units int height = 0; - const double aspect_ratio; + /** + * \aspect_ratio ratio of the img so that scaling will not become weird + * + * cannot be const because if Animator component is addded then ratio becomes scuffed and + * does it need to be calculated again in the Animator + */ + double aspect_ratio; public: /** @@ -90,7 +96,7 @@ private: friend class AnimatorSystem; //! Render area of the sprite this will also be adjusted by the AnimatorSystem if an Animator - // object is present in GameObject + // object is present in GameObject. this is in sprite pixels Rect sprite_rect; }; diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f49539c..29a8195 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -5,11 +5,9 @@ #include <SDL2/SDL_render.h> #include <SDL2/SDL_surface.h> #include <SDL2/SDL_video.h> -#include <algorithm> #include <cmath> #include <cstddef> #include <functional> -#include <iostream> #include <memory> #include <stdexcept> @@ -109,21 +107,24 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const { .h = sprite.sprite_rect.h, }; } -SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, - const Vector2 & scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, + const double & img_scale, const Vector2 & cam_scale) const { int pixel_width, pixel_height; if (sprite.sprite_rect.w > sprite.sprite_rect.h) { - pixel_width = static_cast<int>(sprite.width * scale.x); + pixel_width = static_cast<int>(sprite.width * cam_scale.x); pixel_height = static_cast<int>(pixel_width / sprite.aspect_ratio); } else { - pixel_height = static_cast<int>(sprite.height * scale.y); + pixel_height = static_cast<int>(sprite.height * cam_scale.y); pixel_width = static_cast<int>(pixel_height * sprite.aspect_ratio); } - int pixel_x = static_cast<int>((pos.x - pixel_width / 2)); - int pixel_y = static_cast<int>((pos.y - pixel_height / 2)); + pixel_width *= img_scale; + pixel_height *= img_scale; + + int pixel_x = static_cast<int>((pos.x - cam_pos.x - pixel_width / 2)); + int pixel_y = static_cast<int>((pos.y - cam_pos.y - pixel_height / 2)); return SDL_Rect{ .x = pixel_x, @@ -134,27 +135,27 @@ SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos, } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const Vector2 & scale) { + const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam_pos, img_scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, angle, NULL, render_flip); } -void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale) { SDL_RendererFlip render_flip = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x) | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 68d1630..04c60cc 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,9 +118,9 @@ private: * \param transform Reference to the Transform for positioning. * \param camera Reference to the Camera for view adjustments. */ - void draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & cam_pos, const Vector2 & cam_scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & scale); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & cam_pos, const double & img_scale, const Vector2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -152,7 +152,7 @@ private: * on the camera * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & cam_pos, const double & img_scale , const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index a16fbb5..676ded6 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,13 +72,13 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) for (const Particle & p : em.data.particles) { if (!p.active) continue; - this->context.draw_particle(sprite, p.position, p.angle, this->scale * scale); + this->context.draw_particle(sprite, p.position ,p.angle, this->curr_cam_ref->pos ,scale, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, this->scale * tm.scale); + this->context.draw(sprite, tm , this->curr_cam_ref->pos,this->scale); } void RenderSystem::render() { |