diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-22 16:16:27 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-22 16:16:27 +0100 |
commit | 4ce924b1b1322ee4da3ba50d6da856ad13a2190b (patch) | |
tree | fdb411aa1a79dd6f7e16bdc145607ca8813b118c /src/crepe | |
parent | 385f19a8c896ec126c569f1e5337d6d370d20517 (diff) |
working scaling image with scaling world to screen
Diffstat (limited to 'src/crepe')
-rw-r--r-- | src/crepe/api/Camera.h | 4 | ||||
-rw-r--r-- | src/crepe/api/Sprite.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Sprite.h | 8 | ||||
-rw-r--r-- | src/crepe/api/Vector2.cpp | 4 | ||||
-rw-r--r-- | src/crepe/api/Vector2.h | 3 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 86 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.h | 10 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.cpp | 7 | ||||
-rw-r--r-- | src/crepe/system/RenderSystem.h | 5 |
9 files changed, 62 insertions, 69 deletions
diff --git a/src/crepe/api/Camera.h b/src/crepe/api/Camera.h index c42ed0d..083dc19 100644 --- a/src/crepe/api/Camera.h +++ b/src/crepe/api/Camera.h @@ -38,10 +38,8 @@ public: Vector2 viewport = {2000, 1000}; //! scale scaling factor from world units to pixel coordinates - Vector2 scale = {0, 0}; - //! Zoom level of the camera view. - double zoom = 1.0f; + double zoom = 1.5f; public: /** diff --git a/src/crepe/api/Sprite.cpp b/src/crepe/api/Sprite.cpp index bd2d5cf..3853aab 100644 --- a/src/crepe/api/Sprite.cpp +++ b/src/crepe/api/Sprite.cpp @@ -15,7 +15,9 @@ Sprite::Sprite(game_object_id_t id, const shared_ptr<Texture> image, const Color : Component(id), color(color), flip(flip), - sprite_image(image) { + sprite_image(image), + aspect_ratio(sprite_image->get_width() / sprite_image->get_height()) +{ dbg_trace(); this->sprite_rect.w = sprite_image->get_width(); diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 74a55d4..66599c9 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -1,6 +1,7 @@ #pragma once #include <memory> +#include <sys/types.h> #include "../Component.h" @@ -62,6 +63,13 @@ public: //! Order within the sorting layer uint8_t order_in_layer = 0; + //! width in world units + int width = 0; + //! height in world units + int height = 0; + + const double aspect_ratio; + public: /** * \brief Gets the maximum number of instances allowed for this sprite. diff --git a/src/crepe/api/Vector2.cpp b/src/crepe/api/Vector2.cpp index 8658c00..c8253d7 100644 --- a/src/crepe/api/Vector2.cpp +++ b/src/crepe/api/Vector2.cpp @@ -40,6 +40,10 @@ Vector2 Vector2::operator/(const Vector2 & other) const { return {this->x / other.x, this->y / other.y}; } +Vector2 Vector2::operator/(const double & other) const { + return {this->x / other, this->y / other}; +} + Vector2 Vector2::operator-() const { return {-x, -y}; } bool Vector2::operator==(const Vector2 & other) const { return x == other.x && y == other.y; } diff --git a/src/crepe/api/Vector2.h b/src/crepe/api/Vector2.h index 790160d..5a23699 100644 --- a/src/crepe/api/Vector2.h +++ b/src/crepe/api/Vector2.h @@ -30,6 +30,9 @@ struct Vector2 { //! Divides this vector by another vector element-wise and updates this vector. Vector2 operator/(const Vector2 & other) const; + //! Divides a scalar value to both components of this vector and updates this vector. + Vector2 operator/(const double & other) const; + //! Adds another vector to this vector and updates this vector. Vector2 & operator+=(const Vector2 & other); diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index e187d67..f49539c 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -9,6 +9,7 @@ #include <cmath> #include <cstddef> #include <functional> +#include <iostream> #include <memory> #include <stdexcept> @@ -109,48 +110,60 @@ 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 { + const Vector2 & 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_height = static_cast<int>(pixel_width / sprite.aspect_ratio); + } else { + pixel_height = static_cast<int>(sprite.height * 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)); return SDL_Rect{ - .x = static_cast<int>(sprite.sprite_rect.x - 400 / 2), - .y = static_cast<int>(sprite.sprite_rect.y - 300 / 2), - .w = static_cast<int>(400), - .h = static_cast<int>(300), + .x = pixel_x, + .y = pixel_y, + .w = pixel_width, + .h = pixel_height, }; } void SDLContext::draw_particle(const Sprite & sprite, const Vector2 & pos, - const double & angle, const double & scale, - const Camera & camera) { + const double & angle, const Vector2 & 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, camera); + SDL_Rect dstrect = this->get_dst_rect(sprite, pos, 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 Camera & cam) { +void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Vector2 & 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, transform.scale, cam); + SDL_Rect dstrect = this->get_dst_rect(sprite, transform.position, scale); SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect, &dstrect, transform.rotation, NULL, render_flip); } -void SDLContext::set_camera(Camera & cam) { +void SDLContext::set_camera(const Camera & cam, Vector2 & scale) { - if (this->viewport.w != (int)cam.screen.x && this->viewport.h != (int)cam.screen.y) { - SDL_SetWindowSize(this->game_window.get(), (int)cam.screen.x, (int)cam.screen.y); + if (this->viewport.w != (int) cam.screen.x && this->viewport.h != (int) cam.screen.y) { + SDL_SetWindowSize(this->game_window.get(), (int) cam.screen.x, (int) cam.screen.y); this->viewport.h = cam.screen.y; this->viewport.w = cam.screen.x; } @@ -158,16 +171,18 @@ void SDLContext::set_camera(Camera & cam) { double screen_aspect = cam.screen.x / cam.screen.y; double viewport_aspect = cam.viewport.x / cam.viewport.y; + scale = cam.screen / cam.viewport * cam.zoom; + SDL_Rect view; if (screen_aspect > viewport_aspect) { - view.h = static_cast<int>(cam.screen.y); + view.h = static_cast<int>(cam.screen.y / cam.zoom); view.w = static_cast<int>(cam.screen.y * viewport_aspect); view.x = static_cast<int>(cam.screen.x - view.w) / 2; view.y = 0; } else { - view.w = static_cast<int>(cam.screen.x); view.h = static_cast<int>(cam.screen.x / viewport_aspect); + view.w = static_cast<int>(cam.screen.x / cam.zoom); view.x = 0; view.y = static_cast<int>(cam.screen.y - view.h) / 2; } @@ -175,7 +190,7 @@ void SDLContext::set_camera(Camera & cam) { SDL_RenderSetLogicalSize(this->game_renderer.get(), cam.viewport.x, cam.viewport.y); SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g, - cam.bg_color.b, cam.bg_color.a); + cam.bg_color.b, cam.bg_color.a); SDL_Rect bg = { .x = 0, .y = 0, @@ -183,45 +198,6 @@ void SDLContext::set_camera(Camera & cam) { .h = static_cast<int>(cam.viewport.y), }; SDL_RenderFillRect(this->game_renderer.get(), &bg); - - /* - float offset_x = 0, offset_y = 0; - - - double scale_factor = min(cam.screen.x / cam.viewport.x, cam.screen.y / cam.viewport.y); - cam.scale.x = scale_factor * cam.viewport.x; - cam.scale.y = scale_factor * cam.viewport.y; - - offset_x = (cam.screen.x - cam.scale.x) / 2; - offset_y = (cam.screen.y - cam.scale.y) / 2; - - float bar_w = cam.screen.x - cam.scale.x; - float bar_h = cam.screen.y - cam.scale.y; - - SDL_SetRenderDrawColor(this->game_renderer.get(), 0, 0, 0, 255); - if (bar_w > 0) { - SDL_Rect left_bar = {0, 0, static_cast<int>(offset_x), static_cast<int>(cam.screen.y)}; - SDL_RenderDrawRect(this->game_renderer.get(), &left_bar); - - SDL_Rect right_bar = {static_cast<int>(offset_x + cam.scale.x), 0, - static_cast<int>(offset_x), static_cast<int>(cam.screen.y)}; - SDL_RenderDrawRect(this->game_renderer.get(), &right_bar); - } - - if (screen_aspect > viewport_aspect) { - // pillarboxing - cam.scale.x = cam.scale.y = cam.screen.x / cam.viewport.x; - offset_y = (cam.screen.y - (cam.viewport.y * cam.scale.y)) / 2; - } else if (screen_aspect < viewport_aspect) { - // lettor boxing - cam.scale.y = cam.scale.x = cam.screen.y / zoomed_viewport.y; - offset_x = (cam.screen.x - (cam.viewport.x * cam.scale.x)) / 2; - } else { - // screen ration is even - offset_y = (cam.screen.y - (cam.viewport.y * cam.scale.y)) / 2; - offset_x = (cam.screen.x - (cam.viewport.x * cam.scale.x)) / 2; - } - */ } uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); } diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 45bbda6..68d1630 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -118,10 +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 Camera & camera); + void draw(const Sprite & sprite, const Transform & transform, const Vector2 & scale); - void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, - const double & scale, const Camera & camera); + void draw_particle(const Sprite & sprite, const Vector2 & pos, const double & angle, const Vector2 & scale); //! Clears the screen, preparing for a new frame. void clear_screen(); @@ -133,7 +132,7 @@ private: * \brief sets the background of the camera (will be adjusted in future PR) * \param camera Reference to the Camera object. */ - void set_camera(Camera & camera); + void set_camera(const Camera & camera, Vector2 & scale); private: /** @@ -153,8 +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 double & scale, - const Camera & cam) const; + SDL_Rect get_dst_rect(const Sprite & sprite, const Vector2 & pos, const Vector2 & scale) const; private: //! sdl Window diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index ad510f5..a16fbb5 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -30,7 +30,7 @@ void RenderSystem::update_camera() { for (Camera & cam : cameras) { if (!cam.active) continue; - this->context.set_camera(cam); + this->context.set_camera(cam, this->scale); this->curr_cam_ref = &cam; } } @@ -72,14 +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, scale, - *this->curr_cam_ref); + this->context.draw_particle(sprite, p.position, p.angle, this->scale * scale); } } return rendering_particles; } void RenderSystem::render_normal(const Sprite & sprite, const Transform & tm) { - this->context.draw(sprite, tm, *this->curr_cam_ref); + this->context.draw(sprite, tm, this->scale * tm.scale); } void RenderSystem::render() { diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 30b41cf..19edc02 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -3,6 +3,7 @@ #include <functional> #include <vector> +#include "api/Vector2.h" #include "facade/SDLContext.h" #include "System.h" @@ -80,8 +81,12 @@ private: //! Pointer to the current active camera for rendering Camera * curr_cam_ref = nullptr; // TODO: needs a better solution + + Vector2 scale; SDLContext & context = SDLContext::get_instance(); + + }; } // namespace crepe |