diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-26 19:19:21 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-26 19:19:21 +0100 |
commit | c5d1b46ba804eaabdb4fc2f4f4295292032def65 (patch) | |
tree | ca3b6410a6f3c1b9f5291e182fd9d25e39efc08c /src/crepe | |
parent | b4c07db977ab18302a0d953ef22a32a171e688f8 (diff) |
implemented all the feedback
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Texture.cpp | 2 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 39 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 8 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 6 |
4 files changed, 24 insertions, 31 deletions
diff --git a/src/crepe/api/Texture.cpp b/src/crepe/api/Texture.cpp index 264d7b1..eeb86e9 100644 --- a/src/crepe/api/Texture.cpp +++ b/src/crepe/api/Texture.cpp @@ -1,5 +1,3 @@ -#include <SDL2/SDL_render.h> - #include "../facade/SDLContext.h" #include "../util/Log.h" diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index ac6b089..55b0082 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -105,37 +105,32 @@ 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 vec2 & pos, - const vec2 & cam_pos, const double & img_scale, - const vec2 & cam_scale) const { +SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const double & img_scale, const vec2 & cam_scale) const { - int pixel_width, pixel_height; + int width, height; if (sprite.sprite_rect.w > sprite.sprite_rect.h) { - pixel_width = static_cast<int>(sprite.width * cam_scale.x); - pixel_height = static_cast<int>(pixel_width / sprite.aspect_ratio); + width = static_cast<int>(sprite.width * cam_scale.x); + height = static_cast<int>(width / sprite.aspect_ratio); } else { - pixel_height = static_cast<int>(sprite.height * cam_scale.y); - pixel_width = static_cast<int>(pixel_height * sprite.aspect_ratio); + height = static_cast<int>(sprite.height * cam_scale.y); + width = static_cast<int>(height * sprite.aspect_ratio); } - pixel_width *= img_scale; - pixel_height *= img_scale; - - int pixel_x = static_cast<int>((pos.x - cam_pos.x + this->window.x / 2 - pixel_width / 2)); - int pixel_y - = static_cast<int>((pos.y - cam_pos.y + this->window.y / 2 - pixel_height / 2)); + width *= img_scale; + height *= img_scale; return SDL_Rect{ - .x = pixel_x, - .y = pixel_y, - .w = pixel_width, - .h = pixel_height, + .x = static_cast<int>((pos.x - cam.pos.x + (cam.viewport.x / 2) - width / 2)), + .y = static_cast<int>((pos.y - cam.pos.y + (cam.viewport.y / 2) - height / 2)), + .w = width, + .h = height, }; } void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const vec2 & cam_pos, const double & img_scale, + const double & img_scale, const Camera & cam, const vec2 & cam_scale) { SDL_RendererFlip render_flip @@ -143,13 +138,13 @@ void SDLContext::draw_particle(const Sprite & sprite, const vec2 & pos, const do | (SDL_FLIP_VERTICAL * sprite.flip.flip_y)); SDL_Rect srcrect = this->get_src_rect(sprite); - SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam_pos, img_scale, cam_scale); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, cam , 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 vec2 & cam_pos, +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam, const vec2 & cam_scale) { SDL_RendererFlip render_flip @@ -158,7 +153,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const SDL_Rect srcrect = this->get_src_rect(sprite); SDL_Rect dstrect - = this->get_dst_rect(sprite, transform.position, cam_pos, transform.scale, cam_scale); + = this->get_dst_rect(sprite, transform.position, cam, 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 542a8bf..1a9316e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -121,7 +121,7 @@ private: * \param cam_pos position of the current camera in the scene * \param cam_scale multiplier for the world to screen */ - void draw(const Sprite & sprite, const Transform & transform, const vec2 & cam_pos, + void draw(const Sprite & sprite, const Transform & transform, const Camera & cam, const vec2 & cam_scale); /** @@ -135,7 +135,7 @@ private: * \param cam_scale camera scalar for world to screen */ void draw_particle(const Sprite & sprite, const vec2 & pos, const double & angle, - const vec2 & cam_pos, const double & img_scale, const vec2 & cam_scale); + const double & img_scale, const Camera & cam, const vec2 & cam_scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -168,8 +168,8 @@ private: * \param scale the multiplier for world to screen * \return sdl rectangle to draw a dst image to draw on the screen */ - SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const vec2 & cam_pos, - const double & img_scale, const vec2 & scale) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const vec2 & pos, const Camera & cam, + const double & img_scale, const vec2 & cam_scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index 1dd1699..564fc79 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -72,14 +72,14 @@ 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->curr_cam_ref->pos, - scale, this->scale); + this->context.draw_particle(sprite, p.position, p.angle, scale, + *this->curr_cam_ref, this->scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, this->curr_cam_ref->pos, this->scale); + this->context.draw(sprite, tm, *this->curr_cam_ref, this->scale); } void RenderSystem::render() { |