meilleur format

This commit is contained in:
2024-12-14 15:22:51 +01:00
parent 54e7f38ff2
commit af546b8440
7 changed files with 362 additions and 447 deletions

140
display.c
View File

@@ -8,113 +8,109 @@
#include <SDL2/SDL_surface.h> #include <SDL2/SDL_surface.h>
#include <stdio.h> #include <stdio.h>
void void screenDisplay (unsigned short int **tab, int size)
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 "); // 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', '.', '*', '@', '+' }; char element[7] = { ' ', '#', 'S', '.', '*', '@', '+' };
for (int i = 0; i < size; ++i) 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]]);
{
printf ("%c ", element[tab[i][j]]);
}
puts ("");
} }
puts ("");
}
} }
void void screenDisplaySDL (unsigned short int **tab, dis *display_user)
screenDisplaySDL (unsigned short int **tab, dis *display_user)
{ {
int size = display_user->size_window / display_user->size_box; int size = display_user->size_window / display_user->size_box;
int i, j; int i, j;
for (i = 0; i < display_user->size_box; ++i) 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;
SDL_Surface *img; vect pos = { i * size, j * size };
SDL_Texture *texture; switch (tab[j][i])
vect pos = { i*size, j *size}; {
switch(tab[j][i]) case EMPTY:
{ img = IMG_Load ("empty.png");
case EMPTY: break;
img = IMG_Load("empty.png") ; case WALL:
break; img = IMG_Load ("wall.png");
case WALL: break;
img = IMG_Load("wall.png") ; case PLAYER:
break; img = IMG_Load ("player.png");
case PLAYER: break;
img = IMG_Load("player.png"); case TARGET:
break; img = IMG_Load ("target.png");
case TARGET: break;
img = IMG_Load("target.png") ; case BOX:
break; img = IMG_Load ("box.png");
case BOX: break;
img = IMG_Load("box.png"); case BOX_ON_TARGET:
break; img = IMG_Load ("box_on_target.png");
case BOX_ON_TARGET: break;
img = IMG_Load("box_on_target.png") ; case PLAYER_ON_TARGET:
break; img = IMG_Load ("player_on_target.png");
case PLAYER_ON_TARGET: break;
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);
texture = SDL_CreateTextureFromSurface(display_user->renderer, img); SDL_DestroyTexture (texture);
displayImage (display_user->renderer, texture, pos, size);
SDL_FreeSurface(img);
SDL_DestroyTexture(texture);
}
} }
}
SDL_RenderPresent(display_user->renderer); SDL_RenderPresent (display_user->renderer);
} }
int int getMaxSize ()
getMaxSize ()
{ {
SDL_Init (SDL_INIT_VIDEO); // init if error SDL_Init (SDL_INIT_VIDEO); // init if error
SDL_DisplayMode display; SDL_DisplayMode display;
SDL_GetCurrentDisplayMode (0, &display); // get dim display user SDL_GetCurrentDisplayMode (0, &display); // get dim display user
int result = 0; int result = 0;
if (display.w <= display.h) if (display.w <= display.h)
{ {
result = display.w; result = display.w;
} }
else else
{ {
result = display.h; result = display.h;
} }
SDL_Quit(); SDL_Quit ();
return (result - 50); // margin return (result - 50); // margin
} }
void void initSDL (dis *display_user)
initSDL (dis *display_user)
{ {
SDL_Init (SDL_INIT_VIDEO); SDL_Init (SDL_INIT_VIDEO);
display_user->window = SDL_CreateWindow ("Sokoman", SDL_WINDOWPOS_CENTERED, display_user->window = SDL_CreateWindow (
SDL_WINDOWPOS_CENTERED, display_user->size_window, display_user->size_window, "Sokoman", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SDL_WINDOW_SHOWN); display_user->size_window, display_user->size_window, SDL_WINDOW_SHOWN);
if (!display_user->window) if (!display_user->window)
{ {
SDL_Quit (); SDL_Quit ();
perror ("Window null"); perror ("Window null");
exit (-1); 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) if (!display_user->renderer)
{ {
SDL_Quit (); SDL_Quit ();
perror ("Renderer null"); perror ("Renderer null");
exit (-1); exit (-1);
} }
} }
void void displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos,
displayImage (SDL_Renderer *renderer, SDL_Texture *texture, vect pos, int size) int size)
{ {
SDL_Rect rect = { pos.x, pos.y, size, size }; SDL_Rect rect = { pos.x, pos.y, size, size };
SDL_RenderCopy (renderer, texture, NULL, &rect); SDL_RenderCopy (renderer, texture, NULL, &rect);

View File

@@ -1,15 +1,14 @@
#ifndef DISPLAY_H #ifndef DISPLAY_H
#define DISPLAY_H #define DISPLAY_H
#include <SDL2/SDL.h>
#include "function.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 (); 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 initSDL (dis *display_user);
void screenDisplaySDL (unsigned short int **tab, dis *display_user); void screenDisplaySDL (unsigned short int **tab, dis *display_user);
#endif // !DISPLAY_H #endif // !DISPLAY_H

View File

@@ -6,97 +6,94 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
unsigned short int ** unsigned short int **creatArea2D (const unsigned int N)
creatArea2D (const unsigned int N)
{ {
unsigned short int **tab2d; unsigned short int **tab2d;
tab2d = calloc (N, sizeof (unsigned short int *)); tab2d = calloc (N, sizeof (unsigned short int *));
if (tab2d == NULL) if (tab2d == NULL)
{ {
return NULL; return NULL;
} }
bool fail = false; bool fail = false;
unsigned int i; unsigned int i;
for (i = 0; i < N && !fail; ++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)); fail = true;
if (tab2d[i] == NULL)
{
fail = true;
}
} }
}
if (fail) if (fail)
{
unsigned int j;
for (j = 0; j < i; ++j)
{ {
unsigned int j; free (tab2d[j]);
for (j = 0; j < i; ++j)
{
free (tab2d[j]);
}
free (tab2d);
return NULL;
} }
free (tab2d);
return NULL;
}
return tab2d; return tab2d;
} }
void void free2D (unsigned short int **tab, int N)
free2D (unsigned short int **tab, int N)
{ {
int i; int i;
for (i = 0; i < N; ++i) for (i = 0; i < N; ++i)
{ {
free (tab[i]); free (tab[i]);
} }
free (tab); free (tab);
return; return;
} }
short int short int canIGoDirection (short int valueOfNCase,
canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase) short int valueOfNPlusOneCase)
{ {
if (valueOfNCase != WALL) if (valueOfNCase != WALL)
{
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY)
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET))
{ {
if ((valueOfNCase == BOX && valueOfNPlusOneCase == EMPTY) if (valueOfNPlusOneCase == TARGET)
|| (valueOfNCase == BOX && valueOfNPlusOneCase == TARGET)) {
{ // Box on target
if (valueOfNPlusOneCase == TARGET) return 3;
{ }
// Box on target // move the box
return 3; return 2;
}
// 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 ((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; return 0;
} }
void void move (unsigned short int **tab, vect *playerPos, vect direction)
move (unsigned short int **tab, vect *playerPos, vect direction)
{ {
short int valueOfNCase short int valueOfNCase
= tab[playerPos->x + direction.x][playerPos->y + direction.y]; = 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; short int valueOfNPlusOneCase;
if (add.x < 0 || add.y < 0) if (add.x < 0 || add.y < 0)
{ {
valueOfNPlusOneCase = WALL; valueOfNPlusOneCase = WALL;
} }
else else
{ {
valueOfNPlusOneCase = tab[add.x][add.y]; valueOfNPlusOneCase = tab[add.x][add.y];
} }
short int returnValue = canIGoDirection (valueOfNCase, valueOfNPlusOneCase); short int returnValue = canIGoDirection (valueOfNCase, valueOfNPlusOneCase);
short int playerState = tab[playerPos->x][playerPos->y]; short int playerState = tab[playerPos->x][playerPos->y];
switch (returnValue) switch (returnValue)
{ {
case 0: case 0:
break; break;
case 1: case 1:
// move player // move player
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
break; break;
case 2: case 2:
// move player and the box // move player and the box
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
= BOX; break;
break; case 3:
case 3: // move player and the box is well-placed
// move player and the box is well-placed
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER; tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET; = BOX_ON_TARGET;
break; break;
case 4: case 4:
// move player on a target // move player on a target
tab[playerPos->x + direction.x][playerPos->y + direction.y] tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET; = PLAYER_ON_TARGET;
break; break;
case 5: case 5:
// move box and player on a target // move box and player on a target
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
= BOX; tab[playerPos->x + direction.x][playerPos->y + direction.y]
tab[playerPos->x + direction.x][playerPos->y + direction.y] = PLAYER_ON_TARGET;
= PLAYER_ON_TARGET; break;
break; case 6:
case 6: // move player on a target and box on a target
// move player on a target and box on a target
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2]
= BOX_ON_TARGET; = BOX_ON_TARGET;
tab[playerPos->x + direction.x][playerPos->y + direction.y] tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET; = PLAYER_ON_TARGET;
break; break;
default: default:
printf ("Commande inconnue !\n"); printf ("Commande inconnue !\n");
} }
if (returnValue != 0) if (returnValue != 0)
{
if (playerState == PLAYER_ON_TARGET)
{ {
if (playerState == PLAYER_ON_TARGET) tab[playerPos->x][playerPos->y] = 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;
} }
else
{
tab[playerPos->x][playerPos->y] = EMPTY;
}
playerPos->x = playerPos->x + direction.x;
playerPos->y = playerPos->y + direction.y;
}
} }
void void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, vect *targets, int nbr_targets, dis *display_user)
int nbr_targets, dis *display_user)
{ {
vect direction = { 0, 0 }; vect direction = { 0, 0 };
char input; char input;
bool finish = false; bool finish = false;
while (!finish) 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 : "); break; // Quitter le jeu
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);
} }
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 bool isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
{ {
int i; int i;
for (i = 0; i < nbr_targets; ++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; return false;
int y = targets[i].y;
if (tab2d[x][y] != BOX_ON_TARGET)
{
return false;
}
} }
}
return true; return true;
} }
bool bool islose (unsigned short int **tab2d, const int N)
islose (unsigned short int **tab2d, const int N)
{ {
int x = 0, y = 0; int x = 0, y = 0;
for (x = 0; x < N; ++x) 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)
bool isblock = false; {
if (tab2d[x][y] == BOX) vect box;
{ box.x = x;
vect box; box.y = y;
box.x = x; isblock = blockBox (tab2d, box);
box.y = y; }
isblock = blockBox (tab2d, box);
}
if (isblock) if (isblock)
{ {
return true; return true;
} }
}
} }
}
return false; return false;
} }
vect vect plusVect (vect one, vect two)
plusVect (vect one, vect two)
{ {
vect result; vect result;
result.x = one.x + two.x; result.x = one.x + two.x;
@@ -293,14 +284,9 @@ plusVect (vect one, vect two)
return result; return result;
} }
int int lengthVect (vect vector) { return abs (vector.x) + abs (vector.y); }
lengthVect (vect vector)
{
return abs (vector.x) + abs (vector.y);
}
bool bool blockBox (unsigned short int **tab2d, vect box_coor)
blockBox (unsigned short int **tab2d, vect box_coor)
{ {
int nbr_touch = 0; int nbr_touch = 0;
vect card[4] = { { 0, -1 }, { 1, 0 }, { 0, 1 }, { -1, 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 id_box = -1;
int i; int i;
for (i = 0; i < 4; ++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]); nbr_touch++;
unsigned short int val = tab2d[test.x][test.y]; if (val == BOX || val == BOX_ON_TARGET)
if (val == BOX || val == WALL || val == BOX_ON_TARGET) {
{ id_box = nbr_touch - 1;
nbr_touch++; }
if (val == BOX || val == BOX_ON_TARGET) indice_card[nbr_touch - 1] = i;
{
id_box = nbr_touch - 1;
}
indice_card[nbr_touch - 1] = i;
}
} }
}
if (nbr_touch == 0) if (nbr_touch == 0)
return false; return false;
if (nbr_touch >= 3) 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]]);
vect test = plusVect (card[indice_card[(id_box + 1) % 3]], if (test.x == 0 && test.y == 0)
card[indice_card[(id_box + 2) % 3]]); return false;
if (test.x == 0 && test.y == 0)
return false;
}
return true;
} }
return true;
}
if (nbr_touch == 2) 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]; if (id_box == -1)
int id2 = indice_card[1]; return true;
vect add = plusVect (card[id1], card[id2]); vect test = plusVect (card[indice_card[id_box]],
if (lengthVect (add) == 2) card[indice_card[(id_box + 1) % 2]]);
{ test = plusVect (test, box_coor);
if (id_box == -1) if (tab2d[test.x][test.y] == WALL)
return true; 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;
} }
return false;
}
vect normal = card[indice_card[0]]; vect normal = card[indice_card[0]];
vect director1 = card[(indice_card[0] + 1) % 4]; vect director1 = card[(indice_card[0] + 1) % 4];
@@ -362,24 +348,24 @@ blockBox (unsigned short int **tab2d, vect box_coor)
bool wall = false; bool wall = false;
for (int i = 0; i < 2; ++i) 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]); unsigned short int val = tab2d[current.x][current.y];
while (!wall) if (val == WALL)
{ wall = true;
unsigned short int val = tab2d[current.x][current.y]; if (val == TARGET)
if (val == WALL) return false;
wall = true;
if (val == TARGET)
return false;
vect cur_nor = plusVect (current, normal); vect cur_nor = plusVect (current, normal);
val = tab2d[cur_nor.x][cur_nor.y]; val = tab2d[cur_nor.x][cur_nor.y];
if (val != WALL || val != BOX || val != BOX_ON_TARGET) if (val != WALL || val != BOX || val != BOX_ON_TARGET)
{ {
return false; return false;
} }
}
} }
}
return true; return true;
perror ("error in blockBox"); perror ("error in blockBox");
exit (0); exit (0);

