From 0da607ab3d9fa1f80053c465bd08af059360ace7 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Fri, 12 Dec 2014 20:04:48 +0100 Subject: [PATCH] Progressing with the implementation --- CMakeLists.txt | 6 +++ include/components/layer.hpp | 20 ++++++++-- include/components/tileiterator.hpp | 26 ++++++++---- include/components/tyler.hpp | 8 ++-- include/components/viewport.hpp | 18 +++++++-- include/implem/layer.inl | 43 +++++++++++++++++--- include/implem/tileiterator.inl | 61 +++++++++++++++++------------ include/implem/tyler.inl | 16 ++++---- include/implem/viewport.inl | 15 +++++++ include/primitivetypes.hpp | 2 + src/doorkeeperConfig.h.in | 10 +++++ src/main.cpp | 19 +++++++-- test.map | 6 +++ test_2.map | 1 + 14 files changed, 192 insertions(+), 59 deletions(-) create mode 100644 include/implem/viewport.inl create mode 100644 src/doorkeeperConfig.h.in create mode 100644 test.map create mode 120000 test_2.map diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c46b54..d477d72 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,11 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -O3 -Wall -We find_library(Boost 1.21.0 REQUIRED) +configure_file( + "${CMAKE_SOURCE_DIR}/src/${PROJECT_NAME}Config.h.in" + "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.h" +) + include_directories(SYSTEM ${Boost_INCLUDE_DIRECTORIES} ) @@ -14,6 +19,7 @@ include_directories(SYSTEM include_directories( src/ include/ + "${PROJECT_BINARY_DIR}" ) add_executable(${PROJECT_NAME} diff --git a/include/components/layer.hpp b/include/components/layer.hpp index abf57a9..28819f2 100644 --- a/include/components/layer.hpp +++ b/include/components/layer.hpp @@ -3,20 +3,24 @@ #include "primitivetypes.hpp" #include "components/tilestreamer.hpp" +#include "components/tileiterator.hpp" #include #include namespace dk { + template + class Viewport; + template class LayerBase { public: typedef Vector coords; - LayerBase ( const coords& parSize, const coords& parTileSize, const coords& parMasterTileSize ); + LayerBase ( const coords& parCount, const coords& parTileSize, const coords& parMasterTileSize ); virtual ~LayerBase ( void ) noexcept = default; protected: - coords m_size; + coords m_count; coords m_tilesize; coords m_mastersize; }; @@ -26,18 +30,28 @@ namespace dk { public: typedef typename LayerBase::coords coords; typedef TileStreamer streamer_type; + typedef TileIterator iterator; Layer ( const Layer& ) = delete; Layer ( Layer&& ) = default; - Layer ( const coords& parSize, const coords& parTileSize, const coords& parMasterTileSize, streamer_type&& parStreamer ); + Layer ( const coords& parCount, const coords& parTileSize, const coords& parMasterTileSize, streamer_type&& parStreamer ); virtual ~Layer ( void ) noexcept = default; Layer& operator= ( const Layer& ) = delete; + iterator begin ( void ); + void setActiveViewport ( const coords& parFrom, const coords& parCount ); + private: streamer_type m_streamer; + Viewport m_activeViewport; std::vector m_tiles; }; + + namespace implem { + template + typename LayerBase::coords::value_type area ( const typename LayerBase::coords& parVec ) a_pure; + } } //namespace dk #include "implem/layer.inl" diff --git a/include/components/tileiterator.hpp b/include/components/tileiterator.hpp index 9e48074..0b8dc65 100644 --- a/include/components/tileiterator.hpp +++ b/include/components/tileiterator.hpp @@ -7,26 +7,38 @@ #include namespace dk { + namespace implem { + template + inline size_t get_index_from_pos ( const Vector& parPos, const Vector& parSize ) a_pure; + +#if defined(NDEBUG) + template <> + inline size_t get_index_from_pos ( const Vector& parPos, const Vector& parSize ) a_pure; +#endif + } //namespace implem + template - class TileIterator : public boost::iterator_facade, T, boost::biderectional_traversal_tag> { + class TileIterator : public boost::iterator_facade, T, boost::bidirectional_traversal_tag> { friend class boost::iterator_core_access; public: typedef Vector coords; TileIterator ( void ); TileIterator ( const TileIterator& parOther ) = default; - TileIterator ( const coords& parFrom, const coords& parTo ); - TileIterator ( const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parTotal ); + TileIterator ( std::vector* parData, const coords& parFrom, const coords& parTo ); + TileIterator ( std::vector* parData, const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parTotal ); ~TileIterator ( void ) = default; + const coords& position ( void ) const { return m_pos; } + private: void increment ( void ); void decrement ( void ); void advance ( size_t parAdvance ); - ptrdiff_t distance_to ( const MeshSelectionIterator& parOther ); - bool equal ( const MeshSelectionIterator& parOther ) const; - T& dereference ( void ) const { return m_data[get_current_index()]; } - size_t get_current_index ( void ) const; + ptrdiff_t distance_to ( const TileIterator& parOther ); + bool equal ( const TileIterator& parOther ) const; + T& dereference ( void ) const { return (*m_data)[get_current_index()]; } + size_t get_current_index ( void ) const { return implem::get_index_from_pos(m_pos, m_total); } coords m_pos; coords m_from; diff --git a/include/components/tyler.hpp b/include/components/tyler.hpp index 4ada3c3..e3844bf 100644 --- a/include/components/tyler.hpp +++ b/include/components/tyler.hpp @@ -8,9 +8,6 @@ #include namespace dk { - template - class Viewport; - template class Tyler { typedef std::unique_ptr> LayerPtr; @@ -24,10 +21,11 @@ namespace dk { typename coords::value_type tiles_count ( void ) const; const coords& map_size ( void ) const { return m_size; } + template - size_t push_layer ( typename Layer::streamer_type&& parStreamer ); + Layer& push_layer ( typename Layer::streamer_type&& parStreamer ); template - size_t push_layer ( typename Layer::streamer_type&& parStreamer, const coords& parSubdiv ); + Layer& push_layer ( typename Layer::streamer_type&& parStreamer, const coords& parSubdiv ); private: const coords m_size; diff --git a/include/components/viewport.hpp b/include/components/viewport.hpp index 793fb4c..5c35f61 100644 --- a/include/components/viewport.hpp +++ b/include/components/viewport.hpp @@ -2,23 +2,35 @@ #define id0ADBCC15BA574485BF3267254090D99B #include "primitivetypes.hpp" +#include "components/tileiterator.hpp" +#include "components/layer.hpp" namespace dk { - template + template + class Layer; + + template class Viewport { public: typedef Vector coords; + typedef typename Layer::iterator iterator; - explicit Viewport ( const coords& parSize ); - Viewport ( const coords& parSize, const coords& parPos ); + explicit Viewport ( Layer& parLayer ); + Viewport ( Layer& parLayer, const coords& parSize, const coords& parPos ); ~Viewport ( void ) noexcept = default; Viewport& operator= ( const Viewport& ) = default; + void setSize ( const coords& parSize ) { m_size = parSize; } + void setFrom ( const coords& parFrom ) { m_position = parFrom; } + private: coords m_size; coords m_position; + Layer& m_layer; }; } //namespace dk +#include "implem/viewport.inl" + #endif diff --git a/include/implem/layer.inl b/include/implem/layer.inl index 3934ae8..d3ebd29 100644 --- a/include/implem/layer.inl +++ b/include/implem/layer.inl @@ -1,9 +1,22 @@ namespace dk { + namespace implem { + ///---------------------------------------------------------------------- + ///---------------------------------------------------------------------- + template + typename LayerBase::coords::value_type area (const typename LayerBase::coords& parVec) { + typename LayerBase::coords::value_type retval(1); + for (size_t d = 0; d < D; ++d) { + retval *= parVec[d]; + } + return retval; + } + } + ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- template - LayerBase::LayerBase (const coords& parSize, const coords& parTileSize, const coords& parMasterTileSize) : - m_size(parSize), + LayerBase::LayerBase (const coords& parCount, const coords& parTileSize, const coords& parMasterTileSize) : + m_count(parCount), m_tilesize(parTileSize), m_mastersize(parMasterTileSize) { @@ -13,9 +26,29 @@ namespace dk { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- template - Layer::Layer (const coords& parSize, const coords& parTileSize, const coords& parMasterTileSize, streamer_type&& parStreamer) : - LayerBase(parSize, parTileSize, parMasterTileSize), - m_streamer(std::move(parStreamer)) + Layer::Layer (const coords& parCount, const coords& parTileSize, const coords& parMasterTileSize, streamer_type&& parStreamer) : + LayerBase(parCount, parTileSize, parMasterTileSize), + m_streamer(std::move(parStreamer)), + m_activeViewport(*this) { } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + typename Layer::iterator Layer::begin() { + return iterator(&m_tiles, coords(0), this->m_count); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + template + void Layer::setActiveViewport (const coords& parFrom, const coords& parCount) { + m_tiles.clear(); + const auto area = implem::area(parCount); + m_tiles.reserve(area); + m_streamer.copy(m_tiles, parFrom, parFrom + parCount); + m_activeViewport.setFrom(parFrom); + m_activeViewport.setSize(parCount); + } } diff --git a/include/implem/tileiterator.inl b/include/implem/tileiterator.inl index 8535177..2da0ced 100644 --- a/include/implem/tileiterator.inl +++ b/include/implem/tileiterator.inl @@ -1,4 +1,27 @@ namespace dk { + namespace implem { + template + inline size_t get_index_from_pos (const Vector& parPos, const Vector& parSize) { + size_t index = 0; + for (size_t d = 0; d < D; ++d) { + size_t pos = static_cast(parPos[D - 1 - d]); + for (size_t p = 0; p < D - 1 - d; ++p) { + pos *= static_cast(parSize[p]); + } + + index += pos; + } + return index; + } + +#if defined(NDEBUG) + template <> + inline size_t get_index_from_pos (const Vector& parPos, const Vector& parSize) { + return parPos.y() * parSize.x() + parPos.x(); + } +#endif + } //namespace implem + template TileIterator::TileIterator() : m_pos(CoordinateScalarType()), @@ -6,30 +29,35 @@ namespace dk { m_to(CoordinateScalarType()), m_areafrom(CoordinateScalarType()), m_areato(CoordinateScalarType()), - m_total(CoordinateScalarType()) + m_total(CoordinateScalarType()), + m_data(nullptr) { } template - TileIterator::TileIterator (const coords& parFrom, const coords& parTo) : + TileIterator::TileIterator (std::vector* parData, const coords& parFrom, const coords& parTo) : m_pos(parFrom), m_from(parFrom), m_to(parTo), m_areafrom(parFrom), m_areato(parTo), - m_total(parTo - parFrom) + m_total(parTo - parFrom), + m_data(parData) { + DK_ASSERT(parData); } template - TileIterator::TileIterator (const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parTotal) : + TileIterator::TileIterator (std::vector* parData, const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parTotal) : m_pos(parFrom), m_from(parFrom), m_to(parTo), m_areafrom(parAreaFrom), m_areato(parAreaTo), - m_total(parTotal) + m_total(parTotal), + m_data(parData) { + DK_ASSERT(parData); } template @@ -58,33 +86,18 @@ namespace dk { ++m_pos[D - 1]; } - template - size_t TileIterator::get_current_index() const { - size_t index = 0; - for (size_t d = 0; d < D; ++d) { - size_t pos = static_cast(m_pos[D - 1 - d]); - for (size_t p = 0; p < D - 1 - d; ++p) { - pos *= static_cast(m_total[p]); - } - - index += pos; - } - return index; - } - template void TileIterator::advance (size_t parAdvance) { //TODO: implement } template - ptrdiff_t TileIterator::distance_to (const MeshSelectionIterator& parOther) { - //TODO: implement + ptrdiff_t TileIterator::distance_to (const TileIterator& parOther) { + return std::distance(this->get_current_index(), parOther.get_current_index()); } template - bool TileIterator::equal (const MeshSelectionIterator& parOther) const { - //TODO: implement - return true; + bool TileIterator::equal (const TileIterator& parOther) const { + return m_data == parOther.m_data and m_pos == parOther.m_pos; } } //namespace dk diff --git a/include/implem/tyler.inl b/include/implem/tyler.inl index 5c7c407..ab65344 100644 --- a/include/implem/tyler.inl +++ b/include/implem/tyler.inl @@ -24,19 +24,19 @@ namespace dk { ///-------------------------------------------------------------------------- template template - size_t Tyler::push_layer (typename Layer::streamer_type&& parStreamer) { - const size_t retval = m_layers.size(); - m_layers.push_back(LayerPtr(new Layer(m_size, m_tilesize, coords(1), std::move(parStreamer)))); - return retval; + Layer& Tyler::push_layer (typename Layer::streamer_type&& parStreamer) { + auto newLayer = new Layer(m_size, m_tilesize, m_tilesize, std::move(parStreamer)); + m_layers.push_back(LayerPtr(newLayer)); + return *newLayer; } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- template template - size_t Tyler::push_layer (typename Layer::streamer_type&& parStreamer, const coords& parSubdiv) { - const size_t retval = m_layers.size(); - m_layers.push_back(LayerPtr(new Layer(m_size * parSubdiv, m_tilesize / parSubdiv, m_tilesize, std::move(parStreamer)))); - return retval; + Layer& Tyler::push_layer (typename Layer::streamer_type&& parStreamer, const coords& parSubdiv) { + auto newLayer = new Layer(m_size * parSubdiv, m_tilesize / parSubdiv, m_tilesize, std::move(parStreamer)); + m_layers.push_back(LayerPtr(newLayer)); + return *newLayer; } } //namespace dk diff --git a/include/implem/viewport.inl b/include/implem/viewport.inl new file mode 100644 index 0000000..c0fb04b --- /dev/null +++ b/include/implem/viewport.inl @@ -0,0 +1,15 @@ +namespace dk { + template + Viewport::Viewport (Layer& parLayer) : + m_layer(parLayer) + { + } + + template + Viewport::Viewport (Layer& parLayer, const coords& parSize, const coords& parPos) : + m_size(parSize), + m_position(parPos), + m_layer(parLayer) + { + } +} //namespace dk diff --git a/include/primitivetypes.hpp b/include/primitivetypes.hpp index 42121d0..9abcf27 100644 --- a/include/primitivetypes.hpp +++ b/include/primitivetypes.hpp @@ -1,6 +1,8 @@ #ifndef id583F11FE0934446E9135B4ADCFA1E4F9 #define id583F11FE0934446E9135B4ADCFA1E4F9 +#include +#include "implem/compatibility.h" #include "implem/configuration.h" #include "implem/vector.hpp" diff --git a/src/doorkeeperConfig.h.in b/src/doorkeeperConfig.h.in new file mode 100644 index 0000000..cc312d8 --- /dev/null +++ b/src/doorkeeperConfig.h.in @@ -0,0 +1,10 @@ +#ifndef idFEDC74BA6A4345AD8D76106228CB2E89 +#define idFEDC74BA6A4345AD8D76106228CB2E89 + +#if !defined(NDEBUG) +# define DATA_PATH "@CMAKE_SOURCE_DIR@" +#else +# define DATA_PATH "" +#endif + +#endif diff --git a/src/main.cpp b/src/main.cpp index ff4ccd3..b1bae91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,16 +1,27 @@ +#include "doorkeeper.hpp" +#include "doorkeeperConfig.h" #include #include #include -#include "doorkeeper.hpp" int main() { typedef dk::Tyler<2> Tiler; typedef std::unique_ptr ifstreamptr; - Tiler tiler(Tiler::coords(200, 150), Tiler::coords(64)); + Tiler tiler(Tiler::coords(8, 6), Tiler::coords(64)); - tiler.push_layer(dk::Layer::streamer_type(ifstreamptr(new std::ifstream("test.map")))); - tiler.push_layer(dk::Layer::streamer_type(ifstreamptr(new std::ifstream("test_2.map")))); + auto bottom_layer = &tiler.push_layer(dk::Layer::streamer_type(ifstreamptr(new std::ifstream(DATA_PATH"test.map")))); + tiler.push_layer(dk::Layer::streamer_type(ifstreamptr(new std::ifstream(DATA_PATH"test_2.map")))); + + { + auto ittest = bottom_layer->begin(); + for (int z = 0; z < 10; ++z) { +#if !defined(NDEBUG) + std::cout << "At tile " << ittest.position() << " value " << *ittest << '\n'; +#endif + ++ittest; + } + } #if !defined(NDEBUG) std::cout << "Map size: " << tiler.map_size() << '\n'; diff --git a/test.map b/test.map new file mode 100644 index 0000000..eef23ca --- /dev/null +++ b/test.map @@ -0,0 +1,6 @@ +1101111011 +1100011000 +1111011110 +1111000110 +0000010111 +1111110111 diff --git a/test_2.map b/test_2.map new file mode 120000 index 0000000..d5572f6 --- /dev/null +++ b/test_2.map @@ -0,0 +1 @@ +test.map \ No newline at end of file