diff --git a/Makefile b/Makefile index 284f38c..51bc559 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Variable CC = gcc CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2) -OBJ = main.o +OBJ = main.o function.o TARGET = sokoban all: $(TARGET) @@ -9,9 +9,11 @@ all: $(TARGET) $(TARGET): $(OBJ) $(CC) $(CFLAGS) $(OBJ) -o $(TARGET) -main.c : main.c +main.o : main.c $(CC) $(CFLAGS) -c main.c +function.o : function.c + $(CC) $(CFLAGS) -c function clean : rm -f $(OBJ) $(TARGET) diff --git a/function.c b/function.c index 53c5fdf..b26e16e 100644 --- a/function.c +++ b/function.c @@ -1 +1,41 @@ +#include "function.h" #include +#include +#include + + + +unsigned short int **creatArea2D(const unsigned int N) +{ + unsigned short int **tab2d; + tab2d = malloc(sizeof(unsigned short int* ) * N); + if (tab2d == NULL) + { + return NULL; + } + + bool fail = false; + int i; + for (i = 0; i < N && !fail; ++i) + { + tab2d[i] = malloc(sizeof(unsigned short int) * N); + if (tab2d[i] == NULL) + { + fail = true; + } + } + + if (fail) + { + int j; + for (j = 0; j < i; ++j) + { + free(tab2d[j]); + } + free(tab2d); + return NULL; + } + return tab2d; +} + + diff --git a/function.h b/function.h new file mode 100644 index 0000000..0157953 --- /dev/null +++ b/function.h @@ -0,0 +1,17 @@ +#ifdef FONCTION_H +#define FONCTION_H + +#include + +#define EMPTY 0 +#define WALL 1 +#define BOX 2 +#define TARGET 3 +#define BOX_ON_TARGET 4 +#define PLAYER 5 +#define PLAYER_ON_TARGET 6 + +unsigned short int **creatArea2D(const unsigned int N); +#endif // FONCTION_H + + diff --git a/main.c b/main.c index 96ca265..5e0bf4e 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,8 @@ #include +#include "function.h" int main() { - puts("main"); + printf("main\n"); return 0; }