Fix in asciimapsource
This commit is contained in:
parent
931acf3293
commit
d5c199f3b9
4 changed files with 13 additions and 91 deletions
|
@ -1,44 +0,0 @@
|
||||||
#ifndef id9B1C02049E474F6997D367932C4C2D21
|
|
||||||
#define id9B1C02049E474F6997D367932C4C2D21
|
|
||||||
|
|
||||||
#include "primitivetypes.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
#include <algorithm>
|
|
||||||
#include <memory>
|
|
||||||
#include <iterator>
|
|
||||||
|
|
||||||
//This code might need to be removed, but for now it's handy to read a simple
|
|
||||||
//ascii file into a vector without worrying too much about details.
|
|
||||||
//TODO: remove along with the code in the constructor
|
|
||||||
#include "implem/asciimap_parser.hpp"
|
|
||||||
#include <iomanip>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
namespace dk {
|
|
||||||
template <typename T, size_t D>
|
|
||||||
class TileStreamer {
|
|
||||||
public:
|
|
||||||
typedef Vector<CoordinateScalarType, D> coords;
|
|
||||||
typedef std::unique_ptr<std::istream> StreamPtr;
|
|
||||||
|
|
||||||
explicit TileStreamer ( StreamPtr&& parStream );
|
|
||||||
TileStreamer ( const TileStreamer& ) = delete;
|
|
||||||
TileStreamer ( TileStreamer&& ) = default;
|
|
||||||
~TileStreamer ( void ) noexcept = default;
|
|
||||||
|
|
||||||
TileStreamer& operator= ( const TileStreamer& ) = delete;
|
|
||||||
|
|
||||||
void copy ( std::vector<T>& parDest, const coords& parFrom, const coords& parTo );
|
|
||||||
const coords& tilesCount ( void ) const { return m_count; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
StreamPtr m_stream;
|
|
||||||
coords m_count;
|
|
||||||
std::vector<T> m_wholedata;
|
|
||||||
};
|
|
||||||
} //namespace dk
|
|
||||||
|
|
||||||
#include "implem/tilestreamer.inl"
|
|
||||||
|
|
||||||
#endif
|
|
|
@ -1,13 +1,18 @@
|
||||||
#ifndef id263B7DAF9A1C40BB8517D5D328DF8A1B
|
#ifndef id263B7DAF9A1C40BB8517D5D328DF8A1B
|
||||||
#define id263B7DAF9A1C40BB8517D5D328DF8A1B
|
#define id263B7DAF9A1C40BB8517D5D328DF8A1B
|
||||||
|
|
||||||
|
#include "primitivetypes.hpp"
|
||||||
|
#include "implem/vector.hpp"
|
||||||
#include <streambuf>
|
#include <streambuf>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <boost/iostreams/categories.hpp> // source_tag
|
||||||
|
|
||||||
namespace dkh {
|
namespace dkh {
|
||||||
class AsciiMapSource : std::streambuf {
|
class AsciiMapSource : public std::streambuf {
|
||||||
public:
|
public:
|
||||||
|
typedef dk::Vector<dk::CoordinateScalarType, 2> coords;
|
||||||
typedef int MapTileType;
|
typedef int MapTileType;
|
||||||
|
typedef boost::iostreams::source_tag category;
|
||||||
enum {
|
enum {
|
||||||
MapDimensions = 2
|
MapDimensions = 2
|
||||||
};
|
};
|
||||||
|
@ -22,13 +27,14 @@ namespace dkh {
|
||||||
AsciiMapSource ( I parDataFrom, I parDataTo );
|
AsciiMapSource ( I parDataFrom, I parDataTo );
|
||||||
~AsciiMapSource ( void ) noexcept = default;
|
~AsciiMapSource ( void ) noexcept = default;
|
||||||
|
|
||||||
|
const coords& mapSize ( void ) const noexcept { return m_mapSize; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void parse_map_data ( std::istream& parSrc );
|
void parse_map_data ( std::istream& parSrc );
|
||||||
|
|
||||||
std::vector<MapTileType> m_wholedata;
|
std::vector<MapTileType> m_wholedata;
|
||||||
int m_width;
|
coords m_mapSize;
|
||||||
int m_height;
|
|
||||||
};
|
};
|
||||||
} //namespace dk
|
} //namespace dkh
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
namespace dk {
|
|
||||||
template <typename T, size_t D>
|
|
||||||
TileStreamer<T, D>::TileStreamer (StreamPtr&& parStream) :
|
|
||||||
m_stream(std::move(parStream))
|
|
||||||
{
|
|
||||||
//TODO: this loader /only/ works with 2D tilesets, remove it asap
|
|
||||||
|
|
||||||
typedef std::istream_iterator<char> DataIterator;
|
|
||||||
typedef boost::spirit::multi_pass<DataIterator> WrappedIterator;
|
|
||||||
|
|
||||||
*m_stream >> std::noskipws;
|
|
||||||
|
|
||||||
auto first = WrappedIterator(DataIterator(*m_stream));
|
|
||||||
auto last = WrappedIterator(DataIterator());
|
|
||||||
implem::AsciiMapGrammar<WrappedIterator, T> grammar(first);
|
|
||||||
const bool p = boost::spirit::qi::parse(
|
|
||||||
first,
|
|
||||||
last,
|
|
||||||
grammar,
|
|
||||||
m_wholedata
|
|
||||||
);
|
|
||||||
|
|
||||||
if (not p or last != first) {
|
|
||||||
throw std::runtime_error("Invalid data: can't parse file");
|
|
||||||
}
|
|
||||||
|
|
||||||
//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_count.x() = grammar.lengths.front();
|
|
||||||
m_count.y() = m_wholedata.size() / m_count.x();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T, size_t D>
|
|
||||||
void TileStreamer<T, D>::copy (std::vector<T>& parDest, const coords& /*parFrom*/, const coords& /*parTo*/) {
|
|
||||||
parDest.clear();
|
|
||||||
parDest.reserve(m_wholedata.size());
|
|
||||||
std::copy(m_wholedata.begin(), m_wholedata.end(), std::back_inserter(parDest));
|
|
||||||
}
|
|
||||||
} //namespace dk
|
|
|
@ -60,7 +60,7 @@ namespace dkh {
|
||||||
//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_width = grammar.lengths.front();
|
m_mapSize.x() = grammar.lengths.front();
|
||||||
m_height = m_wholedata.size() / m_width;
|
m_mapSize.y() = m_wholedata.size() / m_mapSize.x();
|
||||||
}
|
}
|
||||||
} //namespace dk
|
} //namespace dkh
|
||||||
|
|
Loading…
Add table
Reference in a new issue