From 7e8657b9d8f90dc4f5faaace864e4c541e8d4c2a Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 27 Oct 2016 10:29:37 +0200 Subject: [PATCH] Allow viewport to move past the top-left edge of the map. --- src/tileiterator.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/tileiterator.cpp b/src/tileiterator.cpp index 1718c4b..0f108b9 100644 --- a/src/tileiterator.cpp +++ b/src/tileiterator.cpp @@ -1,19 +1,31 @@ #include "tileiterator.hpp" #include "worldviewport.hpp" #include "worldgrid.hpp" +#include "compatibility.h" #include #include namespace curry { + namespace { + vec2i first_col_starting_pixel (const vec2i& parViewportPos, const vec2i& parTileSize) a_pure; + + vec2i first_col_starting_pixel (const vec2i& parViewportPos, const vec2i& parTileSize) { + const auto& pos = parViewportPos; + const auto& ts = parTileSize; + + return vec2i( + (pos.x() > 0 ? -pos.x() % ts.x() : -pos.x()), + (pos.y() > 0 ? -pos.y() % ts.y() : -pos.y()) + ); + } + } //unnamed namespace + TileIterator::TileIterator (WorldViewport* parViewport, bool parEnd) : m_viewport(parViewport), m_index(parEnd ? -1 : 0) { assert(m_viewport); - m_pixel_pos = vec2i( - -m_viewport->position().x() % m_viewport->world()->tile_size().x(), - -m_viewport->position().y() % m_viewport->world()->tile_size().y() - ); + m_pixel_pos = first_col_starting_pixel(m_viewport->position(), m_viewport->world()->tile_size()); } void TileIterator::increment() { @@ -21,9 +33,9 @@ namespace curry { const WorldGrid& world = *m_viewport->world(); vec2i tile_size(world.tile_size()); - auto first_tile_pos = vec2i( - -m_viewport->position().x() % tile_size.x(), - -m_viewport->position().y() % tile_size.y() + auto first_tile_pos = first_col_starting_pixel( + m_viewport->position(), + m_viewport->world()->tile_size() ); vec2i pos(tile_size * m_index + first_tile_pos);