diff tools/tileset/main.cpp @ 182:3107ce017c3a

Misc: switch back to SDL Qt Quick and QML was an exciting experiment but it's definitely not enough flexible and easy to use for game development. Using SDL2 will let us focusing on our own drawing functions without any kind of overhead. While here, start massive cleanup.
author David Demelier <markand@malikania.fr>
date Fri, 19 Oct 2018 20:18:19 +0200
parents 4b292c20124c
children 61dd98874d82
line wrap: on
line diff
--- a/tools/tileset/main.cpp	Fri Oct 12 20:24:40 2018 +0200
+++ b/tools/tileset/main.cpp	Fri Oct 19 20:18:19 2018 +0200
@@ -44,36 +44,36 @@
  *
  * This function check for the two case and return the XML data.
  */
-ptree::ptree load_tileset(const fs::path& path)
+auto load_tileset(const fs::path& path) -> ptree::ptree
 {
-    ptree::ptree xml;
+	ptree::ptree xml;
 
-    ptree::read_xml(path.string(), xml);
+	ptree::read_xml(path.string(), xml);
 
-    if (path.extension() == ".tmx") {
-        auto tileset = xml.get_child_optional("map.tileset");
+	if (path.extension() == ".tmx") {
+		auto tileset = xml.get_child_optional("map.tileset");
 
-        if (!tileset)
-            throw std::runtime_error("no <tileset> node found");
+		if (!tileset)
+			throw std::runtime_error("no <tileset> node found");
 
-        // Check if it's external or embeded.
-        auto source = tileset->get_optional<std::string>("<xmlattr>.source");
+		// Check if it's external or embeded.
+		const auto source = tileset->get_optional<std::string>("<xmlattr>.source");
 
-        if (source) {
-            fs::path source_path(*source);
+		if (source) {
+			fs::path source_path(*source);
 
-            if (source_path.is_relative())
-                source_path = path.parent_path() / *source;
+			if (source_path.is_relative())
+				source_path = path.parent_path() / *source;
 
-            ptree::read_xml(source_path.string(), xml);
+			ptree::read_xml(source_path.string(), xml);
 
-            xml = xml.get_child("tileset");
-        } else
-            xml = *tileset;
-    } else
-        xml = xml.get_child("tileset");
+			xml = xml.get_child("tileset");
+		} else
+			xml = *tileset;
+	} else
+		xml = xml.get_child("tileset");
 
-    return xml;
+	return xml;
 }
 
 /*
@@ -85,26 +85,26 @@
  * The XML looks like this:
  *
  * <tile id="123">
- *      <properties>
- *          <property name="abc" value"def" />
- *      </properties>
+ *	  <properties>
+ *		  <property name="abc" value"def" />
+ *	  </properties>
  * </tile>
  */
-nlohmann::json read_tileset_properties(const ptree::ptree& tree)
+auto read_tileset_properties(const ptree::ptree& tree) -> nlohmann::json
 {
-    auto properties = tree.get_child_optional("properties");
-    auto json = nlohmann::json::object();
+	auto properties = tree.get_child_optional("properties");
+	auto json = nlohmann::json::object();
 
-    if (properties) {
-        for (const auto& prop : *properties) {
-            auto name = prop.second.get<std::string>("<xmlattr>.name");
-            auto value = prop.second.get<std::string>("<xmlattr>.value");
+	if (properties) {
+		for (const auto& prop : *properties) {
+			auto name = prop.second.get<std::string>("<xmlattr>.name");
+			auto value = prop.second.get<std::string>("<xmlattr>.value");
 
-            json[name] = value;
-        }
-    }
+			json[name] = value;
+		}
+	}
 
-    return json;
+	return json;
 }
 
 /*
@@ -117,7 +117,7 @@
                              const std::string& id,
                              const ptree::ptree& tree)
 {
-    tiles[id]["properties"] = read_tileset_properties(tree);
+	tiles[id]["properties"] = read_tileset_properties(tree);
 }
 
 /*
@@ -129,21 +129,21 @@
  * This function is used for rectangle and ellipses because they use the same
  * attributes.
  */
-nlohmann::json read_tileset_collision(const ptree::ptree& tree)
+auto read_tileset_collision(const ptree::ptree& tree) -> nlohmann::json
 {
-    auto ret = nlohmann::json::object({
-        { "x", tree.get<double>("<xmlattr>.x") },
-        { "y", tree.get<double>("<xmlattr>.y") },
-        { "width", tree.get<double>("<xmlattr>.width") },
-        { "height", tree.get<double>("<xmlattr>.height") }
-    });
+	auto ret = nlohmann::json::object({
+		{ "x", tree.get<double>("<xmlattr>.x") },
+		{ "y", tree.get<double>("<xmlattr>.y") },
+		{ "width", tree.get<double>("<xmlattr>.width") },
+		{ "height", tree.get<double>("<xmlattr>.height") }
+	});
 
-    if (tree.count("ellipse") > 0)
-        ret["type"] = "ellipse";
-    else
-        ret["type"] = "rectangle";
+	if (tree.count("ellipse") > 0)
+		ret["type"] = "ellipse";
+	else
+		ret["type"] = "rectangle";
 
-    return ret;
+	return ret;
 }
 
 /*
@@ -152,18 +152,18 @@
  *
  * Extract the collision description from a object of type polyline.
  */
