Add map readings and movements based on map stuff
[dungeon1] / map.c
1 #include "main.h"
2
3
4 struct map *
5 readmap(char *fname)
6 {
7         struct map *map;
8         char buf[BUFSIZ];
9         FILE *fp;
10         int x, y;
11
12         map = calloc(1, sizeof(struct map));
13         map->name = strdup(fname);
14
15         snprintf(buf, sizeof(buf), "%s/%s", MAPDIR, fname);
16         if (!(fp = fopen(buf, "r"))) {
17                 fprintf(stderr, "error fopen() %s\n", buf);
18                 return NULL;
19         }
20
21         x = y = 0;
22         for (y=0; y<H; y++) {
23                 for (x=0; x<W; x++) {
24                         map->map[y][x] = fgetc(fp);
25                 }
26                 if (fgetc(fp) != '\n') {
27                         fprintf(stderr, "error file format: no NL at y=%d x=%d\n", y, x);
28                         return NULL;
29                 }
30         }
31         return map;
32 }
33
34 void
35 showmap(struct map* map)
36 {
37         int x, y;
38
39         mvprintw(0, 50, "map: %s", map->name);
40         for (y=T; y<H; y++) {
41                 for (x=0; x<W; x++) {
42                         mvaddch(y, x, map->map[y][x]);
43                 }
44         }
45 }
46