Store tile size in PixelConv instead of taking it in to_pixel.

This commit is contained in:
King_DuckZ 2015-08-18 00:00:59 +02:00
parent e8584d87fa
commit a1ae683601
4 changed files with 54 additions and 45 deletions

View File

@ -16,12 +16,14 @@ namespace dk {
public: public:
typedef Vector<D> coords; typedef Vector<D> 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; MapTypes map_type ( void ) const;
const coords& tile_size ( void ) const;
private: private:
const coords m_tile_size;
const MapTypes m_map_type; const MapTypes m_map_type;
}; };
@ -31,10 +33,10 @@ namespace dk {
using base_class = PixelConv<D>; using base_class = PixelConv<D>;
using typename base_class::coords; using typename base_class::coords;
PixelConvSquare ( void ); explicit PixelConvSquare ( const coords& parTileSize );
virtual ~PixelConvSquare ( void ) noexcept = default; 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> { class PixelConvDiamond : public PixelConv<2> {
@ -42,11 +44,11 @@ namespace dk {
using base_class = PixelConv<2>; using base_class = PixelConv<2>;
using base_class::coords; using base_class::coords;
PixelConvDiamond ( bool parStaggered, bool parFirstReentrant ); PixelConvDiamond ( const coords& parTileSize, bool parStaggered, bool parFirstReentrant );
PixelConvDiamond ( bool parStaggered, bool parFirstReentrant, const coords& parRatio ); PixelConvDiamond ( const coords& parTileSize, bool parStaggered, bool parFirstReentrant, const coords& parRatio );
virtual ~PixelConvDiamond ( void ) noexcept = default; 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: protected:
const coords m_ratio; const coords m_ratio;
@ -59,10 +61,10 @@ namespace dk {
using base_class = PixelConv<2>; using base_class = PixelConv<2>;
using base_class::coords; using base_class::coords;
explicit PixelConvHex ( bool parFirstReentrant ); PixelConvHex ( const coords& parTileSize, bool parFirstReentrant );
virtual ~PixelConvHex ( void ) noexcept = default; 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 <typename T, typename T1> template <typename T, typename T1>

View File

@ -2,33 +2,34 @@ namespace dk {
template <typename T, typename T1> template <typename T, typename T1>
inline inline
Vector<2> get_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) { Vector<2> get_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvDiamond pconv(true, false); PixelConvDiamond pconv(parSize, true, false);
return pconv.to_pixel(parIterator.position(), parSize); return pconv.to_pixel(parIterator.position());
} }
template <typename T, typename T1> template <typename T, typename T1>
inline inline
Vector<2> get_half_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) { Vector<2> get_half_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvDiamond pconv(true, false, PixelConvDiamond::coords(1, 2)); PixelConvDiamond pconv(parSize, true, false, PixelConvDiamond::coords(1, 2));
return pconv.to_pixel(parIterator.position(), parSize); return pconv.to_pixel(parIterator.position());
} }
template <uint32_t D, typename T, typename T1> template <uint32_t D, typename T, typename T1>
inline inline
Vector<D> get_square_coordinates (const TileIterator<T, D, T1>& parIterator, const Vector<D>& parSize) { Vector<D> get_square_coordinates (const TileIterator<T, D, T1>& parIterator, const Vector<D>& parSize) {
PixelConvSquare<D> pconv; PixelConvSquare<D> pconv((parSize));
return pconv.to_pixel(parIterator.position(), parSize); return pconv.to_pixel(parIterator.position());
} }
template <typename T, typename T1> template <typename T, typename T1>
inline inline
Vector<2> get_hex_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) { Vector<2> get_hex_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvHex pconv(true); PixelConvHex pconv(parSize, true);
return pconv.to_pixel(parIterator.position(), parSize); return pconv.to_pixel(parIterator.position());
} }
template <uint32_t D> template <uint32_t D>
PixelConv<D>::PixelConv (MapTypes parType) : PixelConv<D>::PixelConv (MapTypes parType, const coords& parTileSize) :
m_tile_size(parTileSize),
m_map_type(parType) m_map_type(parType)
{ {
} }
@ -39,13 +40,18 @@ namespace dk {
} }
template <uint32_t D> template <uint32_t D>
PixelConvSquare<D>::PixelConvSquare() : auto PixelConv<D>::tile_size() const -> const coords& {
base_class(MapType_Orthogonal) return m_tile_size;
}
template <uint32_t D>
PixelConvSquare<D>::PixelConvSquare (const coords& parTileSize) :
base_class(MapType_Orthogonal, parTileSize)
{ {
} }
template <uint32_t D> template <uint32_t D>
auto PixelConvSquare<D>::to_pixel (const coords& parFrom, const coords& parSize) const -> coords { auto PixelConvSquare<D>::to_pixel (const coords& parFrom) const -> coords {
return parFrom * parSize; return parFrom * this->tile_size();
} }
} //namespace dk } //namespace dk

