AsciiMapReader works on raw data, so it needs to know
the map size (2D only) from the outside.
This commit is contained in:
parent
eaca77c2de
commit
2d61a84c13
3 changed files with 29 additions and 15 deletions
|
@ -20,11 +20,11 @@ namespace dkh {
|
||||||
AsciiMapSource ( void ) = delete;
|
AsciiMapSource ( void ) = delete;
|
||||||
AsciiMapSource ( const AsciiMapSource& ) = delete;
|
AsciiMapSource ( const AsciiMapSource& ) = delete;
|
||||||
AsciiMapSource ( AsciiMapSource&& parOther ) = default;
|
AsciiMapSource ( AsciiMapSource&& parOther ) = default;
|
||||||
explicit AsciiMapSource ( const char* parFilename );
|
AsciiMapSource ( const char* parFilename, const coords& parSize );
|
||||||
explicit AsciiMapSource ( const std::string& parFilename );
|
AsciiMapSource ( const std::string& parFilename, const coords& parSize );
|
||||||
explicit AsciiMapSource ( std::istream& parData );
|
AsciiMapSource ( std::istream& parData, const coords& parSize );
|
||||||
template <typename I>
|
template <typename I>
|
||||||
AsciiMapSource ( I parDataFrom, I parDataTo );
|
AsciiMapSource ( I parDataFrom, I parDataTo, const coords& parSize );
|
||||||
~AsciiMapSource ( void ) noexcept = default;
|
~AsciiMapSource ( void ) noexcept = default;
|
||||||
|
|
||||||
const coords& mapSize ( void ) const noexcept { return m_mapSize; }
|
const coords& mapSize ( void ) const noexcept { return m_mapSize; }
|
||||||
|
@ -45,7 +45,7 @@ namespace dkh {
|
||||||
void parse_map_data ( std::istream& parSrc );
|
void parse_map_data ( std::istream& parSrc );
|
||||||
|
|
||||||
std::vector<MapTileType> m_wholedata;
|
std::vector<MapTileType> m_wholedata;
|
||||||
coords m_mapSize;
|
const coords m_mapSize;
|
||||||
std::size_t m_bytepos;
|
std::size_t m_bytepos;
|
||||||
};
|
};
|
||||||
} //namespace dkh
|
} //namespace dkh
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
namespace dkh {
|
namespace dkh {
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -19,24 +20,30 @@ namespace dkh {
|
||||||
|
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
AsciiMapSource::AsciiMapSource (const char* parFilename) {
|
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize) :
|
||||||
m_bytepos = 0;
|
m_mapSize(parSize),
|
||||||
|
m_bytepos(0)
|
||||||
|
{
|
||||||
std::ifstream src(parFilename);
|
std::ifstream src(parFilename);
|
||||||
parse_map_data(src);
|
parse_map_data(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
AsciiMapSource::AsciiMapSource (const std::string& parFilename) {
|
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize) :
|
||||||
m_bytepos = 0;
|
m_mapSize(parSize),
|
||||||
|
m_bytepos(0)
|
||||||
|
{
|
||||||
std::ifstream src(parFilename);
|
std::ifstream src(parFilename);
|
||||||
parse_map_data(src);
|
parse_map_data(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
AsciiMapSource::AsciiMapSource (std::istream& parData) {
|
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize) :
|
||||||
m_bytepos = 0;
|
m_mapSize(parSize),
|
||||||
|
m_bytepos(0)
|
||||||
|
{
|
||||||
parse_map_data(parData);
|
parse_map_data(parData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,16 +109,23 @@ namespace dkh {
|
||||||
throw std::runtime_error("Invalid data: can't parse file");
|
throw std::runtime_error("Invalid data: can't parse file");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
coords map_size;
|
||||||
if (grammar.lengths.empty()) {
|
if (grammar.lengths.empty()) {
|
||||||
m_mapSize.x() = m_mapSize.y() = 0;
|
map_size.x() = map_size.y() = 0;
|
||||||
throw std::runtime_error("Invalid data: can't guess map size");
|
throw std::runtime_error("Invalid data: can't guess map size");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Only look at the front. Besides a bug in spirit makes the other counts
|
//Only look at the front. Besides a bug in spirit makes the other counts
|
||||||
//invalid, see
|
//invalid, see
|
||||||
//http://boost.2283326.n4.nabble.com/Possible-bug-in-line-pos-iterator-td4636592.html
|
//http://boost.2283326.n4.nabble.com/Possible-bug-in-line-pos-iterator-td4636592.html
|
||||||
m_mapSize.x() = grammar.lengths.front();
|
map_size.x() = grammar.lengths.front();
|
||||||
m_mapSize.y() = m_wholedata.size() / m_mapSize.x();
|
map_size.y() = m_wholedata.size() / map_size.x();
|
||||||
|
if (map_size != m_mapSize) {
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << "Calculated map size is " << map_size <<
|
||||||
|
" which is different than the expected size " << m_mapSize;
|
||||||
|
throw std::runtime_error(oss.str());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///-------------------------------------------------------------------------
|
///-------------------------------------------------------------------------
|
||||||
|
|
|
@ -65,7 +65,7 @@ namespace {
|
||||||
|
|
||||||
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath) {
|
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* parPath) {
|
||||||
parLayerInfo.path = parPath;
|
parLayerInfo.path = parPath;
|
||||||
parLayerInfo.device = std::unique_ptr<dkh::AsciiMapSource>(new dkh::AsciiMapSource(parLayerInfo.path));
|
parLayerInfo.device = std::unique_ptr<dkh::AsciiMapSource>(new dkh::AsciiMapSource(parLayerInfo.path, dkh::AsciiMapSource::coords(10, 6)));
|
||||||
std::unique_ptr<dk::MapStreamRaw<int, 2>> stream(new dk::MapStreamRaw<int, 2>(*parLayerInfo.device));
|
std::unique_ptr<dk::MapStreamRaw<int, 2>> stream(new dk::MapStreamRaw<int, 2>(*parLayerInfo.device));
|
||||||
parLayerInfo.tilemap = std::unique_ptr<dk::TileMapData<int, 2>>(new dk::TileMapData<int, 2>(std::move(stream)));
|
parLayerInfo.tilemap = std::unique_ptr<dk::TileMapData<int, 2>>(new dk::TileMapData<int, 2>(std::move(stream)));
|
||||||
parLayerInfo.layer = &parTiler.push_layer(*parLayerInfo.tilemap);
|
parLayerInfo.layer = &parTiler.push_layer(*parLayerInfo.tilemap);
|
||||||
|
|
Loading…
Add table
Reference in a new issue