aboutsummaryrefslogtreecommitdiff
path: root/src/tiles.c
blob: 2432736103e59a774a3d1bd0775ecbc0a9f82fb9 (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
#include <stdio.h>
#include <stdlib.h>
#include "ppm.h"

extern small_t *(tiles[]);
extern size_t TILES;

extern size_t TILE_WIDTH;
extern size_t TILE_HEIGHT;

extern small_t tile_connections[];

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++)
    {
        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');
    }
}

void load_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);
}

#include <dirent.h>

size_t 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);

    return file_count-1;
}