2015-08-19 19:06:58 +00:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-12-09 22:53:09 +00:00
|
|
|
namespace dk {
|
2015-08-25 18:53:35 +00:00
|
|
|
namespace implem {
|
|
|
|
///----------------------------------------------------------------------
|
|
|
|
///----------------------------------------------------------------------
|
|
|
|
template <uint32_t D, typename O>
|
|
|
|
inline bool at_least_one (const Vector<D>& parLeft, const Vector<D>& parRight, O parOp) {
|
|
|
|
for (uint32_t z = 0; z < D; ++z) {
|
|
|
|
if (parOp(parLeft[z], parRight[z])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} //namespace implem
|
|
|
|
|
2014-12-09 22:53:09 +00:00
|
|
|
///--------------------------------------------------------------------------
|
|
|
|
///--------------------------------------------------------------------------
|
2015-01-08 11:20:17 +00:00
|
|
|
template <uint32_t D>
|
2015-08-25 18:53:35 +00:00
|
|
|
LayerBase<D>::LayerBase (const coords& parCount, const coords& parTileSize) :
|
|
|
|
m_count(parCount),
|
2014-12-11 22:58:52 +00:00
|
|
|
m_tilesize(parTileSize),
|
2015-01-05 17:01:28 +00:00
|
|
|
m_haveFrom(0),
|
|
|
|
m_haveTo(0)
|
2014-12-09 22:53:09 +00:00
|
|
|
{
|
2015-08-25 18:53:35 +00:00
|
|
|
DK_ASSERT(parCount > 0);
|
2014-12-09 22:53:09 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 17:01:28 +00:00
|
|
|
///--------------------------------------------------------------------------
|
|
|
|
///--------------------------------------------------------------------------
|
2015-01-08 11:20:17 +00:00
|
|
|
template <uint32_t D>
|
2015-01-05 17:01:28 +00:00
|
|
|
void LayerBase<D>::preload (const coords& parFrom, const coords& parTo) {
|
2015-08-25 18:53:35 +00:00
|
|
|
using implem::at_least_one;
|
|
|
|
using less = std::less<typename coords::scalar_type>;
|
|
|
|
using greater = std::greater<typename coords::scalar_type>;
|
|
|
|
|
|
|
|
if (at_least_one<D>(parFrom, m_haveFrom, less()) or at_least_one<D>(parTo, m_haveTo, greater())) {
|
2015-01-05 17:01:28 +00:00
|
|
|
this->onPreload(parFrom, parTo);
|
|
|
|
m_haveFrom = parFrom;
|
|
|
|
m_haveTo = parTo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-09 22:53:09 +00:00
|
|
|
///--------------------------------------------------------------------------
|
|
|
|
///--------------------------------------------------------------------------
|
2015-01-08 11:20:17 +00:00
|
|
|
template <typename T, uint32_t D>
|
2015-08-26 16:08:05 +00:00
|
|
|
Layer<T, D>::Layer (BaseMapSource<D>* parTilemap) :
|
|
|
|
LayerBase<D>(parTilemap->mapSize(), parTilemap->tileSize()),
|
2014-12-29 15:17:59 +00:00
|
|
|
m_tilemap(parTilemap)
|
2014-12-09 22:53:09 +00:00
|
|
|
{
|
2015-05-24 23:37:20 +00:00
|
|
|
DK_ASSERT(m_tilemap);
|
2014-12-09 22:53:09 +00:00
|
|
|
}
|
2014-12-12 19:04:48 +00:00
|
|
|
|
|
|
|
///--------------------------------------------------------------------------
|
|
|
|
///--------------------------------------------------------------------------
|
2015-01-08 11:20:17 +00:00
|
|
|
template <typename T, uint32_t D>
|
2014-12-12 19:04:48 +00:00
|
|
|
typename Layer<T, D>::iterator Layer<T, D>::begin() {
|
2015-08-17 22:03:27 +00:00
|
|
|
return iterator(&m_tiles, m_tilemap->pixel_conv(), coords(0), this->m_count);
|
2014-12-12 19:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///--------------------------------------------------------------------------
|
|
|
|
///--------------------------------------------------------------------------
|
2015-01-08 11:20:17 +00:00
|
|
|
template <typename T, uint32_t D>
|
2014-12-14 12:13:10 +00:00
|
|
|
typename Layer<T, D>::iterator Layer<T, D>::end() {
|
2015-08-17 22:03:27 +00:00
|
|
|
return iterator(&m_tiles, m_tilemap->pixel_conv(), make_past_end_coords<D>(this->m_count - 1), make_past_end_coords<D>(this->m_count - 1));
|
2014-12-14 12:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///--------------------------------------------------------------------------
|
|
|
|
///--------------------------------------------------------------------------
|
2015-01-08 11:20:17 +00:00
|
|
|
template <typename T, uint32_t D>
|
2015-01-05 17:01:28 +00:00
|
|
|
void Layer<T, D>::onPreload (const coords& parFrom, const coords& parTo) {
|
2014-12-12 19:04:48 +00:00
|
|
|
m_tiles.clear();
|
2015-05-24 23:37:20 +00:00
|
|
|
m_tilemap->fetch(m_tiles, parFrom, parTo);
|
2015-05-24 15:19:10 +00:00
|
|
|
#if !defined(NDEBUG)
|
|
|
|
std::cout << "Preloading layer from " << parFrom << " to " << parTo << '\n';
|
|
|
|
#endif
|
2014-12-12 19:04:48 +00:00
|
|
|
}
|
2014-12-09 22:53:09 +00:00
|
|
|
}
|