/** * \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 #include #include #define SIZE_PLAY 20 #define SIZE_MENU 200 // Définir une seed globale unsigned int seed; /** * \brief Cette fonction permet de joueur des effet sonor. * \param sfx un int designant le numero du son a jouer. * du joueur. * \return Void * */ void playAudio(int sfx) { // Charger un fichier son Mix_Chunk* sfx_win = Mix_LoadWAV("sfx/win.wav"); Mix_Chunk* sfx_lose = Mix_LoadWAV("sfx/lose.wav"); Mix_Chunk* sfx_cant_move = Mix_LoadWAV("sfx/cantmove.wav"); Mix_Chunk* sfx_block_on_target = Mix_LoadWAV("sfx/blockontarget.wav"); Mix_Chunk* sfx_move = Mix_LoadWAV("sfx/move.mp3"); Mix_Chunk* sfx_move_block = Mix_LoadWAV("sfx/moveblock.wav"); switch (sfx) { case 0: Mix_PlayChannel(-1, sfx_win, 0); return; case 1: Mix_PlayChannel(-1, sfx_lose, 0); return; case 2: Mix_PlayChannel(-1, sfx_cant_move, 0); return; case 3: Mix_PlayChannel(-1, sfx_block_on_target, 0); return; case 4: Mix_PlayChannel(-1, sfx_move, 0); return; case 5: Mix_PlayChannel(-1, sfx_move_block, 0); return; } Mix_FreeChunk(sfx_win); Mix_FreeChunk(sfx_lose); Mix_FreeChunk(sfx_cant_move); Mix_FreeChunk(sfx_block_on_target); Mix_FreeChunk(sfx_move); Mix_FreeChunk(sfx_move_block); } int main () { seed = (unsigned int)time(NULL); 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; //mixeur audio // Initialisation de SDL_mixer Mix_Init(MIX_INIT_MP3); // Ouverture du mixeur audio Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096); // Charger une musique Mix_Music* music_menu = Mix_LoadMUS("sfx/musicMenu.mp3"); Mix_Music* music_game = Mix_LoadMUS("sfx/levelPlay.mp3"); Mix_Music* music_editor = Mix_LoadMUS("sfx/editor.mp3"); Mix_Volume(-1,MIX_MAX_VOLUME); // Définit le volume maximal pour tous les canaux Mix_VolumeMusic(MIX_MAX_VOLUME / 2); // Loop pour le jeu entier menu et les jeux. while(output != 0) { SDL_RenderClear(display_user.renderer); if(output == -1) { Mix_PlayMusic(music_menu, -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) { Mix_PlayMusic(music_editor, -1); backgroundDisplay(&display_user,2); output = inEditorLoop(tab2d, &dim, playerPos, targets, nbr_targets, &display_user,&score_user ,num_fichier); } else { Mix_PlayMusic(music_game, -1); 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); Mix_CloseAudio(); SDL_Quit (); return 0; }