Make the viewport able to return the tile of a given index.

This commit is contained in:
King_DuckZ 2016-10-31 01:36:30 +01:00
parent 458ec6bcce
commit 087e1cd8b1
3 changed files with 20 additions and 1 deletions

View file

@ -66,7 +66,7 @@ namespace curry {
ScreenTile TileIterator::dereference() const {
auto index = vector_cast<vec2us>(m_index);
return ScreenTile {
m_viewport->world()->tile(index),
m_viewport->tile(index),
m_pixel_pos
};
}

View file

@ -1,8 +1,21 @@
#include "worldviewport.hpp"
#include "worldgrid.hpp"
#include "compatibility.h"
#include <cassert>
namespace curry {
namespace {
vec2us starting_index (const WorldViewport& parViewport) a_pure;
vec2us starting_index (const WorldViewport& parViewport) {
const auto pos = vector_cast<vec2i>(parViewport.position());
const auto ts = parViewport.world()->tile_size();
return vec2us(
static_cast<uint16_t>(pos.x() / ts.x()),
static_cast<uint16_t>(pos.y() / ts.y())
);
}
} //unnamed namespace
WorldViewport::WorldViewport (WorldGrid* parWorld, vec2f parSize) :
m_position(0),
m_size(parSize),
@ -34,4 +47,8 @@ namespace curry {
void WorldViewport::set_position (const vec2f& parPos) {
m_position = parPos; //TODO: assert validity
}
WorldViewport::TileIndex WorldViewport::tile (const vec2us& parIndex) const {
return world()->tile(parIndex + starting_index(*this));
}
} //namespace curry

View file

@ -9,11 +9,13 @@ namespace curry {
class WorldViewport {
public:
typedef TileIterator iterator;
typedef int TileIndex;
WorldViewport (WorldGrid* parWorld, vec2f parSize);
const WorldGrid* world() const;
const vec2f& position() const;
const vec2f& size() const;
TileIndex tile (const vec2us& parIndex) const;
void set_position (const vec2f& parPos);