aboutsummaryrefslogtreecommitdiff
path: root/src/tiles.c
blob: cecee5a9ff18bd81a80aac4825f2dcc211b7d708 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include "typedef.h"
#include "tiles.h"
#include "ppm.h"

extern size_t TILES;

extern size_t TILE_WIDTH;
extern size_t TILE_HEIGHT;

small_t *(tiles[TILES_CAP]);
small_t tile_connections[TILES_CAP];

static int rotate(int x, int y, int r, int w)
{
    switch(r) {
    case 0: // no roatate
        return y * w + x;
    case 1: // right
        return (w*w - w) + y - (x * w);
    case 2: // left
        return (w-1) - y + (x * w);
    case 3: // down
        return (w*w-1) - (y * w) - x;
    }

    return -1;
}

static void rotate_tiles(char *t, int i, int r)
{
    tiles[i] = malloc(TILE_WIDTH * TILE_HEIGHT * 3);

    for(size_t y = 0; y < TILE_HEIGHT; y++)
        for(size_t x = 0; x < TILE_WIDTH; x++)
            for(int k = 0; k < 3; k++)
                tiles[i][(y * TILE_WIDTH + x)*3 + k] = t[(rotate(x, y, r, TILE_WIDTH))*3 + k];
}

static void rotate_connections(char *tc, int i, int n, int r)
{
    tile_connections[n] = 0;
    for(int y = 0; y < 2; y++)
        for(int x = 0; x < 2; x++)
            tile_connections[n] |= (((tc[i] >> rotate(x, y, r, 2)) & 1) << (y * 2 + x));
}

static void load_tile_data(char *tc, char *ts, size_t *tid)
{
    char file_path[32] = "files/tiles/tiles.dat";
    FILE *fp = fopen(file_path, "rb");
    if(!fp) {
        fprintf(stderr, "Could not open file: %s\n", file_path);
        exit(EXIT_FAILURE);
    }

    fread(tid, sizeof(size_t), 1, fp);
    fread(tc, 1, *tid, fp);
    fread(ts, 1, *tid, fp);

    fclose(fp);
}

void load_tiles()
{
    size_t tid = 0;
    char tile_con[TILES_CAP];
    char tile_sym[TILES_CAP];
    load_tile_data(tile_con, tile_sym, &tid);

    if(tid == 0) {
        fprintf(stderr, "ERROR: No tiles could be loaded\n");
        exit(EXIT_FAILURE);
    }

    size_t n = 0;
    for(size_t i = 0; i < tid; i++, n++)
    {
        char file_path[32];
        sprintf(file_path, "files/tiles/tile_%ld.ppm", i);
        char *t = load_from_ppm(file_path, &TILE_WIDTH, &TILE_HEIGHT);

        switch(tile_sym[i])
        {
        case 'X':
            rotate_tiles(t, n, 0);
            rotate_connections(tile_con, i, n, 0);
            break;
        case 'T':
            for(int j = 0; j < 4; j++) {
                rotate_tiles(t, n, j);
                rotate_connections(tile_con, i, n, j);
                n++;
             } n--;
            break;
        case 'I':
            rotate_tiles(t, n, 0);
            rotate_connections(tile_con, i, n, 0);
            n++;
            rotate_tiles(t, n, 1);
            rotate_connections(tile_con, i, n, 1);
        }

        free_ppm(t);
    }

    TILES = n;

    if(TILES > TILES_CAP) {
        fprintf(stderr, "ERROR: Too many tiles: %ld\n", TILES);
        exit(EXIT_FAILURE);
    }
}

void free_tiles()
{
    for(size_t i = 0; i < TILES; i++)
        free(tiles[i]);
}

void print_tiles()
{
    for(size_t i = 0; i < TILES; i++)
    {
        printf("Tile: %ld\n", i);
        printf("Connections: %b\n", tile_connections[i]);
        for(size_t y = 0; y < TILE_HEIGHT; y++)
        {
            for(size_t x = 0; x < TILE_WIDTH; x++)
                putchar((tiles[i][(y * TILE_WIDTH + x)*3 + 0]) == 0 ? '#' : '.');
            putchar('\n');
        }
        putchar('\n');
    }
}

int get_tile_pixel(size_t t, size_t x, size_t y, int k)
{
    return tiles[t][(y * TILE_WIDTH + x)*3 + k];
}

small_t *get_tile_connections()
{
    return tile_connections;
}