/** * \file main.c * * Le main permet de stocker et de lancer les fonctions permetant de lancer le jeu. */ #include "../include/display.h" #include "../include/function.h" #include "../include/read.h" #include #include #include #include #include #include #define SIZE_PLAY 20 #define SIZE_MENU 200 int main () { dis display_user; display_user.size_menu = SIZE_MENU; display_user.size_box = SIZE_PLAY; display_user.size_window = getMaxSize (display_user); initSDL (&display_user); vect *playerPos = (vect *)malloc (sizeof (vect)); vect *targets = {0}; int nbr_targets; score playerScore = {0}; 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; vect dim_menu = {0, 0}; vect pos = {4, 1}; char**menu = generatorMenu("maps", &dim_menu, &pos); backgroundDisplay(&display_user,0); vect null = {0, 0}; output = inGameLoop (menu, &dim_menu, &pos, &null , 1, &display_user, &score_user, true); nullScore(&score_user); SDL_RenderClear(display_user.renderer); SDL_Event event; // Loop pour le jeu entier menu et les jeux. while(output != 0) { SDL_RenderClear(display_user.renderer); if(output == -1) { backgroundDisplay(&display_user,0); menu = generatorMenu("maps", &dim_menu, &pos); pos.x = 4; pos.y = 1; output = inGameLoop(menu, &dim_menu, &pos, &null, 1, &display_user, &score_user, true); nullScore(&score_user); } else if(output == -2) { backgroundDisplay(&display_user,2); output = inEditorLoop(tab2d, &dim, playerPos, targets, nbr_targets, &display_user,&score_user ,num_fichier); } else { backgroundDisplay(&display_user,1); score_user.before = time(NULL); char txt[30] = ""; if(output <= 3) { snprintf(txt, 30, "maps/original_%d.txt", output); } else { snprintf(txt, 30, "maps/custom_%d.txt", output - 4); } if(access(txt, F_OK)==0) { if (targets != NULL) free(targets); if (tab2d != NULL) free2D(tab2d, SIZE_PLAY); tab2d = creatArea2D(SIZE_PLAY, SIZE_PLAY); targets = fileToTab2D(txt, tab2d, SIZE_PLAY, playerPos, &nbr_targets); output = inGameLoop(tab2d, &dim, playerPos, targets, nbr_targets, &display_user, &score_user, false); SDL_RenderClear(display_user.renderer); score_user.after = time(NULL); if(output != -1000) winOrLoseLoop(&display_user, &score_user, output==1); output = -1; } 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 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(tmp, 1, sizeof(tmp), src)) > 0) { fwrite(tmp, 1, bytesRead, file); } fclose(src); fclose(file); tab2d = creatArea2D(SIZE_PLAY, SIZE_PLAY); targets = fileToTab2D(txt, tab2d, SIZE_PLAY, playerPos, &nbr_targets); output = -2; } } } //screenDisplay(tab2d, SIZE_PLAY, SIZE_PLAY); SDL_DestroyWindow (display_user.window); SDL_DestroyRenderer (display_user.renderer); if (tab2d != NULL)free2D (tab2d, SIZE_PLAY); if (playerPos != NULL)free (playerPos); if (targets != NULL)free (targets); SDL_Quit (); return 0; }