diff --git a/include/doorkeeper/components/pixelconv.hpp b/include/doorkeeper/components/pixelconv.hpp index fca3237..e1b9c84 100644 --- a/include/doorkeeper/components/pixelconv.hpp +++ b/include/doorkeeper/components/pixelconv.hpp @@ -16,12 +16,14 @@ namespace dk { public: typedef Vector coords; - explicit PixelConv ( MapTypes parType ); + PixelConv ( MapTypes parType, const coords& parTileSize ); - virtual coords to_pixel ( const coords& parFrom, const coords& parSize ) const = 0; + virtual coords to_pixel ( const coords& parFrom ) const = 0; MapTypes map_type ( void ) const; + const coords& tile_size ( void ) const; private: + const coords m_tile_size; const MapTypes m_map_type; }; @@ -31,10 +33,10 @@ namespace dk { using base_class = PixelConv; using typename base_class::coords; - PixelConvSquare ( void ); + explicit PixelConvSquare ( const coords& parTileSize ); virtual ~PixelConvSquare ( void ) noexcept = default; - virtual coords to_pixel ( const coords& parFrom, const coords& parSize ) const; + virtual coords to_pixel ( const coords& parFrom ) const; }; class PixelConvDiamond : public PixelConv<2> { @@ -42,11 +44,11 @@ namespace dk { using base_class = PixelConv<2>; using base_class::coords; - PixelConvDiamond ( bool parStaggered, bool parFirstReentrant ); - PixelConvDiamond ( bool parStaggered, bool parFirstReentrant, const coords& parRatio ); + PixelConvDiamond ( const coords& parTileSize, bool parStaggered, bool parFirstReentrant ); + PixelConvDiamond ( const coords& parTileSize, bool parStaggered, bool parFirstReentrant, const coords& parRatio ); virtual ~PixelConvDiamond ( void ) noexcept = default; - virtual coords to_pixel ( const coords& parFrom, const coords& parSize ) const; + virtual coords to_pixel ( const coords& parFrom ) const; protected: const coords m_ratio; @@ -59,10 +61,10 @@ namespace dk { using base_class = PixelConv<2>; using base_class::coords; - explicit PixelConvHex ( bool parFirstReentrant ); + PixelConvHex ( const coords& parTileSize, bool parFirstReentrant ); virtual ~PixelConvHex ( void ) noexcept = default; - virtual coords to_pixel ( const coords& parFrom, const coords& parSize ) const; + virtual coords to_pixel ( const coords& parFrom ) const; }; template diff --git a/include/doorkeeper/implem/pixelconv.inl b/include/doorkeeper/implem/pixelconv.inl index 1f62282..bbb4ff7 100644 --- a/include/doorkeeper/implem/pixelconv.inl +++ b/include/doorkeeper/implem/pixelconv.inl @@ -2,33 +2,34 @@ namespace dk { template inline Vector<2> get_diamond_coordinates (const TileIterator& parIterator, const Vector<2>& parSize) { - PixelConvDiamond pconv(true, false); - return pconv.to_pixel(parIterator.position(), parSize); + PixelConvDiamond pconv(parSize, true, false); + return pconv.to_pixel(parIterator.position()); } template inline Vector<2> get_half_diamond_coordinates (const TileIterator& parIterator, const Vector<2>& parSize) { - PixelConvDiamond pconv(true, false, PixelConvDiamond::coords(1, 2)); - return pconv.to_pixel(parIterator.position(), parSize); + PixelConvDiamond pconv(parSize, true, false, PixelConvDiamond::coords(1, 2)); + return pconv.to_pixel(parIterator.position()); } template inline Vector get_square_coordinates (const TileIterator& parIterator, const Vector& parSize) { - PixelConvSquare pconv; - return pconv.to_pixel(parIterator.position(), parSize); + PixelConvSquare pconv((parSize)); + return pconv.to_pixel(parIterator.position()); } template inline Vector<2> get_hex_coordinates (const TileIterator& parIterator, const Vector<2>& parSize) { - PixelConvHex pconv(true); - return pconv.to_pixel(parIterator.position(), parSize); + PixelConvHex pconv(parSize, true); + return pconv.to_pixel(parIterator.position()); } template - PixelConv::PixelConv (MapTypes parType) : + PixelConv::PixelConv (MapTypes parType, const coords& parTileSize) : + m_tile_size(parTileSize), m_map_type(parType) { } @@ -39,13 +40,18 @@ namespace dk { } template - PixelConvSquare::PixelConvSquare() : - base_class(MapType_Orthogonal) + auto PixelConv::tile_size() const -> const coords& { + return m_tile_size; + } + + template + PixelConvSquare::PixelConvSquare (const coords& parTileSize) : + base_class(MapType_Orthogonal, parTileSize) { } template - auto PixelConvSquare::to_pixel (const coords& parFrom, const coords& parSize) const -> coords { - return parFrom * parSize; + auto PixelConvSquare::to_pixel (const coords& parFrom) const -> coords { + return parFrom * this->tile_size(); } } //namespace dk diff --git a/src/pixelconv.cpp b/src/pixelconv.cpp index c6fdb2c..6854c18 100644 --- a/src/pixelconv.cpp +++ b/src/pixelconv.cpp @@ -1,16 +1,16 @@ #include "doorkeeper/components/pixelconv.hpp" namespace dk { - PixelConvDiamond::PixelConvDiamond (bool parStaggered, bool parFirstReentrant) : - base_class(parStaggered ? MapType_IsometricStaggered : MapType_Isometric), + PixelConvDiamond::PixelConvDiamond (const coords& parTileSize, bool parStaggered, bool parFirstReentrant) : + base_class(parStaggered ? MapType_IsometricStaggered : MapType_Isometric, parTileSize), m_ratio(1), m_first_reentr(parFirstReentrant ? 1 : 0), m_staggered(parStaggered) { } - PixelConvDiamond::PixelConvDiamond (bool parStaggered, bool parFirstReentrant, const coords& parRatio) : - base_class(MapType_IsometricStaggered), + PixelConvDiamond::PixelConvDiamond (const coords& parTileSize, bool parStaggered, bool parFirstReentrant, const coords& parRatio) : + base_class(MapType_IsometricStaggered, parTileSize), m_ratio(parRatio), m_first_reentr(parFirstReentrant ? 1 : 0), m_staggered(parStaggered) @@ -18,12 +18,13 @@ namespace dk { DK_ASSERT(parRatio > 0); } - auto PixelConvDiamond::to_pixel (const coords& parFrom, const coords& parSize) const -> coords { + auto PixelConvDiamond::to_pixel (const coords& parFrom) const -> coords { if (m_staggered) { const auto from(parFrom); const CoordinateScalarType xoffs = m_first_reentr xor (from.y() bitand 1); + const auto& tile_size = this->tile_size(); - return (from * parSize + coords(xoffs * parSize.x() / 2, 0)) / m_ratio; + return (from * tile_size + coords(xoffs * tile_size.x() / 2, 0)) / m_ratio; } else { DK_ASSERT(false); //not implemented @@ -31,12 +32,12 @@ namespace dk { } } - PixelConvHex::PixelConvHex (bool parFirstReentrant) : - base_class(MapType_Hex) + PixelConvHex::PixelConvHex (const coords& parTileSize, bool parFirstReentrant) : + base_class(MapType_Hex, parTileSize) { } - auto PixelConvHex::to_pixel (const coords& parFrom, const coords& parSize) const -> coords { + auto PixelConvHex::to_pixel (const coords& parFrom) const -> coords { DK_ASSERT(false); //not implemented return coords(0); } diff --git a/test/unit/asciimapsource.cpp b/test/unit/asciimapsource.cpp index 4968d82..735919b 100644 --- a/test/unit/asciimapsource.cpp +++ b/test/unit/asciimapsource.cpp @@ -50,40 +50,40 @@ TEST_F(asciimapsource, load) { } TEST_F(asciimapsource, coordinates) { - const coords2 tile_size(32); + const coords2 tsize(tile_size); 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); + dk::PixelConvSquare<2> iso_conv((tsize)); + dk::PixelConvDiamond diamond_conv(tsize, true, false); + dk::PixelConvDiamond diamond_conv2(tsize, 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(iso_conv.to_pixel(itTile.position(), tile_size)); + const coords2 expected_coords(tsize * itTile.position()); + const auto returned_coords(iso_conv.to_pixel(itTile.position())); 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 auto xoffs = (itTile.position().y() % 2 == 0 ? 0 : tsize.x() / 2); const coords2 expected_coords( - xoffs + itTile.position().x() * tile_size.x(), - itTile.position().y() * tile_size.y() + xoffs + itTile.position().x() * tsize.x(), + itTile.position().y() * tsize.y() ); - const auto returned_coords(diamond_conv.to_pixel(itTile.position(), tile_size)); + const auto returned_coords(diamond_conv.to_pixel(itTile.position())); 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 auto xoffs = (itTile.position().y() % 2 == 1 ? 0 : tsize.x() / 2); const coords2 expected_coords( - xoffs + itTile.position().x() * tile_size.x(), - itTile.position().y() * tile_size.y() + xoffs + itTile.position().x() * tsize.x(), + itTile.position().y() * tsize.y() ); - const auto returned_coords(diamond_conv2.to_pixel(itTile.position(), tile_size)); + const auto returned_coords(diamond_conv2.to_pixel(itTile.position())); EXPECT_EQ(expected_coords, returned_coords); } }