Progressing with the implementation

This commit is contained in:
King_DuckZ 2014-12-12 20:04:48 +01:00
parent 9086de8dae
commit 0da607ab3d
14 changed files with 192 additions and 59 deletions

View File

@ -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}

View File

@ -3,20 +3,24 @@
#include "primitivetypes.hpp"
#include "components/tilestreamer.hpp"
#include "components/tileiterator.hpp"
#include <algorithm>
#include <vector>
namespace dk {
template <typename T, size_t D>
class Viewport;
template <size_t D>
class LayerBase {
public:
typedef Vector<CoordinateScalarType, D> 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<D>::coords coords;
typedef TileStreamer<T, D> streamer_type;
typedef TileIterator<T, D> 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<T, D> m_activeViewport;
std::vector<T> m_tiles;
};
namespace implem {
template <size_t D>
typename LayerBase<D>::coords::value_type area ( const typename LayerBase<D>::coords& parVec ) a_pure;
}
} //namespace dk
#include "implem/layer.inl"

View File

@ -7,26 +7,38 @@
#include <vector>
namespace dk {
namespace implem {
template <size_t D>
inline size_t get_index_from_pos ( const Vector<CoordinateScalarType, D>& parPos, const Vector<CoordinateScalarType, D>& parSize ) a_pure;
#if defined(NDEBUG)
template <>
inline size_t get_index_from_pos ( const Vector<CoordinateScalarType, 2>& parPos, const Vector<CoordinateScalarType, 2>& parSize ) a_pure;
#endif
} //namespace implem
template <typename T, size_t D>
class TileIterator : public boost::iterator_facade<TileIterator<T, D>, T, boost::biderectional_traversal_tag> {
class TileIterator : public boost::iterator_facade<TileIterator<T, D>, T, boost::bidirectional_traversal_tag> {
friend class boost::iterator_core_access;
public:
typedef Vector<CoordinateScalarType, D> 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<T>* parData, const coords& parFrom, const coords& parTo );
TileIterator ( std::vector<T>* 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;

View File

@ -8,9 +8,6 @@
#include <memory>
namespace dk {
template <size_t D>
class Viewport;
template <size_t D>
class Tyler {
typedef std::unique_ptr<LayerBase<D>> 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 <typename T>
size_t push_layer ( typename Layer<T, D>::streamer_type&& parStreamer );
Layer<T, D>& push_layer ( typename Layer<T, D>::streamer_type&& parStreamer );
template <typename T>
size_t push_layer ( typename Layer<T, D>::streamer_type&& parStreamer, const coords& parSubdiv );
Layer<T, D>& push_layer ( typename Layer<T, D>::streamer_type&& parStreamer, const coords& parSubdiv );
private:
const coords m_size;

View File

@ -2,23 +2,35 @@
#define id0ADBCC15BA574485BF3267254090D99B
#include "primitivetypes.hpp"
#include "components/tileiterator.hpp"
#include "components/layer.hpp"
namespace dk {
template <size_t D>
template <typename T, size_t D>
class Layer;
template <typename T, size_t D>
class Viewport {
public:
typedef Vector<CoordinateScalarType, D> coords;
typedef typename Layer<T, D>::iterator iterator;
explicit Viewport ( const coords& parSize );
Viewport ( const coords& parSize, const coords& parPos );
explicit Viewport ( Layer<T, D>& parLayer );
Viewport ( Layer<T, D>& 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<T, D>& m_layer;
};
} //namespace dk
#include "implem/viewport.inl"
#endif

View File

@ -1,9 +1,22 @@
namespace dk {
namespace implem {
///----------------------------------------------------------------------
///----------------------------------------------------------------------
template <size_t D>
typename LayerBase<D>::coords::value_type area (const typename LayerBase<D>::coords& parVec) {
typename LayerBase<D>::coords::value_type retval(1);
for (size_t d = 0; d < D; ++d) {
retval *= parVec[d];
}
return retval;
}
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <size_t D>
LayerBase<D>::LayerBase (const coords& parSize, const coords& parTileSize, const coords& parMasterTileSize) :
m_size(parSize),
LayerBase<D>::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 <typename T, size_t D>
Layer<T, D>::Layer (const coords& parSize, const coords& parTileSize, const coords& parMasterTileSize, streamer_type&& parStreamer) :
LayerBase<D>(parSize, parTileSize, parMasterTileSize),
m_streamer(std::move(parStreamer))
Layer<T, D>::Layer (const coords& parCount, const coords& parTileSize, const coords& parMasterTileSize, streamer_type&& parStreamer) :
LayerBase<D>(parCount, parTileSize, parMasterTileSize),
m_streamer(std::move(parStreamer)),
m_activeViewport(*this)
{
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, size_t D>
typename Layer<T, D>::iterator Layer<T, D>::begin() {
return iterator(&m_tiles, coords(0), this->m_count);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <typename T, size_t D>
void Layer<T, D>::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);
}
}

View File

@ -1,4 +1,27 @@
namespace dk {
namespace implem {
template <size_t D>
inline size_t get_index_from_pos (const Vector<CoordinateScalarType, D>& parPos, const Vector<CoordinateScalarType, D>& parSize) {
size_t index = 0;
for (size_t d = 0; d < D; ++d) {
size_t pos = static_cast<size_t>(parPos[D - 1 - d]);
for (size_t p = 0; p < D - 1 - d; ++p) {
pos *= static_cast<size_t>(parSize[p]);
}
index += pos;
}
return index;
}
#if defined(NDEBUG)
template <>
inline size_t get_index_from_pos (const Vector<CoordinateScalarType, 2>& parPos, const Vector<CoordinateScalarType, 2>& parSize) {
return parPos.y() * parSize.x() + parPos.x();
}
#endif
} //namespace implem
template <typename T, size_t D>
TileIterator<T, D>::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 <typename T, size_t D>
TileIterator<T, D>::TileIterator (const coords& parFrom, const coords& parTo) :
TileIterator<T, D>::TileIterator (std::vector<T>* 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 <typename T, size_t D>
TileIterator<T, D>::TileIterator (const coords& parFrom, const coords& parTo, const coords& parAreaFrom, const coords& parAreaTo, const coords& parTotal) :
TileIterator<T, D>::TileIterator (std::vector<T>* 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 <typename T, size_t D>
@ -58,33 +86,18 @@ namespace dk {
++m_pos[D - 1];
}
template <typename T, size_t D>
size_t TileIterator<T, D>::get_current_index() const {
size_t index = 0;
for (size_t d = 0; d < D; ++d) {
size_t pos = static_cast<size_t>(m_pos[D - 1 - d]);
for (size_t p = 0; p < D - 1 - d; ++p) {
pos *= static_cast<size_t>(m_total[p]);
}
index += pos;
}
return index;
}
template <typename T, size_t D>
void TileIterator<T, D>::advance (size_t parAdvance) {
//TODO: implement
}
template <typename T, size_t D>
ptrdiff_t TileIterator<T, D>::distance_to (const MeshSelectionIterator& parOther) {
//TODO: implement
ptrdiff_t TileIterator<T, D>::distance_to (const TileIterator& parOther) {
return std::distance(this->get_current_index(), parOther.get_current_index());
}
template <typename T, size_t D>
bool TileIterator<T, D>::equal (const MeshSelectionIterator& parOther) const {
//TODO: implement
return true;
bool TileIterator<T, D>::equal (const TileIterator& parOther) const {
return m_data == parOther.m_data and m_pos == parOther.m_pos;
}
} //namespace dk

View File

@ -24,19 +24,19 @@ namespace dk {
///--------------------------------------------------------------------------
template <size_t D>
template <typename T>
size_t Tyler<D>::push_layer (typename Layer<T, D>::streamer_type&& parStreamer) {
const size_t retval = m_layers.size();
m_layers.push_back(LayerPtr(new Layer<T, D>(m_size, m_tilesize, coords(1), std::move(parStreamer))));
return retval;
Layer<T, D>& Tyler<D>::push_layer (typename Layer<T, D>::streamer_type&& parStreamer) {
auto newLayer = new Layer<T, D>(m_size, m_tilesize, m_tilesize, std::move(parStreamer));
m_layers.push_back(LayerPtr(newLayer));
return *newLayer;
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
template <size_t D>
template <typename T>
size_t Tyler<D>::push_layer (typename Layer<T, D>::streamer_type&& parStreamer, const coords& parSubdiv) {
const size_t retval = m_layers.size();
m_layers.push_back(LayerPtr(new Layer<T, D>(m_size * parSubdiv, m_tilesize / parSubdiv, m_tilesize, std::move(parStreamer))));
return retval;
Layer<T, D>& Tyler<D>::push_layer (typename Layer<T, D>::streamer_type&& parStreamer, const coords& parSubdiv) {
auto newLayer = new Layer<T, D>(m_size * parSubdiv, m_tilesize / parSubdiv, m_tilesize, std::move(parStreamer));
m_layers.push_back(LayerPtr(newLayer));
return *newLayer;
}
} //namespace dk

View File

@ -0,0 +1,15 @@
namespace dk {
template <typename T, size_t D>
Viewport<T, D>::Viewport (Layer<T, D>& parLayer) :
m_layer(parLayer)
{
}
template <typename T, size_t D>
Viewport<T, D>::Viewport (Layer<T, D>& parLayer, const coords& parSize, const coords& parPos) :
m_size(parSize),
m_position(parPos),
m_layer(parLayer)
{
}
} //namespace dk

View File

@ -1,6 +1,8 @@
#ifndef id583F11FE0934446E9135B4ADCFA1E4F9
#define id583F11FE0934446E9135B4ADCFA1E4F9
#include <ciso646>
#include "implem/compatibility.h"
#include "implem/configuration.h"
#include "implem/vector.hpp"

10
src/doorkeeperConfig.h.in Normal file
View File

@ -0,0 +1,10 @@
#ifndef idFEDC74BA6A4345AD8D76106228CB2E89
#define idFEDC74BA6A4345AD8D76106228CB2E89
#if !defined(NDEBUG)
# define DATA_PATH "@CMAKE_SOURCE_DIR@"
#else
# define DATA_PATH ""
#endif
#endif

View File

@ -1,16 +1,27 @@
#include "doorkeeper.hpp"
#include "doorkeeperConfig.h"
#include <iostream>
#include <fstream>
#include <memory>
#include "doorkeeper.hpp"
int main() {
typedef dk::Tyler<2> Tiler;
typedef std::unique_ptr<std::ifstream> ifstreamptr;
Tiler tiler(Tiler::coords(200, 150), Tiler::coords(64));
Tiler tiler(Tiler::coords(8, 6), Tiler::coords(64));
tiler.push_layer<int>(dk::Layer<int, 2>::streamer_type(ifstreamptr(new std::ifstream("test.map"))));
tiler.push_layer<char>(dk::Layer<char, 2>::streamer_type(ifstreamptr(new std::ifstream("test_2.map"))));
auto bottom_layer = &tiler.push_layer<int>(dk::Layer<int, 2>::streamer_type(ifstreamptr(new std::ifstream(DATA_PATH"test.map"))));
tiler.push_layer<char>(dk::Layer<char, 2>::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';

6
test.map Normal file
View File

@ -0,0 +1,6 @@
1101111011
1100011000
1111011110
1111000110
0000010111
1111110111

1
test_2.map Symbolic link
View File

@ -0,0 +1 @@
test.map