View File

@@ -1,12 +1,12 @@
#ifndef FONCTION_H #ifndef FONCTION_H
#define FONCTION_H #define FONCTION_H
#include <SDL2/SDL.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <SDL2/SDL.h>
#define EMPTY 0 #define EMPTY 0
#define WALL 1 #define WALL 1
@@ -32,11 +32,11 @@ typedef struct Score
typedef struct essential_sdl typedef struct essential_sdl
{ {
SDL_Window *window ; SDL_Window *window;
SDL_Renderer *renderer; SDL_Renderer *renderer;
unsigned int size_window; unsigned int size_window;
unsigned int size_box; unsigned int size_box;
}dis; } dis;
unsigned short int **creatArea2D (const unsigned int N); unsigned short int **creatArea2D (const unsigned int N);
void free2D (unsigned short int **tab, 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 islose (unsigned short int **tab2d, const int N);
bool blockBox (unsigned short int **tab2d, vect box_coor); bool blockBox (unsigned short int **tab2d, vect box_coor);
vect plusVect (vect one, vect two); vect plusVect (vect one, vect two);
void void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets, vect *targets, int nbr_targets, dis *display_user);
int nbr_targets, dis *display_user);
#endif // FONCTION_H #endif // FONCTION_H

30
main.c
View File

