Add --noshading option, Fix SIGABRT because of uncaught exception when any exception occured

This commit is contained in:
Sfan5 2014-03-05 17:19:37 +01:00
parent 3be2d489e2
commit 33f323b1e3
3 changed files with 37 additions and 24 deletions

View File

@ -9,6 +9,7 @@
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <fstream>
#include <gdfontmb.h>
#include <iostream>
@ -66,7 +67,9 @@ static inline int readBlockContent(const unsigned char *mapData, int version, in
}
}
else {
throw VersionError();
std::ostringstream oss;
oss << "Unsupported map version " << version;
throw std::runtime_error(oss.str());
}
}
@ -91,13 +94,14 @@ TileGenerator::TileGenerator():
m_drawOrigin(false),
m_drawPlayers(false),
m_drawScale(false),
m_shading(true),
m_border(0),
m_db(0),
m_image(0),
m_xMin(0),
m_xMax(0),
m_zMin(0),
m_zMax(0),
m_xMin(INT_MAX),
m_xMax(INT_MIN),
m_zMin(INT_MAX),
m_zMax(INT_MIN),
m_geomX(-50),
m_geomY(-50),
m_geomX2(50),
@ -140,10 +144,10 @@ Color TileGenerator::parseColor(const std::string &color)
{
Color parsed;
if (color.length() != 7) {
throw ColorError();
throw std::runtime_error("Color not 7 characters long");
}
if (color[0] != '#') {
throw ColorError();
throw std::runtime_error("Color does not begin with #");
}
long col = strtol(color.c_str() + 1, NULL, 16);
parsed.b = col % 256;
@ -172,6 +176,11 @@ void TileGenerator::setDrawScale(bool drawScale)
}
}
void TileGenerator::setShading(bool shading)
{
m_shading = shading;
}
void TileGenerator::setGeometry(int x, int y, int w, int h)
{
if (x > 0) {
@ -268,8 +277,7 @@ void TileGenerator::openDb(const std::string &input)
{
string db_name = input + "map.sqlite";
if (sqlite3_open_v2(db_name.c_str(), &m_db, SQLITE_OPEN_READONLY | SQLITE_OPEN_PRIVATECACHE, 0) != SQLITE_OK) {
std::cerr << sqlite3_errmsg(m_db) << ", Database file: " << db_name << std::endl;
throw DbError();
throw std::runtime_error(std::string(sqlite3_errmsg(m_db)) + ", Database file: " + db_name);
}
}
@ -309,7 +317,7 @@ void TileGenerator::loadBlocks()
m_positions.unique();
}
else {
throw DbError();
throw std::runtime_error("Failed to get list of MapBlocks");
}
}
@ -339,7 +347,7 @@ void TileGenerator::renderMap()
sqlite3_stmt *statement;
string sql = "SELECT pos, data FROM blocks WHERE (pos >= ? AND pos <= ?)";
if (sqlite3_prepare_v2(m_db, sql.c_str(), sql.length(), &statement, 0) != SQLITE_OK) {
throw DbError();
throw std::runtime_error("Failed to get MapBlock");
}
std::list<int> zlist = getZValueList();
@ -454,6 +462,7 @@ void TileGenerator::renderMap()
}
}
}
if(m_shading)
renderShading(zPos);
}
}

View File

@ -56,15 +56,6 @@ struct BlockPos {
}
};
class DbError {
};
class ColorError {
};
class VersionError {
};
class TileGenerator
{
@ -84,6 +75,7 @@ public:
void setDrawOrigin(bool drawOrigin);
void setDrawPlayers(bool drawPlayers);
void setDrawScale(bool drawScale);
void setShading(bool shading);
void setGeometry(int x, int y, int w, int h);
void parseColorsFile(const std::string &fileName);
void generate(const std::string &input, const std::string &output);
@ -115,6 +107,7 @@ private:
bool m_drawOrigin;
bool m_drawPlayers;
bool m_drawScale;
bool m_shading;
int m_border;
sqlite3 *m_db;

View File

@ -29,6 +29,7 @@ void usage()
" --drawscale\n"
" --drawplayers\n"
" --draworigin\n"
" --noshading\n"
" --geometry x:y+w+h\n"
"Color format: '#000000'\n";
std::cout << usage_text;
@ -48,6 +49,7 @@ int main(int argc, char *argv[])
{"draworigin", no_argument, 0, 'R'},
{"drawplayers", no_argument, 0, 'P'},
{"drawscale", no_argument, 0, 'S'},
{"noshading", no_argument, 0, 'H'},
{"geometry", required_argument, 0, 'g'},
};
@ -63,14 +65,14 @@ int main(int argc, char *argv[])
if (c == -1) {
if (input.empty() || output.empty()) {
usage();
exit(-1);
return -1;
}
break;
}
switch (c) {
case 'h':
usage();
exit(0);
return 0;
break;
case 'i':
input = optarg;
@ -99,6 +101,9 @@ int main(int argc, char *argv[])
case 'S':
generator.setDrawScale(true);
break;
case 'H':
generator.setShading(false);
break;
case 'g': {
istringstream geometry;
geometry.str(optarg);
@ -116,5 +121,11 @@ int main(int argc, char *argv[])
abort();
}
}
try {
generator.generate(input, output);
} catch(std::runtime_error e) {
std::cout<<"Exception: "<<e.what()<<std::endl;
return 1;
}
return 0;
}