Allow viewport to move past the top-left edge of the map.

This commit is contained in:
King_DuckZ 2016-10-27 10:29:37 +02:00
parent 86bf492507
commit 7e8657b9d8

View file

@ -1,19 +1,31 @@
#include "tileiterator.hpp"
#include "worldviewport.hpp"
#include "worldgrid.hpp"
#include "compatibility.h"
#include <cassert>
#include <ciso646>
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);