diff --git a/include/doorkeeper/components/tileiterator.hpp b/include/doorkeeper/components/tileiterator.hpp index efd7980..884aacf 100644 --- a/include/doorkeeper/components/tileiterator.hpp +++ b/include/doorkeeper/components/tileiterator.hpp @@ -61,6 +61,9 @@ namespace dk { TileCoordsType m_tile_range;; qualif_vector_type* m_data; }; + + template + Vector make_past_end_coords ( const Vector& parTo ) a_pure; } //namespace dk #include "doorkeeper/implem/tileiterator.inl" diff --git a/include/doorkeeper/implem/basemapsource.inl b/include/doorkeeper/implem/basemapsource.inl index 2ebc9ed..e57fca5 100644 --- a/include/doorkeeper/implem/basemapsource.inl +++ b/include/doorkeeper/implem/basemapsource.inl @@ -27,7 +27,7 @@ namespace dk { template template void BaseMapSource::fetch (std::vector& parOut, const coords& parFrom, const coords& parTo) { - const auto tile_count = tile_volume(parTo - parFrom + 1); + const auto tile_count = tile_volume(parTo - parFrom); const std::size_t casted_tile_count = static_cast(tile_count); DK_ASSERT(static_cast(casted_tile_count) == tile_count); diff --git a/include/doorkeeper/implem/layer.inl b/include/doorkeeper/implem/layer.inl index 314f4b1..09694b5 100644 --- a/include/doorkeeper/implem/layer.inl +++ b/include/doorkeeper/implem/layer.inl @@ -44,7 +44,7 @@ namespace dk { ///-------------------------------------------------------------------------- template typename Layer::iterator Layer::end() { - return iterator(&m_tiles, this->m_count, this->m_count); + return iterator(&m_tiles, make_past_end_coords(this->m_count - 1), make_past_end_coords(this->m_count - 1)); } ///-------------------------------------------------------------------------- diff --git a/include/doorkeeper/implem/tileiterator.inl b/include/doorkeeper/implem/tileiterator.inl index 84459d1..92c5e67 100644 --- a/include/doorkeeper/implem/tileiterator.inl +++ b/include/doorkeeper/implem/tileiterator.inl @@ -37,7 +37,7 @@ namespace dk { template bool TileIterator::equal (const TileIterator& parOther) const { - return m_data == parOther.m_data and m_tile_range == parOther.m_tile_range and m_tile_range == parOther.m_tile_range; + return m_data == parOther.m_data and m_tile_range.position() == parOther.m_tile_range.position(); } template @@ -57,4 +57,14 @@ namespace dk { auto TileIterator::position() const -> const coords& { return m_tile_range.position(); } + + template + inline Vector make_past_end_coords (const Vector& parTo) { + Vector retval(0); + //for (CoordinateDistType d = 0; d < D - 1; ++d) { + //retval[d] = parFrom[d]; + //} + retval[D - 1] = parTo[D - 1] + 1; + return retval; + } } //namespace dk diff --git a/include/doorkeeper/implem/viewport.inl b/include/doorkeeper/implem/viewport.inl index 187e975..a60d94d 100644 --- a/include/doorkeeper/implem/viewport.inl +++ b/include/doorkeeper/implem/viewport.inl @@ -20,15 +20,14 @@ namespace dk { template typename Layer::iterator Viewport::begin (Layer& parLayer) const { typedef typename Layer::iterator IterType; - return IterType(&parLayer.m_tiles, m_position, m_position + m_size, coords(0), m_tyler.map_size()); + return IterType(&parLayer.m_tiles, m_position, m_tyler.map_size() - 1); } template template typename Layer::iterator Viewport::end (Layer& parLayer) const { typedef typename Layer::iterator IterType; - const auto to(m_position + m_size); - return IterType(&parLayer.m_tiles, m_tyler.map_size(), m_tyler.map_size()); + return IterType(&parLayer.m_tiles, make_past_end_coords(m_tyler.map_size() - 1), make_past_end_coords(m_tyler.map_size() - 1)); } template @@ -47,15 +46,14 @@ namespace dk { template typename Layer::const_iterator Viewport::cbegin (const Layer& parLayer) const { typedef typename Layer::const_iterator IterType; - return IterType(&parLayer.m_tiles, m_position, m_position + m_size, m_tyler.map_size()); + return IterType(&parLayer.m_tiles, m_position, m_tyler.map_size() - 1); } template template typename Layer::const_iterator Viewport::cend (const Layer& parLayer) const { typedef typename Layer::const_iterator IterType; - const auto to(m_position + m_size); - return IterType(&parLayer.m_tiles, m_tyler.map_size(), m_tyler.map_size()); + return IterType(&parLayer.m_tiles, make_past_end_coords(m_tyler.map_size() - 1), make_past_end_coords(m_tyler.map_size() - 1)); } template diff --git a/src/asciimapsource.cpp b/src/asciimapsource.cpp index bf8c29f..86f8634 100644 --- a/src/asciimapsource.cpp +++ b/src/asciimapsource.cpp @@ -93,10 +93,10 @@ namespace dkh { void AsciiMapSource::fetch_raw (char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize) { const std::size_t from = parFrom.x() + parFrom.y() * m_mapSize.x(); - const std::size_t to = parTo.x() + parTo.y() * m_mapSize.x() + 1; + const std::size_t to = parTo.x() - 1 + (parTo.y() - 1) * m_mapSize.x() + 1; DK_ASSERT(to >= from); DK_ASSERT(parSize / sizeof(MapTileType) == to - from); - std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, parOut); + std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, reinterpret_cast(parOut)); } dk::MapTypes AsciiMapSource::mapType() const { diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 686d40a..f92e931 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -13,6 +13,7 @@ add_executable(${PROJECT_NAME} tilecoords.cpp vector.cpp tileiterator.cpp + asciimapsource.cpp ) target_link_libraries(${PROJECT_NAME} diff --git a/test/unit/asciimapsource.cpp b/test/unit/asciimapsource.cpp new file mode 100644 index 0000000..d2f399f --- /dev/null +++ b/test/unit/asciimapsource.cpp @@ -0,0 +1,43 @@ +#include +#include "doorkeeper/helpers/asciimapsource.hpp" +#include "doorkeeper/doorkeeper.hpp" +#include +#include + +TEST(load, asciimapsource) { + typedef dk::Tyler<2>::coords coords2; + typedef std::unique_ptr AsciiMapSourceUPtr; + + const char map_data[] = + "1101111001\n1101111001\n1110111100\n1110111000\n" + "1111011011\n1111010111\n1111101111\n1111111111\n"; + const dk::CoordinateScalarType tile_size = 64; + const dk::CoordinateScalarType map_width = 10; + const dk::CoordinateScalarType map_height = 8; + + std::istringstream iss((std::string(map_data))); + dk::Tyler<2> tiler((coords2(tile_size))); + AsciiMapSourceUPtr loader(new dkh::AsciiMapSource(iss, coords2(map_width, map_height), dk::MapType_Isometric, coords2(tile_size))); + auto layer = &tiler.push_layer(loader.get(), 0); + + dk::Viewport<2> full_view(tiler, coords2(map_width, map_height), coords2(0)); + + EXPECT_EQ(coords2(map_width, map_height), tiler.map_size()); + + dk::CoordinateDistType index = 0; + int data_index = 0; + full_view.setFrom(coords2(0)); + for (auto itTile = full_view.begin(*layer), itTileEND = full_view.end(*layer); itTile != itTileEND; ++itTile) { + EXPECT_EQ(index, dk::to_index(itTile.raw_coords())); + EXPECT_LT(index, sizeof(map_data)); + ASSERT_LT(data_index, sizeof(map_data)); + const auto expected_value = static_cast(map_data[data_index] - '0'); + EXPECT_EQ(expected_value, *itTile); + ++index; + ++data_index; + if (map_data[data_index] == '\n') { + ++data_index; + } + } + EXPECT_EQ(map_width * map_height, index); +}