diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gen_tiles.c | 16 | ||||
-rw-r--r-- | src/main.c | 67 | ||||
-rw-r--r-- | src/ppm.c | 18 | ||||
-rw-r--r-- | src/ppm.h | 2 |
4 files changed, 48 insertions, 55 deletions
diff --git a/src/gen_tiles.c b/src/gen_tiles.c index 87e50bd..3133805 100644 --- a/src/gen_tiles.c +++ b/src/gen_tiles.c @@ -31,15 +31,21 @@ int tile_4[9] = { 1, 1, 1, 0, 1, 0 }; +int tile_5[9] = { + 0, 1, 0, + 1, 1, 1, + 0, 1, 0 +}; void gen() { mkdir("files/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); - save_as_ppm("files/tile_0.ppm", tile_0, 3, 3); - save_as_ppm("files/tile_1.ppm", tile_1, 3, 3); - save_as_ppm("files/tile_2.ppm", tile_2, 3, 3); - save_as_ppm("files/tile_3.ppm", tile_3, 3, 3); - save_as_ppm("files/tile_4.ppm", tile_4, 3, 3); + 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); } int main(void) @@ -1,28 +1,29 @@ #include <stdio.h> #include <stdlib.h> -#include <stdint.h> #include <string.h> #include <time.h> #include "tiles.h" #include "ppm.h" -#define TILES 5 +#define TILES 6 int *(tiles[TILES]) = {0}; size_t TILE_WIDTH; size_t TILE_HEIGHT; int tile_masks[TILES][4] = { - { 0b00011, 0b00101, 0b01001, 0b10001 }, + { 0b000011, 0b000101, 0b001001, 0b010001 }, - { 0b11100, 0b11010, 0b10110, 0b10001 }, - { 0b11100, 0b11010, 0b01001, 0b01110 }, - { 0b11100, 0b00101, 0b10110, 0b01110 }, - { 0b00011, 0b11010, 0b10110, 0b01110 } + { 0b111100, 0b111010, 0b110110, 0b010001 }, + { 0b111100, 0b111010, 0b001001, 0b101110 }, + { 0b111100, 0b000101, 0b110110, 0b101110 }, + { 0b000011, 0b111010, 0b110110, 0b101110 }, + + { 0b111100, 0b111010, 0b110110, 0b101110 } }; -#define WIDTH_SCALER 3 -#define HEIGHT_SCALER 3 +#define WIDTH_SCALER 10 +#define HEIGHT_SCALER 10 int tiles_board[WIDTH_SCALER * HEIGHT_SCALER] = {0}; @@ -80,15 +81,15 @@ int get_least_entropy_index() void set_index(int i) { - if(count_1s(tiles_board[i]) == 1) { - tiles_board[i] |= 1 << TILES; - goto modify_neighbors; + int n; + + if(count_1s(tiles_board[i]) == 0) { + fprintf(stderr, "ERROR: No possible tiles for this position: %d\n", i); + exit(EXIT_FAILURE); } - // bad way - int n; - while(((tiles_board[i] >> TILES) & 1) != 1) - { + // this bad + do { n = rand() % TILES; if(((tiles_board[i] >> n) & 1) != 1) continue; @@ -96,9 +97,7 @@ void set_index(int i) tiles_board[i] = 0; tiles_board[i] |= 1 << n; tiles_board[i] |= 1 << TILES; - } - -modify_neighbors: + } while(((tiles_board[i] >> TILES) & 1) != 1); if(i / WIDTH_SCALER != 0) // up if(count_1s(tiles_board[i-WIDTH_SCALER]) != (TILES + 1)) @@ -115,19 +114,15 @@ modify_neighbors: if(i / WIDTH_SCALER != (HEIGHT_SCALER - 1)) // down if(count_1s(tiles_board[i+WIDTH_SCALER]) != (TILES + 1)) tiles_board[i+WIDTH_SCALER] &= tile_masks[n][3]; - } int main(void) { time_t seed = time(0); - srand(420); + srand(seed); printf("seed %ld\n", seed); init_tiles_board(); - load_tiles(TILES); - - print_tiles(TILES); while(1) { int lei; @@ -137,23 +132,18 @@ int main(void) break; // every index is set else set_index(lei); - - // for(int i = 0; i < HEIGHT_SCALER; i++) { - // for(int j = 0; j < WIDTH_SCALER; j++) - // printf("%6b ", tiles_board[i * WIDTH_SCALER + j]); - // putchar('\n'); - // } - // putchar('\n'); } + load_tiles(TILES); + int img_wdt = TILE_WIDTH * WIDTH_SCALER; int img_hgt = TILE_HEIGHT * HEIGHT_SCALER; int *image = malloc(img_wdt * img_hgt * sizeof(int)); memset(image, 0, img_wdt * img_hgt * sizeof(int)); - for(int i = 0; i < HEIGHT_SCALER; i++) + for(size_t i = 0; i < HEIGHT_SCALER; i++) { - for(int j = 0; j < WIDTH_SCALER; j++) + for(size_t j = 0; j < WIDTH_SCALER; j++) { int t; for(t = 0; t < TILES; t++) @@ -167,9 +157,14 @@ int main(void) } } - save_as_ppm("file.ppm", image, img_wdt, img_hgt); - free(image); - free_tiles(TILES); + + char file_name[64] = {0}; + sprintf(file_name, "files/file_%ld.ppm", seed); + printf("Saved file with name: %s\n", file_name); + + save_as_ppm(file_name, image, img_wdt, img_hgt, 10); + + free(image); return 0; } @@ -3,7 +3,7 @@ #include <string.h> #include "ppm.h" -void save_as_ppm(char* file_path, int *t, size_t width, size_t height) +void save_as_ppm(char* file_path, int *t, size_t width, size_t height, size_t scaler) { FILE *fp = fopen(file_path, "wb"); if(!fp) { @@ -11,17 +11,10 @@ void save_as_ppm(char* file_path, int *t, size_t width, size_t height) exit(EXIT_FAILURE); } - // fprintf(fp, "P6\n%ld %ld 255\n", width, height); - - // for(size_t i = 0; i < height; i++) - // for(size_t j = 0; j < width; j++) - // { - // char c = (t[i * width + j] == 0) ? 255 : 0; - // for(int j = 0; j < 3; j++) - // fwrite(&c, 1, 1, fp); - // } - - int scaler = 5; + if(scaler == 0) { + fprintf(stderr, "ERROR: Invalid value for scaler %d\n", scaler); + exit(EXIT_FAILURE); + } fprintf(fp, "P6\n%d %d 255\n", width*scaler, height*scaler); @@ -34,7 +27,6 @@ void save_as_ppm(char* file_path, int *t, size_t width, size_t height) } - fclose(fp); } @@ -1,7 +1,7 @@ #ifndef PPM_H #define PPM_H -void save_as_ppm(char* file_path, int *t, size_t width, size_t height); +void save_as_ppm(char* file_path, int *t, size_t width, size_t height, size_t scaler); int *load_from_ppm(char *file_path, size_t *width, size_t *height); |