X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=dosdemo;a=blobdiff_plain;f=src%2Ftilemaze.c;h=7407bd48c128f5fee6ac33d2c4b90e4b389f859d;hp=1d413d940b4149c4ed60aa43750091497a698754;hb=ae96c724e905181ca10441a63b1dfe9c2fc5d2ea;hpb=091586fb9501d613f621fceb70fa21ff97c4898e diff --git a/src/tilemaze.c b/src/tilemaze.c index 1d413d9..7407bd4 100644 --- a/src/tilemaze.c +++ b/src/tilemaze.c @@ -1,6 +1,10 @@ +#include #include +#include #include "tilemaze.h" #include "mesh.h" +#include "treestor.h" +#include "dynarr.h" struct object { char *name; @@ -13,6 +17,7 @@ struct object { }; struct tile { + char *name; struct object *objlist; }; @@ -23,6 +28,7 @@ struct tilemaze { struct object *objects; }; +void free_object(struct object *obj); struct tilemaze *load_tilemaze(const char *fname) { @@ -31,7 +37,6 @@ struct tilemaze *load_tilemaze(const char *fname) if(!(tmz = calloc(sizeof *tmz, 1))) { return 0; } - /* TODO */ return tmz; } @@ -60,3 +65,92 @@ void update_tilemaze(struct tilemaze *tmz) void draw_tilemaze(struct tilemaze *tmz) { } + +struct tile *load_tileset(const char *fname, int *count) +{ + int i; + struct tile *tiles = 0, *tile; + struct ts_node *ts = 0, *node; + struct ts_attr *attr; + struct object *obj; + + if(!(ts = ts_load(fname))) { + fprintf(stderr, "load_tileset: failed to load: %s\n", fname); + goto err; + } + if(strcmp(ts->name, "tileset") != 0) { + fprintf(stderr, "load_tileset: %s is not a tileset file\n", fname); + goto err; + } + + if(!(tiles = dynarr_alloc(sizeof *tiles, 0))) { + fprintf(stderr, "load_tileset: failed to create tiles array\n"); + goto err; + } + + node = ts->child_list; + while(node) { + if(strcmp(node->name, "tile") != 0) { + fprintf(stderr, "load_tileset: skipping unexpected node %s in %s\n", node->name, fname); + node = node->next; + continue; + } + + if(!(tile = malloc(sizeof *tile))) { + fprintf(stderr, "load_tileset: failed to allocate tile\n"); + goto err; + } + if(!(tile->name = malloc(strlen(node->name) + 1))) { + fprintf(stderr, "load_tileset: failed to allocate tile name\n"); + free(tile); + goto err; + } + strcpy(tile->name, node->name); + tile->objlist = 0; + + attr = node->attr_list; + while(attr) { + /* + if(strcmp(attr->name, "scn") == 0) { + struct object *last; + if(!(obj = load_objlist(attr->value.str, &last))) { + free(tile); + goto err; + } + last->next = tile->objlist; + tile->objlist = obj; + } + */ + attr = attr->next; + } + + node = node->next; + } + +err: + if(tiles) { + for(i=0; inext; + free_object(obj); + } + } + dynarr_free(tiles); + } + if(ts) ts_free_tree(ts); + return 0; +} + +void free_object(struct object *obj) +{ + if(!obj) return; + + free(obj->name); + free(obj->texels); + destroy_mesh(&obj->mesh); + + free(obj); +}