meilleur format
This commit is contained in:
140
display.c
140
display.c
@@ -8,113 +8,109 @@
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void
|
||||
screenDisplay (unsigned short int **tab, int size)
|
||||
void screenDisplay (unsigned short int **tab, int size)
|
||||
{
|
||||
// puts("0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 ");
|
||||
char element[7] = { ' ', '#', 'S', '.', '*', '@', '+' };
|
||||
for (int i = 0; i < size; ++i)
|
||||
{
|
||||
for (int j = 0; j < size; ++j)
|
||||
{
|
||||
for (int j = 0; j < size; ++j)
|
||||
{
|
||||
printf ("%c ", element[tab[i][j]]);
|
||||
}
|
||||
puts ("");
|
||||
printf ("%c ", element[tab[i][j]]);
|
||||
}
|
||||
puts ("");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
screenDisplaySDL (unsigned short int **tab, dis *display_user)
|
||||
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)
|
||||
{
|
||||
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_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);
|
||||
SDL_RenderPresent (display_user->renderer);
|
||||
}
|
||||
|
||||
int
|
||||
getMaxSize ()
|
||||
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;
|
||||
}
|
||||
{
|
||||
result = display.w;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = display.h;
|
||||
}
|
||||
SDL_Quit();
|
||||
{
|
||||
result = display.h;
|
||||
}
|
||||
SDL_Quit ();
|
||||
return (result - 50); // margin
|
||||
}
|
||||
|
||||
void
|
||||
initSDL (dis *display_user)
|
||||
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);
|
||||
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);
|
||||
}
|
||||
{
|
||||
SDL_Quit ();
|
||||
perror ("Window null");
|
||||
exit (-1);
|
||||
}
|
||||
|
||||
display_user->renderer = SDL_CreateRenderer (display_user->window, -1, SDL_RENDERER_SOFTWARE);
|
||||
display_user->renderer
|
||||
= SDL_CreateRenderer (display_user->window, -1, SDL_RENDERER_SOFTWARE);
|
||||
if (!display_user->renderer)
|
||||
{
|
||||
SDL_Quit ();
|
||||
perror ("Renderer null");
|
||||
exit (-1);
|
||||
}
|
||||
{
|
||||
SDL_Quit ();
|
||||
perror ("Renderer null");
|
||||
exit (-1);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos, int size)
|
||||
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);
|
||||
|
||||
@@ -1,15 +1,14 @@
|
||||
#ifndef DISPLAY_H
|
||||
#define DISPLAY_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "function.h"
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
|
||||
void screenDisplay( unsigned short int **tab,int size);
|
||||
void screenDisplay (unsigned short int **tab, int size);
|
||||
int getMaxSize ();
|
||||
void displayImage(SDL_Renderer *renderer, SDL_Texture *texture, vect pos, int size);
|
||||
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
|
||||
|
||||
|
||||
486
function.c
486
function.c
@@ -6,97 +6,94 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
unsigned short int **
|
||||
creatArea2D (const unsigned int N)
|
||||
unsigned short int **creatArea2D (const unsigned int N)
|
||||
{
|
||||
unsigned short int **tab2d;
|
||||
tab2d = calloc (N, sizeof (unsigned short int *));
|
||||
if (tab2d == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool fail = false;
|
||||
unsigned int i;
|
||||
for (i = 0; i < N && !fail; ++i)
|
||||
{
|
||||
tab2d[i] = calloc (N, sizeof (unsigned short int));
|
||||
if (tab2d[i] == NULL)
|
||||
{
|
||||
tab2d[i] = calloc (N, sizeof (unsigned short int));
|
||||
if (tab2d[i] == NULL)
|
||||
{
|
||||
fail = true;
|
||||
}
|
||||
fail = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail)
|
||||
{
|
||||
unsigned int j;
|
||||
for (j = 0; j < i; ++j)
|
||||
{
|
||||
unsigned int j;
|
||||
for (j = 0; j < i; ++j)
|
||||
{
|
||||
free (tab2d[j]);
|
||||
}
|
||||
free (tab2d);
|
||||
return NULL;
|
||||
free (tab2d[j]);
|
||||
}
|
||||
free (tab2d);
|
||||
return NULL;
|
||||
}
|
||||
return tab2d;
|
||||
}
|
||||
|
||||
void
|
||||
free2D (unsigned short int **tab, int N)
|
||||
void free2D (unsigned short int **tab, int N)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < N; ++i)
|
||||
{
|
||||
free (tab[i]);
|
||||
}
|
||||
{
|
||||
free (tab[i]);
|
||||
}
|
||||
free (tab);
|
||||
return;
|
||||
}
|
||||
|
||||
short int
|
||||
canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
|
||||
short int canIGoDirection (short int valueOfNCase,
|
||||
short int valueOfNPlusOneCase)
|
||||
{
|
||||
if (valueOfNCase != WALL)
|
||||
{
|
||||
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|
||||
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
|
||||
{
|
||||
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|
||||
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
|
||||
{
|
||||
if (valueOfNPlusOneCase == TARGET)
|
||||
{
|
||||
// Box on target
|
||||
return 3;
|
||||
}
|
||||
// move the box
|
||||
return 2;
|
||||
}
|
||||
if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY)
|
||||
|| (valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == TARGET))
|
||||
{
|
||||
if (valueOfNPlusOneCase == TARGET)
|
||||
{
|
||||
// Box on target and player on target too
|
||||
return 6;
|
||||
}
|
||||
// move the box but player on a target
|
||||
return 5;
|
||||
}
|
||||
if (valueOfNCase == TARGET)
|
||||
{
|
||||
// move player on target
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (valueOfNCase == EMPTY)
|
||||
{
|
||||
// move player
|
||||
return 1;
|
||||
}
|
||||
if (valueOfNPlusOneCase == TARGET)
|
||||
{
|
||||
// Box on target
|
||||
return 3;
|
||||
}
|
||||
// move the box
|
||||
return 2;
|
||||
}
|
||||
if ((valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == EMPTY)
|
||||
|| (valueOfNCase == BOX_ON_TARGET && valueOfNPlusOneCase == TARGET))
|
||||
{
|
||||
if (valueOfNPlusOneCase == TARGET)
|
||||
{
|
||||
// Box on target and player on target too
|
||||
return 6;
|
||||
}
|
||||
// move the box but player on a target
|
||||
return 5;
|
||||
}
|
||||
if (valueOfNCase == TARGET)
|
||||
{
|
||||
// move player on target
|
||||
return 4;
|
||||
}
|
||||
|
||||
if (valueOfNCase == EMPTY)
|
||||
{
|
||||
// move player
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
move (unsigned short int **tab, vect *playerPos, vect direction)
|
||||
void move (unsigned short int **tab, vect *playerPos, vect direction)
|
||||
{
|
||||
short int valueOfNCase
|
||||
= tab[playerPos->x + direction.x][playerPos->y + direction.y];
|
||||
@@ -106,186 +103,180 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
|
||||
short int valueOfNPlusOneCase;
|
||||
|
||||
if (add.x < 0 || add.y < 0)
|
||||
{
|
||||
valueOfNPlusOneCase = WALL;
|
||||
}
|
||||
{
|
||||
valueOfNPlusOneCase = WALL;
|
||||
}
|
||||
else
|
||||
{
|
||||
valueOfNPlusOneCase = tab[add.x][add.y];
|
||||
}
|
||||
{
|
||||
valueOfNPlusOneCase = tab[add.x][add.y];
|
||||
}
|
||||
|
||||
short int returnValue = canIGoDirection (valueOfNCase, valueOfNPlusOneCase);
|
||||
short int playerState = tab[playerPos->x][playerPos->y];
|
||||
|
||||
switch (returnValue)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
case 0:
|
||||
|
||||
break;
|
||||
case 1:
|
||||
// move player
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||
break;
|
||||
case 2:
|
||||
// move player and the box
|
||||
break;
|
||||
case 1:
|
||||
// move player
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||
break;
|
||||
case 2:
|
||||
// move player and the box
|
||||
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||
= BOX;
|
||||
break;
|
||||
case 3:
|
||||
// move player and the box is well-placed
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
|
||||
break;
|
||||
case 3:
|
||||
// move player and the box is well-placed
|
||||
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||
= BOX_ON_TARGET;
|
||||
break;
|
||||
case 4:
|
||||
// move player on a target
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||
= BOX_ON_TARGET;
|
||||
break;
|
||||
case 4:
|
||||
// move player on a target
|
||||
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||
= PLAYER_ON_TARGET;
|
||||
break;
|
||||
case 5:
|
||||
// move box and player on a target
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||
= PLAYER_ON_TARGET;
|
||||
break;
|
||||
case 5:
|
||||
// move box and player on a target
|
||||
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||
= BOX;
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||
= PLAYER_ON_TARGET;
|
||||
break;
|
||||
case 6:
|
||||
// move player on a target and box on a target
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||
= PLAYER_ON_TARGET;
|
||||
break;
|
||||
case 6:
|
||||
// move player on a target and box on a target
|
||||
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||
= BOX_ON_TARGET;
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||
= PLAYER_ON_TARGET;
|
||||
break;
|
||||
default:
|
||||
printf ("Commande inconnue !\n");
|
||||
}
|
||||
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
|
||||
= BOX_ON_TARGET;
|
||||
tab[playerPos->x + direction.x][playerPos->y + direction.y]
|
||||
= PLAYER_ON_TARGET;
|
||||
break;
|
||||
default:
|
||||
printf ("Commande inconnue !\n");
|
||||
}
|
||||
|
||||
if (returnValue != 0)
|
||||
{
|
||||
if (playerState == PLAYER_ON_TARGET)
|
||||
{
|
||||
if (playerState == PLAYER_ON_TARGET)
|
||||
{
|
||||
tab[playerPos->x][playerPos->y] = TARGET;
|
||||
}
|
||||
else
|
||||
{
|
||||
tab[playerPos->x][playerPos->y] = EMPTY;
|
||||
}
|
||||
playerPos->x = playerPos->x + direction.x;
|
||||
playerPos->y = playerPos->y + direction.y;
|
||||
tab[playerPos->x][playerPos->y] = TARGET;
|
||||
}
|
||||
else
|
||||
{
|
||||
tab[playerPos->x][playerPos->y] = EMPTY;
|
||||
}
|
||||
playerPos->x = playerPos->x + direction.x;
|
||||
playerPos->y = playerPos->y + direction.y;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
|
||||
int nbr_targets, dis *display_user)
|
||||
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
||||
vect *targets, int nbr_targets, dis *display_user)
|
||||
{
|
||||
vect direction = { 0, 0 };
|
||||
char input;
|
||||
bool finish = false;
|
||||
while (!finish)
|
||||
{
|
||||
printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : ");
|
||||
|
||||
input = getchar ();
|
||||
|
||||
if (input == 'x')
|
||||
{
|
||||
printf ("Utilisez Z/Q/S/D pour bouger, X pour quitter : ");
|
||||
|
||||
input = getchar ();
|
||||
|
||||
if (input == 'x')
|
||||
{
|
||||
break; // Quitter le jeu
|
||||
}
|
||||
|
||||
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");
|
||||
if (isWin (tab2d, targets, nbr_targets))
|
||||
{
|
||||
puts ("Win!");
|
||||
finish = true;
|
||||
}
|
||||
if (islose (tab2d, N))
|
||||
{
|
||||
puts ("lose!");
|
||||
finish = true;
|
||||
}
|
||||
screenDisplay (tab2d, N);
|
||||
screenDisplaySDL(tab2d, display_user);
|
||||
break; // Quitter le jeu
|
||||
}
|
||||
|
||||
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");
|
||||
if (isWin (tab2d, targets, nbr_targets))
|
||||
{
|
||||
puts ("Win!");
|
||||
finish = true;
|
||||
}
|
||||
if (islose (tab2d, N))
|
||||
{
|
||||
puts ("lose!");
|
||||
finish = true;
|
||||
}
|
||||
screenDisplay (tab2d, N);
|
||||
screenDisplaySDL (tab2d, display_user);
|
||||
}
|
||||
}
|
||||
|
||||
bool
|
||||
isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
|
||||
bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < nbr_targets; ++i)
|
||||
{
|
||||
int x = targets[i].x;
|
||||
int y = targets[i].y;
|
||||
if (tab2d[x][y] != BOX_ON_TARGET)
|
||||
{
|
||||
int x = targets[i].x;
|
||||
int y = targets[i].y;
|
||||
if (tab2d[x][y] != BOX_ON_TARGET)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
islose (unsigned short int **tab2d, const int N)
|
||||
bool islose (unsigned short int **tab2d, const int N)
|
||||
{
|
||||
int x = 0, y = 0;
|
||||
for (x = 0; x < N; ++x)
|
||||
{
|
||||
for (y = 0; y < N; ++y)
|
||||
{
|
||||
for (y = 0; y < N; ++y)
|
||||
{
|
||||
bool isblock = false;
|
||||
if (tab2d[x][y] == BOX)
|
||||
{
|
||||
vect box;
|
||||
box.x = x;
|
||||
box.y = y;
|
||||
isblock = blockBox (tab2d, box);
|
||||
}
|
||||
bool isblock = false;
|
||||
if (tab2d[x][y] == BOX)
|
||||
{
|
||||
vect box;
|
||||
box.x = x;
|
||||
box.y = y;
|
||||
isblock = blockBox (tab2d, box);
|
||||
}
|
||||
|
||||
if (isblock)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (isblock)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
vect
|
||||
plusVect (vect one, vect two)
|
||||
vect plusVect (vect one, vect two)
|
||||
{
|
||||
vect result;
|
||||
result.x = one.x + two.x;
|
||||
@@ -293,14 +284,9 @@ plusVect (vect one, vect two)
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
lengthVect (vect vector)
|
||||
{
|
||||
return abs (vector.x) + abs (vector.y);
|
||||
}
|
||||
int lengthVect (vect vector) { return abs (vector.x) + abs (vector.y); }
|
||||
|
||||
bool
|
||||
blockBox (unsigned short int **tab2d, vect box_coor)
|
||||
bool blockBox (unsigned short int **tab2d, vect box_coor)
|
||||
{
|
||||
int nbr_touch = 0;
|
||||
vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 0 } };
|
||||
@@ -308,51 +294,51 @@ blockBox (unsigned short int **tab2d, vect box_coor)
|
||||
int id_box = -1;
|
||||
int i;
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
vect test = plusVect (box_coor, card[i]);
|
||||
unsigned short int val = tab2d[test.x][test.y];
|
||||
if (val == BOX || val == WALL || val == BOX_ON_TARGET)
|
||||
{
|
||||
vect test = plusVect (box_coor, card[i]);
|
||||
unsigned short int val = tab2d[test.x][test.y];
|
||||
if (val == BOX || val == WALL || val == BOX_ON_TARGET)
|
||||
{
|
||||
nbr_touch++;
|
||||
if (val == BOX || val == BOX_ON_TARGET)
|
||||
{
|
||||
id_box = nbr_touch - 1;
|
||||
}
|
||||
indice_card[nbr_touch - 1] = i;
|
||||
}
|
||||
nbr_touch++;
|
||||
if (val == BOX || val == BOX_ON_TARGET)
|
||||
{
|
||||
id_box = nbr_touch - 1;
|
||||
}
|
||||
indice_card[nbr_touch - 1] = i;
|
||||
}
|
||||
}
|
||||
if (nbr_touch == 0)
|
||||
return false;
|
||||
|
||||
if (nbr_touch >= 3)
|
||||
{
|
||||
if (id_box != -1)
|
||||
{
|
||||
if (id_box != -1)
|
||||
{
|
||||
vect test = plusVect (card[indice_card[(id_box + 1) % 3]],
|
||||
card[indice_card[(id_box + 2) % 3]]);
|
||||
if (test.x == 0 && test.y == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
vect test = plusVect (card[indice_card[(id_box + 1) % 3]],
|
||||
card[indice_card[(id_box + 2) % 3]]);
|
||||
if (test.x == 0 && test.y == 0)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
if (nbr_touch == 2)
|
||||
{
|
||||
int id1 = indice_card[0];
|
||||
int id2 = indice_card[1];
|
||||
vect add = plusVect (card[id1], card[id2]);
|
||||
if (lengthVect (add) == 2)
|
||||
{
|
||||
int id1 = indice_card[0];
|
||||
int id2 = indice_card[1];
|
||||
vect add = plusVect (card[id1], card[id2]);
|
||||
if (lengthVect (add) == 2)
|
||||
{
|
||||
if (id_box == -1)
|
||||
return true;
|
||||
vect test = plusVect (card[indice_card[id_box]],
|
||||
card[indice_card[(id_box + 1) % 2]]);
|
||||
test = plusVect (test, box_coor);
|
||||
if (tab2d[test.x][test.y] == WALL)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
if (id_box == -1)
|
||||
return true;
|
||||
vect test = plusVect (card[indice_card[id_box]],
|
||||
card[indice_card[(id_box + 1) % 2]]);
|
||||
test = plusVect (test, box_coor);
|
||||
if (tab2d[test.x][test.y] == WALL)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
vect normal = card[indice_card[0]];
|
||||
vect director1 = card[(indice_card[0] + 1) % 4];
|
||||
@@ -362,24 +348,24 @@ blockBox (unsigned short int **tab2d, vect box_coor)
|
||||
bool wall = false;
|
||||
|
||||
for (int i = 0; i < 2; ++i)
|
||||
{
|
||||
vect current = plusVect (box_coor, dir_test[i]);
|
||||
while (!wall)
|
||||
{
|
||||
vect current = plusVect (box_coor, dir_test[i]);
|
||||
while (!wall)
|
||||
{
|
||||
unsigned short int val = tab2d[current.x][current.y];
|
||||
if (val == WALL)
|
||||
wall = true;
|
||||
if (val == TARGET)
|
||||
return false;
|
||||
unsigned short int val = tab2d[current.x][current.y];
|
||||
if (val == WALL)
|
||||
wall = true;
|
||||
if (val == TARGET)
|
||||
return false;
|
||||
|
||||
vect cur_nor = plusVect (current, normal);
|
||||
val = tab2d[cur_nor.x][cur_nor.y];
|
||||
if (val != WALL || val != BOX || val != BOX_ON_TARGET)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
vect cur_nor = plusVect (current, normal);
|
||||
val = tab2d[cur_nor.x][cur_nor.y];
|
||||
if (val != WALL || val != BOX || val != BOX_ON_TARGET)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
perror ("error in blockBox");
|
||||
exit (0);
|
||||
|
||||
11
function.h
11
function.h
@@ -1,12 +1,12 @@
|
||||
#ifndef FONCTION_H
|
||||
#define FONCTION_H
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#define EMPTY 0
|
||||
#define WALL 1
|
||||
@@ -32,11 +32,11 @@ typedef struct Score
|
||||
|
||||
typedef struct essential_sdl
|
||||
{
|
||||
SDL_Window *window ;
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
unsigned int size_window;
|
||||
unsigned int size_box;
|
||||
}dis;
|
||||
} dis;
|
||||
|
||||
unsigned short int **creatArea2D (const unsigned int N);
|
||||
void free2D (unsigned short int **tab, int N);
|
||||
@@ -45,8 +45,7 @@ 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);
|
||||
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
|
||||
vect *targets, int nbr_targets, dis *display_user);
|
||||
|
||||
#endif // FONCTION_H
|
||||
|
||||
30
main.c
30
main.c
@@ -6,45 +6,39 @@
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
|
||||
#define SIZE_PLAY 30
|
||||
|
||||
int
|
||||
main ()
|
||||
int main ()
|
||||
{
|
||||
dis display_user;
|
||||
display_user.size_window = getMaxSize();
|
||||
display_user.size_window = getMaxSize ();
|
||||
display_user.size_box = SIZE_PLAY;
|
||||
initSDL(&display_user);
|
||||
initSDL (&display_user);
|
||||
|
||||
vect *playerPos = (vect *)malloc (sizeof (vect));
|
||||
vect *targets;
|
||||
int nbr_targets;
|
||||
score playerScore;
|
||||
unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
|
||||
|
||||
|
||||
playerScore.before = time (NULL);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
targets = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
|
||||
screenDisplaySDL(tab2d, &display_user);
|
||||
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, &display_user);
|
||||
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);
|
||||
SDL_DestroyWindow (display_user.window);
|
||||
SDL_DestroyRenderer (display_user.renderer);
|
||||
free2D (tab2d, SIZE_PLAY);
|
||||
free (playerPos);
|
||||
free (targets);
|
||||
SDL_Quit();
|
||||
SDL_Quit ();
|
||||
return 0;
|
||||
}
|
||||
|
||||
75
read.c
75
read.c
@@ -2,52 +2,51 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
vect *
|
||||
fileToTab2D (const char *name_file, unsigned short int **tab, const unsigned N,
|
||||
vect *player, int *nbr_targets)
|
||||
vect *fileToTab2D (const char *name_file, unsigned short int **tab,
|
||||
const unsigned N, vect *player, int *nbr_targets)
|
||||
{
|
||||
vect *targets = malloc (sizeof (vect));
|
||||
(*nbr_targets) = 0;
|
||||
FILE *file = fopen (name_file, "r");
|
||||
unsigned int x = 0, y = 1;
|
||||
while (!feof (file))
|
||||
{
|
||||
char current = fgetc (file);
|
||||
switch (current)
|
||||
{
|
||||
char current = fgetc (file);
|
||||
switch (current)
|
||||
{
|
||||
case '#':
|
||||
tab[x][y] = WALL;
|
||||
break;
|
||||
case 'C':
|
||||
tab[x][y] = BOX;
|
||||
break;
|
||||
case 'I':
|
||||
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
|
||||
targets[nbr_targets[0] - 1].x = x;
|
||||
targets[nbr_targets[0] - 1].y = y;
|
||||
tab[x][y] = TARGET;
|
||||
break;
|
||||
case 'P':
|
||||
player->x = x;
|
||||
player->y = y;
|
||||
tab[x][y] = PLAYER;
|
||||
break;
|
||||
case '\n':
|
||||
y = 0;
|
||||
++x;
|
||||
break;
|
||||
default:
|
||||
tab[x][y] = EMPTY;
|
||||
break;
|
||||
}
|
||||
++y;
|
||||
|
||||
if (x >= N || y >= N)
|
||||
{
|
||||
perror ("Level out of range !");
|
||||
exit (-1);
|
||||
}
|
||||
case '#':
|
||||
tab[x][y] = WALL;
|
||||
break;
|
||||
case 'C':
|
||||
tab[x][y] = BOX;
|
||||
break;
|
||||
case 'I':
|
||||
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
|
||||
targets[nbr_targets[0] - 1].x = x;
|
||||
targets[nbr_targets[0] - 1].y = y;
|
||||
tab[x][y] = TARGET;
|
||||
break;
|
||||
case 'P':
|
||||
player->x = x;
|
||||
player->y = y;
|
||||
tab[x][y] = PLAYER;
|
||||
break;
|
||||
case '\n':
|
||||
y = 0;
|
||||
++x;
|
||||
break;
|
||||
default:
|
||||
tab[x][y] = EMPTY;
|
||||
break;
|
||||
}
|
||||
++y;
|
||||
|
||||
if (x >= N || y >= N)
|
||||
{
|
||||
perror ("Level out of range !");
|
||||
exit (-1);
|
||||
}
|
||||
}
|
||||
fclose (file);
|
||||
return targets;
|
||||
}
|
||||
|
||||
58
sdl_test.c
58
sdl_test.c
@@ -1,58 +0,0 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_events.h>
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
#include <SDL2/SDL_rect.h>
|
||||
#include <SDL2/SDL_render.h>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
#include <SDL2/SDL_video.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
SDL_Rect displayBounds;
|
||||
displayBounds.w = 800;
|
||||
displayBounds.h = 800;
|
||||
SDL_Window *window = SDL_CreateWindow("titre test",
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
SDL_WINDOWPOS_CENTERED,
|
||||
displayBounds.w, displayBounds.h,
|
||||
SDL_WINDOW_SHOWN);
|
||||
|
||||
int running = 1;
|
||||
SDL_Event event;
|
||||
while (running)
|
||||
{
|
||||
while(SDL_PollEvent(&event))
|
||||
{
|
||||
if (event.type ==SDL_QUIT)
|
||||
{
|
||||
running = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
|
||||
SDL_Rect rect;
|
||||
rect.x = 100;
|
||||
rect.y = 100;
|
||||
rect.h = 100;
|
||||
rect.w = 150;
|
||||
SDL_RenderDrawRect(renderer, &rect);
|
||||
rect.x = 300;
|
||||
rect.y = 300;
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
SDL_UpdateWindowSurface(window);
|
||||
}
|
||||
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user