From e8584d87faf23645ec28541b86aebc70a28a92f0 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 17 Aug 2015 23:14:08 +0200 Subject: [PATCH] Merge HalfDiamond into Diamond, store ctor parameters. Also added unit test. --- include/doorkeeper/components/pixelconv.hpp | 20 +++--------- include/doorkeeper/implem/pixelconv.inl | 4 +-- src/pixelconv.cpp | 35 +++++++++++---------- test/unit/asciimapsource.cpp | 28 ++++++++++++++++- 4 files changed, 53 insertions(+), 34 deletions(-) diff --git a/include/doorkeeper/components/pixelconv.hpp b/include/doorkeeper/components/pixelconv.hpp index b754b43..fca3237 100644 --- a/include/doorkeeper/components/pixelconv.hpp +++ b/include/doorkeeper/components/pixelconv.hpp @@ -4,6 +4,7 @@ #include "doorkeeper/components/tileiterator.hpp" #include "doorkeeper/implem/maptypes.hpp" #include +#include namespace dk { //MapType_IsometricStaggered, @@ -42,26 +43,15 @@ namespace dk { using base_class::coords; PixelConvDiamond ( bool parStaggered, bool parFirstReentrant ); + PixelConvDiamond ( bool parStaggered, bool parFirstReentrant, const coords& parRatio ); virtual ~PixelConvDiamond ( void ) noexcept = default; virtual coords to_pixel ( const coords& parFrom, const coords& parSize ) const; protected: - PixelConvDiamond ( MapTypes parType, bool parStaggered, bool parFirstReentrant ); - }; - - class PixelConvHalfDiamond : public PixelConvDiamond { - public: - using base_class = PixelConvDiamond; - using base_class::coords; - - explicit PixelConvHalfDiamond ( bool parHalfVertically ); - virtual ~PixelConvHalfDiamond ( void ) noexcept = default; - - virtual coords to_pixel ( const coords& parFrom, const coords& parSize ) const; - - private: - const coords m_div; + const coords m_ratio; + const CoordinateScalarType m_first_reentr; + const bool m_staggered; }; class PixelConvHex : public PixelConv<2> { diff --git a/include/doorkeeper/implem/pixelconv.inl b/include/doorkeeper/implem/pixelconv.inl index 596d4b1..1f62282 100644 --- a/include/doorkeeper/implem/pixelconv.inl +++ b/include/doorkeeper/implem/pixelconv.inl @@ -2,14 +2,14 @@ namespace dk { template inline Vector<2> get_diamond_coordinates (const TileIterator& parIterator, const Vector<2>& parSize) { - PixelConvDiamond pconv(true, true); + PixelConvDiamond pconv(true, false); return pconv.to_pixel(parIterator.position(), parSize); } template inline Vector<2> get_half_diamond_coordinates (const TileIterator& parIterator, const Vector<2>& parSize) { - PixelConvHalfDiamond pconv(true); + PixelConvDiamond pconv(true, false, PixelConvDiamond::coords(1, 2)); return pconv.to_pixel(parIterator.position(), parSize); } diff --git a/src/pixelconv.cpp b/src/pixelconv.cpp index 0bbb84d..c6fdb2c 100644 --- a/src/pixelconv.cpp +++ b/src/pixelconv.cpp @@ -2,30 +2,33 @@ namespace dk { PixelConvDiamond::PixelConvDiamond (bool parStaggered, bool parFirstReentrant) : - base_class(parStaggered ? MapType_IsometricStaggered : MapType_Isometric) + base_class(parStaggered ? MapType_IsometricStaggered : MapType_Isometric), + m_ratio(1), + m_first_reentr(parFirstReentrant ? 1 : 0), + m_staggered(parStaggered) { } - PixelConvDiamond::PixelConvDiamond (MapTypes parType, bool parStaggered, bool parFirstReentrant) : - base_class(MapType_IsometricStaggered) + PixelConvDiamond::PixelConvDiamond (bool parStaggered, bool parFirstReentrant, const coords& parRatio) : + base_class(MapType_IsometricStaggered), + m_ratio(parRatio), + m_first_reentr(parFirstReentrant ? 1 : 0), + m_staggered(parStaggered) { + DK_ASSERT(parRatio > 0); } auto PixelConvDiamond::to_pixel (const coords& parFrom, const coords& parSize) const -> coords { - const auto from(parFrom); - const CoordinateScalarType xoffs = from.y() % 2; + if (m_staggered) { + const auto from(parFrom); + const CoordinateScalarType xoffs = m_first_reentr xor (from.y() bitand 1); - return from * parSize + coords(xoffs * parSize.x() / 2, 0); - } - - PixelConvHalfDiamond::PixelConvHalfDiamond (bool parHalfVertically) : - base_class(MapType_IsometricStaggered, true, true), - m_div(parHalfVertically ? coords(1, 2) : coords(2, 1)) - { - } - - auto PixelConvHalfDiamond::to_pixel (const coords& parFrom, const coords& parSize) const -> coords { - return base_class::to_pixel(parFrom, parSize / m_div); + return (from * parSize + coords(xoffs * parSize.x() / 2, 0)) / m_ratio; + } + else { + DK_ASSERT(false); //not implemented + return coords(0); + } } PixelConvHex::PixelConvHex (bool parFirstReentrant) : diff --git a/test/unit/asciimapsource.cpp b/test/unit/asciimapsource.cpp index 5015d4c..4968d82 100644 --- a/test/unit/asciimapsource.cpp +++ b/test/unit/asciimapsource.cpp @@ -52,12 +52,38 @@ TEST_F(asciimapsource, load) { TEST_F(asciimapsource, coordinates) { const coords2 tile_size(32); dk::Viewport<2> full_view(tiler, coords2(map_width, map_height), coords2(0)); + dk::PixelConvSquare<2> iso_conv; + dk::PixelConvDiamond diamond_conv(true, false); + dk::PixelConvDiamond diamond_conv2(true, true); full_view.setFrom(coords2(0)); for (auto itTile = full_view.begin(*layer), itTileEND = full_view.end(*layer); itTile != itTileEND; ++itTile) { + //Check isometric coordinates { const coords2 expected_coords(tile_size * itTile.position()); - const auto returned_coords(dk::get_square_coordinates<2>(itTile, tile_size)); + const auto returned_coords(iso_conv.to_pixel(itTile.position(), tile_size)); + EXPECT_EQ(expected_coords, returned_coords); + } + + //Check staggered diamond coordinates, second row reentrant + { + const auto xoffs = (itTile.position().y() % 2 == 0 ? 0 : tile_size.x() / 2); + const coords2 expected_coords( + xoffs + itTile.position().x() * tile_size.x(), + itTile.position().y() * tile_size.y() + ); + const auto returned_coords(diamond_conv.to_pixel(itTile.position(), tile_size)); + EXPECT_EQ(expected_coords, returned_coords); + } + + //Check staggered diamond coordinates, first row reentrant + { + const auto xoffs = (itTile.position().y() % 2 == 1 ? 0 : tile_size.x() / 2); + const coords2 expected_coords( + xoffs + itTile.position().x() * tile_size.x(), + itTile.position().y() * tile_size.y() + ); + const auto returned_coords(diamond_conv2.to_pixel(itTile.position(), tile_size)); EXPECT_EQ(expected_coords, returned_coords); } }