AsciiMapReader works on raw data, so it needs to know

the map size (2D only) from the outside.
This commit is contained in:
King_DuckZ 2015-05-21 09:52:45 +02:00
parent eaca77c2de
commit 2d61a84c13
3 changed files with 29 additions and 15 deletions

View file

@ -20,11 +20,11 @@ namespace dkh {
AsciiMapSource ( void ) = delete;
AsciiMapSource ( const AsciiMapSource& ) = delete;
AsciiMapSource ( AsciiMapSource&& parOther ) = default;
explicit AsciiMapSource ( const char* parFilename );
explicit AsciiMapSource ( const std::string& parFilename );
explicit AsciiMapSource ( std::istream& parData );
AsciiMapSource ( const char* parFilename, const coords& parSize );
AsciiMapSource ( const std::string& parFilename, const coords& parSize );
AsciiMapSource ( std::istream& parData, const coords& parSize );
template <typename I>
AsciiMapSource ( I parDataFrom, I parDataTo );
AsciiMapSource ( I parDataFrom, I parDataTo, const coords& parSize );
~AsciiMapSource ( void ) noexcept = default;
const coords& mapSize ( void ) const noexcept { return m_mapSize; }
@ -45,7 +45,7 @@ namespace dkh {
void parse_map_data ( std::istream& parSrc );
std::vector<MapTileType> m_wholedata;
coords m_mapSize;
const coords m_mapSize;
std::size_t m_bytepos;
};
} //namespace dkh

View file

@ -5,6 +5,7 @@
#include <iomanip>
#include <stdexcept>
#include <algorithm>
#include <sstream>
namespace dkh {
namespace {
@ -19,24 +20,30 @@ namespace dkh {
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const char* parFilename) {
m_bytepos = 0;
AsciiMapSource::AsciiMapSource (const char* parFilename, const coords& parSize) :
m_mapSize(parSize),
m_bytepos(0)
{
std::ifstream src(parFilename);
parse_map_data(src);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const std::string& parFilename) {
m_bytepos = 0;
AsciiMapSource::AsciiMapSource (const std::string& parFilename, const coords& parSize) :
m_mapSize(parSize),
m_bytepos(0)
{
std::ifstream src(parFilename);
parse_map_data(src);
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (std::istream& parData) {
m_bytepos = 0;
AsciiMapSource::AsciiMapSource (std::istream& parData, const coords& parSize) :
m_mapSize(parSize),
m_bytepos(0)
{
parse_map_data(parData);
}
@ -102,16 +109,23 @@ namespace dkh {
throw std::runtime_error("Invalid data: can't parse file");
}
coords map_size;
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");
}
//Only look at the front. Besides a bug in spirit makes the other counts
//invalid, see
//http://boost.2283326.n4.nabble.com/Possible-bug-in-line-pos-iterator-td4636592.html
m_mapSize.x() = grammar.lengths.front();
m_mapSize.y() = m_wholedata.size() / m_mapSize.x();
map_size.x() = grammar.lengths.front();
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());
}
}
///-------------------------------------------------------------------------

View file

@ -65,7 +65,7 @@ namespace {
void addLayer (dk::Tyler<2>& parTiler, LayerWithData<dkh::AsciiMapSource, int>& parLayerInfo, const char* 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));
parLayerInfo.tilemap = std::unique_ptr<dk::TileMapData<int, 2>>(new dk::TileMapData<int, 2>(std::move(stream)));
parLayerInfo.layer = &parTiler.push_layer(*parLayerInfo.tilemap);