aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gen_tiles.c49
-rw-r--r--src/main.c17
-rw-r--r--src/ppm.c81
-rw-r--r--src/ppm.h10
-rw-r--r--src/tiles.c39
-rw-r--r--src/tiles.h8
6 files changed, 204 insertions, 0 deletions
diff --git a/src/gen_tiles.c b/src/gen_tiles.c
new file mode 100644
index 0000000..87e50bd
--- /dev/null
+++ b/src/gen_tiles.c
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <sys/stat.h>
+#include "ppm.h"
+
+int tile_0[9] = {
+ 0, 0, 0,
+ 0, 0, 0,
+ 0, 0, 0
+};
+
+int tile_1[9] = {
+ 0, 1, 0,
+ 1, 1, 1,
+ 0, 0, 0
+};
+
+int tile_2[9] = {
+ 0, 1, 0,
+ 0, 1, 1,
+ 0, 1, 0
+};
+
+int tile_3[9] = {
+ 0, 1, 0,
+ 1, 1, 0,
+ 0, 1, 0
+};
+
+int tile_4[9] = {
+ 0, 0, 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);
+}
+
+int main(void)
+{
+ gen();
+ return 0;
+}
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..1db4858
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+#include "tiles.h"
+#include "ppm.h"
+
+#define TILES 5
+
+int *(tiles[TILES]) = {0};
+int TILE_WIDTH;
+int TILE_HEIGHT;
+
+int main(void)
+{
+ load_tiles(TILES);
+ print_tiles(TILES);
+ free_tiles(TILES);
+ return 0;
+}
diff --git a/src/ppm.c b/src/ppm.c
new file mode 100644
index 0000000..7ac29f3
--- /dev/null
+++ b/src/ppm.c
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "ppm.h"
+
+void save_as_ppm(char* file_path, int *t, int width, int height)
+{
+ FILE *fp = fopen(file_path, "wb");
+ if(!fp) {
+ fprintf(stderr, "ERROR: Could not open file: %s\n", file_path);
+ exit(EXIT_FAILURE);
+ }
+
+ fprintf(fp, "P6\n%d %d 255\n", width, height);
+
+ for(int i = 0; i < height; i++)
+ for(int 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 = 10;
+
+ // fprintf(fp, "P6\n%d %d 255\n", width*scaler, height*scaler);
+
+ // for(int i = 0; i < height * scaler; i++)
+ // for(int j = 0; j < width * scaler; j++)
+ // {
+ // char c = (t[(i/scaler) * width + (j/scaler)] == 0) ? 255 : 0;
+ // for(int j = 0; j < 3; j++)
+ // fwrite(&c, 1, 1, fp);
+ // }
+
+
+
+ fclose(fp);
+}
+
+int *load_from_ppm(char *file_path, int *width, int *height)
+{
+ FILE *fp = fopen(file_path, "rb");
+ if(!fp) {
+ fprintf(stderr, "ERROR: Could not open file: %s\n", file_path);
+ exit(EXIT_FAILURE);
+ }
+
+ char line[64] = {0};
+
+ fgets(line, sizeof(line), fp);
+ if(strncmp(line, "P6", 2) != 0) {
+ fprintf(stderr, "ERROR: PPM header is not correct of file: %s\n", file_path);
+ exit(EXIT_FAILURE);
+ }
+
+ fgets(line, sizeof(line), fp);
+ *width = atoi(strtok(line, " "));
+ *height = atoi(strtok(NULL, " "));
+ (void)strtok(NULL, " ");
+
+ char *pixels = malloc((*width) * (*height ) * 3);
+
+ fread(pixels, *width*3, *height, fp);
+
+ int *t = malloc((*width) * (*height ) * sizeof(int));
+ for(int i = 0; i < *height; i++)
+ for(int j = 0; j < *width; j++)
+ t[i * *width + j] = (pixels[(i * *width + j) * 3] == 0) ? 1 : 0;
+
+ free(pixels);
+
+ fclose(fp);
+
+ return t;
+}
+
+void free_ppm(int *t)
+{
+ free(t);
+}
diff --git a/src/ppm.h b/src/ppm.h
new file mode 100644
index 0000000..b68e77c
--- /dev/null
+++ b/src/ppm.h
@@ -0,0 +1,10 @@
+#ifndef PPM_H
+#define PPM_H
+
+void save_as_ppm(char* file_path, int *t, int width, int height);
+
+int *load_from_ppm(char *file_path, int *width, int *height);
+
+void free_ppm(int *img);
+
+#endif
diff --git a/src/tiles.c b/src/tiles.c
new file mode 100644
index 0000000..b09ca95
--- /dev/null
+++ b/src/tiles.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include "ppm.h"
+
+extern int *(tiles[]);
+extern int TILE_WIDTH;
+extern int TILE_HEIGHT;
+
+void load_tiles(int n)
+{
+ int width, height;
+ for(int i = 0; i < n; i++)
+ {
+ char file_path[26];
+ sprintf(file_path, "files/tile_%d.ppm", i);
+ tiles[i] = load_from_ppm(file_path, &width, &height);
+ }
+ TILE_WIDTH = width;
+ TILE_HEIGHT = height;
+}
+
+void free_tiles(int n)
+{
+ for(int i = 0; i < n; i++)
+ free_ppm(tiles[i]);
+}
+
+void print_tiles(int n)
+{
+ for(int i = 0; i < n; i++)
+ {
+ for(int y = 0; y < TILE_HEIGHT; y++)
+ {
+ for(int x = 0; x < TILE_WIDTH; x++)
+ putchar(tiles[i][y * TILE_WIDTH + x] + '0');
+ putchar('\n');
+ }
+ putchar('\n');
+ }
+}
diff --git a/src/tiles.h b/src/tiles.h
new file mode 100644
index 0000000..14e4dd8
--- /dev/null
+++ b/src/tiles.h
@@ -0,0 +1,8 @@
+#ifndef TILES_H
+#define TILES_H
+
+void load_tiles(int n);
+void free_tiles(int n);
+void print_tiles(int n);
+
+#endif