From 112ccc5d080a2f953e3e0af63f17294a8c7f73f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bend=C3=ADk?= Date: Sat, 1 Sep 2012 15:51:02 +0200 Subject: [PATCH] File colors.txt compiled into binary --- CMakeLists.txt | 25 +++++++++++++++++++-- TileGenerator.cpp | 57 ++++++++++++++++++++++++++--------------------- TileGenerator.h | 4 +++- 3 files changed, 58 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8324f61..d5a5a20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,11 +19,32 @@ find_path(LIBSQLITE3_INCLUDE_DIR sqlite3.h HINTS ${PC_LIBSQLITE_INCLUDEDIR} ${PC find_library(LIBSQLITE3_LIBRARY NAMES sqlite3 libsqlite3 HINTS ${PC_LIBSQLITE_LIBDIR} ${PC_LIBSQLITE_LIBRARY_DIRS}) set(LIBSQLITE3_LIBRARIES ${LIBSQLITE3_LIBRARY} ) -set(LIBSQLITE3_INCLUDE_DIRS ${LIBSQLITE3_INCLUDE_DIR} ) +set(LIBSQLITE3_INCLUDE_DIRS ${LIBSQLITE3_INCLUDE_DIR}) find_package_handle_standard_args(LibSqlite3 DEFAULT_MSG LIBSQLITE3_LIBRARY LIBSQLITE3_INCLUDE_DIR) -mark_as_advanced(LIBSQLITE3_INCLUDE_DIR LIBSQLITE3_LIBRARY ) +mark_as_advanced(LIBSQLITE3_INCLUDE_DIR LIBSQLITE3_LIBRARY) + +if (LIBSQLITE3_INCLUDE_DIR) +else (LIBSQLITE3_INCLUDE_DIR) + message(FATAL_ERROR "Could not find sqlite3") +endif (LIBSQLITE3_INCLUDE_DIR) + +find_program(XXD_EXECUTABLE xxd) + +if (XXD_EXECUTABLE) + message(STATUS "Found xxd") +else (XXD_EXECUTABLE) + message(FATAL_ERROR "Executable xxd not found") +endif (XXD_EXECUTABLE) + +add_custom_command( + OUTPUT colors.h + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/colors.txt colors.txt + COMMAND xxd -i colors.txt colors.h + DEPENDS colors.txt +) +set_property(SOURCE TileGenerator.cpp APPEND PROPERTY OBJECT_DEPENDS colors.h) include_directories( "${PROJECT_BINARY_DIR}" diff --git a/TileGenerator.cpp b/TileGenerator.cpp index 8ed0f0e..ce9caba 100644 --- a/TileGenerator.cpp +++ b/TileGenerator.cpp @@ -17,6 +17,7 @@ #include #include "config.h" #include "TileGenerator.h" +#include "colors.h" using namespace std; @@ -132,6 +133,9 @@ TileGenerator::TileGenerator(): m_zMin(0), m_zMax(0) { + string colors_txt_data(reinterpret_cast(colors_txt), colors_txt_len); + istringstream colors_stream(colors_txt_data); + parseColorsStream(colors_stream); } TileGenerator::~TileGenerator() @@ -203,32 +207,9 @@ void TileGenerator::parseColorsFile(const std::string &fileName) ifstream in; in.open(fileName.c_str(), ifstream::in); if (!in.is_open()) { - std::cerr << "File colors.txt does not exist" << std::endl; - exit(-2); - } - - while (in.good()) { - string name; - Color color; - in >> name; - if (name[0] == '#') { - in.ignore(65536, '\n'); - in >> name; - } - while (name == "\n" && in.good()) { - in >> name; - } - int r, g, b; - in >> r; - in >> g; - in >> b; - if (in.good()) { - color.r = r; - color.g = g; - color.b = b; - m_colors[name] = color; - } + return; } + parseColorsStream(in); } void TileGenerator::generate(const std::string &input, const std::string &output) @@ -255,6 +236,32 @@ void TileGenerator::generate(const std::string &input, const std::string &output printUnknown(); } +void TileGenerator::parseColorsStream(std::istream &in) +{ + while (in.good()) { + string name; + Color color; + in >> name; + if (name[0] == '#') { + in.ignore(65536, '\n'); + in >> name; + } + while (name == "\n" && in.good()) { + in >> name; + } + int r, g, b; + in >> r; + in >> g; + in >> b; + if (in.good()) { + color.r = r; + color.g = g; + color.b = b; + m_colors[name] = color; + } + } +} + void TileGenerator::openDb(const std::string &input) { string db_name = input + "map.sqlite"; diff --git a/TileGenerator.h b/TileGenerator.h index cedf31a..01bf1c5 100644 --- a/TileGenerator.h +++ b/TileGenerator.h @@ -11,12 +11,13 @@ #define TILEGENERATOR_H_JJNUCARH #include +#include #include #include +#include #include #include #include -#include #include "PixelAttributes.h" struct Color { @@ -89,6 +90,7 @@ public: void generate(const std::string &input, const std::string &output); private: + void parseColorsStream(std::istream &in); void openDb(const std::string &input); void loadBlocks(); BlockPos decodeBlockPos(sqlite3_int64 blockId) const;