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

View file

@ -1,5 +1,4 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(doorkeeper CXX)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++11 -g -O0 -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -O3 -Wall -Wextra")
@ -7,22 +6,13 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++11 -O3 -Wall -We
find_library(Boost 1.21.0 REQUIRED)
configure_file(
"${CMAKE_SOURCE_DIR}/src/${PROJECT_NAME}Config.h.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.h"
)
include_directories(SYSTEM
${Boost_INCLUDE_DIRECTORIES}
)
include_directories(
src/
include/
"${PROJECT_BINARY_DIR}"
)
add_executable(${PROJECT_NAME}
src/main.cpp
src/doorkeeper.cpp
)
add_subdirectory(src)
add_subdirectory(test)

View file

@ -0,0 +1,34 @@
#ifndef id263B7DAF9A1C40BB8517D5D328DF8A1B
#define id263B7DAF9A1C40BB8517D5D328DF8A1B
#include <streambuf>
#include <vector>
namespace dkh {
class AsciiMapSource : std::streambuf {
public:
typedef int MapTileType;
enum {
MapDimensions = 2
};
AsciiMapSource ( void ) = delete;
AsciiMapSource ( const AsciiMapSource& ) = default;
AsciiMapSource ( AsciiMapSource&& parOther ) = delete;
explicit AsciiMapSource ( const char* parFilename );
explicit AsciiMapSource ( const std::string& parFilename );
explicit AsciiMapSource ( std::istream& parData );
template <typename I>
AsciiMapSource ( I parDataFrom, I parDataTo );
~AsciiMapSource ( void ) noexcept = default;
private:
void parse_map_data ( std::istream& parSrc );
std::vector<MapTileType> m_wholedata;
int m_width;
int m_height;
};
} //namespace dk
#endif

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

21
test/CMakeLists.txt Normal file
View file

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(doorkeepertest CXX)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.h.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.h"
)
include_directories(
.
"${CMAKE_CURRENT_BINARY_DIR}"
)
add_executable(${PROJECT_NAME}
doorkeeper.cpp
main.cpp
)
target_link_libraries(${PROJECT_NAME}
doorkeeper
)