diff --git a/src/character.cpp b/src/character.cpp index fd572ea..630bea0 100644 --- a/src/character.cpp +++ b/src/character.cpp @@ -22,25 +22,20 @@ namespace curry { namespace { - vec2f get_safe_width_height (const Texture& parTex) { - return (parTex.texture() ? - vector_cast(parTex.width_height()) - : - vec2f(0.0f) - ); - } } //unnamed namespace Character::Character (const WorldSizeNotifiable::DeferredRegister& parDeferredRegister) : WorldSizeNotifiable(parDeferredRegister), - m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)) + m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)), + m_width_height(0.0f) { } void Character::load (const char* parTexturePath, cloonel::SDLMain& parSDLMain) { - const auto old_wh = get_safe_width_height(m_texture); + const auto old_wh = m_width_height; m_texture.load(parTexturePath, parSDLMain); - const auto new_wh = vector_cast(m_texture.width_height()); + m_width_height = vector_cast(m_texture.width_height()); + const auto& new_wh = m_width_height; if (m_position.get_min() != m_position.get_max()) m_position.change_minmax(m_position.get_min(), m_position.get_max() + old_wh - new_wh); } @@ -57,8 +52,8 @@ namespace curry { return m_position.get(); } - vec2us Character::width_height() const { - return get_safe_width_height(m_texture); + const vec2f& Character::width_height() const { + return m_width_height; } Texture& Character::texture() { @@ -68,14 +63,14 @@ namespace curry { Rect Character::destination_rect (const vec2f& parWorldPos) const { return Rect( m_position.get() - parWorldPos, - m_position.get() - parWorldPos + vector_cast(width_height()) + m_position.get() - parWorldPos + width_height() ); } Rect Character::source_rect() const { return Rect( vec2f(0.0f), - vector_cast(width_height()) + width_height() ); } diff --git a/src/character.hpp b/src/character.hpp index 43abf80..9db2503 100644 --- a/src/character.hpp +++ b/src/character.hpp @@ -39,7 +39,7 @@ namespace curry { void unload(); const vec2f& position() const; void set_position (const vec2f& parPos); - vec2us width_height() const; + const vec2f& width_height() const; Texture& texture(); Rect destination_rect (const vec2f& parWorldPos) const; Rect source_rect() const; @@ -48,5 +48,6 @@ namespace curry { private: ConstrainedPosition m_position; Texture m_texture; + vec2f m_width_height; }; } //namespace curry diff --git a/src/ingamescene.cpp b/src/ingamescene.cpp index edc77bc..f7bb66b 100644 --- a/src/ingamescene.cpp +++ b/src/ingamescene.cpp @@ -114,8 +114,8 @@ namespace curry { offset.y() -= speed; if (cloonel::IsPressed(input_bag(), ActionDown)) offset.y() += speed; - viewport.set_position(viewport.position() + offset); m_local_data->character.set_position(m_local_data->character.position() + offset); + viewport.set_position(m_local_data->character.position() - (viewport.size() - m_local_data->character.width_height()) / 2.0f); const auto tilesize(static_cast(world.tile_size())); const auto tilecount(m_local_data->worldtiles.width_height() / world.tile_size());