10 static int match_prefix(const char *str, const char *prefix);
12 static struct tileset *tset_list;
14 int load_tileset(struct tileset *tset, const char *fname)
16 struct ts_node *ts, *node, *iter;
22 float xform[16], *vec;
24 if(!(ts = ts_load(fname))) {
25 fprintf(stderr, "failed to load tileset: %s\n", fname);
28 if(strcmp(ts->name, "tileset") != 0) {
29 fprintf(stderr, "invalid or corrupted tileset file: %s\n", fname);
34 if(!(str = ts_get_attr_str(ts, "file", 0))) {
35 fprintf(stderr, "tileset %s is missing the file attribute\n", fname);
39 path = alloca(strlen(fname) + strlen(str) + 2);
41 combine_path(path, str, path);
43 if(load_scenefile(&tset->scn, path) == -1) {
44 fprintf(stderr, "tileset %s: failed to load scene file: %s\n", fname, path);
49 tset->fname = strdup(fname);
50 tset->name = strdup(ts_get_attr_str(ts, "name", fname));
52 iter = ts->child_list;
56 if(strcmp(node->name, "tile") == 0) {
57 if(!(str = ts_get_attr_str(node, "prefix", 0))) {
61 if((type = tile_type(ts_get_attr_str(node, "type", 0))) == -1) {
62 fprintf(stderr, "load_tileset: missing or invalid tile type\n");
66 if(!(tile = malloc(sizeof *tile))) {
67 fprintf(stderr, "load_tileset: failed to allocate tile\n");
73 if((vec = ts_get_attr_vec(node, "pos", 0))) {
74 cgm_mtranslation(xform, vec[0], vec[1], vec[2]);
77 init_meshgroup(&tile->mgrp);
79 mesh = tset->scn.meshlist;
81 if(match_prefix(mesh->name, str)) {
83 xform_mesh(mesh, xform);
85 add_meshgroup_mesh(&tile->mgrp, mesh);
95 void destroy_tileset(struct tileset *tset)
104 tset->tiles = tile->next;
110 destroy_scenefile(&tset->scn);
113 struct tileset *get_tileset(const char *fname)
115 struct tileset *ts = tset_list;
117 if(strcmp(ts->fname, fname) == 0) {
123 if(!(ts = malloc(sizeof *ts))) {
124 fprintf(stderr, "failed to allocate tileset\n");
127 if(load_tileset(ts, fname) == -1) {
131 ts->next = tset_list;
136 void free_all_tilesets(void)
142 tset_list = ts->next;
148 struct tile *get_tile(struct tileset *tset, int ttype)
150 struct tile *tile = tset->tiles;
152 if(tile->type == ttype) {
160 int tile_type(const char *tstr)
162 static const char *typenames[] = {
163 "open", "straight", "corner", "tee", "cross", "str2open", "stropen", 0
169 for(i=0; typenames[i]; i++) {
170 if(strcmp(tstr, typenames[i]) == 0) {
177 static int match_prefix(const char *str, const char *prefix)
179 while(*str && *prefix) {
180 if(*str++ != *prefix++) {