From 57315da56fa3b036f8e3e2d32a5f90a11ae7c3de Mon Sep 17 00:00:00 2001 From: kartofen Date: Thu, 11 Aug 2022 16:41:00 +0300 Subject: works with images --- src/tiles.c | 135 +++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 97 insertions(+), 38 deletions(-) (limited to 'src/tiles.c') diff --git a/src/tiles.c b/src/tiles.c index d3fdc9d..cb135fa 100644 --- a/src/tiles.c +++ b/src/tiles.c @@ -12,30 +12,119 @@ extern size_t TILE_HEIGHT; small_t *(tiles[TILES_CAP]); small_t tile_connections[TILES_CAP]; +static 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; +} + +static void rotate_tiles(small_t *t, int i, int r) +{ + tiles[i] = malloc(TILE_WIDTH * TILE_HEIGHT * 3); + + for(size_t y = 0; y < TILE_HEIGHT; y++) + for(size_t x = 0; x < TILE_WIDTH; x++) + for(int k = 0; k < 3; k++) + tiles[i][(y * TILE_WIDTH + x)*3 + k] = t[(rotate(x, y, r, TILE_WIDTH))*3 + k]; +} + +static void rotate_connections(small_t *tc, 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)); +} + +static void load_tile_data(char *tc, 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); + exit(EXIT_FAILURE); + } + + fread(tid, sizeof(size_t), sizeof(size_t), fp); + fread(tc, 1, *tid, fp); + fread(ts, 1, *tid, fp); + + fclose(fp); +} + void load_tiles() { - size_t width, height; - for(size_t i = 0; i < TILES; i++) + size_t tid = 0; + char tile_con[TILES_CAP]; + char tile_sym[TILES_CAP]; + load_tile_data(tile_con, tile_sym, &tid); + + if(tid == 0) { + fprintf(stderr, "ERROR: No tiles could be loaded\n"); + exit(EXIT_FAILURE); + } + + size_t n = 0; + for(size_t i = 0; i < tid; i++, n++) { char file_path[32]; sprintf(file_path, "files/tiles/tile_%ld.ppm", i); - tiles[i] = load_from_ppm(file_path, &width, &height); + small_t *t = load_from_ppm(file_path, &TILE_WIDTH, &TILE_HEIGHT); + + switch(tile_sym[i]) + { + case 'X': + rotate_tiles(t, n, 0); + rotate_connections(tile_con, i, n, 0); + break; + case 'T': + for(int j = 0; j < 4; j++) { + rotate_tiles(t, n, j); + rotate_connections(tile_con, i, n, j); + n++; + } n--; + break; + case 'I': + rotate_tiles(t, n, 0); + rotate_connections(tile_con, i, n, 0); + n++; + rotate_tiles(t, n, 1); + rotate_connections(tile_con, i, n, 1); + } + + free_ppm(t); + } + + TILES = n; + + if(TILES > TILES_CAP) { + fprintf(stderr, "ERROR: Too many tiles: %ld\n", TILES); + exit(EXIT_FAILURE); } - TILE_WIDTH = width; - TILE_HEIGHT = height; } void free_tiles() { for(size_t i = 0; i < TILES; i++) - free_ppm(tiles[i]); + free(tiles[i]); } void print_tiles() { for(size_t i = 0; i < TILES; i++) { - printf("%ld\n", i); + printf("Tile: %ld\n", i); + printf("Connections: %b\n", tile_connections[i]); for(size_t y = 0; y < TILE_HEIGHT; y++) { for(size_t x = 0; x < TILE_WIDTH; x++) @@ -51,37 +140,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 *load_tile_connections() +small_t *get_tile_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); - return tile_connections; } - -#include - -void 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); - - TILES = file_count - 1; -} -- cgit v1.2.3