2D ascii tile loader refactored out of TileStreamer.

This also adds a static lib part to DoorKeeper that will only contain helper functions.
This commit is contained in:
King_DuckZ 2014-12-22 16:59:09 +01:00
parent 59801755cc
commit 931acf3293
6 changed files with 135 additions and 14 deletions

10
src/CMakeLists.txt Normal file
View file

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(doorkeeper CXX)
include_directories(
.
)
add_library(${PROJECT_NAME}
asciimapsource.cpp
)

View file

@ -11,7 +11,7 @@
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_stl.hpp>
namespace dk { namespace implem {
namespace dkh { namespace implem {
struct get_line_f {
template <typename> struct result { typedef size_t type; };
template <typename I> size_t operator()(const I& parStart, const I& parPosIter) const {
@ -49,7 +49,7 @@ namespace dk { namespace implem {
}
}} //namespace dk::implem
}} //namespace dkh::implem
#undef BOOST_SPIRIT_USE_PHOENIX_V3

66
src/asciimapsource.cpp Normal file
View file

@ -0,0 +1,66 @@
#include "helpers/asciimapsource.hpp"
#include "asciimap_parser.hpp"
#include <fstream>
#include <iomanip>
#include <stdexcept>
#include <algorithm>
namespace dkh {
namespace {
} //unnamed namespace
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const char* parFilename) {
std::ifstream src(parFilename);
parse_map_data(src);
char* const base = reinterpret_cast<char*>(&m_wholedata.front());
setg(base, base, base + m_wholedata.size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (const std::string& parFilename) {
std::ifstream src(parFilename);
parse_map_data(src);
char* const base = reinterpret_cast<char*>(&m_wholedata.front());
setg(base, base, base + m_wholedata.size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
AsciiMapSource::AsciiMapSource (std::istream& parData) {
parse_map_data(parData);
char* const base = reinterpret_cast<char*>(&m_wholedata.front());
setg(base, base, base + m_wholedata.size());
}
///-------------------------------------------------------------------------
///-------------------------------------------------------------------------
void AsciiMapSource::parse_map_data (std::istream& parSrc) {
typedef std::istream_iterator<char> DataIterator;
typedef boost::spirit::multi_pass<DataIterator> WrappedIterator;
parSrc >> std::noskipws;
auto first = WrappedIterator(DataIterator(parSrc));
auto last = WrappedIterator(DataIterator());
implem::AsciiMapGrammar<WrappedIterator, MapTileType> 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_width = grammar.lengths.front();
m_height = m_wholedata.size() / m_width;
}
} //namespace dk