From 89ac57eee7163c1a7546e23c5db161cb9d4dbccf Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 7 Nov 2016 22:31:52 +0100 Subject: [PATCH] Don't crash when going past the bottom/right border. --- src/tileiterator.cpp | 13 +++++++++++-- src/worldgrid.cpp | 8 ++++++++ src/worldgrid.hpp | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/tileiterator.cpp b/src/tileiterator.cpp index 2371da5..c78e2bf 100644 --- a/src/tileiterator.cpp +++ b/src/tileiterator.cpp @@ -46,10 +46,19 @@ namespace curry { 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()); - if (pos.x() + tile_size.x() < m_viewport->size().x()) { + const float right_border = std::min( + m_viewport->size().x(), + static_cast(world_size_pixel(world).x()) - m_viewport->position().x() + ); + const float bottom_border = std::min( + m_viewport->size().y(), + static_cast(world_size_pixel(world).y()) - m_viewport->position().y() + ); + + if (static_cast(pos.x() + tile_size.x()) < right_border) { ++m_index.x(); } - else if (pos.y() + tile_size.y() < m_viewport->size().y()) { + else if (static_cast(pos.y() + tile_size.y()) < bottom_border) { m_index.x() = 0; ++m_index.y(); m_pixel_pos.x() = first_tile_pos.x(); diff --git a/src/worldgrid.cpp b/src/worldgrid.cpp index 2db07d2..8f1aee1 100644 --- a/src/worldgrid.cpp +++ b/src/worldgrid.cpp @@ -15,6 +15,10 @@ namespace curry { return m_tile_size; } + const vec2us& WorldGrid::world_size() const { + return m_world_size; + } + const TileIndex* WorldGrid::tile (const vec2us& parIndex) const { const std::size_t index = parIndex.x() + parIndex.y() * m_world_size.x(); assert(m_layer_count); @@ -74,4 +78,8 @@ namespace curry { m_layers.clear(); m_layer_count = 0; } + + vec2i world_size_pixel (const WorldGrid& parWorld) { + return parWorld.tile_size() * parWorld.world_size(); + } } //namespace curry diff --git a/src/worldgrid.hpp b/src/worldgrid.hpp index e3553ee..4e5d048 100644 --- a/src/worldgrid.hpp +++ b/src/worldgrid.hpp @@ -11,6 +11,7 @@ namespace curry { explicit WorldGrid (vec2us parTileSize); const vec2us& tile_size() const; + const vec2us& world_size() const; uint16_t layer_count() const; const TileIndex* tile (const vec2us& parIndex) const; void add_layer (vec2us parWorldSize, const std::vector& parLayer); @@ -25,4 +26,6 @@ namespace curry { vec2us m_world_size; uint16_t m_layer_count; }; + + vec2i world_size_pixel (const WorldGrid& parWorld); } //namespace curry