diff --git a/src/character.cpp b/src/character.cpp index e507f6c..1dd6f29 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -20,6 +20,7 @@ #include "character.hpp" #include "sdlmain.hpp" #include "texture.hpp" +#include namespace cloonel { ///------------------------------------------------------------------------- @@ -27,8 +28,13 @@ namespace cloonel { Character::Character (const std::string& parPath, SDLMain* parMain, float2 parSize) : Placeable(float2(0.0f)), Drawable(parSize), + m_screenRatio(), + m_screenRatioID(0), + m_sdlmain(parMain), m_texture(new Texture(parPath, parMain, false)) { + assert(parMain); + m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio); } ///------------------------------------------------------------------------- @@ -36,13 +42,19 @@ namespace cloonel { Character::Character (const std::string&& parPath, SDLMain* parMain, float2 parSize) : Placeable(float2(0.0f)), Drawable(parSize), + m_screenRatio(), + m_screenRatioID(0), + m_sdlmain(parMain), m_texture(new Texture(parPath, parMain, false)) { + assert(parMain); + m_screenRatioID = parMain->RegisterForResChange(&m_screenRatio); } ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- Character::~Character() noexcept { + m_sdlmain->UnregisterForResChange(m_screenRatioID); } ///------------------------------------------------------------------------- @@ -63,6 +75,6 @@ namespace cloonel { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- void Character::Draw() const { - m_texture->Render(GetPos(), WidthHeight()); + m_texture->Render(GetPos(), WidthHeight(), m_screenRatio.Ratio()); } } //namespace cloonel diff --git a/src/character.hpp b/src/character.hpp index 3ca8d29..61da69d 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -23,6 +23,7 @@ #include "placeable.hpp" #include "drawable.hpp" #include "vector.hpp" +#include "sizenotifiable.hpp" #include #include @@ -41,6 +42,9 @@ namespace cloonel { virtual void Draw ( void ) const; private: + SizeNotifiable m_screenRatio; + size_t m_screenRatioID; + SDLMain* const m_sdlmain; const std::unique_ptr m_texture; }; } //unnamed namespace diff --git a/src/sdlmain.cpp b/src/sdlmain.cpp index 1579220..38cbc75 100644 --- a/src/sdlmain.cpp +++ b/src/sdlmain.cpp @@ -109,6 +109,7 @@ namespace cloonel { ///------------------------------------------------------------------------ ///------------------------------------------------------------------------ size_t SDLMain::RegisterForResChange (SizeNotifiable* parNotif) { + parNotif->NotifyResChanged(m_localData->sizeratio); return m_localData->resChangeNotifList.Add(parNotif); } @@ -123,10 +124,4 @@ namespace cloonel { ushort2 SDLMain::WidthHeight() const noexcept { return static_cast(m_localData->sizeratio.Resolution()); } - - ///------------------------------------------------------------------------ - ///------------------------------------------------------------------------ - const float2& SDLMain::WHScaling() const noexcept { - return m_localData->sizeratio.Ratio(); - } } //namespace cloonel diff --git a/src/sdlmain.hpp b/src/sdlmain.hpp index 786fcbc..ae3bdea 100644 --- a/src/sdlmain.hpp +++ b/src/sdlmain.hpp @@ -36,7 +36,6 @@ namespace cloonel { void Init ( void ); SDL_Renderer* GetRenderer ( void ); ushort2 WidthHeight ( void ) const noexcept; - const float2& WHScaling ( void ) const noexcept; void SetResolution ( ushort2 parRes ); size_t RegisterForResChange ( SizeNotifiable* parNotif ); diff --git a/src/sizenotifiable.cpp b/src/sizenotifiable.cpp index 6c6f2b0..5f3b58e 100644 --- a/src/sizenotifiable.cpp +++ b/src/sizenotifiable.cpp @@ -1,8 +1,10 @@ #include "sizenotifiable.hpp" +#include "sizeratio.hpp" namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void SizeNotifiable::NotifyResChanged (const SizeRatio& parSize) { + m_scaleRatio = parSize.Ratio(); } } //namespace cloonel diff --git a/src/sizenotifiable.hpp b/src/sizenotifiable.hpp index 232ac01..c107840 100644 --- a/src/sizenotifiable.hpp +++ b/src/sizenotifiable.hpp @@ -31,6 +31,7 @@ namespace cloonel { virtual ~SizeNotifiable ( void ) noexcept = default; virtual void NotifyResChanged ( const SizeRatio& parSize ); + const float2& Ratio ( void ) const noexcept { return m_scaleRatio; } private: float2 m_scaleRatio; diff --git a/src/sizeratio.cpp b/src/sizeratio.cpp index c6180eb..aef51ca 100644 --- a/src/sizeratio.cpp +++ b/src/sizeratio.cpp @@ -7,7 +7,7 @@ namespace cloonel { ///---------------------------------------------------------------------- ///---------------------------------------------------------------------- float2 CalculateRatio (float2 parOriginal, float2 parResolution) { - return parOriginal / parResolution; + return parResolution / parOriginal; } } //unnamed namespace diff --git a/src/texture.cpp b/src/texture.cpp index eb9c4ff..43966c7 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -269,11 +269,10 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- - void Texture::Render (const float2& parPos, const float2& parSize) const { + void Texture::Render (const float2& parPos, const float2& parSize, const float2& parScaling) const { assert(IsLoaded()); - const float2 scaling(m_sdlmain->WHScaling()); - const ushort2 pos(static_cast(parPos * scaling + 0.5f)); - const ushort2 siz(static_cast(parSize * scaling + 0.5f)); + const ushort2 pos(static_cast(parPos * parScaling + 0.5f)); + const ushort2 siz(static_cast(parSize * parScaling + 0.5f)); const int screenHeight = m_sdlmain->WidthHeight().y(); const SDL_Rect dest = { pos.x(), screenHeight - pos.y() - siz.y(), siz.x(), siz.y() }; SDL_RenderCopy(m_sdlmain->GetRenderer(), m_texture, nullptr, &dest); diff --git a/src/texture.hpp b/src/texture.hpp index d4ca914..4c1477e 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -36,8 +36,8 @@ namespace cloonel { void Reload ( void ); void Destroy ( void ) noexcept; bool IsLoaded ( void ) const { return nullptr != m_texture; } - void Render ( const float2& parPos ) const { Render(parPos, m_size); } - void Render ( const float2& parPos, const float2& parSize ) const; + void Render ( const float2& parPos, const float2& parScaling ) const { Render(parPos, m_size, parScaling); } + void Render ( const float2& parPos, const float2& parSize, const float2& parScaling ) const; const SDLMain* SDLObject ( void ) const { return m_sdlmain; } private: diff --git a/src/tiledwallpaper.cpp b/src/tiledwallpaper.cpp index d002e4b..c9ebea8 100644 --- a/src/tiledwallpaper.cpp +++ b/src/tiledwallpaper.cpp @@ -50,6 +50,6 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::Draw() const { - m_tile->Render(float2(0.0f)); + m_tile->Render(float2(0.0f), float2(1.0f)); } } //namespace cloonel