summaryrefslogtreecommitdiffstats
path: root/src/resources
diff options
context:
space:
mode:
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/map.c43
-rw-r--r--src/resources/map.h15
2 files changed, 38 insertions, 20 deletions
diff --git a/src/resources/map.c b/src/resources/map.c
index 67dbfd2..ea2144f 100644
--- a/src/resources/map.c
+++ b/src/resources/map.c
@@ -17,7 +17,7 @@ static void XMLCALL tmx_map_end(void *, const char *);
static void XMLCALL tmx_map_el_start(void *, const char *, const char **);
static void XMLCALL tmx_tileset_start(void *, const char *, const char **);
static void XMLCALL tmx_tileset_end(void *, const char *);
-static void XMLCALL tmx_tilesheetemb_end(void *, const char *);
+static void XMLCALL tmx_tilesetemb_end(void *, const char *);
static void XMLCALL tmx_tileset_el_start(void *, const char *, const char **);
static void XMLCALL tmx_image_end(void *, const char *);
static void XMLCALL tmx_unused_start(void *, const char *, const char **);
@@ -108,14 +108,25 @@ map_add_layer(struct map *m, struct layer *l)
}
void
-map_add_tileset(struct map *m, struct tileset *t)
+map_add_tileset(struct map *m, struct tileset *t, int firstgid)
{
+ struct map_tileset *mts;
+
+ mts = malloc(sizeof(*mts));
+ if (mts == NULL) {
+ return;
+ }
+
+ mts->tileset = t;
+ mts->firstgid = firstgid;
+ mts->next = NULL;
+
if (m->tilesets_head == NULL) {
- m->tilesets_head = t;
+ m->tilesets_head = mts;
} else {
- m->tilesets_tail->next = t;
+ m->tilesets_tail->next = mts;
}
- m->tilesets_tail = t;
+ m->tilesets_tail = mts;
}
static struct tileset *
@@ -227,6 +238,7 @@ static void XMLCALL
tmx_map_el_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
+ struct map *m;
char *dirname;
char *source;
char *path;
@@ -238,11 +250,12 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
/* Not used by engine. */
xml_node_push(p, NULL, tmx_unused_start, tmx_unused_end, NULL);
} else if (xml_check_tag(name, "tileset")) {
- dirname = ((struct map *) xml_node_peek(p))->dirname;
+ m = xml_node_peek(p);
+ dirname = m->dirname;
source = NULL;
xml_get_string_attr(p, attr, "source", &source, 0);
if (source != NULL) {
- /* External tilesheet. */
+ /* External tileset. */
path = malloc(strlen(dirname) + strlen(source) + 2);
if (path == NULL) {
return;
@@ -251,9 +264,9 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
ts = tileset_get(path, dirname);
free(source);
xml_node_push(p, ts, tmx_invalid_start,
- tmx_tilesheetemb_end, NULL);
+ tmx_tilesetemb_end, NULL);
} else {
- /* Embedded tilesheet. */
+ /* Embedded tileset. */
ts = resource_alloc("internal", sizeof(*ts));
if (ts == NULL) {
return;
@@ -265,9 +278,9 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
xml_get_int_attr(p, attr, "tileheight", &ts->tileheight,
1);
xml_node_push(p, ts, tmx_tileset_el_start,
- tmx_tilesheetemb_end, NULL);
+ tmx_tilesetemb_end, NULL);
}
- xml_get_int_attr(p, attr, "firstgid", &ts->firstgid, 1);
+ xml_get_int_attr(p, attr, "firstgid", &m->cur_ts_firstgid, 1);
} else {
xml_unexpected_start_tag(p, name,
"properties, tileset, layer, or objectgroup");
@@ -309,16 +322,18 @@ tmx_tileset_end(void *pv, const char *name)
}
static void XMLCALL
-tmx_tilesheetemb_end(void *pv, const char *name)
+tmx_tilesetemb_end(void *pv, const char *name)
{
XML_Parser p = (XML_Parser) pv;
struct tileset *ts;
+ struct map *m;
debug("</%s> (tileset)", name);
if (xml_check_tag(name, "tileset")) {
- ts = (struct tileset *) xml_node_pop(p);
- map_add_tileset((struct map *) xml_node_peek(p), ts);
+ ts = xml_node_pop(p);
+ m = xml_node_peek(p);
+ map_add_tileset(m, ts, m->cur_ts_firstgid);
} else {
xml_unexpected_end_tag(p, name, "tileset");
}
diff --git a/src/resources/map.h b/src/resources/map.h
index 673a01d..2926aa5 100644
--- a/src/resources/map.h
+++ b/src/resources/map.h
@@ -9,13 +9,15 @@
struct tileset {
struct resource res;
char *dirname;
- /* TODO: firstgid shouldn't apply to all uses of a tileset. */
- int firstgid;
char *name;
int tilewidth;
int tileheight;
struct image *image;
- struct tileset *next;
+};
+struct map_tileset {
+ struct tileset *tileset;
+ int firstgid;
+ struct map_tileset *next;
};
struct map {
struct resource res;
@@ -24,16 +26,17 @@ struct map {
int height;
int tilewidth;
int tileheight;
- struct tileset *tilesets_head;
- struct tileset *tilesets_tail;
+ struct map_tileset *tilesets_head;
+ struct map_tileset *tilesets_tail;
struct layer *layers_head;
struct layer *layers_tail;
Uint8 *collision;
+ int cur_ts_firstgid;
};
struct map *map_get(const char *path);
void map_free(struct map *map);
void map_add_layer(struct map *m, struct layer *l);
-void map_add_tileset(struct map *m, struct tileset *t);
+void map_add_tileset(struct map *m, struct tileset *t, int firstgid);
#endif