Don't crash when going past the bottom/right border.
This commit is contained in:
parent
20f8e75937
commit
89ac57eee7
3 changed files with 22 additions and 2 deletions
|
@ -46,10 +46,19 @@ namespace curry {
|
||||||
assert(pos.x() > -tile_size.x() and pos.x() < m_viewport->size().x());
|
assert(pos.x() > -tile_size.x() and pos.x() < m_viewport->size().x());
|
||||||
assert(pos.y() > -tile_size.y() and pos.y() < m_viewport->size().y());
|
assert(pos.y() > -tile_size.y() and pos.y() < m_viewport->size().y());
|
||||||
|
|
||||||
if (pos.x() + tile_size.x() < m_viewport->size().x()) {
|
const float right_border = std::min(
|
||||||
|
m_viewport->size().x(),
|
||||||
|
static_cast<float>(world_size_pixel(world).x()) - m_viewport->position().x()
|
||||||
|
);
|
||||||
|
const float bottom_border = std::min(
|
||||||
|
m_viewport->size().y(),
|
||||||
|
static_cast<float>(world_size_pixel(world).y()) - m_viewport->position().y()
|
||||||
|
);
|
||||||
|
|
||||||
|
if (static_cast<float>(pos.x() + tile_size.x()) < right_border) {
|
||||||
++m_index.x();
|
++m_index.x();
|
||||||
}
|
}
|
||||||
else if (pos.y() + tile_size.y() < m_viewport->size().y()) {
|
else if (static_cast<float>(pos.y() + tile_size.y()) < bottom_border) {
|
||||||
m_index.x() = 0;
|
m_index.x() = 0;
|
||||||
++m_index.y();
|
++m_index.y();
|
||||||
m_pixel_pos.x() = first_tile_pos.x();
|
m_pixel_pos.x() = first_tile_pos.x();
|
||||||
|
|
|
@ -15,6 +15,10 @@ namespace curry {
|
||||||
return m_tile_size;
|
return m_tile_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const vec2us& WorldGrid::world_size() const {
|
||||||
|
return m_world_size;
|
||||||
|
}
|
||||||
|
|
||||||
const TileIndex* WorldGrid::tile (const vec2us& parIndex) const {
|
const TileIndex* WorldGrid::tile (const vec2us& parIndex) const {
|
||||||
const std::size_t index = parIndex.x() + parIndex.y() * m_world_size.x();
|
const std::size_t index = parIndex.x() + parIndex.y() * m_world_size.x();
|
||||||
assert(m_layer_count);
|
assert(m_layer_count);
|
||||||
|
@ -74,4 +78,8 @@ namespace curry {
|
||||||
m_layers.clear();
|
m_layers.clear();
|
||||||
m_layer_count = 0;
|
m_layer_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vec2i world_size_pixel (const WorldGrid& parWorld) {
|
||||||
|
return parWorld.tile_size() * parWorld.world_size();
|
||||||
|
}
|
||||||
} //namespace curry
|
} //namespace curry
|
||||||
|
|
|
@ -11,6 +11,7 @@ namespace curry {
|
||||||
explicit WorldGrid (vec2us parTileSize);
|
explicit WorldGrid (vec2us parTileSize);
|
||||||
|
|
||||||
const vec2us& tile_size() const;
|
const vec2us& tile_size() const;
|
||||||
|
const vec2us& world_size() const;
|
||||||
uint16_t layer_count() const;
|
uint16_t layer_count() const;
|
||||||
const TileIndex* tile (const vec2us& parIndex) const;
|
const TileIndex* tile (const vec2us& parIndex) const;
|
||||||
void add_layer (vec2us parWorldSize, const std::vector<TileIndex>& parLayer);
|
void add_layer (vec2us parWorldSize, const std::vector<TileIndex>& parLayer);
|
||||||
|
@ -25,4 +26,6 @@ namespace curry {
|
||||||
vec2us m_world_size;
|
vec2us m_world_size;
|
||||||
uint16_t m_layer_count;
|
uint16_t m_layer_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
vec2i world_size_pixel (const WorldGrid& parWorld);
|
||||||
} //namespace curry
|
} //namespace curry
|
||||||
|
|
Loading…
Reference in a new issue