From 69e319a7303c471a29329f5e1bb2de4ef0a5a0ce Mon Sep 17 00:00:00 2001 From: Mrna Date: Tue, 7 Oct 2025 14:03:00 +0200 Subject: [PATCH] Fixed sprite size on zoom --- cpp/src/camera.hpp | 3 +++ cpp/src/gameloop.cpp | 2 +- cpp/src/sprite.hpp | 4 ++-- cpp/src/window.cpp | 6 +++--- cpp/src/window.hpp | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cpp/src/camera.hpp b/cpp/src/camera.hpp index ed61e00..f57b441 100644 --- a/cpp/src/camera.hpp +++ b/cpp/src/camera.hpp @@ -8,6 +8,9 @@ public: void Pan(const WorldPos& delta); void Zoom(float delta); + WorldPos GetPan() const { return m_Pan; } + float GetZoom() const { return m_Zoom; } + WindowPos WorldToWindow(WorldPos) const; WorldPos WindowToWorld(WindowPos) const; WindowSize WorldToWindowSize(WorldSize) const; diff --git a/cpp/src/gameloop.cpp b/cpp/src/gameloop.cpp index 671548c..3f97015 100644 --- a/cpp/src/gameloop.cpp +++ b/cpp/src/gameloop.cpp @@ -55,7 +55,7 @@ void GameLoop::Run() { // draw all the entities (player etc) for (auto &entity : m_Game->GetEntities()) { const auto& camera = m_Game->GetCamera(); - m_Window->DrawSprite(camera.WorldToWindow(entity->GetPosition()), entity->GetSprite()); + m_Window->DrawSprite(camera.WorldToWindow(entity->GetPosition()), entity->GetSprite(), camera.GetZoom()); } m_Window->Flush(); diff --git a/cpp/src/sprite.hpp b/cpp/src/sprite.hpp index 5b9b7e6..051d732 100644 --- a/cpp/src/sprite.hpp +++ b/cpp/src/sprite.hpp @@ -25,7 +25,7 @@ public: // GetTexture cannot return pointer to const, as SDL_RenderTexture modifies it SDL_Texture *GetTexture() { return m_Texture.get(); } - WorldPos GetSize() const { return m_Size; } + WorldSize GetSize() const { return m_Size; } WorldPos GetCenter() const { return m_ImageCenter; } void LoadImage(std::string path, WorldPos image_center = WorldPos{}); @@ -33,7 +33,7 @@ public: private: static std::shared_ptr m_Renderer; std::unique_ptr m_Texture; - WorldPos m_Size; + WorldSize m_Size; WorldPos m_ImageCenter; float m_TextureWidth = 0; float m_TextureHeight = 0; diff --git a/cpp/src/window.cpp b/cpp/src/window.cpp index b49d15b..91b9ab6 100644 --- a/cpp/src/window.cpp +++ b/cpp/src/window.cpp @@ -76,9 +76,9 @@ Window::~Window() { LOG_DEBUG("."); } -void Window::DrawSprite(const WindowPos &position, Sprite &s) { - WorldPos size = s.GetSize(); - WorldPos img_center = s.GetCenter(); +void Window::DrawSprite(const WindowPos &position, Sprite &s, float scale) { + WorldSize size = s.GetSize() * scale; + WorldPos img_center = s.GetCenter() * scale; SDL_FRect rect = {position.x() - img_center.x(), position.y() - img_center.y(), size.x(), size.y()}; SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect); diff --git a/cpp/src/window.hpp b/cpp/src/window.hpp index 5a16152..8453d67 100644 --- a/cpp/src/window.hpp +++ b/cpp/src/window.hpp @@ -22,7 +22,7 @@ public: Window &operator=(Window &&) = delete; std::expected Init(); - void DrawSprite(const WindowPos &position, Sprite &s); + void DrawSprite(const WindowPos &position, Sprite &s, float scale = 1.0f); void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R, uint8_t G, uint8_t B, uint8_t A); void ClearWindow();