Helper recursive map loader.

Recursion not tested since the file format is not ready yet, sorry.
This commit is contained in:
King_DuckZ 2015-05-28 00:20:40 +02:00
commit a82e8c16f6
9 changed files with 148 additions and 8 deletions

View file

@ -4,6 +4,8 @@
#include "doorkeeper/implem/maptypes.hpp"
#include "doorkeeper/primitivetypes.hpp"
#include <cstdint>
#include <vector>
#include <string>
namespace dk {
template <typename T, uint32_t D>
@ -15,8 +17,10 @@ namespace dk {
virtual void fetch ( std::vector<T>& parOut, const coords& parFrom, const coords& parTo ) = 0;
virtual const coords& mapSize ( void ) const = 0;
virtual const coords& tileSize ( void ) const = 0;
virtual MapTypes mapType ( void ) const = 0;
virtual int layersCount ( void ) const = 0;
virtual void chainedMaps ( std::vector<std::string>& parOut ) const = 0;
};
} //namespace dk

View file

@ -21,6 +21,7 @@ namespace dk {
typedef typename LayerBase<D>::coords coords;
Tyler ( void ) = delete;
Tyler ( Tyler&& ) = default;
explicit Tyler ( const coords& parTileSize );
~Tyler ( void ) noexcept = default;

View file

@ -2,12 +2,22 @@
#define id6327FA76CFB44F65A459C69096EC65D5
#include "doorkeeper.hpp"
#include <cstdint>
namespace dk {
typedef Tyler<2>::coords coords2;
typedef Tyler<2> Tyler2d;
template <typename T> using Layer2d = Layer<T, 2>;
typedef Viewport<2> Viewport2d;
template <typename T> using BaseMapSource2d = BaseMapSource<T, 2>;
} //namespace dk
namespace dkh {
template <typename T, uint32_t D, typename C>
struct MapLoaderPool;
template <typename T, typename C>
using MapLoaderPool2d = MapLoaderPool<T, 2, C>;
} //namespace dkh
#endif

View file

@ -23,15 +23,17 @@ namespace dkh {
AsciiMapSource ( void ) = delete;
AsciiMapSource ( const AsciiMapSource& ) = delete;
AsciiMapSource ( AsciiMapSource&& parOther ) = default;
AsciiMapSource ( const char* parFilename, const coords& parSize, dk::MapTypes parMapType );
AsciiMapSource ( const std::string& parFilename, const coords& parSize, dk::MapTypes parMapType );
AsciiMapSource ( std::istream& parData, const coords& parSize, dk::MapTypes parMapType );
AsciiMapSource ( const char* parFilename, const coords& parSize, dk::MapTypes parMapType, const coords& parTileSize );
AsciiMapSource ( const std::string& parFilename, const coords& parSize, dk::MapTypes parMapType, const coords& parTileSize );
AsciiMapSource ( std::istream& parData, const coords& parSize, dk::MapTypes parMapType, const coords& parTileSize );
virtual ~AsciiMapSource ( void ) noexcept = default;
virtual const coords& mapSize ( void ) const;
virtual void fetch ( std::vector<MapTileType>& parOut, const coords& parFrom, const coords& parTo );
virtual dk::MapTypes mapType ( void ) const;
virtual int layersCount ( void ) const;
virtual const coords& tileSize ( void ) const;
virtual void chainedMaps ( std::vector<std::string>& parOut ) const;
private:
enum {
@ -42,6 +44,7 @@ namespace dkh {
std::vector<MapTileType> m_wholedata;
const coords m_mapSize;
const coords m_tileSize;
std::size_t m_bytepos;
const dk::MapTypes m_mapType;
};

View file

@ -0,0 +1,45 @@
#ifndef idA52FEA0859494D3FBDF8ED5565091C59
#define idA52FEA0859494D3FBDF8ED5565091C59
#include "doorkeeper/components/tyler.hpp"
#include "doorkeeper/components/basemapsource.hpp"
#include <cstdint>
#include <utility>
#include <string>
#include <memory>
#include <map>
#include <stack>
#include <ciso646>
namespace dkh {
template <typename T, uint32_t D, typename C>
struct MapLoaderPool {
typedef std::unique_ptr<dk::BaseMapSource<T, D>> BaseMapSourceUPtr;
typedef std::map<std::string, BaseMapSourceUPtr> PoolMapType;
enum { dimensions = D };
typedef C opener_type;
typedef T tile_type;
PoolMapType pool;
C opener;
dk::BaseMapSource<T, D>* operator() ( const std::string& parName );
};
template <typename M>
dk::Tyler<M::dimensions> call_map_load ( M& parFileOpener, const std::string& parOpen );
template <typename M>
dk::Tyler<M::dimensions> call_map_load ( dk::Tyler<M::dimensions>& parTyler, M& parFileOpener, const std::string& parOpen );
template <typename T, uint32_t D, typename C>
dk::Tyler<D> map_load ( C& parFileOpener, const std::string& parOpen );
template <typename T, uint32_t D, typename C>
dk::Tyler<D>& map_load ( dk::Tyler<D>& parTyler, C& parFileOpener, const std::string& parOpen );
} //namespace dkh
#include "doorkeeper/implem/maploader.inl"
#endif

View file

@ -0,0 +1,59 @@
namespace dkh {
template <typename T, uint32_t D, typename C>
dk::Tyler<D> map_load (C& parFileOpener, const std::string& parOpen) {
dk::BaseMapSource<T, D>* reader = parFileOpener(parOpen);
dk::Tyler<D> tyler(reader->tileSize());
tyler.push_layer(reader);
std::vector<std::string> submaps;
reader->chainedMaps(submaps);
for (const auto& name : submaps) {
map_load<T, D, C>(tyler, parFileOpener, name);
}
return std::move(tyler);
}
template <typename T, uint32_t D, typename C>
dk::Tyler<D>& map_load (dk::Tyler<D>& parTyler, C& parFileOpener, const std::string& parOpen) {
std::stack<std::string, std::vector<std::string>> name_stack;
std::vector<std::string> submaps;
name_stack.push(parOpen);
do {
dk::BaseMapSource<T, D>* reader = parFileOpener(name_stack.top());
name_stack.pop();
submaps.clear();
reader->chainedMaps(submaps);
for (auto&& curr_name : submaps) {
name_stack.emplace(std::move(curr_name));
}
parTyler.push_layer(reader);
} while (not name_stack.empty());
return parTyler;
}
template <typename T, uint32_t D, typename C>
dk::BaseMapSource<T, D>* MapLoaderPool<T, D, C>::operator() (const std::string& parName) {
auto it_found = pool.find(parName);
if (pool.end() != it_found) {
return it_found->second.get();
}
else {
std::pair<typename PoolMapType::iterator, bool> new_item = pool.insert(std::make_pair(parName, BaseMapSourceUPtr(nullptr)));
DK_ASSERT(new_item.second);
new_item.first->second.reset(opener(parName));
return new_item.first->second.get();
}
}
template <typename M>
dk::Tyler<M::dimensions> call_map_load (M& parFileOpener, const std::string& parOpen) {
return map_load<typename M::tile_type, M::dimensions, M>(parFileOpener, parOpen);
}
template <typename M>
dk::Tyler<M::dimensions> call_map_load (dk::Tyler<M::dimensions>& parTyler, M& parFileOpener, const std::string& parOpen) {
return map_load<typename M::tile_type, M::dimensions, M>(parTyler, parFileOpener, parOpen);
}
} //namespace dkh