2012-08-22 15:49:45 +02:00
|
|
|
#include <cstdlib>
|
2012-08-23 12:30:09 +02:00
|
|
|
#include <getopt.h>
|
2016-08-09 17:31:56 +02:00
|
|
|
#include <fstream>
|
2012-08-22 15:49:45 +02:00
|
|
|
#include <iostream>
|
2012-08-23 12:30:09 +02:00
|
|
|
#include <map>
|
2012-08-22 16:04:43 +02:00
|
|
|
#include <string>
|
2012-11-24 19:25:13 +01:00
|
|
|
#include <sstream>
|
2014-03-09 12:32:13 +01:00
|
|
|
#include <stdexcept>
|
2016-08-09 17:31:56 +02:00
|
|
|
#include "cmake_config.h"
|
2012-08-23 12:46:22 +02:00
|
|
|
#include "TileGenerator.h"
|
2012-08-22 15:49:45 +02:00
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
2013-07-29 18:16:08 +02:00
|
|
|
const char *usage_text = "minetestmapper [options]\n"
|
|
|
|
" -i/--input <world_path>\n"
|
|
|
|
" -o/--output <output_image.png>\n"
|
|
|
|
" --bgcolor <color>\n"
|
|
|
|
" --scalecolor <color>\n"
|
|
|
|
" --playercolor <color>\n"
|
|
|
|
" --origincolor <color>\n"
|
|
|
|
" --drawscale\n"
|
|
|
|
" --drawplayers\n"
|
|
|
|
" --draworigin\n"
|
2014-04-03 20:32:48 +02:00
|
|
|
" --drawalpha\n"
|
2014-03-05 17:19:37 +01:00
|
|
|
" --noshading\n"
|
2014-03-05 21:41:27 +01:00
|
|
|
" --min-y <y>\n"
|
|
|
|
" --max-y <y>\n"
|
2014-07-11 10:54:50 +02:00
|
|
|
" --backend <backend>\n"
|
2013-07-29 18:16:08 +02:00
|
|
|
" --geometry x:y+w+h\n"
|
2016-07-06 21:45:38 +02:00
|
|
|
" --zoom <zoomlevel>\n"
|
2016-08-09 16:45:39 +02:00
|
|
|
" --colors <colors.txt>\n"
|
2013-07-29 18:16:08 +02:00
|
|
|
"Color format: '#000000'\n";
|
2012-08-22 15:49:45 +02:00
|
|
|
std::cout << usage_text;
|
|
|
|
}
|
|
|
|
|
2016-08-09 17:31:56 +02:00
|
|
|
bool file_exists(const std::string &path)
|
2016-08-09 16:45:39 +02:00
|
|
|
{
|
2016-08-09 17:31:56 +02:00
|
|
|
std::ifstream ifs(path.c_str());
|
|
|
|
return ifs.is_open();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string search_colors(const std::string &worldpath)
|
|
|
|
{
|
|
|
|
if(file_exists(worldpath + "/colors.txt"))
|
|
|
|
return worldpath + "/colors.txt";
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
char *home = std::getenv("HOME");
|
|
|
|
if(home) {
|
|
|
|
std::string check = ((std::string) home) + "/.minetest/colors.txt";
|
|
|
|
if(file_exists(check))
|
|
|
|
return check;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(!(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0') && file_exists(SHAREDIR "/colors.txt"))
|
|
|
|
return SHAREDIR "/colors.txt";
|
|
|
|
|
|
|
|
std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl;
|
2016-08-09 16:45:39 +02:00
|
|
|
return "colors.txt";
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:22:01 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2012-08-22 15:49:45 +02:00
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{"input", required_argument, 0, 'i'},
|
|
|
|
{"output", required_argument, 0, 'o'},
|
|
|
|
{"bgcolor", required_argument, 0, 'b'},
|
|
|
|
{"scalecolor", required_argument, 0, 's'},
|
|
|
|
{"origincolor", required_argument, 0, 'r'},
|
|
|
|
{"playercolor", required_argument, 0, 'p'},
|
2012-08-22 16:04:43 +02:00
|
|
|
{"draworigin", no_argument, 0, 'R'},
|
|
|
|
{"drawplayers", no_argument, 0, 'P'},
|
|
|
|
{"drawscale", no_argument, 0, 'S'},
|
2014-04-03 20:32:48 +02:00
|
|
|
{"drawalpha", no_argument, 0, 'e'},
|
2014-03-05 17:19:37 +01:00
|
|
|
{"noshading", no_argument, 0, 'H'},
|
2014-07-11 10:54:50 +02:00
|
|
|
{"backend", required_argument, 0, 'd'},
|
2012-11-24 19:25:13 +01:00
|
|
|
{"geometry", required_argument, 0, 'g'},
|
2014-03-05 18:06:05 +01:00
|
|
|
{"min-y", required_argument, 0, 'a'},
|
2016-07-06 21:45:38 +02:00
|
|
|
{"max-y", required_argument, 0, 'c'},
|
2016-08-09 16:45:39 +02:00
|
|
|
{"zoom", required_argument, 0, 'z'},
|
|
|
|
{"colors", required_argument, 0, 'C'},
|
2012-08-22 15:49:45 +02:00
|
|
|
};
|
2012-08-23 12:46:22 +02:00
|
|
|
|
2016-08-09 16:45:39 +02:00
|
|
|
std::string input;
|
|
|
|
std::string output;
|
|
|
|
std::string colors = "";
|
2012-08-22 16:04:43 +02:00
|
|
|
|
2012-08-23 12:46:22 +02:00
|
|
|
TileGenerator generator;
|
2012-08-22 15:49:45 +02:00
|
|
|
int option_index = 0;
|
2012-08-22 16:04:43 +02:00
|
|
|
int c = 0;
|
2012-08-22 15:49:45 +02:00
|
|
|
while (1) {
|
|
|
|
c = getopt_long(argc, argv, "hi:o:", long_options, &option_index);
|
|
|
|
if (c == -1) {
|
2012-08-22 16:04:43 +02:00
|
|
|
if (input.empty() || output.empty()) {
|
|
|
|
usage();
|
2014-03-05 18:06:05 +01:00
|
|
|
return 0;
|
2012-08-22 16:04:43 +02:00
|
|
|
}
|
2012-08-22 15:49:45 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-08-22 16:04:43 +02:00
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage();
|
2014-03-05 17:19:37 +01:00
|
|
|
return 0;
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
input = optarg;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
output = optarg;
|
|
|
|
break;
|
|
|
|
case 'b':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setBgColor(optarg);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 's':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setScaleColor(optarg);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 'r':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setOriginColor(optarg);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 'p':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setPlayerColor(optarg);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 'R':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setDrawOrigin(true);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 'P':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setDrawPlayers(true);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
|
|
|
case 'S':
|
2012-08-23 12:46:22 +02:00
|
|
|
generator.setDrawScale(true);
|
2012-08-22 16:04:43 +02:00
|
|
|
break;
|
2014-04-03 20:32:48 +02:00
|
|
|
case 'e':
|
2014-07-11 10:54:50 +02:00
|
|
|
generator.setDrawAlpha(true);
|
|
|
|
break;
|
2014-03-05 17:19:37 +01:00
|
|
|
case 'H':
|
|
|
|
generator.setShading(false);
|
|
|
|
break;
|
2014-07-11 10:54:50 +02:00
|
|
|
case 'd':
|
|
|
|
generator.setBackend(optarg);
|
|
|
|
break;
|
2014-03-05 18:06:05 +01:00
|
|
|
case 'a': {
|
2016-08-09 16:45:39 +02:00
|
|
|
std::istringstream iss;
|
2014-03-05 18:06:05 +01:00
|
|
|
iss.str(optarg);
|
|
|
|
int miny;
|
|
|
|
iss >> miny;
|
|
|
|
generator.setMinY(miny);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c': {
|
2016-08-09 16:45:39 +02:00
|
|
|
std::istringstream iss;
|
2014-03-05 18:06:05 +01:00
|
|
|
iss.str(optarg);
|
|
|
|
int maxy;
|
|
|
|
iss >> maxy;
|
|
|
|
generator.setMaxY(maxy);
|
|
|
|
}
|
|
|
|
break;
|
2012-11-24 19:25:13 +01:00
|
|
|
case 'g': {
|
2016-08-09 16:45:39 +02:00
|
|
|
std::istringstream geometry;
|
2012-11-24 19:25:13 +01:00
|
|
|
geometry.str(optarg);
|
|
|
|
int x, y, w, h;
|
|
|
|
char c;
|
|
|
|
geometry >> x >> c >> y >> w >> h;
|
|
|
|
if (geometry.fail() || c != ':' || w < 1 || h < 1) {
|
|
|
|
usage();
|
2014-03-05 18:06:05 +01:00
|
|
|
exit(1);
|
2012-11-24 19:25:13 +01:00
|
|
|
}
|
|
|
|
generator.setGeometry(x, y, w, h);
|
|
|
|
}
|
|
|
|
break;
|
2016-07-06 21:45:38 +02:00
|
|
|
case 'z': {
|
2016-08-09 16:45:39 +02:00
|
|
|
std::istringstream iss;
|
2016-07-06 21:45:38 +02:00
|
|
|
iss.str(optarg);
|
|
|
|
int zoom;
|
|
|
|
iss >> zoom;
|
|
|
|
generator.setZoom(zoom);
|
|
|
|
}
|
|
|
|
break;
|
2016-08-09 16:45:39 +02:00
|
|
|
case 'C':
|
|
|
|
colors = optarg;
|
|
|
|
break;
|
2012-08-22 16:04:43 +02:00
|
|
|
default:
|
2014-03-05 18:06:05 +01:00
|
|
|
exit(1);
|
2012-08-22 16:04:43 +02:00
|
|
|
}
|
2012-08-22 15:49:45 +02:00
|
|
|
}
|
2016-08-09 16:45:39 +02:00
|
|
|
if(colors == "")
|
2016-08-09 17:31:56 +02:00
|
|
|
colors = search_colors(input);
|
|
|
|
std::cerr << "is at " << colors << std::endl;
|
2014-03-05 17:19:37 +01:00
|
|
|
try {
|
2016-08-09 16:45:39 +02:00
|
|
|
generator.parseColorsFile(colors);
|
2014-03-05 17:19:37 +01:00
|
|
|
generator.generate(input, output);
|
|
|
|
} catch(std::runtime_error e) {
|
2016-08-09 16:45:39 +02:00
|
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
2014-03-05 17:19:37 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-08-22 15:22:01 +02:00
|
|
|
}
|