#include #include #include "typedef.h" #include "tiles.h" #include "ppm.h" extern size_t TILES; extern size_t TILE_WIDTH; extern size_t TILE_HEIGHT; small_t *(tiles[TILES_CAP]); small_t tile_connections[TILES_CAP]; void load_tiles() { size_t width, height; for(size_t i = 0; i < TILES; i++) { char file_path[32]; sprintf(file_path, "files/tiles/tile_%ld.ppm", i); tiles[i] = load_from_ppm(file_path, &width, &height); } TILE_WIDTH = width; TILE_HEIGHT = height; } void free_tiles() { for(size_t i = 0; i < TILES; i++) free_ppm(tiles[i]); } void print_tiles() { for(size_t i = 0; i < TILES; i++) { printf("%ld\n", i); for(size_t y = 0; y < TILE_HEIGHT; y++) { for(size_t x = 0; x < TILE_WIDTH; x++) putchar((tiles[i][(y * TILE_WIDTH + x)*3 + 0]) == 0 ? '#' : '.'); putchar('\n'); } putchar('\n'); } } 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() { 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; }