Merge HalfDiamond into Diamond, store ctor parameters.

Also added unit test.
This commit is contained in:
King_DuckZ 2015-08-17 23:14:08 +02:00
parent ad127b8fbe
commit e8584d87fa
4 changed files with 53 additions and 34 deletions

View file

@ -4,6 +4,7 @@
#include "doorkeeper/components/tileiterator.hpp"
#include "doorkeeper/implem/maptypes.hpp"
#include <cstdint>
#include <ciso646>
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> {

View file

@ -2,14 +2,14 @@ namespace dk {
template <typename T, typename T1>
inline
Vector<2> get_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvDiamond pconv(true, true);
PixelConvDiamond pconv(true, false);
return pconv.to_pixel(parIterator.position(), parSize);
}
template <typename T, typename T1>
inline
Vector<2> get_half_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvHalfDiamond pconv(true);
PixelConvDiamond pconv(true, false, PixelConvDiamond::coords(1, 2));
return pconv.to_pixel(parIterator.position(), parSize);
}

View file

@ -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 {
if (m_staggered) {
const auto from(parFrom);
const CoordinateScalarType xoffs = from.y() % 2;
const CoordinateScalarType xoffs = m_first_reentr xor (from.y() bitand 1);
return from * parSize + coords(xoffs * parSize.x() / 2, 0);
return (from * parSize + coords(xoffs * parSize.x() / 2, 0)) / m_ratio;
}
PixelConvHalfDiamond::PixelConvHalfDiamond (bool parHalfVertically) :
base_class(MapType_IsometricStaggered, true, true),
m_div(parHalfVertically ? coords(1, 2) : coords(2, 1))
{
else {
DK_ASSERT(false); //not implemented
return coords(0);
}
auto PixelConvHalfDiamond::to_pixel (const coords& parFrom, const coords& parSize) const -> coords {
return base_class::to_pixel(parFrom, parSize / m_div);
}
PixelConvHex::PixelConvHex (bool parFirstReentrant) :

View file

@ -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);
}
}