diff --git a/function.c b/function.c index f39467f..3f32277 100644 --- a/function.c +++ b/function.c @@ -1,7 +1,10 @@ #include "function.h" #include "display.h" +#include +#include #include +#include #include #include #include @@ -183,48 +186,52 @@ void move (unsigned short int **tab, vect *playerPos, vect direction) void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, int nbr_targets, dis *display_user) { + char last = SDL_SCANCODE_1; vect direction = { 0, 0 }; char input; bool finish = false; + SDL_Event event; while (!finish) { - printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : "); + //printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : "); + SDL_PollEvent (&event); - input = getchar (); - - if (input == 'x') + if (event.type == SDL_KEYDOWN) { - break; // Quitter le jeu + input = event.key.keysym.scancode; + printf ("%c\n", input); + switch (input) + { + case SDL_SCANCODE_ESCAPE: + finish = true; + break; + case SDL_SCANCODE_D: + direction.y = 1; + direction.x = 0; + break; + case SDL_SCANCODE_A: + direction.y = -1; + direction.x = 0; + break; + case SDL_SCANCODE_W: + direction.x = -1; + direction.y = 0; + break; + case SDL_SCANCODE_S: + direction.x = 1; + direction.y = 0; + break; + default: + direction.x = 0; + direction.y = 0; + last = SDL_SCANCODE_1; + + } } - switch (input) - { - case 'd': - direction.y = 1; - direction.x = 0; - - break; - case 'q': - direction.y = -1; - direction.x = 0; - break; - case 'z': - direction.x = -1; - direction.y = 0; - break; - case 's': - direction.x = 1; - direction.y = 0; - break; - default: - direction.x = 0; - direction.y = 0; - } - - system ("clear"); - move (tab2d, playerPos, direction); - - printf ("\n"); + //system ("clear"); + if (last != input) move (tab2d, playerPos, direction); + last = input; if (isWin (tab2d, targets, nbr_targets)) { puts ("Win!"); @@ -235,8 +242,9 @@ void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, puts ("lose!"); finish = true; } - screenDisplay (tab2d, N); + //screenDisplay (tab2d, N); screenDisplaySDL (tab2d, display_user); + //SDL_Delay (1000); } }