DELEME2
This commit is contained in:
parent
9ccbe880a4
commit
a7dd322da6
10 changed files with 158 additions and 15 deletions
|
@ -22,12 +22,22 @@
|
|||
#include "doorkeeper/components/viewport.hpp"
|
||||
#include "doorkeeper/components/tyler.hpp"
|
||||
#include "doorkeeper/components/layer.hpp"
|
||||
#include <map>
|
||||
#include <boost/iterator/transform_iterator.hpp>
|
||||
#include <utility>
|
||||
#include <functional>
|
||||
|
||||
namespace dk {
|
||||
template <uint32_t D>
|
||||
class Tyler;
|
||||
|
||||
template <uint32_t D>
|
||||
class LayeredViewport {
|
||||
typedef std::map<TileInfo<D>, Viewport<D>> ViewportList;
|
||||
public:
|
||||
typedef VectorT<CoordinateAccumType, D> coords_f;
|
||||
typedef boost::transform_iterator<std::function<Viewport<D>&(const LayerBase<D>&)>, typename Tyler<D>::iterator> iterator;
|
||||
typedef boost::transform_iterator<std::function<const Viewport<D>&(const LayerBase<D>&)>, typename Tyler<D>::const_iterator> const_iterator;
|
||||
|
||||
explicit LayeredViewport ( Tyler<D>& parTyler );
|
||||
~LayeredViewport ( void ) noexcept;
|
||||
|
@ -37,9 +47,15 @@ namespace dk {
|
|||
|
||||
void on_layer_added ( const LayerBase<D>* parLayer );
|
||||
void on_layer_removed ( const LayerBase<D>* parLayer );
|
||||
iterator begin ( void );
|
||||
const_iterator begin ( void ) const;
|
||||
iterator end ( void );
|
||||
const_iterator end ( void ) const;
|
||||
Viewport<D>& viewport ( const LayerBase<D>& parLayer );
|
||||
const Viewport<D>& viewport ( const LayerBase<D>& parLayer ) const;
|
||||
|
||||
private:
|
||||
typedef std::vector<Viewport<D>> ViewportList;
|
||||
ViewportList m_views;
|
||||
Tyler<D>& m_tyler;
|
||||
};
|
||||
} //namespace dk
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include <ciso646>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <boost/iterator/indirect_iterator.hpp>
|
||||
|
||||
namespace dk {
|
||||
template <uint32_t D>
|
||||
|
@ -35,6 +36,8 @@ namespace dk {
|
|||
typedef std::vector<LayerPtr> LayerList;
|
||||
public:
|
||||
typedef typename LayerBase<D>::coords coords;
|
||||
typedef boost::indirect_iterator<typename LayerList::iterator> iterator;
|
||||
typedef boost::indirect_iterator<typename LayerList::const_iterator> const_iterator;
|
||||
|
||||
Tyler ( void );
|
||||
Tyler ( Tyler&& ) = default;
|
||||
|
@ -45,6 +48,11 @@ namespace dk {
|
|||
template <typename T>
|
||||
void push_layer_void ( BaseMapSource<D>* parTilemap, int parIndex );
|
||||
|
||||
iterator begin ( void );
|
||||
const_iterator begin ( void ) const;
|
||||
iterator end ( void );
|
||||
const_iterator end ( void ) const;
|
||||
|
||||
void preload ( const coords& parFrom, const coords& parTo );
|
||||
|
||||
private:
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
#include "doorkeeper/components/tilecoords.hpp"
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <boost/functional/hash_fwd.hpp>
|
||||
|
||||
namespace dk {
|
||||
template <uint32_t D>
|
||||
|
@ -32,6 +33,17 @@ namespace dk {
|
|||
template <typename T, uint32_t D>
|
||||
class Layer;
|
||||
|
||||
template <uint32_t D>
|
||||
struct TileInfo {
|
||||
typedef Vector<D> coords;
|
||||
|
||||
coords tile_size;
|
||||
coords map_size;
|
||||
};
|
||||
|
||||
template <uint32_t D>
|
||||
std::size_t hash_value ( const TileInfo<D>& parTProps ) noexcept a_pure;
|
||||
|
||||
template <uint32_t D>
|
||||
class Viewport {
|
||||
public:
|
||||
|
@ -39,8 +51,8 @@ namespace dk {
|
|||
typedef VectorT<CoordinateAccumType, D> coords_f;
|
||||
|
||||
Viewport ( const Viewport& parOther ) = default;
|
||||
explicit Viewport ( Tyler<D>& parTyler );
|
||||
Viewport ( Tyler<D>& parTyler, const coords& parSize, const coords& parPos );
|
||||
Viewport ( Tyler<D>& parTyler, const TileInfo<D>* parTileInfo );
|
||||
Viewport ( Tyler<D>& parTyler, const TileInfo<D>* parTileInfo, const coords& parPos );
|
||||
~Viewport ( void ) noexcept = default;
|
||||
|
||||
Viewport& operator= ( const Viewport& ) = default;
|
||||
|
@ -68,8 +80,9 @@ namespace dk {
|
|||
void clip_pixel_offset ( void );
|
||||
|
||||
TileCoords<D> m_view;
|
||||
Tyler<D>& m_tyler;
|
||||
coords_f m_pixel_offset;
|
||||
Tyler<D>& m_tyler;
|
||||
const TileInfo<D>* const m_tile_info;
|
||||
};
|
||||
} //namespace dk
|
||||
|
||||
|
|
|
@ -22,5 +22,6 @@
|
|||
#include "components/tyler.hpp"
|
||||
#include "components/viewport.hpp"
|
||||
#include "components/layer.hpp"
|
||||
#include "components/layeredviewport.hpp"
|
||||
|
||||
#endif
|
||||
|
|
|
@ -28,11 +28,17 @@ namespace dk {
|
|||
|
||||
template <uint32_t D>
|
||||
LayeredViewport<D>& LayeredViewport<D>::operator+= (const coords_f& parValue) {
|
||||
for (auto& view : m_views) {
|
||||
view.second += parValue;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
LayeredViewport<D>& LayeredViewport<D>::operator-= (const coords_f& parValue) {
|
||||
for (auto& view : m_views) {
|
||||
view.second -= parValue;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -43,4 +49,32 @@ namespace dk {
|
|||
template <uint32_t D>
|
||||
void LayeredViewport<D>::on_layer_removed (const LayerBase<D>* parLayer) {
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
auto LayeredViewport<D>::begin() -> iterator {
|
||||
return iterator(m_view.begin(), std::bind(&LayeredViewport<D>::viewport, this, std::placeholders::_1));
|
||||
|
||||
template <uint32_t D>
|
||||
auto LayeredViewport<D>::begin() const -> const_iterator {
|
||||
return const_iterator(m_view.begin(), std::bind(&LayeredViewport<D>::viewport, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
auto LayeredViewport<D>::end() -> iterator {
|
||||
return iterator(m_view.end(), std::bind(&LayeredViewport<D>::viewport, this, std::placeholders::_1));
|
||||
|
||||
template <uint32_t D>
|
||||
auto LayeredViewport<D>::end() const -> const_iterator {
|
||||
return const_iterator(m_view.end(), std::bind(&LayeredViewport<D>::viewport, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
Viewport<D>& LayeredViewport<D>::viewport (const LayerBase<D>& parLayer) {
|
||||
return const_cast<Viewport<D>&>(const_cast<const LayeredViewport<D>*>(this)->viewport(parLayer));
|
||||
|
||||
template <uint32_t D>
|
||||
const Viewport<D>& LayeredViewport<D>::viewport (const LayerBase<D>& parLayer) const {
|
||||
const TileInfo tinfo { parLayer.tile_size(), parLayer.map_size() };
|
||||
return m_views[tinfo];
|
||||
}
|
||||
} //namespace dk
|
||||
|
|
|
@ -51,4 +51,32 @@ namespace dk {
|
|||
layer->preload(parFrom, parTo);
|
||||
}
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <uint32_t D>
|
||||
auto Tyler<D>::begin() -> iterator {
|
||||
return iterator(m_layers.begin());
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <uint32_t D>
|
||||
auto Tyler<D>::begin() const -> const_iterator {
|
||||
return const_iterator(m_layers.begin());
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <uint32_t D>
|
||||
auto Tyler<D>::end() -> iterator {
|
||||
return iterator(m_layers.end());
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <uint32_t D>
|
||||
auto Tyler<D>::end() const -> const_iterator {
|
||||
return const_iterator(m_layers.end());
|
||||
}
|
||||
} //namespace dk
|
||||
|
|
|
@ -17,20 +17,35 @@
|
|||
|
||||
namespace dk {
|
||||
template <uint32_t D>
|
||||
Viewport<D>::Viewport (Tyler<D>& parTyler) :
|
||||
m_tyler(parTyler),
|
||||
m_pixel_offset(0)
|
||||
{
|
||||
inline std::size_t hash_value (const TileInfo<D>& parTProps) noexcept {
|
||||
std::size_t seed = 0;
|
||||
for (uint32_t z = 0; z < D; ++z) {
|
||||
boost::hash_combine(seed, parTProps.map_size[z]);
|
||||
boost::hash_combine(seed, parTProps.tile_size[z]);
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
Viewport<D>::Viewport (Tyler<D>& parTyler, const coords& parSize, const coords& parPos) :
|
||||
m_view(parPos, parSize - 1),
|
||||
Viewport<D>::Viewport (Tyler<D>& parTyler, const TileInfo<D>* parTileInfo) :
|
||||
m_view(parTileInfo->map_size - 1),
|
||||
m_pixel_offset(0),
|
||||
m_tyler(parTyler),
|
||||
m_pixel_offset(0)
|
||||
m_tile_info(parTileInfo)
|
||||
{
|
||||
DK_ASSERT(parPos < parPos + parSize); //parSize > 0
|
||||
DK_ASSERT(parSize + parPos <= parTyler.map_size());
|
||||
DK_ASSERT(m_tile_info);
|
||||
}
|
||||
|
||||
template <uint32_t D>
|
||||
Viewport<D>::Viewport (Tyler<D>& parTyler, const TileInfo<D>* parTileInfo, const coords& parPos) :
|
||||
m_view(parPos, parTileInfo->map_size - 1),
|
||||
m_pixel_offset(0),
|
||||
m_tyler(parTyler),
|
||||
m_tile_info(parTileInfo)
|
||||
{
|
||||
DK_ASSERT(m_tile_info);
|
||||
DK_ASSERT(parPos < parPos + m_tile_info->map_size); //parSize > 0
|
||||
DK_ASSERT(m_tile_info->map_size + parPos <= m_tile_info->map_size);
|
||||
m_tyler.preload(m_view.position(), m_view.position() + this->size());
|
||||
}
|
||||
|
||||
|
@ -76,7 +91,7 @@ namespace dk {
|
|||
|
||||
template <uint32_t D>
|
||||
void Viewport<D>::setFrom (const coords& parFrom) {
|
||||
DK_ASSERT(this->size() + parFrom <= m_tyler.map_size());
|
||||
DK_ASSERT(this->size() + parFrom <= m_tile_info->map_size);
|
||||
m_view.set_position(parFrom);
|
||||
m_tyler.preload(m_view.position(), m_view.position() + this->size());
|
||||
}
|
||||
|
@ -102,7 +117,7 @@ namespace dk {
|
|||
|
||||
template <uint32_t D>
|
||||
void Viewport<D>::clip_pixel_offset() {
|
||||
const auto& tile_size = m_tyler.tile_size();
|
||||
const auto& tile_size = m_tile_info->tile_size;
|
||||
for (uint32_t z = 0; z < D; ++z) {
|
||||
auto offs = static_cast<CoordinateScalarType>(m_pixel_offset[z]);
|
||||
if (std::abs(offs) >= tile_size[z]) {
|
||||
|
|
|
@ -95,6 +95,8 @@ int main() {
|
|||
LayerWithData<AsciiMapSource, int> topLayer;
|
||||
addLayer(tiler, topLayer, DATA_PATH"/test_2.map");
|
||||
|
||||
dk::TileInfo<2> tinfo_10_6 { coords2(64), coords2(10, 6) };
|
||||
dk::TileInfo<2> tinfo_4_4 { coords2(64), coords2(4, 4) };
|
||||
printViewport(dk::Viewport<2>(tiler, coords2(10, 6), coords2(0)), *bottomLayer.layer);
|
||||
printViewport(dk::Viewport<2>(tiler, coords2(4, 4), coords2(0)), *bottomLayer.layer);
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ add_executable(${PROJECT_NAME}
|
|||
vector.cpp
|
||||
tileiterator.cpp
|
||||
asciimapsource.cpp
|
||||
layeredviewport.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
|
25
test/unit/layeredviewport.cpp
Normal file
25
test/unit/layeredviewport.cpp
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* Copyright 2015, Michele Santullo
|
||||
* This file is part of DoorKeeper.
|
||||
*
|
||||
* DoorKeeper is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* DoorKeeper is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with DoorKeeper. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "doorkeeper/components/layeredviewport.hpp"
|
||||
#include "doorkeeper/components/tyler.hpp"
|
||||
|
||||
TEST(components, layeredviewport) {
|
||||
dk::Tyler<3> tiler;
|
||||
dk::layeredviewport<3> lview(tiler);
|
||||
}
|
Loading…
Add table
Reference in a new issue