Parsing of colors moved to TileGenerator.

This commit is contained in:
Miroslav Bendík 2012-08-23 12:55:31 +02:00
parent 3d54e5edaa
commit 957a72e2b4
4 changed files with 58 additions and 49 deletions

@ -3,6 +3,12 @@ cmake_minimum_required(VERSION 2.6)
cmake_policy(SET CMP0003 NEW) cmake_policy(SET CMP0003 NEW)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS_RELEASE "-std=c++0x -Wall -DNDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -std=c++0x -Wall -Wextra -DDEBUG")
endif(CMAKE_COMPILER_IS_GNUCXX)
find_package(PkgConfig) find_package(PkgConfig)
pkg_check_modules(PC_LIBSQLITE QUIET sqlite3) pkg_check_modules(PC_LIBSQLITE QUIET sqlite3)
set(LIBSQLITE3_DEFINITIONS ${PC_LIBSQLITE_CFLAGS_OTHER}) set(LIBSQLITE3_DEFINITIONS ${PC_LIBSQLITE_CFLAGS_OTHER})

@ -7,8 +7,13 @@
* ===================================================================== * =====================================================================
*/ */
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "TileGenerator.h" #include "TileGenerator.h"
using namespace std;
TileGenerator::TileGenerator(): TileGenerator::TileGenerator():
m_bgColor("#ffffff"), m_bgColor("#ffffff"),
m_scaleColor("#000000"), m_scaleColor("#000000"),
@ -70,3 +75,36 @@ void TileGenerator::generate(const std::string &/*input*/, const std::string &/*
{ {
} }
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()) {
m_colors[name] = color;
color.r = r;
color.g = g;
color.b = b;
}
}
}

@ -10,10 +10,21 @@
#ifndef TILEGENERATOR_H_JJNUCARH #ifndef TILEGENERATOR_H_JJNUCARH
#define TILEGENERATOR_H_JJNUCARH #define TILEGENERATOR_H_JJNUCARH
#include <stdint.h>
#include <string> #include <string>
#include <map>
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
};
class TileGenerator class TileGenerator
{ {
private:
typedef std::map<std::string, Color> ColorMap;
public: public:
TileGenerator(); TileGenerator();
~TileGenerator(); ~TileGenerator();
@ -26,6 +37,7 @@ public:
void setDrawScale(bool drawScale); void setDrawScale(bool drawScale);
void setDrawUnderground(bool drawUnderground); void setDrawUnderground(bool drawUnderground);
void generate(const std::string &input, const std::string &output); void generate(const std::string &input, const std::string &output);
void parseColorsFile(const std::string &fileName);
private: private:
std::string m_bgColor; std::string m_bgColor;
@ -36,6 +48,7 @@ private:
bool m_drawPlayers; bool m_drawPlayers;
bool m_drawScale; bool m_drawScale;
bool m_drawUnderground; bool m_drawUnderground;
ColorMap m_colors;
}; /* ----- end of class TileGenerator ----- */ }; /* ----- end of class TileGenerator ----- */
#endif /* end of include guard: TILEGENERATOR_H_JJNUCARH */ #endif /* end of include guard: TILEGENERATOR_H_JJNUCARH */

@ -8,61 +8,14 @@
*/ */
#include <cstdlib> #include <cstdlib>
#include <fstream>
#include <getopt.h> #include <getopt.h>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <stdint.h>
#include <string> #include <string>
#include "TileGenerator.h" #include "TileGenerator.h"
using namespace std; using namespace std;
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
};
typedef map<string, Color> ColorMap;
ColorMap parse_colors()
{
ColorMap parsed;
ifstream in;
in.open("colors.txt", 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()) {
parsed[name] = color;
color.r = r;
color.g = g;
color.b = b;
}
}
return parsed;
}
void usage() void usage()
{ {
const char *usage_text = "minetestmapper.py [options]\n\ const char *usage_text = "minetestmapper.py [options]\n\
@ -101,6 +54,7 @@ int main(int argc, char *argv[])
string output; string output;
TileGenerator generator; TileGenerator generator;
generator.parseColorsFile("colors.txt");
int option_index = 0; int option_index = 0;
int c = 0; int c = 0;
while (1) { while (1) {
@ -151,6 +105,4 @@ int main(int argc, char *argv[])
abort(); abort();
} }
} }
ColorMap colors = parse_colors();
} }