diff --git a/.gitignore b/.gitignore index 5afec89..08f6683 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.o sokoban - +*.out diff --git a/Makefile b/Makefile index 2c176bb..1160cae 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Variable CC = gcc CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2) -OBJ = main.o function.o display.o +OBJ = main.o function.o display.o read.o TARGET = sokoban all: $(TARGET) @@ -9,7 +9,7 @@ all: $(TARGET) $(TARGET): $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o $(TARGET) -main.o : main.c function.h +main.o : main.c function.h display.h read.h $(CC) $(CFLAGS) -c main.c function.o : function.c function.h @@ -18,6 +18,9 @@ function.o : function.c function.h display.o: display.c display.h $(CC) $(CFLAGS) -c display.c +read.o: read.c read.h function.h + $(CC) $(CFLAGS) -c read.c + clean : rm -f $(OBJ) $(TARGET) diff --git a/a.out b/a.out deleted file mode 100755 index 746dd02..0000000 Binary files a/a.out and /dev/null differ diff --git a/display.c b/display.c index eb404ba..0ef9822 100644 --- a/display.c +++ b/display.c @@ -13,7 +13,3 @@ void screenDisplay( unsigned short int **tab,int size) } } - - - - diff --git a/function.c b/function.c index 9ff395f..bae5c66 100644 --- a/function.c +++ b/function.c @@ -15,7 +15,7 @@ unsigned short int **creatArea2D(const unsigned int N) } bool fail = false; - int i; + unsigned int i; for (i = 0; i < N && !fail; ++i) { tab2d[i] = calloc(N, sizeof(unsigned short int)); @@ -27,7 +27,7 @@ unsigned short int **creatArea2D(const unsigned int N) if (fail) { - int j; + unsigned int j; for (j = 0; j < i; ++j) { free(tab2d[j]); diff --git a/main.c b/main.c index bcb3566..c0b8fb9 100644 --- a/main.c +++ b/main.c @@ -1,11 +1,13 @@ #include #include "function.h" #include "display.h" +#include "read.h" int main() { - unsigned short int **tab2d = creatArea2D(5); - screenDisplay(tab2d, 5); + unsigned short int **tab2d = creatArea2D(32); + fileToTab2D("test.txt", tab2d, 32); + screenDisplay(tab2d, 32); printf("main\n"); return 0; } diff --git a/read.c b/read.c new file mode 100644 index 0000000..87f5413 --- /dev/null +++ b/read.c @@ -0,0 +1,40 @@ +#include +#include +#include "function.h" + +void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N) +{ + FILE *file = fopen(name_file, "r"); + unsigned int x = 0, y = 0; + while(!feof(file)) + { + char current =fgetc(file); + switch (current) { + case ' ': + tab[x][y] = EMPTY; + break; + case '#': + tab[x][y] = WALL; + break; + case '$': + tab[x][y] = BOX; + break; + case '.': + tab[x][y] = TARGET; + break; + case '@': + tab[x][y] = PLAYER; + break; + case '\n': + y = 0; + ++x; + break; + } + ++y; + if (x >= N || y >= N) + { + perror("Level out of range !"); + exit(-1); + } + } +} diff --git a/read.h b/read.h new file mode 100644 index 0000000..a265059 --- /dev/null +++ b/read.h @@ -0,0 +1,5 @@ +#ifndef READ_H +#define READ_H + +void fileToTab2D(const char* name_file, unsigned short int **tab, const unsigned N); +#endif // !READ_H diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..00f580a --- /dev/null +++ b/test.txt @@ -0,0 +1,11 @@ + ##### + # # + #$ # + ### $## + # $ $ # +### # ## # ###### +# # ## ##### ..# +# $ $ ..# +##### ### #@## ..# + # ######### + #######