From 13fd66cecb7f9a98ee11a58906bb1bed59223bd2 Mon Sep 17 00:00:00 2001 From: cyjullien1 Date: Mon, 6 Jan 2025 16:05:47 +0100 Subject: [PATCH] createur de map fini --- include/function.h | 2 +- maps/custom_1.txt | 8 ------- maps/custom_2.txt | 6 ----- maps/custom_3.txt | 5 ---- script/function.c | 60 +++++++++++++++++++++++++++++++++------------- script/main.c | 10 ++++---- script/read.c | 1 + 7 files changed, 52 insertions(+), 40 deletions(-) delete mode 100644 maps/custom_1.txt delete mode 100644 maps/custom_2.txt delete mode 100644 maps/custom_3.txt diff --git a/include/function.h b/include/function.h index 425fba2..4f4371f 100644 --- a/include/function.h +++ b/include/function.h @@ -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 diff --git a/maps/custom_1.txt b/maps/custom_1.txt deleted file mode 100644 index c1af780..0000000 --- a/maps/custom_1.txt +++ /dev/null @@ -1,8 +0,0 @@ -################ -# # -# IIIIIIIIIII # -# CCCCCCCCCCC # -# # -# # -# P # -################ diff --git a/maps/custom_2.txt b/maps/custom_2.txt deleted file mode 100644 index 65df458..0000000 --- a/maps/custom_2.txt +++ /dev/null @@ -1,6 +0,0 @@ -############# -##### I # -# C # -# # -# P # -############# diff --git a/maps/custom_3.txt b/maps/custom_3.txt deleted file mode 100644 index 521facf..0000000 --- a/maps/custom_3.txt +++ /dev/null @@ -1,5 +0,0 @@ -########## -#PC I# -# C # -#I # -########## diff --git a/script/function.c b/script/function.c index d50b912..c381107 100644 --- a/script/function.c +++ b/script/function.c @@ -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,19 +542,13 @@ 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) { // Lorsque le clic gauche est effectué 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,13 +565,10 @@ 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"); - } + } } } if (event.type == SDL_KEYDOWN) @@ -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: diff --git a/script/main.c b/script/main.c index e96246c..e0398c5 100644 --- a/script/main.c +++ b/script/main.c @@ -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); diff --git a/script/read.c b/script/read.c index 5b58e98..37fd119 100644 --- a/script/read.c +++ b/script/read.c @@ -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;