148 lines
4.8 KiB
C++
148 lines
4.8 KiB
C++
/* 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/>.
|
|
*/
|
|
|
|
namespace dk {
|
|
template <uint32_t D>
|
|
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 TileInfo<D>* parTileInfo) :
|
|
m_view(parTileInfo->map_size - 1),
|
|
m_pixel_offset(0),
|
|
m_tyler(parTyler),
|
|
m_tile_info(parTileInfo)
|
|
{
|
|
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());
|
|
}
|
|
|
|
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_view.position(), m_view.upper());
|
|
}
|
|
|
|
template <uint32_t D>
|
|
template <typename T>
|
|
typename Layer<T, D>::iterator Viewport<D>::end (Layer<T, D>& parLayer) const {
|
|
typedef typename Layer<T, D>::iterator IterType;
|
|
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), make_past_end_coords<D>(m_view.upper()), make_past_end_coords<D>(m_view.upper()));
|
|
}
|
|
|
|
template <uint32_t D>
|
|
template <typename T>
|
|
typename Layer<T, D>::const_iterator Viewport<D>::begin (const Layer<T, D>& parLayer) const {
|
|
return this->cbegin(parLayer);
|
|
}
|
|
|
|
template <uint32_t D>
|
|
template <typename T>
|
|
typename Layer<T, D>::const_iterator Viewport<D>::end (const Layer<T, D>& parLayer) const {
|
|
return this->cend(parLayer);
|
|
}
|
|
|
|
template <uint32_t D>
|
|
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_view.position(), m_view.upper());
|
|
}
|
|
|
|
template <uint32_t D>
|
|
template <typename T>
|
|
typename Layer<T, D>::const_iterator Viewport<D>::cend (const Layer<T, D>& parLayer) const {
|
|
typedef typename Layer<T, D>::const_iterator IterType;
|
|
return IterType(&parLayer.m_tiles, parLayer.m_tilemap->pixel_conv(), make_past_end_coords<D>(m_view.upper()), make_past_end_coords<D>(m_view.upper()));
|
|
}
|
|
|
|
template <uint32_t D>
|
|
void Viewport<D>::setFrom (const coords& parFrom) {
|
|
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());
|
|
}
|
|
|
|
template <uint32_t D>
|
|
typename Viewport<D>::coords Viewport<D>::size() const {
|
|
return (m_view.upper() + 1);
|
|
}
|
|
|
|
template <uint32_t D>
|
|
Viewport<D>& Viewport<D>::operator+= (const coords_f& parValue) {
|
|
m_pixel_offset += parValue;
|
|
clip_pixel_offset();
|
|
return *this;
|
|
}
|
|
|
|
template <uint32_t D>
|
|
Viewport<D>& Viewport<D>::operator-= (const coords_f& parValue) {
|
|
m_pixel_offset -= parValue;
|
|
clip_pixel_offset();
|
|
return *this;
|
|
}
|
|
|
|
template <uint32_t D>
|
|
void Viewport<D>::clip_pixel_offset() {
|
|
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]) {
|
|
auto curr_tile_size_f = static_cast<CoordinateAccumType>(tile_size[z]);
|
|
if (offs < 0) {
|
|
do {
|
|
offs += tile_size[z];
|
|
m_pixel_offset[z] += curr_tile_size_f;
|
|
--m_view;
|
|
} while(offs >= tile_size[z]);
|
|
}
|
|
else {
|
|
DK_ASSERT(offs > 0);
|
|
do {
|
|
offs -= tile_size[z];
|
|
m_pixel_offset[z] -= curr_tile_size_f;
|
|
++m_view;
|
|
} while(offs >= tile_size[z]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
template <uint32_t D>
|
|
auto Viewport<D>::pixel_offset() const -> coords {
|
|
return static_cast<coords>(m_pixel_offset);
|
|
}
|
|
} //namespace dk
|