added treestore and started a bit on the tilesets for the maze parts
[dosdemo] / src / tilemaze.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "tilemaze.h"
5 #include "mesh.h"
6 #include "treestor.h"
7 #include "dynarr.h"
8
9 struct object {
10         char *name;
11         struct g3d_mesh mesh;
12
13         void *texels;
14         int tex_width, tex_height;
15
16         struct object *next;
17 };
18
19 struct tile {
20         char *name;
21         struct object *objlist;
22 };
23
24 struct tilemaze {
25         struct tile *tileset;
26         int num_tiles;
27
28         struct object *objects;
29 };
30
31 void free_object(struct object *obj);
32
33 struct tilemaze *load_tilemaze(const char *fname)
34 {
35         struct tilemaze *tmz;
36
37         if(!(tmz = calloc(sizeof *tmz, 1))) {
38                 return 0;
39         }
40         return tmz;
41 }
42
43 void destroy_tilemaze(struct tilemaze *tmz)
44 {
45         if(!tmz) return;
46
47         free(tmz->tileset);
48
49         while(tmz->objects) {
50                 struct object *o = tmz->objects;
51                 tmz->objects = tmz->objects->next;
52
53                 free(o->mesh.varr);
54                 free(o->mesh.iarr);
55                 free(o);
56         }
57
58         free(tmz);
59 }
60
61 void update_tilemaze(struct tilemaze *tmz)
62 {
63 }
64
65 void draw_tilemaze(struct tilemaze *tmz)
66 {
67 }
68
69 struct tile *load_tileset(const char *fname, int *count)
70 {
71         int i;
72         struct tile *tiles = 0, *tile;
73         struct ts_node *ts = 0, *node;
74         struct ts_attr *attr;
75         struct object *obj;
76
77         if(!(ts = ts_load(fname))) {
78                 fprintf(stderr, "load_tileset: failed to load: %s\n", fname);
79                 goto err;
80         }
81         if(strcmp(ts->name, "tileset") != 0) {
82                 fprintf(stderr, "load_tileset: %s is not a tileset file\n", fname);
83                 goto err;
84         }
85
86         if(!(tiles = dynarr_alloc(sizeof *tiles, 0))) {
87                 fprintf(stderr, "load_tileset: failed to create tiles array\n");
88                 goto err;
89         }
90
91         node = ts->child_list;
92         while(node) {
93                 if(strcmp(node->name, "tile") != 0) {
94                         fprintf(stderr, "load_tileset: skipping unexpected node %s in %s\n", node->name, fname);
95                         node = node->next;
96                         continue;
97                 }
98
99                 if(!(tile = malloc(sizeof *tile))) {
100                         fprintf(stderr, "load_tileset: failed to allocate tile\n");
101                         goto err;
102                 }
103                 if(!(tile->name = malloc(strlen(node->name) + 1))) {
104                         fprintf(stderr, "load_tileset: failed to allocate tile name\n");
105                         free(tile);
106                         goto err;
107                 }
108                 strcpy(tile->name, node->name);
109                 tile->objlist = 0;
110
111                 attr = node->attr_list;
112                 while(attr) {
113                         /*
114                         if(strcmp(attr->name, "scn") == 0) {
115                                 struct object *last;
116                                 if(!(obj = load_objlist(attr->value.str, &last))) {
117                                         free(tile);
118                                         goto err;
119                                 }
120                                 last->next = tile->objlist;
121                                 tile->objlist = obj;
122                         }
123                         */
124                         attr = attr->next;
125                 }
126
127                 node = node->next;
128         }
129
130 err:
131         if(tiles) {
132                 for(i=0; i<dynarr_size(tiles); i++) {
133                         free(tiles[i].name);
134                         obj = tiles[i].objlist;
135                         while(obj) {
136                                 struct object *tmp = obj;
137                                 obj = obj->next;
138                                 free_object(obj);
139                         }
140                 }
141                 dynarr_free(tiles);
142         }
143         if(ts) ts_free_tree(ts);
144         return 0;
145 }
146
147 void free_object(struct object *obj)
148 {
149         if(!obj) return;
150
151         free(obj->name);
152         free(obj->texels);
153         destroy_mesh(&obj->mesh);
154
155         free(obj);
156 }