summaryrefslogtreecommitdiffstats
path: root/src/resources/map.c
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2013-03-03 00:53:51 (EST)
committer P. J. McDermott <pjm@nac.net>2013-03-03 00:53:51 (EST)
commit665e85b511496e0b1c6cbef5c65d4f5df2ba80ef (patch)
tree15ba83747ef42084dd709e55b464edb23120bb6c /src/resources/map.c
parent9995f492ca8a044a27726c0aa871f5f8c7d18076 (diff)
downloadoverworld-rpg-665e85b511496e0b1c6cbef5c65d4f5df2ba80ef.zip
overworld-rpg-665e85b511496e0b1c6cbef5c65d4f5df2ba80ef.tar.gz
overworld-rpg-665e85b511496e0b1c6cbef5c65d4f5df2ba80ef.tar.bz2
Support palette properties in maps.
Diffstat (limited to 'src/resources/map.c')
-rw-r--r--src/resources/map.c76
1 files changed, 74 insertions, 2 deletions
diff --git a/src/resources/map.c b/src/resources/map.c
index ea62608..667781d 100644
--- a/src/resources/map.c
+++ b/src/resources/map.c
@@ -2,6 +2,7 @@
#include <string.h>
#include <libgen.h>
#include <ctype.h>
+#include <inttypes.h>
#include <expat.h>
#include <config.h>
#include "map.h"
@@ -18,6 +19,7 @@ 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 **);
+static void XMLCALL tmx_map_property_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_tilesetemb_end(void *, const char *);
@@ -141,6 +143,22 @@ map_add_tileset(struct map *m, struct tileset *t, Uint32 firstgid)
m->tilesets_tail = mts;
}
+struct map_palette *
+map_get_palette(struct map *m, const char *name)
+{
+ if (strcmp(name, "morn") == 0) {
+ return &m->palettes[MAP_PALETTE_MORN];
+ } else if (strcmp(name, "day") == 0) {
+ return &m->palettes[MAP_PALETTE_DAY];
+ } else if (strcmp(name, "eve") == 0) {
+ return &m->palettes[MAP_PALETTE_EVE];
+ } else if (strcmp(name, "night") == 0) {
+ return &m->palettes[MAP_PALETTE_NIGHT];
+ } else {
+ return NULL;
+ }
+}
+
struct layer *
map_get_layer(struct map *m, const char *name)
{
@@ -315,8 +333,8 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
m = xml_node_peek(p);
if (xml_check_tag(name, "properties")) {
- /* Not used by engine. */
- xml_node_push(p, NULL, tmx_unused_start, tmx_unused_end, NULL);
+ xml_node_push(p, m, tmx_map_property_start, tmx_unused_end,
+ NULL);
} else if (xml_check_tag(name, "tileset")) {
dirname = m->dirname;
source = NULL;
@@ -377,6 +395,60 @@ tmx_map_el_start(void *pv, const char *name, const char **attr)
}
static void XMLCALL
+tmx_map_property_start(void *pv, const char *name, const char **attr)
+{
+ XML_Parser p = (XML_Parser) pv;
+ struct map *m;
+ char *attr_name;
+ char *attr_value;
+ char *path;
+ struct map_palette *pal;
+ Uint32 pal_h;
+ Uint32 pal_m;
+ char pal_name[256];
+
+#ifdef DEBUG_TMX
+ debug("<%s> (map property)", name);
+#endif
+
+ m = xml_node_peek(p);
+
+ if (xml_check_tag(name, "property")) {
+ xml_get_string_attr(p, attr, "name", &attr_name, 1);
+ xml_get_string_attr(p, attr, "value", &attr_value, 1);
+ if (strncmp(attr_name, "palette-", 7) == 0) {
+ pal = map_get_palette(m, attr_name + 8);
+ if (pal != NULL) {
+ /* Uint32 is overkill for pal_h and pal_m, but
+ * GCC complains about mismatched "%u" format
+ * modifiers and "Uint8 *"-type arguments. */
+ /* XXX: This won't handle file names with
+ * spaces. */
+ sscanf(attr_value, "%" PRIu32 ":%" PRIu32 ","
+ "%255s",
+ &pal_h, &pal_m, pal_name);
+ path = malloc(strlen(m->dirname) +
+ strlen(pal_name) + 2);
+ if (path == NULL) {
+ /* XXX: This won't run xml_node_push()
+ * and the stack will be corrupted. */
+ return;
+ }
+ sprintf(path, "%s/%s", m->dirname, pal_name);
+ pal->palette = palette_get(path);
+ pal->min = pal_h * 60 + pal_m;
+ free(path);
+ }
+ }
+ free(attr_name);
+ free(attr_value);
+ xml_node_push(p, NULL, tmx_invalid_start, tmx_unused_end, NULL);
+ } else {
+ xml_unexpected_start_tag(p, name, "property");
+ }
+}
+
+static void XMLCALL
tmx_tileset_start(void *pv, const char *name, const char **attr)
{
XML_Parser p = (XML_Parser) pv;