aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp26
1 files changed, 22 insertions, 4 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 00523a6..74af25f 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -18,6 +18,7 @@
#include "../api/Transform.h"
#include "../api/Vector2.h"
#include "../util/Log.h"
+#include "api/Vector2.h"
#include "SDLContext.h"
@@ -107,6 +108,10 @@ SDL_Rect SDLContext::get_src_rect(const Sprite & sprite) const {
SDL_Rect SDLContext::get_dst_rect(const Sprite & sprite, const Vector2 & pos,
const double & scale, const Camera & cam) const {
+ Vector2 pixel_coord = (transform.position - cam.pos) * cam.scale;
+ double pixel_w = sprite.sprite_rect.w * transform.scale * cam.scale.x;
+ double pixel_h = sprite.sprite_rect.h * transform.scale * cam.scale.y;
+
double adjusted_x = (pos.x - cam.x) * cam.zoom;
double adjusted_y = (pos.y - cam.y) * cam.zoom;
double adjusted_w = sprite.sprite_rect.w * scale * cam.zoom;
@@ -149,10 +154,23 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
}
void SDLContext::set_camera(const Camera & cam) {
- this->viewport.w = static_cast<int>(cam.aspect_width);
- this->viewport.h = static_cast<int>(cam.aspect_height);
- this->viewport.x = static_cast<int>(cam.x) - (this->viewport.w / 2);
- this->viewport.y = static_cast<int>(cam.y) - (this->viewport.h / 2);
+
+
+ double screen_aspect = cam.screen.x / cam.screen.y;
+ double viewport_aspect = cam.viewport.x / cam.viewport.y;
+ Vector2 zoomed_viewport = cam.viewport * cam.zoom;
+
+ if (screen_aspect > viewport_aspect) {
+ cam.scale.x = cam.scale.y = cam.screen.x / zoomed_viewport.x;
+ } else {
+ cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y;
+ }
+
+ if (this->viewport.w != cam.screen.x && this->viewport.h != cam.screen.y) {
+ this->viewport.w = cam.screen.x;
+ this->viewport.h = cam.screen.y;
+ SDL_SetWindowSize(this->game_window.get(), cam.screen.x, cam.screen.y);
+ }
SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
cam.bg_color.b, cam.bg_color.a);