@@ -6,45 +6,39 @@
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #include <time.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#define SIZE_PLAY 30 #define SIZE_PLAY 30
int int main ()
main ()
{ {
dis display_user; dis display_user;
display_user.size_window = getMaxSize(); display_user.size_window = getMaxSize ();
display_user.size_box = SIZE_PLAY; display_user.size_box = SIZE_PLAY;
initSDL(&display_user); initSDL (&display_user);
vect *playerPos = (vect *)malloc (sizeof (vect)); vect *playerPos = (vect *)malloc (sizeof (vect));
vect *targets; vect *targets;
int nbr_targets; int nbr_targets;
score playerScore; score playerScore;
unsigned short int **tab2d = creatArea2D (SIZE_PLAY); unsigned short int **tab2d = creatArea2D (SIZE_PLAY);
playerScore.before = time (NULL); 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); 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); playerScore.after = time (NULL);
printf ("%ld\n", playerScore.after - playerScore.before); printf ("%ld\n", playerScore.after - playerScore.before);
SDL_DestroyWindow(display_user.window); SDL_DestroyWindow (display_user.window);
SDL_DestroyRenderer(display_user.renderer); SDL_DestroyRenderer (display_user.renderer);
free2D (tab2d, SIZE_PLAY); free2D (tab2d, SIZE_PLAY);
free (playerPos); free (playerPos);
free (targets); free (targets);
SDL_Quit(); SDL_Quit ();
return 0; return 0;
} }

