diff --git a/include/doorkeeper/components/basemapsource.hpp b/include/doorkeeper/components/basemapsource.hpp new file mode 100644 index 0000000..bcbdaaa --- /dev/null +++ b/include/doorkeeper/components/basemapsource.hpp @@ -0,0 +1,23 @@ +#ifndef id372857FBD20C4DD2A9EDDB3A167682AA +#define id372857FBD20C4DD2A9EDDB3A167682AA + +#include "doorkeeper/implem/maptypes.hpp" +#include "doorkeeper/primitivetypes.hpp" +#include + +namespace dk { + template + class BaseMapSource { + public: + typedef dk::Vector coords; + BaseMapSource ( void ) = default; + virtual ~BaseMapSource ( void ) noexcept = default; + + virtual void fetch ( std::vector& parOut, const coords& parFrom, const coords& parTo ) = 0; + virtual const coords& mapSize ( void ) const = 0; + virtual MapTypes mapType ( void ) const = 0; + virtual int layersCount ( void ) const = 0; + }; +} //namespace dk + +#endif diff --git a/include/doorkeeper/components/exception.hpp b/include/doorkeeper/components/exception.hpp new file mode 100644 index 0000000..f9ef7e2 --- /dev/null +++ b/include/doorkeeper/components/exception.hpp @@ -0,0 +1,14 @@ +#ifndef idE137842DE628405E9154561168FFEE60 +#define idE137842DE628405E9154561168FFEE60 + +#include + +namespace dk { + class DoorKeeperException : public std::exception { + }; + + class SizeMismatchException : public DoorKeeperException { + }; +} //namespace dk + +#endif diff --git a/include/doorkeeper/components/layer.hpp b/include/doorkeeper/components/layer.hpp index 9355769..8c4a7dd 100644 --- a/include/doorkeeper/components/layer.hpp +++ b/include/doorkeeper/components/layer.hpp @@ -3,7 +3,7 @@ #include "doorkeeper/primitivetypes.hpp" #include "doorkeeper/components/tileiterator.hpp" -#include "doorkeeper/components/tilemapdata.hpp" +#include "doorkeeper/components/basemapsource.hpp" #include "doorkeeper/implem/helpers.hpp" #include #include @@ -52,7 +52,7 @@ namespace dk { Layer ( const Layer& ) = delete; Layer ( Layer&& ) = default; - Layer ( const coords& parTileSize, const coords& parMasterTileSize, TileMapData& parTilemap ); + Layer ( const coords& parTileSize, const coords& parMasterTileSize, BaseMapSource* parTilemap ); virtual ~Layer ( void ) noexcept = default; Layer& operator= ( const Layer& ) = delete; @@ -64,7 +64,7 @@ namespace dk { virtual void onPreload ( const coords& parFrom, const coords& parTo ); std::vector m_tiles; - TileMapData& m_tilemap; + BaseMapSource* m_tilemap; }; } //namespace dk diff --git a/include/doorkeeper/components/tilemapdata.hpp b/include/doorkeeper/components/tilemapdata.hpp deleted file mode 100644 index b95abe5..0000000 --- a/include/doorkeeper/components/tilemapdata.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef id8F5106DFABC14263ADF235C56E434207 -#define id8F5106DFABC14263ADF235C56E434207 - -#include "doorkeeper/primitivetypes.hpp" -#include "doorkeeper/implem/compatibility.h" -#include "doorkeeper/implem/vector.hpp" -#include "doorkeeper/implem/helpers.hpp" -#include "doorkeeper/mapreaders/mapstreambase.hpp" -#include -#include -#include -#include -#include - -namespace dk { - template - class TileMapData { - public: - typedef Vector coords; - - TileMapData ( void ) = delete; - explicit TileMapData ( std::unique_ptr>&& parStream ); - ~TileMapData ( void ) noexcept = default; - - void fetch ( std::vector& parOut, const coords& parFrom, const coords& parTo ); - const coords& mapSize ( void ) const; - - private: - const std::unique_ptr> m_mapstream; - }; -} //namespace dk - -#include "doorkeeper/implem/tilemapdata.inl" - -#endif diff --git a/include/doorkeeper/components/tyler.hpp b/include/doorkeeper/components/tyler.hpp index 4416a05..8c2be89 100644 --- a/include/doorkeeper/components/tyler.hpp +++ b/include/doorkeeper/components/tyler.hpp @@ -2,6 +2,7 @@ #define id6FB3FC97331449038D42AAAB4C01ABA1 #include "doorkeeper/components/layer.hpp" +#include "doorkeeper/components/exception.hpp" #include #include #include @@ -10,7 +11,7 @@ namespace dk { template - class TileMapData; + class BaseMapSource; template class Tyler { @@ -20,21 +21,21 @@ namespace dk { typedef typename LayerBase::coords coords; Tyler ( void ) = delete; - Tyler ( const coords& parSize, const coords& parTileSize ); + explicit Tyler ( const coords& parTileSize ); ~Tyler ( void ) noexcept = default; typename coords::value_type tiles_count ( void ) const; const coords& map_size ( void ) const { return m_size; } template - Layer& push_layer ( TileMapData& parTilemap ); + Layer& push_layer ( BaseMapSource* parTilemap ); template - Layer& push_layer ( TileMapData& parTilemap, const coords& parSubdiv ); + Layer& push_layer ( BaseMapSource* parTilemap, const coords& parSubdiv ); void preload ( const coords& parFrom, const coords& parTo ); private: - const coords m_size; + coords m_size; const coords m_tilesize; LayerList m_layers; }; diff --git a/include/doorkeeper/doorkeeper.hpp b/include/doorkeeper/doorkeeper.hpp index 7becd30..3119c68 100644 --- a/include/doorkeeper/doorkeeper.hpp +++ b/include/doorkeeper/doorkeeper.hpp @@ -5,6 +5,5 @@ #include "components/tyler.hpp" #include "components/viewport.hpp" #include "components/layer.hpp" -#include "components/tilemapdata.hpp" #endif diff --git a/include/doorkeeper/doorkeeper2d.hpp b/include/doorkeeper/doorkeeper2d.hpp new file mode 100644 index 0000000..4f35c63 --- /dev/null +++ b/include/doorkeeper/doorkeeper2d.hpp @@ -0,0 +1,13 @@ +#ifndef id6327FA76CFB44F65A459C69096EC65D5 +#define id6327FA76CFB44F65A459C69096EC65D5 + +#include "doorkeeper.hpp" + +namespace dk { + typedef Tyler<2>::coords coords2; + typedef Tyler<2> Tyler2d; + template using Layer2d = Layer; + typedef Viewport<2> Viewport2d; +} //namespace dk + +#endif diff --git a/include/doorkeeper/helpers/asciimapsource.hpp b/include/doorkeeper/helpers/asciimapsource.hpp index 2e8df6d..53ab6e6 100644 --- a/include/doorkeeper/helpers/asciimapsource.hpp +++ b/include/doorkeeper/helpers/asciimapsource.hpp @@ -3,16 +3,19 @@ #include "doorkeeper/primitivetypes.hpp" #include "doorkeeper/implem/vector.hpp" -#include +#include "doorkeeper/components/basemapsource.hpp" #include -#include // source_tag +#include namespace dkh { - class AsciiMapSource : public std::streambuf { + namespace implem { + typedef int AsciiMapTileType; + } //namespace implem + + class AsciiMapSource : public dk::BaseMapSource { public: typedef dk::Vector coords; - typedef int MapTileType; - typedef boost::iostreams::source_tag category; + typedef implem::AsciiMapTileType MapTileType; enum { MapDimensions = 2 }; @@ -20,22 +23,15 @@ namespace dkh { AsciiMapSource ( void ) = delete; AsciiMapSource ( const AsciiMapSource& ) = delete; AsciiMapSource ( AsciiMapSource&& parOther ) = default; - AsciiMapSource ( const char* parFilename, const coords& parSize ); - AsciiMapSource ( const std::string& parFilename, const coords& parSize ); - AsciiMapSource ( std::istream& parData, const coords& parSize ); - template - AsciiMapSource ( I parDataFrom, I parDataTo, const coords& parSize ); - ~AsciiMapSource ( void ) noexcept = 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 ); + virtual ~AsciiMapSource ( void ) noexcept = default; - const coords& mapSize ( void ) const noexcept { return m_mapSize; } - - protected: - virtual int_type underflow ( void ); - virtual int_type uflow ( void ); - virtual int_type pbackfail ( int_type parCh ); - virtual std::streamsize showmanyc ( void ); - virtual pos_type seekoff ( off_type parOff, std::ios_base::seekdir parDir, std::ios_base::openmode parWhich ); - virtual pos_type seekpos( pos_type parPos, std::ios_base::openmode parWhich ); + virtual const coords& mapSize ( void ) const; + virtual void fetch ( std::vector& parOut, const coords& parFrom, const coords& parTo ); + virtual dk::MapTypes mapType ( void ) const; + virtual int layersCount ( void ) const; private: enum { @@ -47,6 +43,7 @@ namespace dkh { std::vector m_wholedata; const coords m_mapSize; std::size_t m_bytepos; + const dk::MapTypes m_mapType; }; } //namespace dkh diff --git a/include/doorkeeper/implem/layer.inl b/include/doorkeeper/implem/layer.inl index 7e6117f..057fbf0 100644 --- a/include/doorkeeper/implem/layer.inl +++ b/include/doorkeeper/implem/layer.inl @@ -26,10 +26,11 @@ namespace dk { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- template - Layer::Layer (const coords& parTileSize, const coords& parMasterTileSize, TileMapData& parTilemap) : - LayerBase(parTilemap.mapSize(), parTileSize, parMasterTileSize), + Layer::Layer (const coords& parTileSize, const coords& parMasterTileSize, BaseMapSource* parTilemap) : + LayerBase(parTilemap->mapSize(), parTileSize, parMasterTileSize), m_tilemap(parTilemap) { + DK_ASSERT(m_tilemap); } ///-------------------------------------------------------------------------- @@ -51,7 +52,7 @@ namespace dk { template void Layer::onPreload (const coords& parFrom, const coords& parTo) { m_tiles.clear(); - m_tilemap.fetch(m_tiles, parFrom, parTo); + m_tilemap->fetch(m_tiles, parFrom, parTo); #if !defined(NDEBUG) std::cout << "Preloading layer from " << parFrom << " to " << parTo << '\n'; #endif diff --git a/include/doorkeeper/implem/mapstreambase.inl b/include/doorkeeper/implem/mapstreambase.inl deleted file mode 100644 index 77a61fa..0000000 --- a/include/doorkeeper/implem/mapstreambase.inl +++ /dev/null @@ -1,21 +0,0 @@ -namespace dk { - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - MapStreamBase::MapStreamBase (const coords& parMapSize) : - m_mapSize(parMapSize) - { - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - void MapStreamBase::read (char* parOut, std::size_t parOutSize, const coords& parFrom, const coords& parTo) { - DK_ASSERT(parOut); - DK_ASSERT(parOutSize > 0); - DK_ASSERT(this->isReadable()); - DK_ASSERT(parFrom < m_mapSize); - DK_ASSERT(parTo <= m_mapSize); - this->dataBlockRequested(parOut, parOutSize, parFrom, parTo); - } -} //namespace dk diff --git a/include/doorkeeper/implem/mapstreamraw.inl b/include/doorkeeper/implem/mapstreamraw.inl deleted file mode 100644 index 3c4ae47..0000000 --- a/include/doorkeeper/implem/mapstreamraw.inl +++ /dev/null @@ -1,74 +0,0 @@ -namespace dk { - namespace implem { - ///--------------------------------------------------------------------- - ///Only enabled if Device::MapDimensions is defined and is the same - ///as D. The function's name is set to give a hint to the user as to - ///why the error has occurred (they are trying to construct a - ///TileMapData with a set dimension that is different from the one - ///hardcoded in the device being passed in). The actual name could be - ///something like get_device_dimensions(). - ///--------------------------------------------------------------------- - template - inline - typename std::enable_if::type err_mismatching_dimension (const Device*) noexcept { - return Device::MapDimensions; - } - - ///--------------------------------------------------------------------- - ///Only enabled if Device::MapDimensions is not defined. The function's - ///name is set to give a hint to the user as to why the error has - ///occurred (they are trying to construct a TileMapData with a set - ///dimension that is different from the one hardcoded in the device - ///being passed in). The actual name could be something like - ///get_device_dimensions(). - ///--------------------------------------------------------------------- - template - inline - typename std::enable_if::result == false, uint32_t>::type err_mismatching_dimension (const Device*) noexcept { - return 0; - } - - ///--------------------------------------------------------------------- - ///--------------------------------------------------------------------- - template - inline - typename std::enable_if>::type retrieve_map_size_from_device (Device* parDevice) { - return parDevice->mapSize(); - } - - ///--------------------------------------------------------------------- - ///--------------------------------------------------------------------- - template - inline - typename std::enable_if::result == false, Vector>::type retrieve_map_size_from_device (Device* parDevice) { - return get_map_size_from_device(parDevice); - } - } //namespace implem - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - template - MapStreamRaw::MapStreamRaw (Device& parDevice) : - MapStreamBase(implem::retrieve_map_size_from_device(&parDevice)), - m_istream(&parDevice), - m_deviceHasDim(D == implem::err_mismatching_dimension(&parDevice)) - { - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - std::size_t MapStreamRaw::dataBlockRequested (char* parOut, std::size_t parOutSize, const coords& parFrom, const coords& parTo) { - DK_ASSERT(parOut); - - const std::size_t totalBlocks = implem::area(parTo - parFrom); - const std::size_t storableBlocks = parOutSize / sizeof(T); - const std::size_t readMem = sizeof(T) * std::min(storableBlocks, totalBlocks); - - const std::streamoff readPos = static_cast(implem::area(parFrom)); - m_istream.seekg(readPos, std::ios_base::beg); - m_istream.read(parOut, readMem); - return readMem; - } -} //namespace dk diff --git a/include/doorkeeper/implem/maptypes.hpp b/include/doorkeeper/implem/maptypes.hpp new file mode 100644 index 0000000..0edacc3 --- /dev/null +++ b/include/doorkeeper/implem/maptypes.hpp @@ -0,0 +1,13 @@ +#ifndef idBDAD2C64DEF94F1A8634605DF7CB1E7B +#define idBDAD2C64DEF94F1A8634605DF7CB1E7B + +namespace dk { + enum MapTypes { + MapType_IsometricStaggered, + MapType_Isometric, + MapType_Orthogonal, + MapType_Hex + }; +} //namespace dk + +#endif diff --git a/include/doorkeeper/implem/tilemapdata.inl b/include/doorkeeper/implem/tilemapdata.inl deleted file mode 100644 index 78474ed..0000000 --- a/include/doorkeeper/implem/tilemapdata.inl +++ /dev/null @@ -1,34 +0,0 @@ -namespace dk { - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - TileMapData::TileMapData (std::unique_ptr>&& parStream) : - m_mapstream(std::move(parStream)) - { - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - void TileMapData::fetch (std::vector& parOut, const coords& parFrom, const coords& parTo) { - DK_ASSERT(parFrom >= coords(0)); - DK_ASSERT(parFrom < parTo); - DK_ASSERT(parFrom < m_mapstream->mapSize()); - DK_ASSERT(parTo <= m_mapstream->mapSize()); - - const auto tileCount = implem::area(parTo - parFrom); - parOut.resize(tileCount); - m_mapstream->read( - reinterpret_cast(parOut.data()), - parOut.size() * sizeof(typename std::vector::value_type), - parFrom, - parTo); - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - template - const typename TileMapData::coords& TileMapData::mapSize() const { - return m_mapstream->mapSize(); - } -} //namespace dk diff --git a/include/doorkeeper/implem/tyler.inl b/include/doorkeeper/implem/tyler.inl index 54450f5..5f01f96 100644 --- a/include/doorkeeper/implem/tyler.inl +++ b/include/doorkeeper/implem/tyler.inl @@ -2,11 +2,10 @@ namespace dk { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- template - Tyler::Tyler (const coords& parSize, const coords& parTileSize) : - m_size(parSize), + Tyler::Tyler (const coords& parTileSize) : + m_size(0), m_tilesize(parTileSize) { - assert(m_size.x() > 0 and m_size.y() > 0); } ///-------------------------------------------------------------------------- @@ -24,21 +23,25 @@ namespace dk { ///-------------------------------------------------------------------------- template template - Layer& Tyler::push_layer (TileMapData& parTilemap) { - auto newLayer = new Layer(m_tilesize, m_tilesize, parTilemap); - DK_ASSERT(newLayer->mapSize() == m_size); - DK_ASSERT(newLayer->count() == m_size); - m_layers.push_back(LayerPtr(newLayer)); - return *newLayer; + Layer& Tyler::push_layer (BaseMapSource* parTilemap) { + return push_layer(parTilemap, coords(static_cast(1))); } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- template template - Layer& Tyler::push_layer (TileMapData& parTilemap, const coords& parSubdiv) { + Layer& Tyler::push_layer (BaseMapSource* parTilemap, const coords& parSubdiv) { auto newLayer = new Layer(m_tilesize / parSubdiv, m_tilesize, parTilemap); - DK_ASSERT(newLayer->mapSize() == m_size); + if (m_size == coords(0)) { + m_size = newLayer->mapSize(); + } + else { + DK_ASSERT(newLayer->mapSize() == m_size); + if (newLayer->mapSize() != m_size) { + throw SizeMismatchException(); + } + } DK_ASSERT(newLayer->count() == m_size * parSubdiv); m_layers.push_back(LayerPtr(newLayer)); return *newLayer; diff --git a/include/doorkeeper/mapreaders/mapstreambase.hpp b/include/doorkeeper/mapreaders/mapstreambase.hpp deleted file mode 100644 index 49292d1..0000000 --- a/include/doorkeeper/mapreaders/mapstreambase.hpp +++ /dev/null @@ -1,33 +0,0 @@ -#ifndef id3CAE7A32F0C3428EA125F91D261C0029 -#define id3CAE7A32F0C3428EA125F91D261C0029 - -#include "doorkeeper/primitivetypes.hpp" -#include "doorkeeper/implem/vector.hpp" -#include -#include - -namespace dk { - template - class MapStreamBase { - public: - typedef Vector coords; - - MapStreamBase ( void ) = delete; - explicit MapStreamBase ( const coords& parMapSize ); - virtual ~MapStreamBase ( void ) noexcept = default; - - virtual bool isReadable ( void ) const = 0; - virtual bool isWriteable ( void ) const = 0; - - const coords& mapSize ( void ) const noexcept { return m_mapSize; } - void read ( char* parOut, std::size_t parOutSize, const coords& parFrom, const coords& parTo ); - - private: - virtual std::size_t dataBlockRequested ( char* parOut, std::size_t parOutSize, const coords& parFrom, const coords& parTo ) = 0; - const coords m_mapSize; - }; -} //namespace dk - -#include "doorkeeper/implem/mapstreambase.inl" - -#endif diff --git a/include/doorkeeper/mapreaders/mapstreamraw.hpp b/include/doorkeeper/mapreaders/mapstreamraw.hpp deleted file mode 100644 index 74ce8c7..0000000 --- a/include/doorkeeper/mapreaders/mapstreamraw.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef id56E2396E102B4E10B20466D6B57A4C66 -#define id56E2396E102B4E10B20466D6B57A4C66 - -#include "doorkeeper/mapreaders/mapstreambase.hpp" -#include "doorkeeper/implem/helpers.hpp" -#include -#include -#include -#include -#include - -namespace dk { - namespace implem { - template - class HasMapDimensions { - class yes { char m; }; - class nope { yes m[2]; }; - static yes deduce ( int a, int b = T::MapDimensions ); - static nope deduce ( ... ); - public: - enum { - result = (sizeof(yes) == sizeof(deduce(1))) - }; - }; - - template - typename std::enable_if::type err_mismatching_dimension ( const Device* ) noexcept a_pure; - - template - typename std::enable_if::result == false, uint32_t>::type err_mismatching_dimension ( const Device* ) noexcept a_pure; - - template - typename std::enable_if>::type retrieve_map_size_from_device ( Device* parDevice ); - - template - typename std::enable_if::result == false, Vector>::type retrieve_map_size_from_device ( Device* parDevice ); - } //namespace implem - - //Customization point - specialize to enable map size retrieval for Device - template - typename Device::coords get_map_size_from_device ( const Device* parDevice ); - - template - class MapStreamRaw : public MapStreamBase { - public: - typedef typename MapStreamBase::coords coords; - - MapStreamRaw ( void ) = delete; - template - explicit MapStreamRaw ( Device& parDevice ); - virtual ~MapStreamRaw ( void ) noexcept = default; - - virtual bool isReadable ( void ) const { return true; } - virtual bool isWriteable ( void ) const { return false; } - - private: - virtual std::size_t dataBlockRequested ( char* parOut, std::size_t parOutSize, const coords& parFrom, const coords& parTo ); - - std::istream m_istream; - const bool m_deviceHasDim; - }; -} //namespace dk - -#include "doorkeeper/implem/mapstreamraw.inl" - -#endif diff --git a/src/asciimapsource.cpp b/src/asciimapsource.cpp index 2ca93f4..c174a38 100644 --- a/src/asciimapsource.cpp +++ b/src/asciimapsource.cpp @@ -8,21 +8,12 @@ #include namespace dkh { - namespace { - char extractByte ( AsciiMapSource::MapTileType parVal, std::size_t parByte ) a_pure; - - char extractByte (AsciiMapSource::MapTileType parVal, std::size_t parByte) { - return static_cast( - (parVal >> (parByte * 8)) & 0xFF - ); - } - } //unnamed namespace - ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- - AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize) : + AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize, dk::MapTypes parMapType) : m_mapSize(parSize), - m_bytepos(0) + m_bytepos(0), + m_mapType(parMapType) { std::ifstream src(parFilename); parse_map_data(src); @@ -30,9 +21,10 @@ namespace dkh { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- - AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize) : + AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize, dk::MapTypes parMapType) : m_mapSize(parSize), - m_bytepos(0) + m_bytepos(0), + m_mapType(parMapType) { std::ifstream src(parFilename); parse_map_data(src); @@ -40,53 +32,14 @@ namespace dkh { ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- - AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize) : + AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize, dk::MapTypes parMapType) : m_mapSize(parSize), - m_bytepos(0) + m_bytepos(0), + m_mapType(parMapType) { parse_map_data(parData); } - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - AsciiMapSource::int_type AsciiMapSource::underflow() { - if (m_bytepos == m_wholedata.size() * DataSize) - return traits_type::eof(); - - const auto index = m_bytepos / DataSize; - const auto byte = m_bytepos % DataSize; - return traits_type::to_int_type(extractByte(m_wholedata[index], byte)); - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - AsciiMapSource::int_type AsciiMapSource::uflow() { - if (m_bytepos == m_wholedata.size() * DataSize) - return traits_type::eof(); - - const auto retval = underflow(); - ++m_bytepos; - return retval; - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - AsciiMapSource::int_type AsciiMapSource::pbackfail (int_type parCh) { - if (0 == m_bytepos or (parCh != traits_type::eof() and parCh != traits_type::to_int_type(extractByte(m_wholedata[m_bytepos / DataSize], m_bytepos % DataSize)))) - return traits_type::eof(); - - --m_bytepos; - DK_ASSERT(underflow() == traits_type::to_int_type(parCh)); - return traits_type::to_int_type(parCh); - } - - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - std::streamsize AsciiMapSource::showmanyc() { - DK_ASSERT(m_bytepos <= m_wholedata.size() * DataSize); - return m_wholedata.size() * DataSize - m_bytepos; - } - ///------------------------------------------------------------------------- ///------------------------------------------------------------------------- void AsciiMapSource::parse_map_data (std::istream& parSrc) { @@ -128,65 +81,23 @@ namespace dkh { } } - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - AsciiMapSource::pos_type AsciiMapSource::seekoff (off_type parOff, std::ios_base::seekdir parDir, std::ios_base::openmode parWhich) { - using std::ios; - - if (parWhich & ios::out) - return traits_type::eof(); - - const std::size_t dataLength = DataSize * m_wholedata.size(); - if (parWhich & ios::in) { - std::size_t desiredPos; - - switch (parDir) { - case ios::beg: - desiredPos = static_cast(parOff); - break; - case ios::cur: - desiredPos = static_cast(m_bytepos + parOff); - break; - case ios::end: - desiredPos = static_cast(dataLength - parOff); - break; - default: - m_bytepos = static_cast(dataLength); - return traits_type::eof(); - } - if (desiredPos >= dataLength) { - m_bytepos = dataLength; - return traits_type::eof(); - } - else { - m_bytepos = desiredPos; - return static_cast(m_bytepos); - } - } - m_bytepos = dataLength; - return traits_type::eof(); + const AsciiMapSource::coords& AsciiMapSource::mapSize() const { + return m_mapSize; } - ///------------------------------------------------------------------------- - ///------------------------------------------------------------------------- - AsciiMapSource::pos_type AsciiMapSource::seekpos(pos_type parPos, std::ios_base::openmode parWhich) { - using std::ios; + void AsciiMapSource::fetch (std::vector& 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; + DK_ASSERT(to >= from); + parOut.resize(to - from); + std::copy(m_wholedata.begin() + from, m_wholedata.begin() + to, parOut.begin()); + } - if (parWhich & ios::out) - return traits_type::eof(); + dk::MapTypes AsciiMapSource::mapType() const { + return m_mapType; + } - const std::size_t dataLength = DataSize * m_wholedata.size(); - if (parWhich & ios::in) { - if (static_cast(parPos) >= dataLength) { - m_bytepos = dataLength; - return traits_type::eof(); - } - else { - m_bytepos = static_cast(parPos); - return static_cast(m_bytepos); - } - } - m_bytepos = static_cast(parPos); - return static_cast(m_bytepos); + int AsciiMapSource::layersCount() const { + return 1; } } //namespace dkh diff --git a/test/main.cpp b/test/main.cpp index d82cb3e..ae7d066 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -4,7 +4,6 @@ #include "platformstrings.h" #include "doorkeeper/helpers/asciimapsource.hpp" #include "doorkeeper/helpers/tilecoordinates.hpp" -#include "doorkeeper/mapreaders/mapstreamraw.hpp" #include #include #include @@ -18,8 +17,7 @@ template struct LayerWithData { - std::unique_ptr> tilemap; - std::unique_ptr device; + std::unique_ptr device; dk::Layer* layer; std::string path; }; @@ -46,7 +44,6 @@ namespace { int main() { typedef dk::Tyler<2>::coords coords2; - using dk::TileMapData; using dkh::AsciiMapSource; SDLSimple sdl_init; @@ -65,7 +62,7 @@ int main() { &SDL_DestroyRenderer ); - dk::Tyler<2> tiler(coords2(10, 8), coords2(64)); + dk::Tyler<2> tiler(coords2(64)); LayerWithData bottomLayer; addLayer(tiler, bottomLayer, DATA_PATH"/test.map"); @@ -148,10 +145,8 @@ namespace { void addLayer (dk::Tyler<2>& parTiler, LayerWithData& parLayerInfo, const char* parPath) { parLayerInfo.path = parPath; - parLayerInfo.device = std::unique_ptr(new dkh::AsciiMapSource(parLayerInfo.path, dkh::AsciiMapSource::coords(10, 8))); - std::unique_ptr> stream(new dk::MapStreamRaw(*parLayerInfo.device)); - parLayerInfo.tilemap = std::unique_ptr>(new dk::TileMapData(std::move(stream))); - parLayerInfo.layer = &parTiler.push_layer(*parLayerInfo.tilemap); + parLayerInfo.device.reset(new dkh::AsciiMapSource(parLayerInfo.path, dkh::AsciiMapSource::coords(10, 8), dk::MapType_IsometricStaggered)); + parLayerInfo.layer = &parTiler.push_layer(parLayerInfo.device.get()); } void printViewport (const dk::Viewport<2>& parView, const dk::Layer& parLayer) {