diff options
Diffstat (limited to 'src/crepe/facade/SDLContext.cpp')
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 39 |
1 files changed, 17 insertions, 22 deletions
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); |