75
read.c
View File

@@ -2,52 +2,51 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
vect * vect *fileToTab2D (const char *name_file, unsigned short int **tab,
fileToTab2D (const char *name_file, unsigned short int **tab, const unsigned N, const unsigned N, vect *player, int *nbr_targets)
vect *player, int *nbr_targets)
{ {
vect *targets = malloc (sizeof (vect)); vect *targets = malloc (sizeof (vect));
(*nbr_targets) = 0; (*nbr_targets) = 0;
FILE *file = fopen (name_file, "r"); FILE *file = fopen (name_file, "r");
unsigned int x = 0, y = 1; unsigned int x = 0, y = 1;
while (!feof (file)) while (!feof (file))
{
char current = fgetc (file);
switch (current)
{ {
char current = fgetc (file); case '#':
switch (current) tab[x][y] = WALL;
{ break;
case '#': case 'C':
tab[x][y] = WALL; tab[x][y] = BOX;
break; break;
case 'C': case 'I':
tab[x][y] = BOX; targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
break; targets[nbr_targets[0] - 1].x = x;
case 'I': targets[nbr_targets[0] - 1].y = y;
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0])); tab[x][y] = TARGET;
targets[nbr_targets[0] - 1].x = x; break;
targets[nbr_targets[0] - 1].y = y; case 'P':
tab[x][y] = TARGET; player->x = x;
break; player->y = y;
case 'P': tab[x][y] = PLAYER;
player->x = x; break;
player->y = y; case '\n':
tab[x][y] = PLAYER; y = 0;
break; ++x;
case '\n': break;
y = 0; default:
++x; tab[x][y] = EMPTY;
break; break;
default:
tab[x][y] = EMPTY;
break;
}
++y;
if (x >= N || y >= N)
{
perror ("Level out of range !");
exit (-1);
}
} }
++y;
if (x >= N || y >= N)
{
perror ("Level out of range !");
exit (-1);
}
}
fclose (file); fclose (file);
return targets; return targets;
} }

View File

@@ -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;
}