aboutsummaryrefslogtreecommitdiff
path: root/src/tiles.c
blob: 6b7a39becb3c1fcaa2e3ee1280cdd85f31257467 (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
#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];

void load_tiles()
{
    size_t width, height;
    for(size_t i = 0; i < TILES; i++)
    {
        char file_path[32];
        sprintf(file_path, "files/tiles/tile_%ld.ppm", i);
        tiles[i] = load_from_ppm(file_path, &width, &height);
    }
    TILE_WIDTH = width;
    TILE_HEIGHT = height;
}

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

void print_tiles()
{
    for(size_t i = 0; i < TILES; i++)
    {
        printf("%ld\n", 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] + '0');
            putchar('\n');
        }
        putchar('\n');
    }
}

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

small_t *load_tile_connections()
{
    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(tile_connections, sizeof(small_t), TILES, fp);

    fclose(fp);

    return tile_connections;
}

#include <dirent.h>

void calc_tiles()
{
    size_t file_count = 0;
    DIR * dirp;
    struct dirent * entry;

    dirp = opendir("files/tiles"); /* There should be error handling after this */
    while ((entry = readdir(dirp)) != NULL) {
        if (entry->d_type == DT_REG) { /* If the entry is a regular file */
            file_count++;
        }
    }
    closedir(dirp);

    TILES = file_count - 1;
}