aboutsummaryrefslogtreecommitdiff
path: root/src/gen_tiles.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-08-09 23:07:37 +0300
committerkartofen <mladenovnasko0@gmail.com>2022-08-09 23:07:37 +0300
commitec57f80b20e2309774b7e3a215640637e1c8e0f7 (patch)
treec68058ab9e2c8294eea5dfe36a18672644d3c67a /src/gen_tiles.c
parent00c3363ca258f160b875ec254e701017be0314db (diff)
better
Diffstat (limited to 'src/gen_tiles.c')
-rw-r--r--src/gen_tiles.c168
1 files changed, 131 insertions, 37 deletions
diff --git a/src/gen_tiles.c b/src/gen_tiles.c
index 3133805..fecba4e 100644
--- a/src/gen_tiles.c
+++ b/src/gen_tiles.c
@@ -1,55 +1,149 @@
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include <sys/stat.h>
#include "ppm.h"
-int tile_0[9] = {
- 0, 0, 0,
- 0, 0, 0,
- 0, 0, 0
-};
+#define TILES 3
+#define TILE_WIDTH 3
+#define TILE_HEIGHT 3
+#define TILE_SZ ((TILE_WIDTH) * (TILE_HEIGHT))
-int tile_1[9] = {
- 0, 1, 0,
- 1, 1, 1,
- 0, 0, 0
-};
+FILE *fp;
-int tile_2[9] = {
- 0, 1, 0,
- 0, 1, 1,
- 0, 1, 0
-};
+// possible types: X T I
+char symetry[TILES] = "XTI";
-int tile_3[9] = {
- 0, 1, 0,
- 1, 1, 0,
- 0, 1, 0
+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, 0, 0,
+ 1, 1, 1,
+ 0, 0, 0
+ }
};
-int tile_4[9] = {
- 0, 0, 0,
- 1, 1, 1,
- 0, 1, 0
-};
-int tile_5[9] = {
- 0, 1, 0,
- 1, 1, 1,
- 0, 1, 0
+small_t tiles_connections[TILES][4] = {
+ { 0, 0, 0, 0 },
+ { 1, 1, 1, 0 },
+ // { 1, 1, 1, 1 },
+ { 0, 1, 1, 0 },
};
-void gen()
+int rotate(int x, int y, int r)
+{
+ switch(r) {
+ case 0: // no roatate
+ return y * TILE_WIDTH + x;
+ case 1: // right
+ return (TILE_SZ-TILE_WIDTH) + y - (x * TILE_WIDTH);
+ case 2: // left
+ return (TILE_WIDTH-1) - y + (x * TILE_WIDTH);
+ case 3: // down
+ return (TILE_SZ-1) - (y * TILE_WIDTH) - x;
+ }
+
+ return -1;
+}
+
+int crotate(int n, int r)
+{
+ switch(r) {
+ case 0:
+ return n;
+ case 1:
+ return (n + 1) % 4;
+ case 2:
+ return (n + 2) % 4;
+ case 3:
+ return (n + 3) % 4;
+ }
+
+ 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)];
+}
+
+void rotate_connections(small_t *tc, int i, int r)
{
- mkdir("files/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
- save_as_ppm("files/tile_0.ppm", tile_0, 3, 3, 1);
- save_as_ppm("files/tile_1.ppm", tile_1, 3, 3, 1);
- save_as_ppm("files/tile_2.ppm", tile_2, 3, 3, 1);
- save_as_ppm("files/tile_3.ppm", tile_3, 3, 3, 1);
- save_as_ppm("files/tile_4.ppm", tile_4, 3, 3, 1);
- save_as_ppm("files/tile_5.ppm", tile_5, 3, 3, 1);
+ for(int j = 0; j < 4; j++)
+ tc[j] = tiles_connections[i][crotate(j, r)];
}
+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, r = 0; i < TILES; i++)
+ {
+ small_t tile[TILE_SZ];
+ small_t tile_connections[4];
+ switch(symetry[i])
+ {
+ case 'X':
+ save(tiles[i], tiles_connections[i], i+r);
+ break;
+ case 'T': ;
+ for(; r < 4; r++) {
+ rotate_tiles(tile, i, r);
+ rotate_connections(tile_connections, i, r);
+
+ save(tile, tile_connections, i+r);
+ }
+ r--;
+ break;
+ case 'I':
+ save(tiles[i], tiles_connections[i], i+r);
+ r++;
+ rotate_tiles(tile, i, 2);
+ rotate_connections(tile_connections, i, 2);
+ save(tile, tile_connections, i+r);
+ break;
+ }
+ }
+
+ fclose(fp);
+}
+
+
int main(void)
{
- gen();
+ gen_rotations();
return 0;
}