Allow block_offset to be passed down.

This commit is contained in:
King_DuckZ 2015-08-25 00:30:33 +02:00
parent f073bb5793
commit 490009df47
12 changed files with 64 additions and 55 deletions

View file

@ -31,8 +31,8 @@ namespace dk {
PixelConv ( MapTypes parType, const coords& parTileSize );
virtual coords to_pixel ( const coords& parFrom ) const = 0;
virtual coords pick_tile ( const coords& parPixelPoint ) const = 0;
virtual coords to_pixel ( const coords& parFrom, const coords& parOffset ) const = 0;
virtual coords pick_tile ( const coords& parPixelPoint, const coords& parOffset ) const = 0;
MapTypes map_type ( void ) const;
const coords& tile_size ( void ) const;
@ -51,8 +51,8 @@ namespace dk {
explicit PixelConvSquare ( const coords& parTileSize );
virtual ~PixelConvSquare ( void ) noexcept = default;
virtual coords to_pixel ( const coords& parFrom ) const;
virtual coords pick_tile ( const coords& parPixelPoint ) const;
virtual coords to_pixel ( const coords& parFrom, const coords& parOffset ) const;
virtual coords pick_tile ( const coords& parPixelPoint, const coords& parOffset ) const;
};
class PixelConvDiamond : public PixelConv<2> {
@ -64,8 +64,8 @@ namespace dk {
PixelConvDiamond ( const coords& parTileSize, bool parStaggered, bool parFirstReentrant, const coords& parRatio );
virtual ~PixelConvDiamond ( void ) noexcept = default;
virtual coords to_pixel ( const coords& parFrom ) const;
virtual coords pick_tile ( const coords& parPixelPoint ) const;
virtual coords to_pixel ( const coords& parFrom, const coords& parOffset ) const;
virtual coords pick_tile ( const coords& parPixelPoint, const coords& parOffset ) const;
protected:
const coords m_ratio;
@ -81,8 +81,8 @@ namespace dk {
PixelConvHex ( const coords& parTileSize, bool parFirstReentrant );
virtual ~PixelConvHex ( void ) noexcept = default;
virtual coords to_pixel ( const coords& parFrom ) const;
virtual coords pick_tile ( const coords& parPixelPoint ) const;
virtual coords to_pixel ( const coords& parFrom, const coords& parOffset ) const;
virtual coords pick_tile ( const coords& parPixelPoint, const coords& parOffset ) const;
private:
const CoordinateScalarType m_first_reentr;

View file

@ -51,6 +51,7 @@ namespace dk {
const CoordinateScalarType& operator[] ( int parIndex ) const;
CoordinateScalarType& operator[] ( int parIndex );
void set_position ( const coords& parPos );
const coords& position ( void ) const;
const coords& upper ( void ) const;

View file

@ -29,6 +29,7 @@
#include <type_traits>
#include <cstdint>
#include <utility>
#include <limits>
namespace dk {
template <typename T, uint32_t D, typename T1>
@ -50,6 +51,12 @@ namespace dk {
};
} //namespace implem
//TODO: replace with a custom iterator rewrite, not using iterator_facade.
//The idea is to have operator* return a Tile<>, which is heavy to
//construct but it's unlikely to be called very often, and then have
//operator-> return a TileHandle<> object that only wraps a pointer to
//the iterator's current state, so it's much more lightweight and it's not
//expected to be kept around by client code.
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::random_access_traversal_tag, Tile<T, D>, CoordinateDistType> {
friend class boost::iterator_core_access;

View file

@ -21,6 +21,7 @@
#include "doorkeeper/primitivetypes.hpp"
#include "doorkeeper/components/tileiterator.hpp"
#include "doorkeeper/components/layer.hpp"
#include "doorkeeper/components/tilecoords.hpp"
#include <cstdint>
namespace dk {
@ -42,10 +43,8 @@ namespace dk {
Viewport& operator= ( const Viewport& ) = default;
void setSize ( const coords& parSize );
void setFrom ( const coords& parFrom );
void setFromSize ( const coords& parFrom, const coords& parSize );
const coords& count ( void ) const { return m_size; }
coords size ( void ) const;
template <typename T>
typename Layer<T, D>::iterator begin ( Layer<T, D>& parLayer ) const;
@ -61,8 +60,7 @@ namespace dk {
typename Layer<T, D>::const_iterator cend ( const Layer<T, D>& parLayer ) const;
private:
coords m_size;
coords m_position;
TileCoords<D> m_view;
Tyler<D>& m_tyler;
};
} //namespace dk

View file

@ -40,12 +40,12 @@ namespace dk {
}
template <uint32_t D>
auto PixelConvSquare<D>::to_pixel (const coords& parFrom) const -> coords {
return parFrom * this->tile_size();
auto PixelConvSquare<D>::to_pixel (const coords& parFrom, const coords& parOffset) const -> coords {
return (parFrom - parOffset) * this->tile_size();
}
template <uint32_t D>
auto PixelConvSquare<D>::pick_tile (const coords& parPixelPoint) const -> coords {
return parPixelPoint / this->tile_size();
auto PixelConvSquare<D>::pick_tile (const coords& parPixelPoint, const coords& parOffset) const -> coords {
return parPixelPoint / this->tile_size() + parOffset;
}
} //namespace dk

View file

@ -49,6 +49,6 @@ namespace dk {
template <typename T, uint32_t D>
auto Tile<T, D>::screen_position() const -> coords {
return m_pixconv->to_pixel(m_position.position());
return m_pixconv->to_pixel(m_position.position(), m_block_offs);
}
} //namespace dk

View file

@ -242,4 +242,10 @@ namespace dk {
auto TileCoords<D>::upper() const -> const coords& {
return m_size;
}
template <uint32_t D>
void TileCoords<D>::set_position (const coords& parPos) {
DK_ASSERT(parPos <= m_size);
m_pos = parPos;
}
} //namespace dk

View file

@ -47,8 +47,19 @@ namespace dk {
}
template <typename T, uint32_t D, typename T1>
void TileIterator<T, D, T1>::advance (size_t /*parAdvance*/) {
//TODO: implement
void TileIterator<T, D, T1>::advance (size_t parAdvance) {
constexpr auto M = std::numeric_limits<CoordinateScalarType>::max();
typedef typename std::conditional<
(static_cast<std::make_unsigned<size_t>::type>(std::numeric_limits<size_t>::max()) > static_cast<std::make_unsigned<CoordinateScalarType>::type>(M)),
size_t,
CoordinateScalarType
>::type LargestType;
while (parAdvance) {
const auto inc = std::min(static_cast<LargestType>(parAdvance), static_cast<LargestType>(M));
m_tile_range += inc;
parAdvance -= static_cast<size_t>(inc);
}
}
template <typename T, uint32_t D, typename T1>

View file

@ -24,20 +24,19 @@ namespace dk {
template <uint32_t D>
Viewport<D>::Viewport (Tyler<D>& parTyler, const coords& parSize, const coords& parPos) :
m_size(parSize),
m_position(parPos),
m_view(parPos, parSize - 1),
m_tyler(parTyler)
{
DK_ASSERT(parPos <= parPos + parSize);
DK_ASSERT(parPos < parPos + parSize); //parSize > 0
DK_ASSERT(parSize + parPos <= parTyler.map_size());
m_tyler.preload(m_position, m_position + m_size);
m_tyler.preload(m_view.position(), m_view.position() + this->size());
}
template <uint32_t D>
template <typename T>
typename Layer<T, D>::iterator Viewport<D>::begin (Layer<T, D>& parLayer) const {
typedef typename Layer<T, D>::iterator IterType;
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), m_position, m_tyler.map_size() - 1);
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), m_view.position(), m_tyler.map_size() - 1);
}
template <uint32_t D>
@ -63,7 +62,7 @@ namespace dk {
template <typename T>
typename Layer<T, D>::const_iterator Viewport<D>::cbegin (const Layer<T, D>& parLayer) const {
typedef typename Layer<T, D>::const_iterator IterType;
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), m_position, m_tyler.map_size() - 1);
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), m_view.position(), m_tyler.map_size() - 1);
}
template <uint32_t D>
@ -73,28 +72,15 @@ namespace dk {
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), make_past_end_coords<D>(m_tyler.map_size() - 1), make_past_end_coords<D>(m_tyler.map_size() - 1));
}
template <uint32_t D>
void Viewport<D>::setSize (const coords& parSize) {
DK_ASSERT(m_position <= m_position + parSize);
DK_ASSERT(parSize + m_position <= m_tyler.map_size());
m_size = parSize;
m_tyler.preload(m_position, m_position + m_size);
}
template <uint32_t D>
void Viewport<D>::setFrom (const coords& parFrom) {
DK_ASSERT(parFrom <= parFrom + m_size);
DK_ASSERT(m_size + parFrom <= m_tyler.map_size());
m_position = parFrom;
m_tyler.preload(m_position, m_position + m_size);
DK_ASSERT(this->size() + parFrom < m_tyler.map_size());
m_view.set_position(parFrom);
m_tyler.preload(m_view.position(), m_view.position() + this->size());
}
template <uint32_t D>
void Viewport<D>::setFromSize (const coords& parFrom, const coords& parSize) {
DK_ASSERT(parFrom <= parFrom + parSize);
DK_ASSERT(parSize + parFrom <= m_tyler.map_size());
m_position = parFrom;
m_size = parSize;
m_tyler.preload(m_position, m_position + m_size);
typename Viewport<D>::coords Viewport<D>::size() const {
return (m_view.upper() + 1);
}
} //namespace dk

View file

@ -35,7 +35,7 @@ namespace dk {
DK_ASSERT(parRatio > 0);
}
auto PixelConvDiamond::to_pixel (const coords& parFrom) const -> coords {
auto PixelConvDiamond::to_pixel (const coords& parFrom, const coords& parOffset) const -> coords {
if (m_staggered) {
const auto from(parFrom);
const CoordinateScalarType xoffs = m_first_reentr xor (from.y() bitand 1);
@ -49,7 +49,7 @@ namespace dk {
}
}
auto PixelConvDiamond::pick_tile (const coords& parPixelPoint) const -> coords {
auto PixelConvDiamond::pick_tile (const coords& parPixelPoint, const coords& parOffset) const -> coords {
DK_ASSERT(false); //not implemented
return coords(0);
}
@ -60,12 +60,12 @@ namespace dk {
{
}
auto PixelConvHex::to_pixel (const coords& parFrom) const -> coords {
auto PixelConvHex::to_pixel (const coords& parFrom, const coords& parOffset) const -> coords {
DK_ASSERT(false); //not implemented
return coords(0);
}
auto PixelConvHex::pick_tile (const coords& parPixelPoint) const -> coords {
auto PixelConvHex::pick_tile (const coords& parPixelPoint, const coords& parOffset) const -> coords {
DK_ASSERT(false); //not implemented
return coords(0);
}

View file

@ -38,7 +38,7 @@ namespace {
inline
dk::Vector<2> get_diamond_coordinates (const dk::TileIterator<T, 2, T1>& parIterator, const dk::Vector<2>& parSize) {
dk::PixelConvDiamond pconv(parSize, true, false);
return pconv.to_pixel(parIterator->block_position());
return pconv.to_pixel(parIterator->block_position(), dk::Vector<2>(0));
}
} //unnamed namespace
@ -113,7 +113,7 @@ int main() {
//Main loop
bool running = true;
int y = 0;
dk::Viewport<2> viewport(tiler, coords2(10, 8), coords2(0));
dk::Viewport<2> viewport(tiler, coords2(10, 6), coords2(0));
coords2 tile_size;
SDL_QueryTexture(tile_0.get(), nullptr, nullptr, &tile_size.x(), &tile_size.y());
do {
@ -168,13 +168,13 @@ namespace {
typedef dkh::AsciiMapSource::coords coords;
parLayerInfo.path = parPath;
parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, coords(10, 8), dk::MapType_IsometricStaggered, coords(64, 64)));
parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, coords(10, 6), dk::MapType_IsometricStaggered, coords(64, 64)));
parLayerInfo.layer = &parTiler.push_layer<int>(parLayerInfo.device.get(), 0);
}
void printViewport (const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer) {
int col = 0;
const auto tilecount = parView.count();
const auto tilecount = parView.size();
for (auto itTile = parView.begin(parLayer), itTileEND = parView.end(parLayer); itTile != itTileEND; ++itTile) {
std::cout << itTile->data();
++col;

View file

@ -78,7 +78,7 @@ TEST_F(asciimapsource, coordinates) {
//Check isometric coordinates
{
const coords2 expected_coords(tsize * itTile->block_position());
const auto returned_coords(iso_conv.to_pixel(itTile->block_position()));
const auto returned_coords(iso_conv.to_pixel(itTile->block_position(), coords2(0)));
EXPECT_EQ(expected_coords, returned_coords);
EXPECT_EQ(expected_coords, itTile->screen_position());
}
@ -90,7 +90,7 @@ TEST_F(asciimapsource, coordinates) {
xoffs + itTile->block_position().x() * tsize.x(),
itTile->block_position().y() * tsize.y()
);
const auto returned_coords(diamond_conv.to_pixel(itTile->block_position()));
const auto returned_coords(diamond_conv.to_pixel(itTile->block_position(), coords2(0)));
EXPECT_EQ(expected_coords, returned_coords);
}
@ -101,7 +101,7 @@ TEST_F(asciimapsource, coordinates) {
xoffs + itTile->block_position().x() * tsize.x(),
itTile->block_position().y() * tsize.y()
);
const auto returned_coords(diamond_conv2.to_pixel(itTile->block_position()));
const auto returned_coords(diamond_conv2.to_pixel(itTile->block_position(), coords2(0)));
EXPECT_EQ(expected_coords, returned_coords);
}
}