Fixed sprite size on zoom

This commit is contained in:
Mrna 2025-10-07 14:03:00 +02:00
parent 783200733a
commit 69e319a730
5 changed files with 10 additions and 7 deletions

View File

@ -8,6 +8,9 @@ public:
void Pan(const WorldPos& delta); void Pan(const WorldPos& delta);
void Zoom(float delta); void Zoom(float delta);
WorldPos GetPan() const { return m_Pan; }
float GetZoom() const { return m_Zoom; }
WindowPos WorldToWindow(WorldPos) const; WindowPos WorldToWindow(WorldPos) const;
WorldPos WindowToWorld(WindowPos) const; WorldPos WindowToWorld(WindowPos) const;
WindowSize WorldToWindowSize(WorldSize) const; WindowSize WorldToWindowSize(WorldSize) const;

View File

@ -55,7 +55,7 @@ void GameLoop::Run() {
// draw all the entities (player etc) // draw all the entities (player etc)
for (auto &entity : m_Game->GetEntities()) { for (auto &entity : m_Game->GetEntities()) {
const auto& camera = m_Game->GetCamera(); 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(); m_Window->Flush();

View File

@ -25,7 +25,7 @@ public:
// GetTexture cannot return pointer to const, as SDL_RenderTexture modifies it // GetTexture cannot return pointer to const, as SDL_RenderTexture modifies it
SDL_Texture *GetTexture() { return m_Texture.get(); } 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; } WorldPos GetCenter() const { return m_ImageCenter; }
void LoadImage(std::string path, WorldPos image_center = WorldPos{}); void LoadImage(std::string path, WorldPos image_center = WorldPos{});
@ -33,7 +33,7 @@ public:
private: private:
static std::shared_ptr<SDL_Renderer> m_Renderer; static std::shared_ptr<SDL_Renderer> m_Renderer;
std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> m_Texture; std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)> m_Texture;
WorldPos m_Size; WorldSize m_Size;
WorldPos m_ImageCenter; WorldPos m_ImageCenter;
float m_TextureWidth = 0; float m_TextureWidth = 0;
float m_TextureHeight = 0; float m_TextureHeight = 0;

View File

@ -76,9 +76,9 @@ Window::~Window() {
LOG_DEBUG("."); LOG_DEBUG(".");
} }
void Window::DrawSprite(const WindowPos &position, Sprite &s) { void Window::DrawSprite(const WindowPos &position, Sprite &s, float scale) {
WorldPos size = s.GetSize(); WorldSize size = s.GetSize() * scale;
WorldPos img_center = s.GetCenter(); WorldPos img_center = s.GetCenter() * scale;
SDL_FRect rect = {position.x() - img_center.x(), position.y() - img_center.y(), SDL_FRect rect = {position.x() - img_center.x(), position.y() - img_center.y(),
size.x(), size.y()}; size.x(), size.y()};
SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect); SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect);

View File

@ -22,7 +22,7 @@ public:
Window &operator=(Window &&) = delete; Window &operator=(Window &&) = delete;
std::expected<void, std::string> Init(); std::expected<void, std::string> 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, void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R,
uint8_t G, uint8_t B, uint8_t A); uint8_t G, uint8_t B, uint8_t A);
void ClearWindow(); void ClearWindow();