diff options
Diffstat (limited to 'src/tiles.c')
-rw-r--r-- | src/tiles.c | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/src/tiles.c b/src/tiles.c index cecee5a..0c31c05 100644 --- a/src/tiles.c +++ b/src/tiles.c @@ -9,8 +9,9 @@ extern size_t TILES; extern size_t TILE_WIDTH; extern size_t TILE_HEIGHT; +small_t DIMENTIONS = 0; small_t *(tiles[TILES_CAP]); -small_t tile_connections[TILES_CAP]; +small_t tile_connections[TILES_CAP][SIDES_MAX]; static int rotate(int x, int y, int r, int w) { @@ -38,26 +39,29 @@ static void rotate_tiles(char *t, int i, int r) tiles[i][(y * TILE_WIDTH + x)*3 + k] = t[(rotate(x, y, r, TILE_WIDTH))*3 + k]; } -static void rotate_connections(char *tc, int i, int n, int r) +static void rotate_connections(small_t (*tc)[SIDES_MAX], int i, int n, int r) { - tile_connections[n] = 0; for(int y = 0; y < 2; y++) for(int x = 0; x < 2; x++) - tile_connections[n] |= (((tc[i] >> rotate(x, y, r, 2)) & 1) << (y * 2 + x)); + tile_connections[n][y*2 + x] = tc[i][rotate(x, y, r, 2)]; } -static void load_tile_data(char *tc, char *ts, size_t *tid) +static void load_tile_data(small_t (*tc)[SIDES_MAX], char *ts, size_t *tid) { 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); + err("Could not open file: %s\n", file_path); exit(EXIT_FAILURE); } + fread(&DIMENTIONS, sizeof(DIMENTIONS), 1, fp); fread(tid, sizeof(size_t), 1, fp); - fread(tc, 1, *tid, fp); - fread(ts, 1, *tid, fp); + + for(size_t i = 0; i < *tid; i++) { + fread(tc[i], sizeof(small_t), SIDES, fp); + fread(&(ts[i]), sizeof(char), 1, fp); + } fclose(fp); } @@ -65,7 +69,7 @@ static void load_tile_data(char *tc, char *ts, size_t *tid) void load_tiles() { size_t tid = 0; - char tile_con[TILES_CAP]; + small_t tile_con[TILES_CAP][SIDES_MAX] = {0}; char tile_sym[TILES_CAP]; load_tile_data(tile_con, tile_sym, &tid); @@ -77,7 +81,7 @@ void load_tiles() size_t n = 0; for(size_t i = 0; i < tid; i++, n++) { - char file_path[32]; + char file_path[128]; sprintf(file_path, "files/tiles/tile_%ld.ppm", i); char *t = load_from_ppm(file_path, &TILE_WIDTH, &TILE_HEIGHT); @@ -88,7 +92,7 @@ void load_tiles() rotate_connections(tile_con, i, n, 0); break; case 'T': - for(int j = 0; j < 4; j++) { + for(int j = 0; j < SIDES; j++) { rotate_tiles(t, n, j); rotate_connections(tile_con, i, n, j); n++; @@ -100,17 +104,28 @@ void load_tiles() n++; rotate_tiles(t, n, 1); rotate_connections(tile_con, i, n, 1); + break; + case '/': + rotate_tiles(t, n, 0); + rotate_connections(tile_con, i, n, 0); + n++; + rotate_tiles(t, n, 3); + rotate_connections(tile_con, i, n, 3); + break; + default: + err("Symetry Character not recognized: '%c'", tile_sym[i]); + exit(EXIT_FAILURE); } free_ppm(t); + + if(n > TILES_CAP) { + err("Too many tiles: %ld\n", n); + exit(EXIT_FAILURE); + } } TILES = n; - - if(TILES > TILES_CAP) { - fprintf(stderr, "ERROR: Too many tiles: %ld\n", TILES); - exit(EXIT_FAILURE); - } } void free_tiles() @@ -124,7 +139,9 @@ void print_tiles() for(size_t i = 0; i < TILES; i++) { printf("Tile: %ld\n", i); - printf("Connections: %b\n", tile_connections[i]); + for(int n = 0; n < 4; n++) { + printf("%d", tile_connections[i][n]); + } printf("\n"); for(size_t y = 0; y < TILE_HEIGHT; y++) { for(size_t x = 0; x < TILE_WIDTH; x++) @@ -140,7 +157,7 @@ int get_tile_pixel(size_t t, size_t x, size_t y, int k) return tiles[t][(y * TILE_WIDTH + x)*3 + k]; } -small_t *get_tile_connections() +small_t (*get_tile_connections())[SIDES_MAX] { return tile_connections; } |