aboutsummaryrefslogtreecommitdiff
path: root/src/gen_tiles.c
blob: 53e10123bcb510ce41c25c4b630a83d0922c3cf4 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "typedef.h"
#include "ppm.h"
#include "config.h"

#define TILE_WIDTH  3
#define TILE_HEIGHT 3

// possible types: X T I
char symetry[TILES_CAP] = "XTIIT";

small_t tiles[TILES_CAP][TILE_WIDTH*TILE_HEIGHT] = {
    {
      0, 0, 0,
      0, 0, 0,
      0, 0, 0
    }, {
      0, 0, 0,
      1, 1, 1,
      0, 1, 0
    }, {
      0, 1, 0,
      1, 1, 1,
      0, 1, 0
    },{
      0, 0, 0,
      1, 1, 1,
      0, 0, 0
    }, {
      0, 1, 0,
      0, 1, 1,
      0, 0, 0
    }
};

small_t tiles_connections[TILES_CAP][4] = {
    { 0, 0, 0, 0 },
    { 0, 1, 1, 1 },
    { 1, 1, 1, 1 },
    { 0, 1, 1, 0 },
    { 1, 1, 0, 0 },
};

void set_color(small_t *pixels, small_t *t, int x, int y, char k, char blank, char connection)
{
    pixels[(y * TILE_WIDTH + x)*3 + k] = (t[y * TILE_WIDTH + x] == 0) ? blank : connection;

}
FILE *fp;

void save(small_t *t, small_t *n, int i)
{
#ifdef GENERATE_PPM_TILES
    char file_name[64] = {0};

    sprintf(file_name, "files/tiles/tile_%d.ppm", i);

    small_t *pixels = malloc(TILE_WIDTH * TILE_HEIGHT *3);
    for(int y = 0; y < TILE_HEIGHT; y++)
        for(int x = 0; x < TILE_WIDTH; x++) {
            set_color(pixels, t, x, y, 0, 255, 0);
            set_color(pixels, t, x, y, 1, 255, 0);
            set_color(pixels, t, x, y, 2, 255, 0);
        }

    save_as_ppm(file_name, pixels, TILE_WIDTH, TILE_HEIGHT, 1);
    free(pixels);
    printf("Saved file: %s\n", file_name);
#endif

    small_t connections = 0;
    for(int c = 0; c < 4; c++)
    {
        connections |= (n[c] << (3-c));
    }

    fwrite(&connections, sizeof(connections), 1, fp);
}

void gen_rotations()
{
    mkdir("files", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
    mkdir("files/tiles", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);

    fp = fopen("files/tiles/tiles.dat", "wb");

    size_t tiles_sz = sizeof(tiles_to_load)/sizeof(int);
    fwrite(&tiles_sz, sizeof(size_t), sizeof(size_t), fp);

    char syms[TILES_CAP];
    size_t sym_sz = 0;

    for(int n = 0; n < tiles_sz; n++)
    {
        int i = tiles_to_load[n];
        save(tiles[i], tiles_connections[i], n);
        syms[sym_sz++] = symetry[i];
    }

    fwrite(syms, 1, sym_sz, fp);

    fclose(fp);
}


int main(void)
{
    gen_rotations();
    return 0;
}