reorganizing tileset handling
[vrlugburz] / src / tileset.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <alloca.h>
5 #include "treestore.h"
6 #include "tileset.h"
7 #include "level.h"
8
9 int load_tileset(struct tileset *tset, const char *fname)
10 {
11         struct ts_node *ts, *node;
12         const char *str;
13         char *path;
14
15         if(!(ts = ts_load(fname))) {
16                 fprintf(stderr, "failed to load tileset: %s\n", fname);
17                 return -1;
18         }
19         if(strcmp(ts->name, "tileset") != 0) {
20                 fprintf(stderr, "invalid or corrupted tileset file: %s\n", fname);
21                 ts_free_tree(ts);
22                 return -1;
23         }
24
25         if(!(str = ts_get_attr_str(ts, "file", 0))) {
26                 fprintf(stderr, "tileset %s is missing the file attribute\n", fname);
27                 ts_free_tree(ts);
28                 return -1;
29         }
30         path = alloca(strlen(fname) + strlen(str) + 2);
31         path_dir(str, path);
32         combine_path(path, str, path);
33
34         if(load_scenefile(&tset->scn, path) == -1) {
35                 fprintf(stderr, "tileset %s: failed to load scene file: %s\n", fname, path);
36                 ts_free_tree(ts);
37                 return -1;
38         }
39
40         tset->name = strdup(ts_get_attr_str(ts, "name", fname));
41 }