Helper recursive map loader.
Recursion not tested since the file format is not ready yet, sorry.
This commit is contained in:
parent
55ca7028b5
commit
a82e8c16f6
9 changed files with 148 additions and 8 deletions
|
@ -1,4 +1,6 @@
|
|||
#include "doorkeeper/doorkeeper2d.hpp"
|
||||
#include "doorkeeper/helpers/maploader.hpp"
|
||||
#include "doorkeeper/helpers/asciimapsource.hpp"
|
||||
#include "gameConfig.h"
|
||||
#include <iostream>
|
||||
|
||||
|
@ -7,9 +9,13 @@ int main() {
|
|||
using dk::coords2;
|
||||
using dk::Viewport2d;
|
||||
using dk::Layer2d;
|
||||
typedef dkh::MapLoaderPool2d<int, std::function<dk::BaseMapSource2d<int>*(const std::string&)>> IntPoolType;
|
||||
|
||||
std::cout << "Welcome to " GAME_NAME " v" << GAME_VER_MAJOR << '.' << GAME_VER_MINOR << '.' << GAME_VER_PATCH << '\n';
|
||||
|
||||
Tyler2d tiler(coords2(64, 33));
|
||||
IntPoolType pool;
|
||||
pool.opener = [](const std::string& parName) { std::cout << "Opening " << parName << std::endl; return new dkh::AsciiMapSource(parName, coords2(10, 8), dk::MapType_IsometricStaggered, coords2(64, 64)); };
|
||||
Tyler2d tiler(dkh::call_map_load(pool, std::string("test_level.dk")));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
45
include/doorkeeper/helpers/maploader.hpp
Normal file
45
include/doorkeeper/helpers/maploader.hpp
Normal 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
|
59
include/doorkeeper/implem/maploader.inl
Normal file
59
include/doorkeeper/implem/maploader.inl
Normal 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
|
|
@ -10,8 +10,9 @@
|
|||
namespace dkh {
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize, dk::MapTypes parMapType) :
|
||||
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize, dk::MapTypes parMapType, const coords& parTileSize) :
|
||||
m_mapSize(parSize),
|
||||
m_tileSize(parTileSize),
|
||||
m_bytepos(0),
|
||||
m_mapType(parMapType)
|
||||
{
|
||||
|
@ -21,8 +22,9 @@ namespace dkh {
|
|||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize, dk::MapTypes parMapType) :
|
||||
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize, dk::MapTypes parMapType, const coords& parTileSize) :
|
||||
m_mapSize(parSize),
|
||||
m_tileSize(parTileSize),
|
||||
m_bytepos(0),
|
||||
m_mapType(parMapType)
|
||||
{
|
||||
|
@ -32,8 +34,9 @@ namespace dkh {
|
|||
|
||||
///-------------------------------------------------------------------------
|
||||
///-------------------------------------------------------------------------
|
||||
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize, dk::MapTypes parMapType) :
|
||||
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize, dk::MapTypes parMapType, const coords& parTileSize) :
|
||||
m_mapSize(parSize),
|
||||
m_tileSize(parTileSize),
|
||||
m_bytepos(0),
|
||||
m_mapType(parMapType)
|
||||
{
|
||||
|
@ -85,6 +88,10 @@ namespace dkh {
|
|||
return m_mapSize;
|
||||
}
|
||||
|
||||
const AsciiMapSource::coords& AsciiMapSource::tileSize() const {
|
||||
return m_tileSize;
|
||||
}
|
||||
|
||||
void AsciiMapSource::fetch (std::vector<MapTileType>& parOut, const coords& parFrom, const coords& parTo) {
|
||||
const std::size_t from = parFrom.x() + parFrom.y() * m_mapSize.x();
|
||||
const std::size_t to = parTo.x() + parTo.y() * m_mapSize.x() + 1;
|
||||
|
@ -100,4 +107,7 @@ namespace dkh {
|
|||
int AsciiMapSource::layersCount() const {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void AsciiMapSource::chainedMaps (std::vector<std::string>&) const {
|
||||
}
|
||||
} //namespace dkh
|
||||
|
|
|
@ -144,8 +144,10 @@ namespace {
|
|||
}
|
||||
|
||||
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath) {
|
||||
typedef dkh::AsciiMapSource::coords coords;
|
||||
|
||||
parLayerInfo.path = parPath;
|
||||
parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, dkh::AsciiMapSource::coords(10, 8), dk::MapType_IsometricStaggered));
|
||||
parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, coords(10, 8), dk::MapType_IsometricStaggered, coords(64, 64)));
|
||||
parLayerInfo.layer = &parTiler.push_layer(parLayerInfo.device.get());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue