createur de map fini

This commit is contained in:
cyjullien1
2025-01-06 16:05:47 +01:00
parent 46dc1a800a
commit 13fd66cecb
7 changed files with 52 additions and 40 deletions

View File

@@ -55,5 +55,5 @@ void nullScore(score *player_score);
void winOrLoseLoop(dis *display_user,score *score_user, bool win);
unsigned int scoreCalculator(score *score_user, bool win);
int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
int nbr_targets, dis *display_user, score *score_user);
int nbr_targets, dis *display_user,score *score_user, int num_fichier);
#endif // FONCTION_H

View File

@@ -1,8 +0,0 @@
################
# #
# IIIIIIIIIII #
# CCCCCCCCCCC #
# #
# #
# P #
################

View File

@@ -1,6 +0,0 @@
#############
##### I #
# C #
# #
# P #
#############

View File

@@ -1,5 +0,0 @@
##########
#PC I#
# C #
#I #
##########

View File

@@ -137,6 +137,8 @@ char canIGoDirection (char valueOfNCase, char valueOfNPlusOneCase)
return 0;
}
/**
* \brief Cette fonction effectue les deplacements du joueur et des boites en
* fonction de la situation.
@@ -428,6 +430,39 @@ int inGameLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
return -1;
}
/**
*
* \brief La fonction permet de retranscrire un niveau creer du tableau vers le fichier.txt
* \param filename Le nom du fichier a remplir.
* \param tab2d Le tableau 2d carre.
*/
void save_grid_to_file(int filename, char **tab2D) {
char path[250];
snprintf(path, sizeof(path), "maps/custom_%d.txt", filename);
FILE *file = fopen(path, "w");
// Parcours et sauvegarde du tableau 2D dans le fichier
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
switch (tab2D[i][j]) {
case EMPTY: fputc(' ',file);break;
case WALL: fputc('#',file); break;
case BOX: fputc('C',file);break;
case TARGET: fputc('I',file); break;
case BOX_ON_TARGET: fputc(' ',file); break;
case PLAYER: fputc('P',file); break;
case PLAYER_ON_TARGET: fputc('P',file); break;
default: return;
}
}
fputc('\n',file); // Nouvelle ligne pour chaque ligne du tableau
}
fclose(file);
}
/**
*
* \brief La fonction permet de faire la boucle de l'éditeur.
@@ -440,10 +475,11 @@ int inGameLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
* \param display_user Tout les information SDL pour afficher le jeu.
* \param score_user Toute les données nécessaire pour calculer le score fini
* du joueur.
* \param num_fichier Simplement le numéro de la map actuel.
* \return renvoie -2 ce qui signifie l'editeur
*/
int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
int nbr_targets, dis *display_user, score *score_user)
int nbr_targets, dis *display_user,score *score_user, int num_fichier)
{
vect direction = { 0, 0 };
int fov = -1;
@@ -469,7 +505,7 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
int GRID_SIZE = 20;
int BLOCK_SIZE = 24;
int BLOCK_SIZE = 25;
int GRID_X_OFFSET =100;
int GRID_Y_OFFSET =0;
while (!finish)
@@ -478,6 +514,7 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
while (SDL_PollEvent (&event))
{
if (event.type == SDL_QUIT) {
save_grid_to_file(num_fichier,tab2d);
return 0; // Quitter si l'utilisateur ferme la fenêtre
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
@@ -486,7 +523,6 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
int click_x, click_y;
SDL_GetMouseState(&click_x, &click_y); // Récupérer la position de la souris
printf("Clic gauche à la position: (%d, %d)\n", click_x, click_y);
if (click_x >= GRID_X_OFFSET && click_x < GRID_X_OFFSET + GRID_SIZE * BLOCK_SIZE &&
click_y >= GRID_Y_OFFSET && click_y < GRID_Y_OFFSET + GRID_SIZE * BLOCK_SIZE) {
@@ -506,10 +542,6 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
}
// Met à jour l'affichage
screenDisplayGameSDL (tab2d, *dim_tab, display_user, playerPos, fov);
printf("Clic détecté dans la case [%d][%d]. Nouvelle valeur : %d\n", row, col, tab2d[row][col]);
} else {
printf("Clic hors de la grille.\n");
}
}
if (event.button.button == SDL_BUTTON_RIGHT) {
@@ -517,8 +549,6 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
int click_x, click_y;
SDL_GetMouseState(&click_x, &click_y); // Récupérer la position de la souris
printf("Clic droit à la position: (%d, %d)\n", click_x, click_y);
if (click_x >= GRID_X_OFFSET && click_x < GRID_X_OFFSET + GRID_SIZE * BLOCK_SIZE &&
click_y >= GRID_Y_OFFSET && click_y < GRID_Y_OFFSET + GRID_SIZE * BLOCK_SIZE) {
@@ -535,12 +565,9 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
{
tab2d[row][col] -= 1;
}
printf("mtn la case est a %hd \n",tab2d[row][col]);
// Met à jour l'affichage
screenDisplayGameSDL (tab2d, *dim_tab, display_user, playerPos, fov);
printf("Clic détecté dans la case [%d][%d]. Nouvelle valeur : %d\n", row, col, tab2d[row][col]);
} else {
printf("Clic hors de la grille.\n");
}
}
}
@@ -551,7 +578,8 @@ int inEditorLoop (char **tab2d, vect *dim_tab, vect *playerPos, vect *targets,
{
case SDL_SCANCODE_ESCAPE:
return -1000;
save_grid_to_file(num_fichier,tab2d);
return -1;
finish = true;
break;
case SDL_SCANCODE_D:

View File

@@ -31,6 +31,7 @@ int main ()
char **tab2d = creatArea2D (SIZE_PLAY, SIZE_PLAY);
score score_user = {0, 0, 0,0};
int num_fichier=0;
vect dim = {SIZE_PLAY, SIZE_PLAY};
int output = -1;
@@ -63,7 +64,7 @@ int main ()
{
printf("editeur de map");
//newmap
output = inEditorLoop(tab2d, &dim, playerPos, targets, nbr_targets, &display_user, &score_user);
output = inEditorLoop(tab2d, &dim, playerPos, targets, nbr_targets, &display_user,&score_user ,num_fichier);
}
else
{
@@ -93,16 +94,17 @@ int main ()
else{
char path[256];
snprintf(path, sizeof(path), "maps/custom_%d.txt", output-4);
num_fichier = output-4;
FILE *src = fopen("maps/viergemap.txt", "r");
FILE *file = fopen(path, "w");
char buffer[1024]; // Tampon pour lire le contenu
char tmp[1024]; // Tampon pour lire le contenu
size_t bytesRead;
// Lire le contenu du fichier source et l'écrire dans le fichier destination
while ((bytesRead = fread(buffer, 1, sizeof(buffer), src)) > 0) {
fwrite(buffer, 1, bytesRead, file);
while ((bytesRead = fread(tmp, 1, sizeof(tmp), src)) > 0) {
fwrite(tmp, 1, bytesRead, file);
}
fclose(src);

View File

@@ -39,6 +39,7 @@ vect *fileToTab2D (const char *name_file, char **tab,
case 'C':
tab[x][y] = BOX;
break;
case 'I':
targets = realloc (targets, sizeof (vect) * (++nbr_targets[0]));
targets[nbr_targets[0] - 1].x = x;