TileIterator returns Tile<T> instead of just T.

This commit is contained in:
King_DuckZ 2015-08-19 00:15:31 +02:00
parent ae05910316
commit 0bb92413db
8 changed files with 110 additions and 49 deletions

View file

@ -0,0 +1,35 @@
#ifndef id4617732A05EA419CA0CD21B7338E5471
#define id4617732A05EA419CA0CD21B7338E5471
#include "doorkeeper/primitivetypes.hpp"
#include "doorkeeper/components/pixelconv.hpp"
#include <vector>
namespace dk {
template <uint32_t D>
class PixelConv;
template <typename T, uint32_t D>
class Tile {
public:
typedef Vector<D> coords;
Tile ( T* parData, const TileCoords<D>& parPos, const coords& parBlockOffs, const PixelConv<D>* parPConv );
const T& data ( void ) const;
T& data ( void );
const coords& block_position ( void ) const;
const TileCoords<D>& raw_coords ( void ) const;
coords screen_position ( void ) const;
private:
const TileCoords<D> m_position;
const coords m_block_offs;
const PixelConv<D>* const m_pixconv;
T* const m_data;
};
} //namespace dk
#include "doorkeeper/implem/tile.inl"
#endif

View file

@ -5,6 +5,7 @@
#include "doorkeeper/implem/helpers.hpp"
#include "doorkeeper/components/tilecoords.hpp"
#include "doorkeeper/components/pixelconv.hpp"
#include "doorkeeper/components/tile.hpp"
#include <boost/iterator/iterator_facade.hpp>
#include <iterator>
#include <vector>
@ -36,13 +37,19 @@ namespace dk {
} //namespace implem
template <typename T, uint32_t D, typename T1=typename std::remove_cv<T>::type>
class TileIterator : public boost::iterator_facade<TileIterator<T, D>, T, boost::bidirectional_traversal_tag> {
class TileIterator : public boost::iterator_facade<TileIterator<T, D>, T, boost::random_access_traversal_tag, Tile<T, D>, CoordinateDistType> {
friend class boost::iterator_core_access;
typedef std::vector<T1> vector_type;
typedef typename implem::TypeWithQualifiers<T, vector_type>::result qualif_vector_type;
typedef boost::iterator_facade<TileIterator<T, D>, T, boost::random_access_traversal_tag, Tile<T, D>, CoordinateDistType> base_class;
public:
typedef TileCoords<D> TileCoordsType;
typedef typename TileCoordsType::coords coords;
typedef typename base_class::difference_type difference_type;
typedef typename base_class::value_type value_type;
typedef typename base_class::pointer pointer;
typedef typename base_class::reference reference;
typedef typename base_class::iterator_category iterator_category;
TileIterator ( void ) = default;
TileIterator ( const TileIterator& parOther ) = default;
@ -51,17 +58,13 @@ namespace dk {
TileIterator ( qualif_vector_type* parData, const PixelConv<D>& parPixConv, const coords& parStart, const coords& parArea );
~TileIterator ( void ) = default;
const TileCoordsType& raw_coords ( void ) const;
const coords& position ( void ) const;
coords pixel_position ( void ) const;
private:
void increment ( void );
void decrement ( void );
void advance ( size_t parAdvance );
ptrdiff_t distance_to ( const TileIterator& parOther );
difference_type distance_to ( const TileIterator& parOther );
bool equal ( const TileIterator& parOther ) const;
T& dereference ( void ) const;
reference dereference ( void ) const;
TileCoordsType m_tile_range;;
qualif_vector_type* m_data;

View file

@ -3,28 +3,28 @@ namespace dk {
inline
Vector<2> get_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvDiamond pconv(parSize, true, false);
return pconv.to_pixel(parIterator.position());
return pconv.to_pixel(parIterator->block_position());
}
template <typename T, typename T1>
inline
Vector<2> get_half_diamond_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvDiamond pconv(parSize, true, false, PixelConvDiamond::coords(1, 2));
return pconv.to_pixel(parIterator.position());
return pconv.to_pixel(parIterator->block_position());
}
template <uint32_t D, typename T, typename T1>
inline
Vector<D> get_square_coordinates (const TileIterator<T, D, T1>& parIterator, const Vector<D>& parSize) {
PixelConvSquare<D> pconv((parSize));
return pconv.to_pixel(parIterator.position());
return pconv.to_pixel(parIterator->block_position());
}
template <typename T, typename T1>
inline
Vector<2> get_hex_coordinates (const TileIterator<T, 2, T1>& parIterator, const Vector<2>& parSize) {
PixelConvHex pconv(parSize, true);
return pconv.to_pixel(parIterator.position());
return pconv.to_pixel(parIterator->block_position());
}
template <uint32_t D>

View file

@ -0,0 +1,37 @@
namespace dk {
template <typename T, uint32_t D>
Tile<T, D>::Tile (T* parData, const TileCoords<D>& parPos, const coords& parBlockOffs, const PixelConv<D>* parPConv) :
m_position(parPos),
m_block_offs(parBlockOffs),
m_pixconv(parPConv),
m_data(parData)
{
DK_ASSERT(m_pixconv);
DK_ASSERT(m_data);
}
template <typename T, uint32_t D>
const T& Tile<T, D>::data() const {
return *m_data;
}
template <typename T, uint32_t D>
T& Tile<T, D>::data() {
return *m_data;
}
template <typename T, uint32_t D>
auto Tile<T, D>::block_position() const -> const coords& {
return m_position.position();
}
template <typename T, uint32_t D>
const TileCoords<D>& Tile<T, D>::raw_coords() const {
return m_position;
}
template <typename T, uint32_t D>
auto Tile<T, D>::screen_position() const -> coords {
return m_pixconv->to_pixel(m_position.position());
}
} //namespace dk

View file

@ -35,7 +35,7 @@ namespace dk {
}
template <typename T, uint32_t D, typename T1>
ptrdiff_t TileIterator<T, D, T1>::distance_to (const TileIterator& parOther) {
auto TileIterator<T, D, T1>::distance_to (const TileIterator& parOther) -> difference_type {
return std::distance(to_index(m_tile_range), to_index(parOther.m_tile_range));
}
@ -45,27 +45,12 @@ namespace dk {
}
template <typename T, uint32_t D, typename T1>
T& TileIterator<T, D, T1>::dereference() const {
auto TileIterator<T, D, T1>::dereference() const -> reference {
const auto index = to_index(m_tile_range);
DK_ASSERT(m_data);
DK_ASSERT(index >= 0);
DK_ASSERT(static_cast<typename std::make_unsigned<decltype(index)>::type>(index) < m_data->size());
return (*m_data)[index];
}
template <typename T, uint32_t D, typename T1>
auto TileIterator<T, D, T1>::raw_coords() const -> const TileCoordsType& {
return m_tile_range;
}
template <typename T, uint32_t D, typename T1>
auto TileIterator<T, D, T1>::position() const -> const coords& {
return m_tile_range.position();
}
template <typename T, uint32_t D, typename T1>
auto TileIterator<T, D, T1>::pixel_position() const -> coords {
return m_pixel_conv.to_pixel(this->position());
return Tile<T, D>(&(*m_data)[index], m_tile_range, coords(0), &m_pixel_conv);
}
template <uint32_t D>

View file

@ -150,7 +150,7 @@ namespace {
int col = 0;
const auto tilecount = parView.count();
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
std::cout << *itTile;
std::cout << itTile->data();
++col;
if (col == tilecount.x()) {
col = 0;
@ -219,14 +219,14 @@ namespace {
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
SDL_Rect rect_dst;
const auto pixel_pos = dk::get_diamond_coordinates(itTile, tilesize);
rect_src.y = offsets[*itTile];
rect_src.h = original_size.y() - offsets[*itTile];
rect_src.y = offsets[itTile->data()];
rect_src.h = original_size.y() - offsets[itTile->data()];
rect_dst.x = pixel_pos.x();
rect_dst.y = pixel_pos.y() + parYOffs;
rect_dst.w = rect_src.w;
rect_dst.h = rect_src.h;
SDL_Texture* const curr_texture = (1 == *itTile ? parTile1 : parTile0);
SDL_Texture* const curr_texture = (1 == itTile->data() ? parTile1 : parTile0);
SDL_RenderCopy(parRenderer, curr_texture, &rect_src, &rect_dst);
}
}

View file

@ -35,11 +35,11 @@ TEST_F(asciimapsource, load) {
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_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<dk::CoordinateScalarType>(map_data[data_index] - '0');
EXPECT_EQ(expected_value, *itTile);
EXPECT_EQ(expected_value, itTile->data());
++index;
++data_index;
if (map_data[data_index] == '\n') {
@ -60,31 +60,31 @@ TEST_F(asciimapsource, coordinates) {
for (auto itTile = full_view.begin(*layer), itTileEND = full_view.end(*layer); itTile != itTileEND; ++itTile) {
//Check isometric coordinates
{
const coords2 expected_coords(tsize * itTile.position());
const auto returned_coords(iso_conv.to_pixel(itTile.position()));
const coords2 expected_coords(tsize * itTile->block_position());
const auto returned_coords(iso_conv.to_pixel(itTile->block_position()));
EXPECT_EQ(expected_coords, returned_coords);
EXPECT_EQ(expected_coords, itTile.pixel_position());
EXPECT_EQ(expected_coords, itTile->screen_position());
}
//Check staggered diamond coordinates, second row reentrant
{
const auto xoffs = (itTile.position().y() % 2 == 0 ? 0 : tsize.x() / 2);
const auto xoffs = (itTile->block_position().y() % 2 == 0 ? 0 : tsize.x() / 2);
const coords2 expected_coords(
xoffs + itTile.position().x() * tsize.x(),
itTile.position().y() * tsize.y()
xoffs + itTile->block_position().x() * tsize.x(),
itTile->block_position().y() * tsize.y()
);
const auto returned_coords(diamond_conv.to_pixel(itTile.position()));
const auto returned_coords(diamond_conv.to_pixel(itTile->block_position()));
EXPECT_EQ(expected_coords, returned_coords);
}
//Check staggered diamond coordinates, first row reentrant
{
const auto xoffs = (itTile.position().y() % 2 == 1 ? 0 : tsize.x() / 2);
const auto xoffs = (itTile->block_position().y() % 2 == 1 ? 0 : tsize.x() / 2);
const coords2 expected_coords(
xoffs + itTile.position().x() * tsize.x(),
itTile.position().y() * tsize.y()
xoffs + itTile->block_position().x() * tsize.x(),
itTile->block_position().y() * tsize.y()
);
const auto returned_coords(diamond_conv2.to_pixel(itTile.position()));
const auto returned_coords(diamond_conv2.to_pixel(itTile->block_position()));
EXPECT_EQ(expected_coords, returned_coords);
}
}

View file

@ -18,11 +18,12 @@ TEST(tileiterator, increment) {
const coords7 max_coords(99, 999, 2, 2, 2, 2, 2);
tileit7 it(&data, pixconv, max_coords);
for (std::size_t z = 0; z < data.size(); ++z) {
EXPECT_EQ(data[z], *it);
for (std::size_t z = 0; z < data.size() - 1; ++z) {
EXPECT_EQ(data[z], it->data());
++it;
}
EXPECT_EQ(coords7(0, 0, 1, 0, 0, 0, 0), it.position());
EXPECT_EQ(data[data.size() - 1], it->data());
EXPECT_EQ(coords7(99, 999, 0, 0, 0, 0, 0), it->block_position());
}
}