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

View File

@@ -8,8 +8,7 @@
#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', '.', '*', '@', '+' };
@@ -23,8 +22,7 @@ screenDisplay (unsigned short int **tab, int size)
} }
} }
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;
@@ -59,7 +57,6 @@ screenDisplaySDL (unsigned short int **tab, dis *display_user)
case PLAYER_ON_TARGET: case PLAYER_ON_TARGET:
img = IMG_Load ("player_on_target.png"); img = IMG_Load ("player_on_target.png");
break; break;
} }
texture = SDL_CreateTextureFromSurface (display_user->renderer, img); texture = SDL_CreateTextureFromSurface (display_user->renderer, img);
displayImage (display_user->renderer, texture, pos, size); displayImage (display_user->renderer, texture, pos, size);
@@ -71,8 +68,7 @@ screenDisplaySDL (unsigned short int **tab, dis *display_user)
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;
@@ -90,13 +86,12 @@ getMaxSize ()
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 ();
@@ -104,7 +99,8 @@ initSDL (dis *display_user)
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 ();
@@ -113,8 +109,8 @@ initSDL (dis *display_user)
} }
} }
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,8 +6,7 @@
#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 *));
@@ -40,8 +39,7 @@ creatArea2D (const unsigned int N)
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)
@@ -52,8 +50,8 @@ free2D (unsigned short int **tab, int N)
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)
{ {
@@ -95,8 +93,7 @@ canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
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];
@@ -130,8 +127,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
// 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
@@ -149,8 +145,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
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;
@@ -181,9 +176,8 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
} }
} }
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;
@@ -242,8 +236,7 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
} }
} }
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)
@@ -258,8 +251,7 @@ isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
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)
@@ -284,8 +276,7 @@ islose (unsigned short int **tab2d, const int N)
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 } };

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

16
main.c
View File

@@ -6,14 +6,10 @@
#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 ();
@@ -28,14 +24,12 @@ main ()
playerScore.before = time (NULL); playerScore.before = time (NULL);
targets
= fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
targets = fileToTab2D ("test.txt", tab2d, SIZE_PLAY, playerPos, &nbr_targets);
screenDisplaySDL (tab2d, &display_user); 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);

5
read.c
View File

@@ -2,9 +2,8 @@
#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;

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