diff --git a/Makefile b/Makefile index 616252e..5a733cf 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Variable CC = gcc -CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2 SDL2_image) -LIBFLAGS = $(shell pkg-config --cflags --libs sdl2 SDL2_image) +CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2 SDL2_image SDL2_ttf) +LIBFLAGS = $(shell pkg-config --cflags --libs sdl2 SDL2_image SDL2_ttf) OBJ = main.o function.o display.o read.o TARGET = sokoban diff --git a/display.c b/display.c index 307aca8..fa8c2ed 100644 --- a/display.c +++ b/display.c @@ -6,6 +6,7 @@ #include #include #include +#include #include void screenDisplay (unsigned short int **tab, int size) @@ -22,7 +23,7 @@ void screenDisplay (unsigned short int **tab, int size) } } -void screenDisplaySDL (unsigned short int **tab, dis *display_user) +void screenDisplayGameSDL (unsigned short int **tab, dis *display_user) { unsigned int display_game = display_user->size_window - display_user->size_menu; int size = display_game / display_user->size_box; @@ -123,3 +124,25 @@ void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos, } +void displayTextSDL(dis *display_user,char *text, vect coor, vect size, int font_size) +{ + TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24); + SDL_Color white = {255,255,255}; + + SDL_Surface* surface_text = TTF_RenderText_Solid(Sans, text, white); + + SDL_Texture* message = SDL_CreateTextureFromSurface(display_user->renderer, surface_text); + + SDL_Rect message_rect ; + message_rect.x = coor.x; + message_rect.y = coor.y; + message_rect.w = size.x; + message_rect.h = size.y; + + SDL_RenderCopy(display_user-> renderer, message, NULL, &message_rect); + + SDL_FreeSurface(surface_text); + SDL_DestroyTexture(message); +} + + diff --git a/display.h b/display.h index 61235db..64c3472 100644 --- a/display.h +++ b/display.h @@ -3,12 +3,15 @@ #include "function.h" #include +#include void screenDisplay (unsigned short int **tab, int size); int getMaxSize (dis display_user); void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos, int size); void initSDL (dis *display_user); -void screenDisplaySDL (unsigned short int **tab, dis *display_user); +void screenDisplayGameSDL (unsigned short int **tab, dis *display_user); +void displayTextSDL(dis *display_user,char *text, vect coor, vect size, int font_size); + #endif // !DISPLAY_H diff --git a/function.c b/function.c index f60ee8b..4cb76ca 100644 --- a/function.c +++ b/function.c @@ -45,7 +45,7 @@ unsigned short int **creatArea2D (const unsigned int N) void free2D (unsigned short int **tab, int N) { int i; - for (i = 0; i < N; ++i) + for (i = 0; i < N ; ++i) { free (tab[i]); } @@ -238,7 +238,7 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, puts ("lose!"); finish = true; } - screenDisplaySDL (tab2d, display_user); + screenDisplayGameSDL (tab2d, display_user); } last = SDL_SCANCODE_F21; } diff --git a/main.c b/main.c index 8d403b1..53667f3 100644 --- a/main.c +++ b/main.c @@ -7,7 +7,7 @@ #include #include -#define SIZE_PLAY 26 +#define SIZE_PLAY 5 #define SIZE_MENU 200; int main () @@ -27,8 +27,11 @@ int main () playerScore.before = time (NULL); targets - = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); - screenDisplaySDL (tab2d, &display_user); + = fileToTab2D ("test3.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); + screenDisplayGameSDL (tab2d, &display_user); + vect size = {100, 100}; + vect pos = {900, 0}; + displayTextSDL(&display_user, "213", pos, size, 24); screenDisplay (tab2d, SIZE_PLAY); inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, &display_user); diff --git a/read.c b/read.c index 7a6596e..f08ecd7 100644 --- a/read.c +++ b/read.c @@ -8,10 +8,15 @@ vect *fileToTab2D (const char *name_file, unsigned short int **tab, vect *targets = malloc (sizeof (vect)); (*nbr_targets) = 0; FILE *file = fopen (name_file, "r"); - unsigned int x = 0, y = 1; + unsigned int x = 0, y = 0; while (!feof (file)) { char current = fgetc (file); + + if (current == EOF) + { + break; + } switch (current) { case '#': @@ -32,7 +37,7 @@ vect *fileToTab2D (const char *name_file, unsigned short int **tab, tab[x][y] = PLAYER; break; case '\n': - y = 0; + y = -1; ++x; break; default: @@ -40,8 +45,8 @@ vect *fileToTab2D (const char *name_file, unsigned short int **tab, break; } ++y; - - if (x >= N || y >= N) + + if (x > N || y > N) { perror ("Level out of range !"); exit (-1); diff --git a/test3.txt b/test3.txt new file mode 100644 index 0000000..4e84f34 --- /dev/null +++ b/test3.txt @@ -0,0 +1,5 @@ +##### +#PCI# +##### +##### +#####