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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "typedef.h"
#include "ppm.h"
#define TILES 2
#define TILE_WIDTH 3
#define TILE_HEIGHT 3
#define TILE_SZ ((TILE_WIDTH) * (TILE_HEIGHT))
FILE *fp;
// possible types: X T I
char symetry[TILES] = "XTXIT";
small_t tiles[TILES][TILE_SZ] = {
{
0, 0, 0,
0, 0, 0,
0, 0, 0
}, {
0, 1, 0,
1, 1, 1,
0, 0, 0
}, {
0, 1, 0,
1, 1, 1,
0, 1, 0
},{
0, 1, 0,
0, 1, 0,
0, 1, 0
}, {
0, 1, 0,
1, 1, 0,
0, 0, 0
}
};
small_t tiles_connections[TILES][4] = {
{ 0, 0, 0, 0 },
{ 1, 1, 1, 0 },
{ 1, 1, 1, 1 },
{ 1, 0, 0, 1 },
{ 1, 0, 1, 0 },
};
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;
}
void rotate_tiles(small_t *tile, int i, int r)
{
for(size_t y = 0; y < TILE_HEIGHT; y++)
for(size_t x = 0; x < TILE_WIDTH; x++)
tile[y * TILE_WIDTH + x] = tiles[i][rotate(x, y, r, TILE_WIDTH)];
}
void rotate_connections(small_t *tc, int i, int r)
{
for(int y = 0; y < 2; y++)
for(int x = 0; x < 2; x++)
tc[y * 2 + x] = tiles_connections[i][rotate(x, y, r, 2)];
}
void save(small_t *t, small_t *n, int i)
{
char file_name[64] = {0};
sprintf(file_name, "files/tiles/tile_%d.ppm", i);
save_as_ppm(file_name, t, TILE_WIDTH, TILE_HEIGHT, 1);
printf("Saved file: %s\n", file_name);
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");
for(int i = 0, n = 0; i < TILES; i++, n++)
{
small_t tile[TILE_SZ];
small_t tile_connections[4];
switch(symetry[i])
{
case 'X':
save(tiles[i], tiles_connections[i], n);
break;
case 'T': ;
for(int j = 0; j < 4; j++) {
rotate_tiles(tile, i, j);
rotate_connections(tile_connections, i, j);
save(tile, tile_connections, n);
n++;
} n--;
break;
case 'I':
save(tiles[i], tiles_connections[i], n);
n++;
rotate_tiles(tile, i, 1);
rotate_connections(tile_connections, i, 1);
save(tile, tile_connections, n);
break;
}
}
fclose(fp);
}
int main(void)
{
gen_rotations();
return 0;
}
|