aboutsummaryrefslogtreecommitdiff
path: root/src/tiles.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2022-12-22 22:43:44 +0200
committerkartofen <mladenovnasko0@gmail.com>2022-12-22 22:43:44 +0200
commit764070a86a7aa98480eaaff64dfba7e2f2943292 (patch)
tree830589ac702d540bfd76cfb0e5faa5e8988432b0 /src/tiles.c
parent4c9fafc4c126af2466dc6c56b9c952f901a1c726 (diff)
refactor
Diffstat (limited to 'src/tiles.c')
-rw-r--r--src/tiles.c76
1 files changed, 30 insertions, 46 deletions
diff --git a/src/tiles.c b/src/tiles.c
index 0c31c05..61fa98f 100644
--- a/src/tiles.c
+++ b/src/tiles.c
@@ -4,12 +4,10 @@
#include "tiles.h"
#include "ppm.h"
-extern size_t TILES;
+size_t TILES;
+size_t TILE_WIDTH;
+size_t TILE_HEIGHT;
-extern size_t TILE_WIDTH;
-extern size_t TILE_HEIGHT;
-
-small_t DIMENTIONS = 0;
small_t *(tiles[TILES_CAP]);
small_t tile_connections[TILES_CAP][SIDES_MAX];
@@ -46,43 +44,29 @@ static void rotate_connections(small_t (*tc)[SIDES_MAX], int i, int n, int r)
tile_connections[n][y*2 + x] = tc[i][rotate(x, y, r, 2)];
}
-static void load_tile_data(small_t (*tc)[SIDES_MAX], char *ts, size_t *tid)
-{
- char file_path[32] = "files/tiles/tiles.dat";
- FILE *fp = fopen(file_path, "rb");
- if(!fp) {
- err("Could not open file: %s\n", file_path);
- exit(EXIT_FAILURE);
- }
-
- fread(&DIMENTIONS, sizeof(DIMENTIONS), 1, fp);
- fread(tid, sizeof(size_t), 1, fp);
-
- for(size_t i = 0; i < *tid; i++) {
- fread(tc[i], sizeof(small_t), SIDES, fp);
- fread(&(ts[i]), sizeof(char), 1, fp);
- }
-
- fclose(fp);
-}
-
-void load_tiles()
+void load_tiles(FILE *fp)
{
size_t tid = 0;
small_t tile_con[TILES_CAP][SIDES_MAX] = {0};
char tile_sym[TILES_CAP];
- load_tile_data(tile_con, tile_sym, &tid);
+ fread(&tid, sizeof(size_t), 1, fp);
if(tid == 0) {
- fprintf(stderr, "ERROR: No tiles could be loaded\n");
+ err("Tiles could not be loaded");
exit(EXIT_FAILURE);
}
+ for(size_t i = 0; i < tid; i++) {
+ fread(tile_con[i], sizeof(small_t), SIDES, fp);
+ fread(&(tile_sym[i]), sizeof(char), 1, fp);
+ }
+
+
size_t n = 0;
for(size_t i = 0; i < tid; i++, n++)
{
char file_path[128];
- sprintf(file_path, "files/tiles/tile_%ld.ppm", i);
+ sprintf(file_path, "%s/tiles/tile_%ld.ppm", PATH, i);
char *t = load_from_ppm(file_path, &TILE_WIDTH, &TILE_HEIGHT);
switch(tile_sym[i])
@@ -134,23 +118,23 @@ void free_tiles()
free(tiles[i]);
}
-void print_tiles()
-{
- for(size_t i = 0; i < TILES; i++)
- {
- printf("Tile: %ld\n", i);
- for(int n = 0; n < 4; n++) {
- printf("%d", tile_connections[i][n]);
- } printf("\n");
- 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');
- }
-}
+// void print_tiles()
+// {
+// for(size_t i = 0; i < TILES; i++)
+// {
+// printf("Tile: %ld\n", i);
+// for(int n = 0; n < 4; n++) {
+// printf("%d", tile_connections[i][n]);
+// } printf("\n");
+// 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)
{