]> git.lizzy.rs Git - nothing.git/commitdiff
(#726) Introduce svg2level compiler
authorrexim <reximkut@gmail.com>
Sun, 17 Mar 2019 18:13:18 +0000 (01:13 +0700)
committerrexim <reximkut@gmail.com>
Sun, 17 Mar 2019 18:13:18 +0000 (01:13 +0700)
CMakeLists.txt
default.nix
devtools/svg2level/main.c [new file with mode: 0644]

index f029ba91fc59c056bc80ea9cc5270315eb9c969d..c7c53fe47d67f2632e1619af70ff20f0cedcf209 100644 (file)
@@ -11,6 +11,7 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
 
 find_package(SDL2 REQUIRED)
 find_package(SDL2_mixer REQUIRED)
+find_package(libxml2 REQUIRED)
 
 include_directories(${CMAKE_BINARY_DIR})
 include_directories(src/)
@@ -22,6 +23,8 @@ else()
 endif()
 
 include_directories(${SDL2_MIXER_INCLUDE_DIR})
+include_directories(${LIBXML2_INCLUDE_DIR})
+include_directories(${LIBXML2_INCLUDE_DIRS})
 
 add_library(system STATIC
   src/system/line_stream.c
@@ -143,6 +146,9 @@ add_executable(nothing
   src/game/level_metadata.c
 )
 
+add_executable(svg2level
+  devtools/svg2level/main.c)
+
 add_custom_command(
   OUTPUT broadcast_lisp.h
   COMMAND ${CMAKE_BINARY_DIR}/baker ${CMAKE_SOURCE_DIR}/src/broadcast.lisp broadcast_lisp.h broadcast_lisp_library
@@ -168,6 +174,7 @@ target_link_libraries(nothing_test ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY} m syste
 target_link_libraries(repl ${SDL2_LIBRARY} ${SDL2_MIXER_LIBRARY} m system ebisp)
 target_link_libraries(ebisp system)
 target_link_libraries(baker m system ebisp)
+target_link_libraries(svg2level ${LIBXML2_LIBRARIES})
 
 if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "CLANG"))
   set(CMAKE_C_FLAGS
index cf19de33b5ce7ba7bf55fbfa0539376b52f3d7c9..ad2b9edd5752249cb9a98e0f7c532622f77c97c7 100644 (file)
@@ -11,6 +11,7 @@ with import <nixpkgs> {}; {
                         racket
                         inotifyTools
                         python3
+                        libxml2
                       ];
     };
 }
diff --git a/devtools/svg2level/main.c b/devtools/svg2level/main.c
new file mode 100644 (file)
index 0000000..35ef964
--- /dev/null
@@ -0,0 +1,164 @@
+#include <stdio.h>
+#include <string.h>
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <libxml/xmlstring.h>
+
+#define RECTS_COUNT 1024
+#define TEXTS_COUNT 1024
+
+#ifdef LIBXML_TREE_ENABLED
+
+static void print_usage(FILE *stream)
+{
+    fprintf(stream, "Usage: ./svg2level <input.svg> <output.txt>");
+}
+
+static void save_title(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_background(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_player(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_platforms(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_goals(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_lavas(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_backplatforms(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_boxes(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_labels(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_script_regions(xmlNode *root, FILE *output_file)
+{
+    (void) root;
+    (void) output_file;
+}
+
+static void save_level(xmlNode *root, FILE *output_file)
+{
+    save_title(root, output_file);
+    save_background(root, output_file);
+    save_player(root, output_file);
+    save_platforms(root, output_file);
+    save_goals(root, output_file);
+    save_lavas(root, output_file);
+    save_backplatforms(root, output_file);
+    save_boxes(root, output_file);
+    save_labels(root, output_file);
+    save_script_regions(root, output_file);
+}
+
+static size_t svg_nodes(xmlNode *root, const xmlChar *node_name, xmlNode **rects, size_t n)
+{
+    (void) node_name;
+
+    if (n == 0) {
+        return 0;
+    }
+
+    const size_t old_n = n;
+
+    for (xmlNode *iter = root; n > 0 && iter; iter = iter->next) {
+        if (iter->type == XML_ELEMENT_NODE && xmlStrEqual(iter->name, node_name)) {
+            rects[0] = iter;
+            n--;
+            rects++;
+
+        }
+
+        const size_t m = svg_nodes(iter->children, node_name, rects, n);
+
+        n -= m;
+        rects += m;
+    }
+
+    return old_n - n;
+}
+
+int main(int argc, char *argv[])
+{
+    if (argc < 3) {
+        print_usage(stderr);
+        return -1;
+    }
+
+    LIBXML_TEST_VERSION
+
+    const char *input_filename = argv[1];
+    const char *output_filename = argv[2];
+
+    xmlDoc *doc = xmlReadFile(input_filename, NULL, 0);
+    if (doc == NULL) {
+        fprintf(stderr, "Could not parse file `%s`\n", input_filename);
+        return -1;
+    }
+
+    xmlNode *root = xmlDocGetRootElement(doc);
+
+    FILE *output_file = fopen(output_filename, "w");
+    if (output_file == NULL) {
+        fprintf(stderr, "Could not open file `%s`\n", output_filename);
+        return -1;
+    }
+
+    xmlNode **rects = calloc(RECTS_COUNT, sizeof(xmlNode*));
+    const size_t rects_count = svg_nodes(root, (const xmlChar*)"rect", rects, RECTS_COUNT);
+
+    xmlNode **texts = calloc(TEXTS_COUNT, sizeof(xmlNode*));
+    const size_t texts_count = svg_nodes(root, (const xmlChar*)"text", texts, TEXTS_COUNT);
+
+    save_level(root, output_file);
+
+    fclose(output_file);
+    xmlFreeDoc(doc);
+    xmlCleanupParser();
+
+    return 0;
+}
+
+#else
+int main(void) {
+    fprintf(stderr, "Tree support not compiled in\n");
+    exit(1);
+}
+#endif