diff --git a/Makefile b/Makefile index 1160cae..c4b98a8 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Variable CC = gcc -CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2) +CFLAGS = -Wall -Wextra $(shell pkg-config --cflags --libs sdl2 SDL2_image) OBJ = main.o function.o display.o read.o TARGET = sokoban @@ -15,7 +15,7 @@ main.o : main.c function.h display.h read.h function.o : function.c function.h $(CC) $(CFLAGS) -c function.c -display.o: display.c display.h +display.o: display.c display.h function.h $(CC) $(CFLAGS) -c display.c read.o: read.c read.h function.h diff --git a/box.png b/box.png new file mode 100644 index 0000000..7bb051a --- /dev/null +++ b/box.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:17dffc2e93e18815bafb4e4c08a8594bc0d10570ec5e1840e990efaeed34fd0a +size 4210 diff --git a/box_on_target.png b/box_on_target.png new file mode 100644 index 0000000..a06da3d --- /dev/null +++ b/box_on_target.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e08d3380f57c28a3d3311be9a7e6262c115b2d45988806965f66cf6010fae49 +size 5127 diff --git a/display.c b/display.c index 45b8ff0..8b70106 100644 --- a/display.c +++ b/display.c @@ -1,4 +1,11 @@ #include "display.h" +#include "function.h" +#include +#include +#include +#include +#include +#include #include void @@ -15,3 +22,100 @@ screenDisplay (unsigned short int **tab, int size) puts (""); } } + +void +screenDisplaySDL (unsigned short int **tab, dis *display_user) +{ + int size = display_user->size_window / display_user->size_box; + int i, j; + + for (i = 0; i < display_user->size_box; ++i) + { + for (j = 0; j < display_user->size_box; ++j) + { + SDL_Surface *img; + SDL_Texture *texture; + vect pos = { i*size, j *size}; + switch(tab[j][i]) + { + case EMPTY: + img = IMG_Load("empty.png") ; + break; + case WALL: + img = IMG_Load("wall.png") ; + break; + case PLAYER: + img = IMG_Load("player.png"); + break; + case TARGET: + img = IMG_Load("target.png") ; + break; + case BOX: + img = IMG_Load("box.png"); + break; + case BOX_ON_TARGET: + img = IMG_Load("box_on_target.png") ; + break; + case PLAYER_ON_TARGET: + img = IMG_Load("player_on_target.png"); + break; + + } + texture = SDL_CreateTextureFromSurface(display_user->renderer, img); + displayImage (display_user->renderer, texture, pos, size); + SDL_FreeSurface(img); + SDL_DestroyTexture(texture); + } + } + + SDL_RenderPresent(display_user->renderer); +} + +int +getMaxSize () +{ + SDL_Init (SDL_INIT_VIDEO); // init if error + SDL_DisplayMode display; + SDL_GetCurrentDisplayMode (0, &display); // get dim display user + int result = 0; + if (display.w <= display.h) + { + result = display.w; + } + else + { + result = display.h; + } + SDL_Quit(); + return (result - 50); // margin +} + +void +initSDL (dis *display_user) +{ + SDL_Init (SDL_INIT_VIDEO); + display_user->window = SDL_CreateWindow ("Sokoman", SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, display_user->size_window, display_user->size_window, + SDL_WINDOW_SHOWN); + if (!display_user->window) + { + SDL_Quit (); + perror ("Window null"); + exit (-1); + } + + display_user->renderer = SDL_CreateRenderer (display_user->window, -1, SDL_RENDERER_SOFTWARE); + if (!display_user->renderer) + { + SDL_Quit (); + perror ("Renderer null"); + exit (-1); + } +} + +void +displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos, int size) +{ + SDL_Rect rect = { pos.x, pos.y, size, size }; + SDL_RenderCopy (renderer, texture, NULL, &rect); +} diff --git a/display.h b/display.h index a063828..f151909 100644 --- a/display.h +++ b/display.h @@ -1,7 +1,15 @@ #ifndef DISPLAY_H #define DISPLAY_H +#include +#include "function.h" + + void screenDisplay( unsigned short int **tab,int size); +int getMaxSize (); +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); #endif // !DISPLAY_H diff --git a/empty.png b/empty.png new file mode 100644 index 0000000..e2aafb4 --- /dev/null +++ b/empty.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d114a0681b60523c40904c0e1af1e2986e5016d90eecec76c96eb0273c0bf6c4 +size 215 diff --git a/function.c b/function.c index e509f5f..c88df77 100644 --- a/function.c +++ b/function.c @@ -1,5 +1,7 @@ #include "function.h" +#include "display.h" +#include #include #include #include @@ -181,7 +183,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction) void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, - int nbr_targets) + int nbr_targets, dis *display_user) { vect direction = { 0, 0 }; char input; @@ -236,6 +238,7 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, finish = true; } screenDisplay (tab2d, N); + screenDisplaySDL(tab2d, display_user); } } diff --git a/function.h b/function.h index c017909..0e218fa 100644 --- a/function.h +++ b/function.h @@ -1,9 +1,12 @@ #ifndef FONCTION_H #define FONCTION_H +#include +#include #include #include #include +#include #define EMPTY 0 #define WALL 1 @@ -27,14 +30,23 @@ typedef struct Score unsigned int move_box; } score; +typedef struct essential_sdl +{ + SDL_Window *window ; + SDL_Renderer *renderer; + unsigned int size_window; + unsigned int size_box; +}dis; + unsigned short int **creatArea2D (const unsigned int N); void free2D (unsigned short int **tab, int N); void screenDisplay (unsigned short int **tab, int size); -void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, - vect *targets, int nbr_targets); bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets); bool islose (unsigned short int **tab2d, const int N); bool blockBox (unsigned short int **tab2d, vect box_coor); vect plusVect (vect one, vect two); +void +inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, + int nbr_targets, dis *display_user); #endif // FONCTION_H diff --git a/main.c b/main.c index 1a2d074..3fbbc1e 100644 --- a/main.c +++ b/main.c @@ -1,29 +1,50 @@ #include "display.h" #include "function.h" #include "read.h" +#include +#include +#include #include #include +#include +#include -#define SIZE_PLAY 24 + +#define SIZE_PLAY 30 int main () { + dis display_user; + display_user.size_window = getMaxSize(); + display_user.size_box = SIZE_PLAY; + initSDL(&display_user); + vect *playerPos = (vect *)malloc (sizeof (vect)); vect *targets; int nbr_targets; score playerScore; - playerScore.before = time (NULL); unsigned short int **tab2d = creatArea2D (SIZE_PLAY); - targets - = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); + + playerScore.before = time (NULL); + + + + + + targets = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets); + screenDisplaySDL(tab2d, &display_user); screenDisplay (tab2d, SIZE_PLAY); - inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets); + inGameLoop (tab2d, SIZE_PLAY, playerPos, targets, nbr_targets, &display_user); playerScore.after = time (NULL); printf ("%ld\n", playerScore.after - playerScore.before); + + SDL_DestroyWindow(display_user.window); + SDL_DestroyRenderer(display_user.renderer); free2D (tab2d, SIZE_PLAY); free (playerPos); free (targets); + SDL_Quit(); return 0; } diff --git a/player.png b/player.png new file mode 100644 index 0000000..3a72ace --- /dev/null +++ b/player.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4886d57b7b6b31b0071b3e50fa38add41e4ad86e390fcc12ec20e05ebf0b5ea5 +size 3651 diff --git a/player_on_target.png b/player_on_target.png new file mode 100644 index 0000000..3a72ace --- /dev/null +++ b/player_on_target.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4886d57b7b6b31b0071b3e50fa38add41e4ad86e390fcc12ec20e05ebf0b5ea5 +size 3651 diff --git a/target.png b/target.png new file mode 100644 index 0000000..7f87103 --- /dev/null +++ b/target.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f5cdaabb8d36a50740dd0ea1bea1e40134201bd1f066d0dd0cdd1012dbaf1305 +size 674 diff --git a/test.png b/test.png new file mode 100644 index 0000000..136dc0e --- /dev/null +++ b/test.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:099e656df97e12fd78e024e706987ffafd6027ba5884f270c704459621d84770 +size 4548 diff --git a/wall.png b/wall.png new file mode 100644 index 0000000..136dc0e --- /dev/null +++ b/wall.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:099e656df97e12fd78e024e706987ffafd6027ba5884f270c704459621d84770 +size 4548