BaseMapSource doesn't need to know T. Only fetch() will need that.

This commit is contained in:
King_DuckZ 2015-06-06 14:14:00 +02:00
parent 49f7d15379
commit ab6ef6c338
14 changed files with 42 additions and 40 deletions

View file

@ -9,7 +9,7 @@ int main() {
using dk::coords2; using dk::coords2;
using dk::Viewport2d; using dk::Viewport2d;
using dk::Layer2d; using dk::Layer2d;
typedef dkh::MapLoaderPool2d<int, std::function<dk::BaseMapSource2d<int>*(const std::string&)>> IntPoolType; typedef dkh::MapLoaderPool2d<int, std::function<dk::BaseMapSource2d*(const std::string&)>> IntPoolType;
std::cout << "Welcome to " GAME_NAME " v" << GAME_VER_MAJOR << '.' << GAME_VER_MINOR << '.' << GAME_VER_PATCH << '\n'; std::cout << "Welcome to " GAME_NAME " v" << GAME_VER_MAJOR << '.' << GAME_VER_MINOR << '.' << GAME_VER_PATCH << '\n';

View file

@ -6,21 +6,27 @@
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <string> #include <string>
#include <cstddef>
namespace dk { namespace dk {
template <typename T, uint32_t D> template <uint32_t D>
class BaseMapSource { class BaseMapSource {
public: public:
typedef dk::Vector<D> coords; typedef dk::Vector<D> coords;
BaseMapSource ( void ) = default; BaseMapSource ( void ) = default;
virtual ~BaseMapSource ( void ) noexcept = default; virtual ~BaseMapSource ( void ) noexcept = default;
virtual void fetch ( std::vector<T>& parOut, const coords& parFrom, const coords& parTo ) = 0; template <typename T>
void fetch ( std::vector<T>& parOut, const coords& parFrom, const coords& parTo );
virtual const coords& mapSize ( void ) const = 0; virtual const coords& mapSize ( void ) const = 0;
virtual const coords& tileSize ( void ) const = 0; virtual const coords& tileSize ( void ) const = 0;
virtual MapTypes mapType ( void ) const = 0; virtual MapTypes mapType ( void ) const = 0;
virtual int layersCount ( void ) const = 0; virtual int layersCount ( void ) const = 0;
virtual void chainedMaps ( std::vector<std::string>& parOut ) const = 0; virtual void chainedMaps ( std::vector<std::string>& parOut ) const = 0;
protected:
virtual void fetch_raw ( char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize ) = 0;
}; };
} //namespace dk } //namespace dk

View file

@ -52,7 +52,7 @@ namespace dk {
Layer ( const Layer& ) = delete; Layer ( const Layer& ) = delete;
Layer ( Layer&& ) = default; Layer ( Layer&& ) = default;
Layer ( const coords& parTileSize, const coords& parMasterTileSize, BaseMapSource<T, D>* parTilemap ); Layer ( const coords& parTileSize, const coords& parMasterTileSize, BaseMapSource<D>* parTilemap );
virtual ~Layer ( void ) noexcept = default; virtual ~Layer ( void ) noexcept = default;
Layer& operator= ( const Layer& ) = delete; Layer& operator= ( const Layer& ) = delete;
@ -64,7 +64,7 @@ namespace dk {
virtual void onPreload ( const coords& parFrom, const coords& parTo ); virtual void onPreload ( const coords& parFrom, const coords& parTo );
std::vector<T> m_tiles; std::vector<T> m_tiles;
BaseMapSource<T, D>* m_tilemap; BaseMapSource<D>* m_tilemap;
}; };
} //namespace dk } //namespace dk

View file

@ -10,7 +10,7 @@
#include <cstdint> #include <cstdint>
namespace dk { namespace dk {
template <typename T, uint32_t D> template <uint32_t D>
class BaseMapSource; class BaseMapSource;
template <uint32_t D> template <uint32_t D>
@ -29,9 +29,9 @@ namespace dk {
const coords& map_size ( void ) const { return m_size; } const coords& map_size ( void ) const { return m_size; }
template <typename T> template <typename T>
Layer<T, D>& push_layer ( BaseMapSource<T, D>* parTilemap ); Layer<T, D>& push_layer ( BaseMapSource<D>* parTilemap );
template <typename T> template <typename T>
Layer<T, D>& push_layer ( BaseMapSource<T, D>* parTilemap, const coords& parSubdiv ); Layer<T, D>& push_layer ( BaseMapSource<D>* parTilemap, const coords& parSubdiv );
void preload ( const coords& parFrom, const coords& parTo ); void preload ( const coords& parFrom, const coords& parTo );

View file

@ -9,7 +9,7 @@ namespace dk {
typedef Tyler<2> Tyler2d; typedef Tyler<2> Tyler2d;
template <typename T> using Layer2d = Layer<T, 2>; template <typename T> using Layer2d = Layer<T, 2>;
typedef Viewport<2> Viewport2d; typedef Viewport<2> Viewport2d;
template <typename T> using BaseMapSource2d = BaseMapSource<T, 2>; typedef BaseMapSource<2> BaseMapSource2d;
} //namespace dk } //namespace dk
namespace dkh { namespace dkh {

View file

@ -12,7 +12,7 @@ namespace dkh {
typedef int AsciiMapTileType; typedef int AsciiMapTileType;
} //namespace implem } //namespace implem
class AsciiMapSource : public dk::BaseMapSource<implem::AsciiMapTileType, 2> { class AsciiMapSource : public dk::BaseMapSource<2> {
public: public:
typedef dk::Vector<2> coords; typedef dk::Vector<2> coords;
typedef implem::AsciiMapTileType MapTileType; typedef implem::AsciiMapTileType MapTileType;
@ -29,18 +29,14 @@ namespace dkh {
virtual ~AsciiMapSource ( void ) noexcept = default; virtual ~AsciiMapSource ( void ) noexcept = default;
virtual const coords& mapSize ( void ) const; 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 dk::MapTypes mapType ( void ) const;
virtual int layersCount ( void ) const; virtual int layersCount ( void ) const;
virtual const coords& tileSize ( void ) const; virtual const coords& tileSize ( void ) const;
virtual void chainedMaps ( std::vector<std::string>& parOut ) const; virtual void chainedMaps ( std::vector<std::string>& parOut ) const;
private: private:
enum {
DataSize = sizeof(MapTileType)
};
void parse_map_data ( std::istream& parSrc ); void parse_map_data ( std::istream& parSrc );
virtual void fetch_raw ( char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize );
std::vector<MapTileType> m_wholedata; std::vector<MapTileType> m_wholedata;
const coords m_mapSize; const coords m_mapSize;

View file

@ -14,7 +14,7 @@
namespace dkh { namespace dkh {
template <typename T, uint32_t D, typename C> template <typename T, uint32_t D, typename C>
struct MapLoaderPool { struct MapLoaderPool {
typedef std::unique_ptr<dk::BaseMapSource<T, D>> BaseMapSourceUPtr; typedef std::unique_ptr<dk::BaseMapSource<D>> BaseMapSourceUPtr;
typedef std::map<std::string, BaseMapSourceUPtr> PoolMapType; typedef std::map<std::string, BaseMapSourceUPtr> PoolMapType;
enum { dimensions = D }; enum { dimensions = D };
@ -24,7 +24,7 @@ namespace dkh {
PoolMapType pool; PoolMapType pool;
C opener; C opener;
dk::BaseMapSource<T, D>* operator() ( const std::string& parName ); dk::BaseMapSource<D>* operator() ( const std::string& parName );
}; };
template <typename M> template <typename M>

View file

@ -17,11 +17,10 @@
#endif #endif
namespace dkh { namespace dkh {
template <typename T, uint32_t D> template <uint32_t D>
class TylerMapSource : public dk::BaseMapSource<T, D> { class TylerMapSource : public dk::BaseMapSource<D> {
public: public:
typedef dk::Vector<D> coords; typedef dk::Vector<D> coords;
typedef T MapTileType;
enum { enum {
MapDimensions = D MapDimensions = D
}; };
@ -36,13 +35,13 @@ namespace dkh {
virtual const coords& mapSize ( void ) const; virtual const coords& mapSize ( void ) const;
virtual const coords& tileSize ( void ) const; virtual const coords& tileSize ( void ) const;
virtual void fetch ( std::vector<MapTileType>& parOut, const coords& parFrom, const coords& parTo );
virtual dk::MapTypes mapType ( void ) const; virtual dk::MapTypes mapType ( void ) const;
virtual int layersCount ( void ) const; virtual int layersCount ( void ) const;
virtual void chainedMaps ( std::vector<std::string>& parOut ) const; virtual void chainedMaps ( std::vector<std::string>& parOut ) const;
private: private:
void parse_map_data ( std::istream& parSrc ); void parse_map_data ( std::istream& parSrc );
virtual void fetch_raw ( char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize );
std::unique_ptr<std::istream> m_stream; std::unique_ptr<std::istream> m_stream;
}; };

View file

@ -26,7 +26,7 @@ namespace dk {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
template <typename T, uint32_t D> template <typename T, uint32_t D>
Layer<T, D>::Layer (const coords& parTileSize, const coords& parMasterTileSize, BaseMapSource<T, D>* parTilemap) : Layer<T, D>::Layer (const coords& parTileSize, const coords& parMasterTileSize, BaseMapSource<D>* parTilemap) :
LayerBase<D>(parTilemap->mapSize(), parTileSize, parMasterTileSize), LayerBase<D>(parTilemap->mapSize(), parTileSize, parMasterTileSize),
m_tilemap(parTilemap) m_tilemap(parTilemap)
{ {

View file

@ -1,9 +1,9 @@
namespace dkh { namespace dkh {
template <typename T, uint32_t D, typename C> template <typename T, uint32_t D, typename C>
dk::Tyler<D> map_load (C& parFileOpener, const std::string& parOpen) { dk::Tyler<D> map_load (C& parFileOpener, const std::string& parOpen) {
dk::BaseMapSource<T, D>* reader = parFileOpener(parOpen); dk::BaseMapSource<D>* reader = parFileOpener(parOpen);
dk::Tyler<D> tyler(reader->tileSize()); dk::Tyler<D> tyler(reader->tileSize());
tyler.push_layer(reader); tyler.template push_layer<T>(reader);
std::vector<std::string> submaps; std::vector<std::string> submaps;
reader->chainedMaps(submaps); reader->chainedMaps(submaps);
for (const auto& name : submaps) { for (const auto& name : submaps) {
@ -19,7 +19,7 @@ namespace dkh {
name_stack.push(parOpen); name_stack.push(parOpen);
do { do {
dk::BaseMapSource<T, D>* reader = parFileOpener(name_stack.top()); dk::BaseMapSource<D>* reader = parFileOpener(name_stack.top());
name_stack.pop(); name_stack.pop();
submaps.clear(); submaps.clear();
@ -28,13 +28,13 @@ namespace dkh {
name_stack.emplace(std::move(curr_name)); name_stack.emplace(std::move(curr_name));
} }
parTyler.push_layer(reader); parTyler.template push_layer<T>(reader);
} while (not name_stack.empty()); } while (not name_stack.empty());
return parTyler; return parTyler;
} }
template <typename T, uint32_t D, typename C> template <typename T, uint32_t D, typename C>
dk::BaseMapSource<T, D>* MapLoaderPool<T, D, C>::operator() (const std::string& parName) { dk::BaseMapSource<D>* MapLoaderPool<T, D, C>::operator() (const std::string& parName) {
auto it_found = pool.find(parName); auto it_found = pool.find(parName);
if (pool.end() != it_found) { if (pool.end() != it_found) {
return it_found->second.get(); return it_found->second.get();

View file

@ -23,15 +23,15 @@ namespace dk {
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
template <uint32_t D> template <uint32_t D>
template <typename T> template <typename T>
Layer<T, D>& Tyler<D>::push_layer (BaseMapSource<T, D>* parTilemap) { Layer<T, D>& Tyler<D>::push_layer (BaseMapSource<D>* parTilemap) {
return push_layer(parTilemap, coords(static_cast<CoordinateScalarType>(1))); return push_layer<T>(parTilemap, coords(static_cast<CoordinateScalarType>(1)));
} }
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
///-------------------------------------------------------------------------- ///--------------------------------------------------------------------------
template <uint32_t D> template <uint32_t D>
template <typename T> template <typename T>
Layer<T, D>& Tyler<D>::push_layer (BaseMapSource<T, D>* parTilemap, const coords& parSubdiv) { Layer<T, D>& Tyler<D>::push_layer (BaseMapSource<D>* parTilemap, const coords& parSubdiv) {
auto newLayer = new Layer<T, D>(m_tilesize / parSubdiv, m_tilesize, parTilemap); auto newLayer = new Layer<T, D>(m_tilesize / parSubdiv, m_tilesize, parTilemap);
if (m_size == coords(0)) { if (m_size == coords(0)) {
m_size = newLayer->mapSize(); m_size = newLayer->mapSize();

View file

@ -1,18 +1,18 @@
namespace dkh { namespace dkh {
template <typename T, uint32_t D> template <uint32_t D>
TylerMapSource<T, D>::TylerMapSource (const char* parFilename) : TylerMapSource<D>::TylerMapSource (const char* parFilename) :
m_stream(new std::ifstream(parFilename)) m_stream(new std::ifstream(parFilename))
{ {
} }
template <typename T, uint32_t D> template <uint32_t D>
TylerMapSource<T, D>::TylerMapSource (const std::string& parFilename) : TylerMapSource<D>::TylerMapSource (const std::string& parFilename) :
m_stream(new std::ifstream(parFilename)) m_stream(new std::ifstream(parFilename))
{ {
} }
template <typename T, uint32_t D> template <uint32_t D>
TylerMapSource<T, D>::TylerMapSource (std::istream* parData) : TylerMapSource<D>::TylerMapSource (std::istream* parData) :
m_stream(parData) m_stream(parData)
{ {
} }

View file

@ -92,12 +92,12 @@ namespace dkh {
return m_tileSize; return m_tileSize;
} }
void AsciiMapSource::fetch (std::vector<MapTileType>& parOut, const coords& parFrom, const coords& parTo) { void AsciiMapSource::fetch_raw (char* parOut, const coords& parFrom, const coords& parTo, std::size_t parSize) {
const std::size_t from = parFrom.x() + parFrom.y() * m_mapSize.x(); 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; const std::size_t to = parTo.x() + parTo.y() * m_mapSize.x() + 1;
DK_ASSERT(to >= from); DK_ASSERT(to >= from);
parOut.resize(to - from); DK_ASSERT(parSize / sizeof(MapTileType) == to - from);
std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, parOut.begin()); std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, parOut);
} }
dk::MapTypes AsciiMapSource::mapType() const { dk::MapTypes AsciiMapSource::mapType() const {

View file

@ -4,6 +4,7 @@
#include "platformstrings.h" #include "platformstrings.h"
#include "doorkeeper/helpers/asciimapsource.hpp" #include "doorkeeper/helpers/asciimapsource.hpp"
#include "doorkeeper/helpers/tilecoordinates.hpp" #include "doorkeeper/helpers/tilecoordinates.hpp"
#include "doorkeeper/helpers/maploader.hpp"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
@ -142,7 +143,7 @@ namespace {
parLayerInfo.path = parPath; parLayerInfo.path = parPath;
parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, coords(10, 8), dk::MapType_IsometricStaggered, coords(64, 64))); 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()); parLayerInfo.layer = &parTiler.push_layer<int>(parLayerInfo.device.get());
} }
void printViewport (const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer) { void printViewport (const dk::Viewport<2>& parView, const dk::Layer<int, 2>& parLayer) {