summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/resources/map.c76
-rw-r--r--src/resources/map.h18
2 files changed, 89 insertions, 5 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;
diff --git a/src/resources/map.h b/src/resources/map.h
index 2f4a60e..116300d 100644
--- a/src/resources/map.h
+++ b/src/resources/map.h
@@ -3,8 +3,16 @@
#include <SDL_stdinc.h>
#include "resource.h"
+#include "palette.h"
#include "image.h"
+enum map_palette_id {
+ MAP_PALETTE_MORN = 0,
+ MAP_PALETTE_DAY,
+ MAP_PALETTE_EVE,
+ MAP_PALETTE_NIGHT,
+ MAP_PALETTES_MAX
+};
enum map_layer {
MAP_LAYER_GROUND = 0,
MAP_LAYER_OBJ_LOW,
@@ -14,6 +22,10 @@ enum map_layer {
MAP_LAYER_WEATHER,
MAP_LAYERS_MAX
};
+struct map_palette {
+ struct palette *palette;
+ Uint16 min;
+};
struct tileset {
struct resource res;
char *dirname;
@@ -79,13 +91,12 @@ struct map_spawn {
struct map {
struct resource res;
char *dirname;
- /* TODO: map_get() should take a name, not a path.
- char *name;
- */
+ /* TODO: Consider making these Uint16s. */
int width;
int height;
int tilewidth;
int tileheight;
+ struct map_palette palettes[MAP_PALETTES_MAX];
struct map_tileset *tilesets_head;
struct map_tileset *tilesets_tail;
struct layer layers[MAP_LAYERS_MAX];
@@ -98,6 +109,7 @@ struct map {
struct map *map_get(const char *path);
void map_free(struct map *map);
void map_add_tileset(struct map *m, struct tileset *t, Uint32 firstgid);
+struct map_palette *map_get_palette(struct map *m, const char *name);
struct layer *map_get_layer(struct map *m, const char *name);
void map_add_exit(struct map *m, struct map_exit *e);