View File

@ -1,16 +1,16 @@
#include "doorkeeper/components/pixelconv.hpp" #include "doorkeeper/components/pixelconv.hpp"
namespace dk { namespace dk {
PixelConvDiamond::PixelConvDiamond (bool parStaggered, bool parFirstReentrant) : PixelConvDiamond::PixelConvDiamond (const coords& parTileSize, bool parStaggered, bool parFirstReentrant) :
base_class(parStaggered ? MapType_IsometricStaggered : MapType_Isometric), base_class(parStaggered ? MapType_IsometricStaggered : MapType_Isometric, parTileSize),
m_ratio(1), m_ratio(1),
m_first_reentr(parFirstReentrant ? 1 : 0), m_first_reentr(parFirstReentrant ? 1 : 0),
m_staggered(parStaggered) m_staggered(parStaggered)
{ {
} }
PixelConvDiamond::PixelConvDiamond (bool parStaggered, bool parFirstReentrant, const coords& parRatio) : PixelConvDiamond::PixelConvDiamond (const coords& parTileSize, bool parStaggered, bool parFirstReentrant, const coords& parRatio) :
base_class(MapType_IsometricStaggered), base_class(MapType_IsometricStaggered, parTileSize),
m_ratio(parRatio), m_ratio(parRatio),
m_first_reentr(parFirstReentrant ? 1 : 0), m_first_reentr(parFirstReentrant ? 1 : 0),
m_staggered(parStaggered) m_staggered(parStaggered)
@ -18,12 +18,13 @@ namespace dk {
DK_ASSERT(parRatio > 0); 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) { if (m_staggered) {
const auto from(parFrom); const auto from(parFrom);
const CoordinateScalarType xoffs = m_first_reentr xor (from.y() bitand 1); 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 { else {
DK_ASSERT(false); //not implemented DK_ASSERT(false); //not implemented
@ -31,12 +32,12 @@ namespace dk {
} }
} }
PixelConvHex::PixelConvHex (bool parFirstReentrant) : PixelConvHex::PixelConvHex (const coords& parTileSize, bool parFirstReentrant) :
base_class(MapType_Hex) 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 DK_ASSERT(false); //not implemented
return coords(0); return coords(0);
} }

View File

@ -50,40 +50,40 @@ TEST_F(asciimapsource, load) {
} }
TEST_F(asciimapsource, coordinates) { 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::Viewport<2> full_view(tiler, coords2(map_width, map_height), coords2(0));
dk::PixelConvSquare<2> iso_conv; dk::PixelConvSquare<2> iso_conv((tsize));
dk::PixelConvDiamond diamond_conv(true, false); dk::PixelConvDiamond diamond_conv(tsize, true, false);
dk::PixelConvDiamond diamond_conv2(true, true); dk::PixelConvDiamond diamond_conv2(tsize, true, true);
full_view.setFrom(coords2(0)); full_view.setFrom(coords2(0));
for (auto itTile = full_view.begin(*layer), itTileEND = full_view.end(*layer); itTile != itTileEND; ++itTile) { for (auto itTile = full_view.begin(*layer), itTileEND = full_view.end(*layer); itTile != itTileEND; ++itTile) {
//Check isometric coordinates //Check isometric coordinates
{ {
const coords2 expected_coords(tile_size * itTile.position()); const coords2 expected_coords(tsize * itTile.position());
const auto returned_coords(iso_conv.to_pixel(itTile.position(), tile_size)); const auto returned_coords(iso_conv.to_pixel(itTile.position()));
EXPECT_EQ(expected_coords, returned_coords); EXPECT_EQ(expected_coords, returned_coords);
} }
//Check staggered diamond coordinates, second row reentrant //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( const coords2 expected_coords(
xoffs + itTile.position().x() * tile_size.x(), xoffs + itTile.position().x() * tsize.x(),
itTile.position().y() * tile_size.y() 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); EXPECT_EQ(expected_coords, returned_coords);
} }
//Check staggered diamond coordinates, first row reentrant //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( const coords2 expected_coords(
xoffs + itTile.position().x() * tile_size.x(), xoffs + itTile.position().x() * tsize.x(),
itTile.position().y() * tile_size.y() 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); EXPECT_EQ(expected_coords, returned_coords);
} }
} }