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 <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', '.', '*', '@', '+' };
@@ -23,8 +22,7 @@ screenDisplay (unsigned short int **tab, int size)
}
}
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;
@@ -59,7 +57,6 @@ screenDisplaySDL (unsigned short int **tab, dis *display_user)
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);
@@ -71,8 +68,7 @@ screenDisplaySDL (unsigned short int **tab, dis *display_user)
SDL_RenderPresent (display_user->renderer);
}
int
getMaxSize ()
int getMaxSize ()
{
SDL_Init (SDL_INIT_VIDEO); // init if error
SDL_DisplayMode display;
@@ -90,13 +86,12 @@ getMaxSize ()
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 ();
@@ -104,7 +99,8 @@ initSDL (dis *display_user)
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 ();
@@ -113,8 +109,8 @@ initSDL (dis *display_user)
}
}
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);

View File

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

View File

@@ -6,8 +6,7 @@
#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 *));
@@ -40,8 +39,7 @@ creatArea2D (const unsigned int N)
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)
@@ -52,8 +50,8 @@ free2D (unsigned short int **tab, int N)
return;
}
short int
canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
short int canIGoDirection (short int valueOfNCase,
short int valueOfNPlusOneCase)
{
if (valueOfNCase != WALL)
{
@@ -95,8 +93,7 @@ canIGoDirection (short int valueOfNCase, short int valueOfNPlusOneCase)
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];
@@ -130,8 +127,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
// 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;
tab[playerPos->x + direction.x * 2][playerPos->y + direction.y * 2] = BOX;
break;
case 3:
// move player and the box is well-placed
@@ -149,8 +145,7 @@ move (unsigned short int **tab, vect *playerPos, vect direction)
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 * 2][playerPos->y + direction.y * 2] = BOX;
tab[playerPos->x + direction.x][playerPos->y + direction.y]
= PLAYER_ON_TARGET;
break;
@@ -181,9 +176,8 @@ 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)
void inGameLoop (unsigned short int **tab2d, int N, vect *playerPos,
vect *targets, int nbr_targets, dis *display_user)
{
vect direction = { 0, 0 };
char input;
@@ -242,8 +236,7 @@ inGameLoop (unsigned short int **tab2d, int N, vect *playerPos, vect *targets,
}
}
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)
@@ -258,8 +251,7 @@ isWin (unsigned short int **tab2d, vect *targets, int nbr_targets)
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)
@@ -284,8 +276,7 @@ islose (unsigned short int **tab2d, const int N)
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 } };

View File

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

16
main.c
View File

@@ -6,14 +6,10 @@
#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 ();
@@ -28,14 +24,12 @@ main ()
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);
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);

5
read.c
View File

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

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