reorganizing tileset handling
authorJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 21 Sep 2021 20:45:36 +0000 (23:45 +0300)
committerJohn Tsiombikas <nuclear@member.fsf.org>
Tue, 21 Sep 2021 20:45:36 +0000 (23:45 +0300)
src/level.c
src/level.h
src/tileset.c [new file with mode: 0644]
src/tileset.h [new file with mode: 0644]
tools/mkdata

index 383ebb5..e741667 100644 (file)
@@ -216,7 +216,9 @@ err:
 
 static int load_tileset(struct level *lvl, struct ts_node *tsn)
 {
-       static const char *tile_types[] = {"empty", "straight", "corner", "door", 0};
+       static const char *tile_types[] = {
+               "open", "straight", "corner", "tee", "cross", "str2open", 0
+       };
 
        int i;
        char *path;
index dd1c988..3a31af6 100644 (file)
@@ -6,10 +6,12 @@
 #define DEF_CELL_SIZE  3.0f
 
 enum {
-       TILE_EMPTY,
-       TILE_STRAIGHT,
+       TILE_OPEN,
+       TILE_STR,
        TILE_CORNER,
-       TILE_DOOR
+       TILE_TEE,
+       TILE_CROSS,
+       TILE_STR2OPEN
 };
 
 enum {
@@ -21,7 +23,6 @@ enum {
 struct tile {
        char *name;
        int type;
-       struct scenefile scn;
        struct tile *next;
 };
 
@@ -44,7 +45,7 @@ struct level {
        float cell_size;
        struct cell *cells;
 
-       struct tile *tiles;
+       struct tileset *tset;
 
        /* meshes owned by the level, constructed during geometry generation or
         * loaded, excluding meshes in tiles scenefiles
diff --git a/src/tileset.c b/src/tileset.c
new file mode 100644 (file)
index 0000000..53a3d38
--- /dev/null
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <alloca.h>
+#include "treestore.h"
+#include "tileset.h"
+#include "level.h"
+
+int load_tileset(struct tileset *tset, const char *fname)
+{
+       struct ts_node *ts, *node;
+       const char *str;
+       char *path;
+
+       if(!(ts = ts_load(fname))) {
+               fprintf(stderr, "failed to load tileset: %s\n", fname);
+               return -1;
+       }
+       if(strcmp(ts->name, "tileset") != 0) {
+               fprintf(stderr, "invalid or corrupted tileset file: %s\n", fname);
+               ts_free_tree(ts);
+               return -1;
+       }
+
+       if(!(str = ts_get_attr_str(ts, "file", 0))) {
+               fprintf(stderr, "tileset %s is missing the file attribute\n", fname);
+               ts_free_tree(ts);
+               return -1;
+       }
+       path = alloca(strlen(fname) + strlen(str) + 2);
+       path_dir(str, path);
+       combine_path(path, str, path);
+
+       if(load_scenefile(&tset->scn, path) == -1) {
+               fprintf(stderr, "tileset %s: failed to load scene file: %s\n", fname, path);
+               ts_free_tree(ts);
+               return -1;
+       }
+
+       tset->name = strdup(ts_get_attr_str(ts, "name", fname));
+}
diff --git a/src/tileset.h b/src/tileset.h
new file mode 100644 (file)
index 0000000..82d4ec3
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef TILESET_H_
+#define TILESET_H_
+
+#include "scenefile.h"
+
+struct tile;
+
+struct tileset {
+       const char *name;
+
+       struct scenefile scn;   /* scene file containing tile geometry */
+       struct tile *tiles;
+};
+
+int load_tileset(struct tileset *tset, const char *fname);
+void destroy_tileset(struct tileset *tset);
+
+struct tileset *get_tileset(const char *name);
+
+#endif /* TILESET_H_ */
index 89b3b4c..728eb4f 100755 (executable)
@@ -2,10 +2,11 @@
 
 mkdir -p data/tex
 
+cp datasrc/test.tileset data
 cp datasrc/test.lvl data
 
-cp datasrc/dwall1.obj data
-cp datasrc/dwall1.mtl data
+cp datasrc/walls.obj data
+cp datasrc/walls.mtl data
 cp datasrc/tex/sfloor_norm.png data/tex
 cp datasrc/tex/sfloor1b_diff.jpg data/tex
 cp datasrc/tex/sfloor1b_spec.jpg data/tex