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