Adding the concept of layers.
Deferring the data loading task to lower level classes. test_tiler removed as Tyler won't require the user to derive from it anymore.
This commit is contained in:
parent
369caf503d
commit
1e5f4fd2f0
9 changed files with 115 additions and 69 deletions
|
@ -19,5 +19,4 @@ include_directories(
|
|||
add_executable(${PROJECT_NAME}
|
||||
src/main.cpp
|
||||
src/doorkeeper.cpp
|
||||
src/test/test_tiler.cpp
|
||||
)
|
||||
|
|
43
include/components/layer.hpp
Normal file
43
include/components/layer.hpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
#ifndef idD3EDC396AA314474B3D909559FFC0247
|
||||
#define idD3EDC396AA314474B3D909559FFC0247
|
||||
|
||||
#include "primitivetypes.hpp"
|
||||
#include "components/tilestreamer.hpp"
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
namespace dk {
|
||||
template <size_t D>
|
||||
class LayerBase {
|
||||
public:
|
||||
typedef Vector<CoordinateScalarType, D> coords;
|
||||
|
||||
explicit LayerBase ( const coords& parSize );
|
||||
virtual ~LayerBase ( void ) noexcept = default;
|
||||
|
||||
protected:
|
||||
coords m_size;
|
||||
};
|
||||
|
||||
template <typename T, size_t D>
|
||||
class Layer : public LayerBase<D> {
|
||||
public:
|
||||
typedef typename LayerBase<D>::coords coords;
|
||||
typedef TileStreamer<T, D> streamer_type;
|
||||
|
||||
Layer ( const Layer& ) = delete;
|
||||
Layer ( Layer&& ) = default;
|
||||
Layer ( const coords& parSize, streamer_type&& parStreamer );
|
||||
virtual ~Layer ( void ) noexcept = default;
|
||||
|
||||
Layer& operator= ( const Layer& ) = delete;
|
||||
|
||||
private:
|
||||
streamer_type m_streamer;
|
||||
std::vector<T> m_tiles;
|
||||
};
|
||||
} //namespace dk
|
||||
|
||||
#include "implem/layer.inl"
|
||||
|
||||
#endif
|
|
@ -1,35 +1,35 @@
|
|||
#ifndef id6FB3FC97331449038D42AAAB4C01ABA1
|
||||
#define id6FB3FC97331449038D42AAAB4C01ABA1
|
||||
|
||||
#include "primitivetypes.hpp"
|
||||
#include "components/tilestreamer.hpp"
|
||||
#include "components/layer.hpp"
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
#include <ciso646>
|
||||
#include <memory>
|
||||
|
||||
namespace dk {
|
||||
template <size_t D>
|
||||
class Viewport;
|
||||
|
||||
template <typename T, size_t D, typename S=TileStreamer<T, D> >
|
||||
template <size_t D>
|
||||
class Tyler {
|
||||
typedef std::unique_ptr<LayerBase<D>> LayerPtr;
|
||||
typedef std::vector<LayerPtr> LayerList;
|
||||
public:
|
||||
typedef S streamer_type;
|
||||
typedef Vector<CoordinateScalarType, D> coords;
|
||||
typedef typename LayerBase<D>::coords coords;
|
||||
|
||||
Tyler ( void ) = delete;
|
||||
Tyler ( const coords& parSize, streamer_type&& );
|
||||
virtual ~Tyler ( void ) noexcept = default;
|
||||
explicit Tyler ( const coords& parSize );
|
||||
~Tyler ( void ) noexcept = default;
|
||||
|
||||
virtual bool batch_load ( const std::vector<T>& parLoad ) = 0;
|
||||
virtual bool single_load ( const T& parLoad ) = 0;
|
||||
|
||||
size_t tiles_count ( void ) const { return m_tiles.size(); }
|
||||
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 );
|
||||
|
||||
private:
|
||||
const coords m_size;
|
||||
std::vector<T> m_tiles;
|
||||
streamer_type m_streamer;
|
||||
LayerList m_layers;
|
||||
};
|
||||
} //namespace dk
|
||||
|
||||
|
|
|
@ -4,5 +4,6 @@
|
|||
#include "primitivetypes.hpp"
|
||||
#include "components/tyler.hpp"
|
||||
#include "components/viewport.hpp"
|
||||
#include "components/layer.hpp"
|
||||
|
||||
#endif
|
||||
|
|
18
include/implem/layer.inl
Normal file
18
include/implem/layer.inl
Normal file
|
@ -0,0 +1,18 @@
|
|||
namespace dk {
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <size_t D>
|
||||
LayerBase<D>::LayerBase (const coords& parSize) :
|
||||
m_size(parSize)
|
||||
{
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <typename T, size_t D>
|
||||
Layer<T, D>::Layer (const coords& parSize, streamer_type&& parStreamer) :
|
||||
LayerBase<D>(parSize),
|
||||
m_streamer(std::move(parStreamer))
|
||||
{
|
||||
}
|
||||
}
|
|
@ -1,12 +1,31 @@
|
|||
namespace dk {
|
||||
///------------------------------------------------------------------------
|
||||
///------------------------------------------------------------------------
|
||||
template <typename T, size_t D, typename S>
|
||||
Tyler<T, D, S>::Tyler (const coords& parSize, streamer_type&& parStream) :
|
||||
m_size(parSize),
|
||||
m_tiles(parSize.x() * parSize.y()),
|
||||
m_streamer(std::move(parStream))
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <size_t D>
|
||||
Tyler<D>::Tyler (const coords& parSize) :
|
||||
m_size(parSize)
|
||||
{
|
||||
assert(m_size.x() > 0 and m_size.y() > 0);
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
template <size_t D>
|
||||
typename Tyler<D>::coords::value_type Tyler<D>::tiles_count() const {
|
||||
typename coords::value_type retval = 1;
|
||||
for (size_t d = 0; d < D; ++d) {
|
||||
retval *= m_size[d];
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
///--------------------------------------------------------------------------
|
||||
///--------------------------------------------------------------------------
|
||||
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, std::move(parStreamer))));
|
||||
return retval;
|
||||
}
|
||||
} //namespace dk
|
||||
|
|
17
src/main.cpp
17
src/main.cpp
|
@ -1,9 +1,20 @@
|
|||
#include <iostream>
|
||||
#include "test/test_tiler.hpp"
|
||||
#include <fstream>
|
||||
#include <memory>
|
||||
#include "doorkeeper.hpp"
|
||||
|
||||
int main() {
|
||||
TestTiler simpleMap(TestTiler::coords(200, 150), "test.map");
|
||||
typedef dk::Tyler<2> Tiler;
|
||||
|
||||
std::cout << "Total tiles: " << simpleMap.tiles_count() << "\n";
|
||||
Tiler tiler(Tiler::coords(200, 150));
|
||||
|
||||
tiler.push_layer<int>(
|
||||
dk::Layer<int, 2>::streamer_type(std::unique_ptr<std::ifstream>(new std::ifstream("test.map")))
|
||||
);
|
||||
|
||||
#if !defined(NDEBUG)
|
||||
std::cout << "Map size: " << tiler.map_size() << '\n';
|
||||
#endif
|
||||
std::cout << "Total tiles: " << tiler.tiles_count() << '\n';
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include "test/test_tiler.hpp"
|
||||
|
||||
typedef dk::TileStreamer<int, 2> TileStreamerType;
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
TestTiler::TestTiler (const coords& parSize, const std::string&& parPath) :
|
||||
parent_type(parSize, TileStreamerType(TileStreamerType::StreamPtr(new std::ifstream(parPath))))
|
||||
{
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
bool TestTiler::batch_load (const std::vector<int>& parLoad) {
|
||||
std::cout << "Would batch-load " << parLoad.size() << " elements\n";
|
||||
return true;
|
||||
}
|
||||
|
||||
///----------------------------------------------------------------------------
|
||||
///----------------------------------------------------------------------------
|
||||
bool TestTiler::single_load (const int&) {
|
||||
std::cout << "Would load 1 element\n";
|
||||
return true;
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
#ifndef idDD0A66B75FF546FCAF01C65E980DF224
|
||||
#define idDD0A66B75FF546FCAF01C65E980DF224
|
||||
|
||||
#include "doorkeeper.hpp"
|
||||
|
||||
class TestTiler : public dk::Tyler<int, 2> {
|
||||
typedef dk::Tyler<int, 2> parent_type;
|
||||
public:
|
||||
typedef parent_type::coords coords;
|
||||
|
||||
TestTiler ( const coords& parSize, const std::string&& parPath );
|
||||
|
||||
virtual ~TestTiler ( void ) noexcept = default;
|
||||
|
||||
virtual bool batch_load ( const std::vector<int>& parLoad );
|
||||
virtual bool single_load ( const int& parLoad );
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in a new issue