aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-18 20:12:31 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-18 20:12:31 +0100
commit88f613cbb2e4aaf3ed55ac0c6490706dd6f6f19d (patch)
tree8db09f0a0f27dc552356a6c5513589245e9ff7b7 /src/crepe/facade
parent9288e4964526f1ce6b7d0aca0f075a04f56ede32 (diff)
rendering based on world unites instead of pixels
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/SDLContext.cpp36
-rw-r--r--src/crepe/facade/SDLContext.h2
2 files changed, 16 insertions, 22 deletions
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 5185adc..785b285 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -111,21 +111,10 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
| (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
- double screen_aspect = cam.screen.x / cam.screen.y;
- double viewport_aspect = cam.viewport.x / cam.viewport.y;
- Vector2 scale;
- if (screen_aspect > viewport_aspect) {
- scale.x = scale.y = cam.screen.x / cam.viewport.x;
- } else {
- scale.y = scale.x = cam.screen.y / cam.viewport.y;
- }
-
- Vector2 zoomed_viewport = cam.viewport * cam.zoom;
- Vector2 pixel_coord = (transform.position - cam.pos) * scale;
-
- double pixel_w = sprite.sprite_rect.w * transform.scale * scale.x;
- double pixel_h = sprite.sprite_rect.h * transform.scale * scale.y;
+ 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;
// decides which part of the sprite will be drawn
SDL_Rect srcrect = {
@@ -147,7 +136,18 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform, const
&dstrect, transform.rotation, NULL, render_flip);
}
-void SDLContext::camera(const Camera & cam) {
+void SDLContext::camera(Camera & cam) {
+
+ 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;
@@ -155,12 +155,6 @@ void SDLContext::camera(const Camera & cam) {
SDL_SetWindowSize(this->game_window.get(), cam.screen.x , cam.screen.y);
}
- /*
- this->viewport.x = static_cast<int>(cam.x) - (SCREEN_WIDTH / 2);
- this->viewport.y = static_cast<int>(cam.y) - (SCREEN_HEIGHT / 2);
-
- */
-
SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
cam.bg_color.b, cam.bg_color.a);
}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 007092b..b69e509 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -137,7 +137,7 @@ private:
* \brief Sets the current camera for rendering.
* \param camera Reference to the Camera object.
*/
- void camera(const Camera & camera);
+ void camera(Camera & camera);
private:
//! sdl Window