summaryrefslogtreecommitdiff
path: root/map.c
blob: b7344904b285634a118b731c17015d091eefe662 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "map.h"

#include <stdlib.h>

Map map_new(size_t width, size_t height) {
    Map map;
    map.width = 0;
    map.height = 0;

    map.tiles = malloc(sizeof(Pixel *) * height);
    if (map.tiles == NULL) return map;
    for (size_t row = 0; row < height; row++) {
        map.tiles[row] = malloc(sizeof(Pixel) * width);
        if (map.tiles[row] == NULL) {
            /* FIXME: free prev rows */
            map.tiles = NULL;
            return map;
        }
        for (size_t col = 0; col < width; col++) {
            map.tiles[row][col] = P_EMPTY;
        }
    }

    map.width = width;
    map.height = height;

    return map;
}

void map_free(Map *map) {
    if (map->tiles == NULL) goto null;

    for (size_t row = 0; row < map->height; row++)
        free(map->tiles[row]);

null:
    map->height = 0;
    map->width = 0;
    return;
}

Map map_default(void) {
    /* haha map map map */
    Map map = map_new(20, 30);

    /* Boundaries */
    Pixel wall = P_RED;
    wall.ch = '#';
    wall.fg.r *= 0.8;
    for (size_t x = 0; x < map.width; x++) { /* Horizontal */
        map.tiles[0][x] = wall;
        map.tiles[map.height - 1][x] = wall;
    }
    for (size_t y = 0; y < map.height; y++) { /* Vertical */
        map.tiles[y][0] = wall;
        map.tiles[y][map.width - 1] = wall;
    }

    /* Some random pillars */
    Pixel pillar = P_GREEN;
    pillar.ch = '|';
    pillar.fg.g *= 0.8;
    map.tiles[5][8]  = pillar;
    map.tiles[20][4] = pillar;
    map.tiles[9][11] = pillar;

    return map;
}