diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/config.h | 5 | ||||
| -rw-r--r-- | src/gen_tiles.c | 100 | ||||
| -rw-r--r-- | src/main.c | 21 | ||||
| -rw-r--r-- | src/ppm.c | 9 | ||||
| -rw-r--r-- | src/tiles.c | 135 | ||||
| -rw-r--r-- | src/tiles.h | 4 | 
6 files changed, 145 insertions, 129 deletions
| diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..c9743e1 --- /dev/null +++ b/src/config.h @@ -0,0 +1,5 @@ +#define GENERATE_PPM_TILES + +int tiles_to_load[] = { +    1, +}; diff --git a/src/gen_tiles.c b/src/gen_tiles.c index 5462ee3..53e1012 100644 --- a/src/gen_tiles.c +++ b/src/gen_tiles.c @@ -4,88 +4,56 @@  #include <sys/stat.h>  #include "typedef.h"  #include "ppm.h" +#include "config.h" -#define TILES 3  #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"; +char symetry[TILES_CAP] = "XTIIT"; -small_t tiles[TILES][TILE_SZ] = { +small_t tiles[TILES_CAP][TILE_WIDTH*TILE_HEIGHT] = {      {        0, 0, 0,        0, 0, 0,        0, 0, 0      }, { -      0, 1, 0, +      0, 0, 0,        1, 1, 1, -      0, 0, 0 +      0, 1, 0      }, {        0, 1, 0,        1, 1, 1,        0, 1, 0      },{ -      0, 1, 0, -      0, 1, 0, -      0, 1, 0 +      0, 0, 0, +      1, 1, 1, +      0, 0, 0      }, {        0, 1, 0, -      1, 1, 0, +      0, 1, 1,        0, 0, 0      }  }; -small_t tiles_connections[TILES][4] = { +small_t tiles_connections[TILES_CAP][4] = {      { 0, 0, 0, 0 }, -    { 1, 1, 1, 0 }, +    { 0, 1, 1, 1 },      { 1, 1, 1, 1 }, -    { 1, 0, 0, 1 }, -    { 1, 0, 1, 0 }, +    { 0, 1, 1, 0 }, +    { 1, 1, 0, 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 set_color(small_t *pixels, small_t *t, int x, int y, char k, char blank, char connection)  {      pixels[(y * TILE_WIDTH + x)*3 + k] = (t[y * TILE_WIDTH + x] == 0) ? blank : connection;  } +FILE *fp;  void save(small_t *t, small_t *n, int i)  { +#ifdef GENERATE_PPM_TILES      char file_name[64] = {0};      sprintf(file_name, "files/tiles/tile_%d.ppm", i); @@ -101,6 +69,7 @@ void save(small_t *t, small_t *n, int i)      save_as_ppm(file_name, pixels, TILE_WIDTH, TILE_HEIGHT, 1);      free(pixels);      printf("Saved file: %s\n", file_name); +#endif      small_t connections = 0;      for(int c = 0; c < 4; c++) @@ -118,34 +87,21 @@ void gen_rotations()      fp = fopen("files/tiles/tiles.dat", "wb"); -    for(int i = 0, n = 0; i < TILES; i++, n++) +    size_t tiles_sz = sizeof(tiles_to_load)/sizeof(int); +    fwrite(&tiles_sz, sizeof(size_t), sizeof(size_t), fp); + +    char syms[TILES_CAP]; +    size_t sym_sz = 0; + +    for(int n = 0; n < tiles_sz; 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; -        } +        int i = tiles_to_load[n]; +        save(tiles[i], tiles_connections[i], n); +        syms[sym_sz++] = symetry[i];      } +    fwrite(syms, 1, sym_sz, fp); +      fclose(fp);  } @@ -1,6 +1,5 @@  #include <stdio.h>  #include <stdlib.h> -#include <string.h>  #include <time.h>  #include "typedef.h"  #include "ppm.h" @@ -12,8 +11,8 @@ size_t TILES;  size_t TILE_WIDTH;  size_t TILE_HEIGHT; -size_t SWIDTH  = 10; -size_t SHEIGHT = 10; +size_t SWIDTH  = 30; +size_t SHEIGHT = 30;  int get_least_entropy_index()  { @@ -85,17 +84,14 @@ void collapse_this(int i)  int main(void)  { -    calc_tiles(); -    if(TILES > TILES_CAP) { -        printf("ERROR: Too many tiles: %ld\n", TILES); -        exit(EXIT_FAILURE); -    } +    load_tiles(); +    print_tiles(); -    time_t seed = 420; +    time_t seed = 69;      srand(seed);      printf("The Seed is %ld\n", seed); -    generate_tile_masks(load_tile_connections()); +    generate_tile_masks(get_tile_connections());      init_tilemap();      while(1) { @@ -108,12 +104,9 @@ int main(void)              collapse_this(lei);      } -    load_tiles(); -      size_t  img_wdt = TILE_WIDTH  * SWIDTH;      size_t  img_hgt = TILE_HEIGHT * SHEIGHT;      small_t *image = malloc(img_wdt * img_hgt * 3); -    memset(image, 0, img_wdt * img_hgt);      for(size_t i = 0; i < SHEIGHT; i++)      { @@ -135,7 +128,7 @@ int main(void)      char file_name[64] = {0};      sprintf(file_name, "files/file_%ld.ppm", seed); -    save_as_ppm(file_name, image, img_wdt, img_hgt, 10); +    save_as_ppm(file_name, image, img_wdt, img_hgt, 9);      printf("Saved file with name: %s\n", file_name);      free(image); @@ -17,7 +17,7 @@ void save_as_ppm(char* file_path, small_t *t, size_t width, size_t height, size_          exit(EXIT_FAILURE);      } -    fprintf(fp, "P6\n%ld %ld 255\n", width*scaler, height*scaler); +    fprintf(fp, "P6\n%ld %ld\n255\n", width*scaler, height*scaler);      for(size_t i = 0; i < height * scaler; i++)          for(size_t j = 0; j < width * scaler; j++) @@ -48,7 +48,12 @@ small_t *load_from_ppm(char *file_path, size_t *width, size_t *height)      fgets(line, sizeof(line), fp);      *width  = atoi(strtok(line, " "));      *height = atoi(strtok(NULL, " ")); -    (void)strtok(NULL, " "); + +    fgets(line, sizeof(line), fp); +    if(atoi(line) != 255) { +        printf("Maximum color value must be 255"); +        exit(1); +    }      small_t *t = malloc((*width) * (*height ) * 3); 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 <dirent.h> - -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; -} diff --git a/src/tiles.h b/src/tiles.h index 49026dc..56bb831 100644 --- a/src/tiles.h +++ b/src/tiles.h @@ -5,8 +5,6 @@ void load_tiles();  void free_tiles();  void print_tiles();  int get_tile_pixel(size_t t, size_t x, size_t y, int k); - -small_t *load_tile_connections(); -void calc_tiles(); +small_t *get_tile_connections();  #endif | 
