Add map readings and movements based on map stuff
[dungeon1] / map.c
diff --git a/map.c b/map.c
new file mode 100644 (file)
index 0000000..4b948ff
--- /dev/null
+++ b/map.c
@@ -0,0 +1,46 @@
+#include "main.h"
+
+
+struct map *
+readmap(char *fname)
+{
+       struct map *map;
+       char buf[BUFSIZ];
+       FILE *fp;
+       int x, y;
+
+       map = calloc(1, sizeof(struct map));
+       map->name = strdup(fname);
+
+       snprintf(buf, sizeof(buf), "%s/%s", MAPDIR, fname);
+       if (!(fp = fopen(buf, "r"))) {
+               fprintf(stderr, "error fopen() %s\n", buf);
+               return NULL;
+       }
+
+       x = y = 0;
+       for (y=0; y<H; y++) {
+               for (x=0; x<W; x++) {
+                       map->map[y][x] = fgetc(fp);
+               }
+               if (fgetc(fp) != '\n') {
+                       fprintf(stderr, "error file format: no NL at y=%d x=%d\n", y, x);
+                       return NULL;
+               }
+       }
+       return map;
+}
+
+void
+showmap(struct map* map)
+{
+       int x, y;
+
+       mvprintw(0, 50, "map: %s", map->name);
+       for (y=T; y<H; y++) {
+               for (x=0; x<W; x++) {
+                       mvaddch(y, x, map->map[y][x]);
+               }
+       }
+}
+