This commit is contained in:
King_DuckZ 2015-08-31 17:11:11 +02:00
parent 4df71e46a1
commit 5637bf4e5c
11 changed files with 183 additions and 69 deletions

View file

@ -30,7 +30,7 @@ set(gdb_scripts_path "${CMAKE_CURRENT_SOURCE_DIR}/tools/gdb")
#add_subdirectory(lib/sprout)
add_subdirectory(src)
add_subdirectory(test)
#add_subdirectory(test)
add_subdirectory(game)
#add_subdirectory(tools/mapconv)
add_subdirectory(test/gtest-1.7.0)

View file

@ -22,7 +22,8 @@
#include "doorkeeper/components/viewport.hpp"
#include "doorkeeper/components/tyler.hpp"
#include "doorkeeper/components/layer.hpp"
#include <map>
#include "doorkeeper/implem/iterableviewport.hpp"
#include <vector>
#include <boost/iterator/transform_iterator.hpp>
#include <utility>
#include <functional>
@ -33,11 +34,12 @@ namespace dk {
template <uint32_t D>
class LayeredViewport {
typedef std::map<TileInfo<D>, Viewport<D>> ViewportList;
struct ViewportItem;
typedef std::vector<ViewportItem> 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;
typedef boost::transform_iterator<std::function<IterableViewport<D>&(const LayerBase<D>&)>, typename Tyler<D>::iterator> iterator;
typedef boost::transform_iterator<std::function<const IterableViewport<D>&(const LayerBase<D>&)>, typename Tyler<D>::const_iterator> const_iterator;
explicit LayeredViewport ( Tyler<D>& parTyler );
~LayeredViewport ( void ) noexcept;
@ -45,18 +47,18 @@ namespace dk {
LayeredViewport<D>& operator+= ( const coords_f& parValue );
LayeredViewport<D>& operator-= ( const coords_f& parValue );
void on_layer_added ( const LayerBase<D>* parLayer );
void on_layer_removed ( const LayerBase<D>* parLayer );
void on_layer ( const LayerBase<D>& parLayer, bool parAdded );
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;
IterableViewport<D> viewport ( const LayerBase<D>& parLayer );
const IterableViewport<D> viewport ( const LayerBase<D>& parLayer ) const;
private:
ViewportList m_views;
Tyler<D>& m_tyler;
typename Tyler<D>::LayerObsID m_tyler_id;
};
} //namespace dk

View file

@ -20,6 +20,7 @@
#include "doorkeeper/components/layer.hpp"
#include "doorkeeper/components/exception.hpp"
#include "doorkeeper/implem/idmanager.hpp"
#include <vector>
#include <ciso646>
#include <memory>
@ -35,11 +36,13 @@ namespace dk {
class Tyler {
typedef std::unique_ptr<LayerBase<D>> LayerPtr;
typedef std::vector<LayerPtr> LayerList;
typedef IDManager<7, 9> LayerObsIDManager;
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;
typedef std::function<void(const LayerBase<D>&, bool) noexcept> layer_callback;
typedef LayerObsIDManager::value_type LayerObsID;
Tyler ( void );
Tyler ( Tyler&& ) = default;
@ -56,12 +59,13 @@ namespace dk {
const_iterator end ( void ) const;
void preload ( const coords& parFrom, const coords& parTo );
void register_for_layeradd ( layer_callback parCallback );
void unregister_for_layeradd ( layer_callback parCallback );
LayerObsID register_for_layeradd ( layer_callback parCallback );
void unregister_for_layeradd ( LayerObsID parID );
private:
LayerObsIDManager m_layerobs_idmanager;
LayerList m_layers;
std::vector<layer_callback> m_layer_add_callbacks;
std::vector<layer_callback> m_layerobs_callbacks;
};
} //namespace dk

View file

@ -24,7 +24,6 @@
#include "doorkeeper/components/tilecoords.hpp"
#include <cstdint>
#include <cmath>
#include <boost/functional/hash_fwd.hpp>
namespace dk {
template <uint32_t D>
@ -39,13 +38,12 @@ namespace dk {
TileInfo ( const LayerBase<D>& parLayer );
bool operator== ( const TileInfo& parOther ) const;
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:
@ -78,8 +76,6 @@ namespace dk {
template <typename T>
typename Layer<T, D>::const_iterator cend ( const Layer<T, D>& parLayer ) const;
void set_tile_info ( const TileInfo<D>* parTileInfo );
private:
void clip_pixel_offset ( void );

View file

@ -0,0 +1,50 @@
/* 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/>.
*/
#ifndef idD358B9737CC045D3B35F2B5C2FCF84FA
#define idD358B9737CC045D3B35F2B5C2FCF84FA
#include "doorkeeper/components/viewport.hpp"
#include <cstdint>
namespace dk {
template <uint32_t D>
class LayerBase;
template <uint32_t D>
class IterableViewport {
public:
typedef typename Viewport<D>::const_iterator const_iterator;
IterableViewport ( const Viewport<D>* parViewport, const LayerBase<D>* parLayerBase ) :
m_viewport(parViewport),
m_layer(parLayerBase)
{
DK_ASSERT(m_viewport);
DK_ASSERT(m_layer);
}
const_iterator begin ( void ) const { return m_viewport->begin(m_layer); }
const_iterator end ( void ) const { return m_viewport->end(m_layer); }
private:
const Viewport<D>* const m_viewport;
const LayerBase<D>* const m_layer;
};
} //namespace dk
#endif

View file

@ -16,31 +16,53 @@
*/
namespace dk {
template <uint32_t D>
struct LayeredViewport<D>::ViewportItem {
ViewportItem ( const TileInfo<D>& parTInfo, Tyler<D>& parTyler );
ViewportItem ( ViewportItem&& ) = default;
TileInfo<D> tile_info;
Viewport<D> viewport;
int ref_count;
bool operator== ( const TileInfo<D>& parOther ) const a_pure;
};
template <uint32_t D>
LayeredViewport<D>::ViewportItem::ViewportItem (const TileInfo<D>& parTInfo, Tyler<D>& parTyler) :
tile_info(parTInfo),
viewport(parTyler, &tile_info),
ref_count(1)
{
}
template <uint32_t D>
bool LayeredViewport<D>::ViewportItem::operator== (const TileInfo<D>& parOther) const {
return tile_info == parOther;
}
template <uint32_t D>
LayeredViewport<D>::LayeredViewport (Tyler<D>& parTyler) :
m_tyler(parTyler)
{
for (const auto& layer : m_tyler) {
const TileInfo<D> tinfo(layer);
auto it_found = m_views.find(tinfo);
if (m_views.end() == it_found) {
auto it_new = m_views.insert(it_found, Viewport<D>(m_tyler, nullptr));
it_new->second.set_tile_info(&it_new->first);
}
this->on_layer(layer, true);
}
m_tyler.register_for_layeradd(this);
using std::placeholders::_1;
using std::placeholders::_2;
m_tyler_id = m_tyler.register_for_layeradd(std::bind(&LayeredViewport<D>::on_layer, this, _1, _2));
}
template <uint32_t D>
LayeredViewport<D>::~LayeredViewport() noexcept {
m_tyler.unregister_for_layeradd(this);
m_tyler.unregister_for_layeradd(m_tyler_id);
}
template <uint32_t D>
LayeredViewport<D>& LayeredViewport<D>::operator+= (const coords_f& parValue) {
for (auto& view : m_views) {
view.second += parValue;
view.viewport += parValue;
}
return *this;
}
@ -48,44 +70,61 @@ namespace dk {
template <uint32_t D>
LayeredViewport<D>& LayeredViewport<D>::operator-= (const coords_f& parValue) {
for (auto& view : m_views) {
view.second -= parValue;
view.viewport -= parValue;
}
return *this;
}
template <uint32_t D>
void LayeredViewport<D>::on_layer_added (const LayerBase<D>* parLayer) {
}
void LayeredViewport<D>::on_layer (const LayerBase<D>& parLayer, bool parAdded) {
const TileInfo<D> tinfo(parLayer);
auto it_found = std::find(m_views.begin(), m_views.end(), tinfo);
template <uint32_t D>
void LayeredViewport<D>::on_layer_removed (const LayerBase<D>* parLayer) {
if (parAdded) {
if (m_views.end() == it_found) {
m_views.push_back(ViewportItem(tinfo, m_tyler));
}
else {
++it_found->ref_count;
}
}
else {
DK_ASSERT(m_views.end() != it_found);
--it_found->ref_count;
if (0 == it_found->ref_count) {
m_views.erase(it_found);
}
}
}
template <uint32_t D>
auto LayeredViewport<D>::begin() -> iterator {
return iterator(m_view.begin(), std::bind(&LayeredViewport<D>::viewport, this, std::placeholders::_1));
return iterator(m_views.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));
return const_iterator(m_views.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));
return iterator(m_views.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));
return const_iterator(m_views.end(), std::bind(&LayeredViewport<D>::viewport, this, std::placeholders::_1));
}
template <uint32_t D>
Viewport<D>& LayeredViewport<D>::viewport (const LayerBase<D>& parLayer) {
IterableViewport<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];
const IterableViewport<D> LayeredViewport<D>::viewport (const LayerBase<D>& parLayer) const {
const TileInfo<D> tinfo { parLayer.tile_size(), parLayer.map_size() };
return IterableViewport<D>(&m_views[tinfo].first, &parLayer);
}
} //namespace dk

View file

@ -32,6 +32,12 @@ namespace dk {
auto newLayer = new Layer<T, D>(parTilemap);
m_layers.push_back(LayerPtr(newLayer));
for (auto& callback : m_layerobs_callbacks) {
if (nullptr != callback) {
callback(*newLayer, true);
}
}
return *newLayer;
}
@ -79,4 +85,31 @@ namespace dk {
auto Tyler<D>::end() const -> const_iterator {
return const_iterator(m_layers.end());
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <uint32_t D>
auto Tyler<D>::register_for_layeradd (layer_callback parCallback) -> LayerObsID {
auto retval = m_layerobs_idmanager.allocate();
DK_ASSERT(retval.index() > 0);
if (retval.index() > m_layerobs_callbacks.size()) {
DK_ASSERT(retval.index() == m_layerobs_callbacks.size() + 1);
m_layerobs_callbacks.resize(retval.index());
}
else {
DK_ASSERT(retval.salt() > 0);
}
m_layerobs_callbacks[retval.index() - 1] = parCallback;
return retval;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <uint32_t D>
void Tyler<D>::unregister_for_layeradd (LayerObsID parID) {
DK_ASSERT(parID.index() <= m_layerobs_callbacks.size());
DK_ASSERT(nullptr != m_layerobs_callbacks[parID.index() - 1]);
m_layerobs_callbacks[parID.index() - 1] = nullptr;
m_layerobs_idmanager.free(parID);
}
} //namespace dk

View file

@ -24,13 +24,8 @@ 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;
bool TileInfo<D>::operator== (const TileInfo& parOther) const {
return tile_size == parOther.tile_size and map_size == parOther.map_size;
}
template <uint32_t D>
@ -40,6 +35,7 @@ namespace dk {
m_tyler(parTyler),
m_tile_info(parTileInfo)
{
DK_ASSERT(parTileInfo);
}
template <uint32_t D>
@ -49,6 +45,7 @@ namespace dk {
m_tyler(parTyler),
m_tile_info(parTileInfo)
{
DK_ASSERT(parTileInfo);
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());
@ -150,9 +147,4 @@ namespace dk {
auto Viewport<D>::pixel_offset() const -> coords {
return static_cast<coords>(m_pixel_offset);
}
template <uint32_t D>
void Viewport<D>::set_tile_info (const TileInfo<D>* parTileInfo) {
m_tile_info = parTileInfo;
}
} //namespace dk

View file

@ -96,8 +96,6 @@ int main() {
addLayer(tiler, topLayer, DATA_PATH"/test_2.map");
dk::LayeredViewport<2> view_10_6(tiler);
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);

View file

@ -31,7 +31,7 @@ const dk::CoordinateScalarType asciimapsource::map_width = 10;
const dk::CoordinateScalarType asciimapsource::map_height = 8;
asciimapsource::asciimapsource() :
tiler(coords2(map_width, map_height), coords2(tile_size)),
tiler(),
layer(nullptr)
{
std::istringstream iss((std::string(map_data)));
@ -44,23 +44,23 @@ void asciimapsource::SetUp() {
}
TEST_F(asciimapsource, load) {
dk::Viewport<2> full_view(tiler, tiler.map_size(), coords2(0));
EXPECT_EQ(coords2(map_width, map_height), tiler.map_size());
dk::LayeredViewport<2> full_view(tiler);
dk::CoordinateDistType index = 0;
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_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->data());
++index;
++data_index;
if (map_data[data_index] == '\n') {
for (const auto& viewport : full_view) {
EXPECT_EQ(coords2(map_width, map_height), viewport.size());
for (const auto& tile : viewport) {
EXPECT_EQ(index, dk::to_index(tile.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, tile.data());
++index;
++data_index;
if (map_data[data_index] == '\n') {
++data_index;
}
}
}
EXPECT_EQ(map_width * map_height, index);

View file

@ -21,5 +21,5 @@
TEST(components, layeredviewport) {
dk::Tyler<3> tiler;
dk::layeredviewport<3> lview(tiler);
dk::LayeredViewport<3> lview(tiler);
}