summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/resources/map.c29
-rw-r--r--src/resources/map.h3
2 files changed, 27 insertions, 5 deletions
diff --git a/src/resources/map.c b/src/resources/map.c
index e0c48c0..67dbfd2 100644
--- a/src/resources/map.c
+++ b/src/resources/map.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <string.h>
+#include <libgen.h>
#include <ctype.h>
#include <expat.h>
#include "map.h"
@@ -10,7 +11,7 @@
struct resource_table ts_res;
struct resource_table map_res;
-static struct tileset *tileset_get(const char *);
+static struct tileset *tileset_get(const char *, const char *);
static void XMLCALL tmx_map_start(void *, const char *, const char **);
static void XMLCALL tmx_map_end(void *, const char *);
static void XMLCALL tmx_map_el_start(void *, const char *, const char **);
@@ -44,6 +45,7 @@ map_get(const char *path)
if (map == NULL) {
return NULL;
}
+ map->dirname = dirname(strdup(path));
p = XML_ParserCreate(NULL);
if (p == NULL) {
@@ -117,7 +119,7 @@ map_add_tileset(struct map *m, struct tileset *t)
}
static struct tileset *
-tileset_get(const char *path)
+tileset_get(const char *path, const char *basedir)
{
struct tileset *ts;
XML_Parser p;
@@ -136,6 +138,7 @@ tileset_get(const char *path)
if (ts == NULL) {
return NULL;
}
+ ts->dirname = strdup(basedir);
p = XML_ParserCreate(NULL);
if (p == NULL) {
@@ -200,7 +203,6 @@ tmx_map_start(void *pv, const char *name, const char **attr)
xml_get_int_attr(p, attr, "height", &m->height, 1);
xml_get_int_attr(p, attr, "tilewidth", &m->tilewidth, 1);
xml_get_int_attr(p, attr, "tileheight", &m->tileheight, 1);
- debug("pushing node");
xml_node_push(p, m, tmx_map_el_start, tmx_map_end, NULL);
} else {
xml_unexpected_start_tag(p, name, "map");
@@ -225,7 +227,9 @@ static void XMLCALL
tmx_map_el_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
+ char *dirname;
char *source;
+ char *path;
struct tileset *ts;
debug("<%s> (map child)", name);
@@ -234,11 +238,17 @@ 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;
source = NULL;
xml_get_string_attr(p, attr, "source", &source, 0);
if (source != NULL) {
/* External tilesheet. */
- ts = tileset_get(source);
+ path = malloc(strlen(dirname) + strlen(source) + 2);
+ if (path == NULL) {
+ return;
+ }
+ sprintf(path, "%s/%s", dirname, source);
+ ts = tileset_get(path, dirname);
free(source);
xml_node_push(p, ts, tmx_invalid_start,
tmx_tilesheetemb_end, NULL);
@@ -248,6 +258,7 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
if (ts == NULL) {
return;
}
+ ts->dirname = dirname;
xml_get_string_attr(p, attr, "name", &ts->name, 1);
xml_get_int_attr(p, attr, "tilewidth", &ts->tilewidth,
1);
@@ -317,14 +328,22 @@ static void XMLCALL
tmx_tileset_el_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;
+ char *dirname;
char *source;
+ char *path;
struct image *img;
debug("<%s> (tileset child)", name);
if (xml_check_tag(name, "image")) {
+ dirname = ((struct tileset *) xml_node_peek(p))->dirname;
xml_get_string_attr(p, attr, "source", &source, 0);
- img = img_png_get(source);
+ path = malloc(strlen(dirname) + strlen(source) + 2);
+ if (path == NULL) {
+ return;
+ }
+ sprintf(path, "%s/%s", dirname, source);
+ img = img_png_get(path);
free(source);
xml_node_push(p, img, tmx_invalid_start, tmx_image_end, NULL);
} else {
diff --git a/src/resources/map.h b/src/resources/map.h
index 81ee0ec..673a01d 100644
--- a/src/resources/map.h
+++ b/src/resources/map.h
@@ -8,6 +8,8 @@
struct tileset {
struct resource res;
+ char *dirname;
+ /* TODO: firstgid shouldn't apply to all uses of a tileset. */
int firstgid;
char *name;
int tilewidth;
@@ -17,6 +19,7 @@ struct tileset {
};
struct map {
struct resource res;
+ char *dirname;
int width;
int height;
int tilewidth;