-nlohmann::json read_tileset_collision_polyline(const ptree::ptree& tree)
+auto read_tileset_collision_polyline(const ptree::ptree& tree) -> nlohmann::json
 {
-    auto points = tree.get<std::string>("polyline.<xmlattr>.points");
-    auto array = nlohmann::json::array();
+	auto points = tree.get<std::string>("polyline.<xmlattr>.points");
+	auto array = nlohmann::json::array();
 
-    for (const auto& p : mlk::util::split(points, " "))
-        array.push_back(std::stod(p));
+	for (const auto& p : mlk::util::split(points, " "))
+		array.push_back(std::stod(p));
 
-    return {
-        { "type", "polyline" },
-        { "points", std::move(array) }
-    };
+	return {
+		{ "type", "polyline" },
+		{ "points", std::move(array) }
+	};
 }
 
 /*
@@ -177,32 +177,32 @@
                              const std::string& id,
                              const ptree::ptree& tree)
 {
-    auto objectgroup = tree.get_child_optional("objectgroup");
+	auto objectgroup = tree.get_child_optional("objectgroup");
 
-    if (!objectgroup)
-        return;
+	if (!objectgroup)
+		return;
 
-    auto array = nlohmann::json::array();
+	auto array = nlohmann::json::array();
 
-    for (const auto& obj : *objectgroup) {
-        if (obj.first != "object")
-            continue;
+	for (const auto& obj : *objectgroup) {
+		if (obj.first != "object")
+			continue;
 
-        nlohmann::json collision;
+		nlohmann::json collision;
 
-        /*
-         * Ellipse and rectangles are defined the same way, only check for
-         * polylines at the moment.
-         */
-        if (obj.second.count("polyline") > 0)
-            collision = read_tileset_collision_polyline(obj.second);
-        else
-            collision = read_tileset_collision(obj.second);
+		/*
+		 * Ellipse and rectangles are defined the same way, only check for
+		 * polylines at the moment.
+		 */
+		if (obj.second.count("polyline") > 0)
+			collision = read_tileset_collision_polyline(obj.second);
+		else
+			collision = read_tileset_collision(obj.second);
 
-        array.push_back(collision);
-    }
+		array.push_back(collision);
+	}
 
-    tiles[id]["collisions"] = std::move(array);
+	tiles[id]["collisions"] = std::move(array);
 }
 
 /*
@@ -213,63 +213,63 @@
  */
 void save_tileset(const fs::path& path, const ptree::ptree& xml)
 {
-    std::ofstream out(path.string(), std::ofstream::trunc);
+	std::ofstream out(path.string(), std::ofstream::trunc);
 
-    if (!out)
-        throw std::runtime_error(std::strerror(errno));
+	if (!out)
+		throw std::runtime_error(std::strerror(errno));
 
-    nlohmann::json json{
-        { "image", xml.get<std::string>("image.<xmlattr>.source") },
-        {
-            "cell", {
-                { "width", xml.get<int>("<xmlattr>.tilewidth"),     },
-                { "height", xml.get<int>("<xmlattr>.tileheight")    }
-            },
-        }
-    };
+	nlohmann::json json{
+		{ "image", xml.get<std::string>("image.<xmlattr>.source") },
+		{
+			"cell", {
+				{ "width", xml.get<int>("<xmlattr>.tilewidth"),	 },
+				{ "height", xml.get<int>("<xmlattr>.tileheight")	}
+			},
+		}
+	};
 
-    nlohmann::json tiles = nlohmann::json::object();
+	nlohmann::json tiles = nlohmann::json::object();
 
-    for (const auto& p : xml) {
-        if (p.first == "tile") {
-            auto id = p.second.get<std::string>("<xmlattr>.id");
+	for (const auto& p : xml) {
+		if (p.first == "tile") {
+			auto id = p.second.get<std::string>("<xmlattr>.id");
 
-            save_tileset_properties(tiles, id, p.second);
-            save_tileset_collisions(tiles, id, p.second);
-        }
-    }
+			save_tileset_properties(tiles, id, p.second);
+			save_tileset_collisions(tiles, id, p.second);
+		}
+	}
 
-    if (!tiles.empty())
-        json["tiles"] = tiles;
+	if (!tiles.empty())
+		json["tiles"] = tiles;
 
-    out << json.dump(4) << std::endl;
+	out << json.dump(4) << std::endl;
 }
 
 } // !namespace
 
 int main(int argc, char** argv)
 {
-    -- argc;
-    ++ argv;
+	-- argc;
+	++ argv;
 
-    if (argc < 1) {
-        std::cerr << "usage mlk-tileset input.tmx [output.json]" << std::endl;
-        std::cerr << "      mlk-tileset input.tsx [output.json]" << std::endl;
-        return 1;
-    }
+	if (argc < 1) {
+		std::cerr << "usage mlk-tileset input.tmx [output.json]" << std::endl;
+		std::cerr << "      mlk-tileset input.tsx [output.json]" << std::endl;
+		return 1;
+	}
 
-    try {
-        fs::path input(argv[0]);
-        fs::path output;
+	try {
+		fs::path input(argv[0]);
+		fs::path output;
 
-        if (argc >= 2)
-            output = argv[1];
-        else
-            output = fs::path(input).replace_extension(".json");
+		if (argc >= 2)
+			output = argv[1];
+		else
+			output = fs::path(input).replace_extension(".json");
 
-        save_tileset(output, load_tileset(input));
-    } catch (const std::exception& ex) {
-        std::cerr << ex.what() << std::endl;
-        return 1;
-    }
+		save_tileset(output, load_tileset(input));
+	} catch (const std::exception& ex) {
+		std::cerr << ex.what() << std::endl;
+		return 1;
+	}
 }