diff --git a/Pontu/entryPoints/main.c b/Pontu/entryPoints/main.c index cf480dc..b679198 100644 --- a/Pontu/entryPoints/main.c +++ b/Pontu/entryPoints/main.c @@ -37,16 +37,17 @@ int main(int argc, char const *argv[]) { exit(2); } FontHandler fontHandler = loadFonts(); + AudioHandler audioHandler = newAudioHandler(128, 128, 128); generalState = GS_MainMenu; while(generalState != GS_Quit){ switch (generalState) { case GS_MainMenu: - mainMenu(renderer,window,&generalState, fontHandler); + mainMenu(renderer,window,&generalState, fontHandler, audioHandler); break; - case GS_EndOfGameMenu:// Coupler avec le menu de jeu + /*case GS_EndOfGameMenu:// Coupler avec le menu de jeu endGameMenu(&generalState, window, renderer, fontHandler, NULL, 0); - break; + break;*/ } } diff --git a/Pontu/include/engine/AudioHandler.h b/Pontu/include/engine/AudioHandler.h index 0163bab..01be82c 100644 --- a/Pontu/include/engine/AudioHandler.h +++ b/Pontu/include/engine/AudioHandler.h @@ -31,7 +31,8 @@ * \sa #MACRO_FOR_ALL_MUSICS(M) */ #define MACRO_FOR_ALL_SFX(M) \ - M(testClick) + M(testClick) \ + M(menu_sound_effect) /** * Macro used to generate the entries for the musics in #EnumAudios. diff --git a/Pontu/include/engine/Button.h b/Pontu/include/engine/Button.h index 1e7be9c..eb728f3 100644 --- a/Pontu/include/engine/Button.h +++ b/Pontu/include/engine/Button.h @@ -58,7 +58,9 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button); * \param[in] y y of the point * \return SDL_TRUE if the point is on the button */ -bool isHover(P_Button* button, int x,int y);// dit si le bouton est survolé en donnant les coordonnées x,y +bool isHover(P_Button* button);// dit si le bouton est survolé en donnant les coordonnées x,y + +bool isButtonEntry(P_Button * button,const int x,const int y); /** * \brief Free the texture of a button. diff --git a/Pontu/include/view/MainMenu.h b/Pontu/include/view/MainMenu.h index d8aa932..af1e5e8 100644 --- a/Pontu/include/view/MainMenu.h +++ b/Pontu/include/view/MainMenu.h @@ -7,9 +7,10 @@ #include "engine/TextureLoader.h" #include "engine/FontLoader.h" #include "engine/GeneralState.h" +#include "engine/AudioHandler.h" P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, unsigned int* nb, const SDL_Rect* windowSize); -int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler); +int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler); #endif diff --git a/Pontu/rsrc/sfx/menu_sound_effect.wav b/Pontu/rsrc/sfx/menu_sound_effect.wav new file mode 100644 index 0000000..773aa5b Binary files /dev/null and b/Pontu/rsrc/sfx/menu_sound_effect.wav differ diff --git a/Pontu/src/engine/AudioHandler.c b/Pontu/src/engine/AudioHandler.c index a217a75..c468e41 100644 --- a/Pontu/src/engine/AudioHandler.c +++ b/Pontu/src/engine/AudioHandler.c @@ -2,7 +2,7 @@ // A channel represents the number of SFX we can play at the same time. // We normally should use only 1 channel, and we add one for safety. -#define NBCHANNELS 2 +#define NBCHANNELS 10 // Local functions @@ -19,7 +19,7 @@ int fadeOut(void* args) { // casting args to a pointer to Mix_Music Mix_Music* music = (Mix_Music*)args; int ret; - + if(Mix_FadeOutMusic(500) == 1) { // Starting the fadeout while (Mix_PlayingMusic()) { ; // Waiting until it's done @@ -60,7 +60,7 @@ AudioHandler newAudioHandler(int masterVol, int volMusic, int volSFX) { audioHandler.masterVol = masterVol; fprintf(stderr,"Musics: %d\nSFX: %d\n",NB_MUSIC_DEFINED,nb_SFX); - + // Loading musics for (size_t i = 0; i < NB_MUSIC_DEFINED; i++) { audioHandler.musics[i] = Mix_LoadMUS(musicsPaths[i]); @@ -140,13 +140,13 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) { fprintf(stderr,"WARNING: tried to play an arbitrary value as a music\n"); return; } - + // Checking if audio has been opened. if (!(audioHandler.canPlayAudio)) { fprintf(stderr,"WARNING: tried to play a music with an unusable AudioHandler\n"); return; } - + // If another music is playing, fading the previous one out if (Mix_PlayingMusic()) { // Creating the thread, passing the music as parameter @@ -180,7 +180,7 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) { fprintf(stderr,"WARNING: tried to play an SFX with an unusable AudioHandler\n"); return; } - + // Getting actual chunk chunkSFX = audioHandler.sfx[sfx - NB_MUSIC_DEFINED - 1]; // Getting first available channel @@ -196,4 +196,3 @@ void playSFX(EnumAudios sfx, AudioHandler audioHandler) { return; } } - diff --git a/Pontu/src/engine/Button.c b/Pontu/src/engine/Button.c index 5e3d0af..273c962 100644 --- a/Pontu/src/engine/Button.c +++ b/Pontu/src/engine/Button.c @@ -27,12 +27,8 @@ bool drawButtonOnRenderer(SDL_Renderer* renderer, P_Button* button) return true; } -bool isHover(P_Button* button,const int x,const int y) +bool isHover(P_Button* button) { - SDL_Point coord; - coord.x = x; - coord.y = y; - button->hover = SDL_PointInRect(&coord,&(button->rect)); return button->hover && button->drawn; } @@ -60,3 +56,15 @@ void freeButton(P_Button * button){ SDL_DestroyTexture(button->texture); SDL_DestroyTexture(button->hoverTexture); } + +bool isButtonEntry(P_Button * button,const int x,const int y){ + SDL_Point coord; + coord.x = x; + coord.y = y; + if(isHover(button)){ + button->hover = SDL_PointInRect(&coord,&(button->rect)); + return false; + } + button->hover = SDL_PointInRect(&coord,&(button->rect)); + return button->hover; +} diff --git a/Pontu/src/engine/GameInputProcessor.c b/Pontu/src/engine/GameInputProcessor.c index 64006c5..73af6e5 100644 --- a/Pontu/src/engine/GameInputProcessor.c +++ b/Pontu/src/engine/GameInputProcessor.c @@ -81,11 +81,10 @@ InputElement proccessGameInput(GameInputProcessor *gameInputProcessor, const SDL { for (size_t i = 0; itabButton.size; ++i) { P_Button* b = &gameInputProcessor->tabButton.elems[i]; - isHover(b, event.motion.x, event.motion.y); + isButtonEntry(b, event.motion.x, event.motion.y); } } } return createInputElementNone(); } - diff --git a/Pontu/src/engine/InputProcessor.c b/Pontu/src/engine/InputProcessor.c index 5b931b3..c970099 100644 --- a/Pontu/src/engine/InputProcessor.c +++ b/Pontu/src/engine/InputProcessor.c @@ -26,7 +26,7 @@ InputElement proccessInput(InputProcessor *inputProcessor) case SDL_MOUSEBUTTONUP: { const SDL_Point mousePoint = {.x = event.button.x, .y = event.button.y}; - + for (size_t i = 0; itabButton.size; ++i) { P_Button* b = &inputProcessor->tabButton.elems[i]; if (SDL_PointInRect(&mousePoint, &b->rect)) { @@ -39,7 +39,7 @@ InputElement proccessInput(InputProcessor *inputProcessor) { for (size_t i = 0; itabButton.size; ++i) { P_Button* b = &inputProcessor->tabButton.elems[i]; - isHover(b, event.motion.x, event.motion.y); + isButtonEntry(b, event.motion.x, event.motion.y); } break; } @@ -47,4 +47,3 @@ InputElement proccessInput(InputProcessor *inputProcessor) return createInputElementNone(); } - diff --git a/Pontu/src/view/MainMenu.c b/Pontu/src/view/MainMenu.c index 1bd2f85..0423fe1 100644 --- a/Pontu/src/view/MainMenu.c +++ b/Pontu/src/view/MainMenu.c @@ -63,7 +63,7 @@ P_Button* drawMainMenu(SDL_Renderer* renderer,const FontHandler fontHandler, uns return buttons; } -int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler){ +int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * generalState,FontHandler fontHandler, AudioHandler audioHandler){ int statut = EXIT_FAILURE; char* path = "../rsrc/img/Lenna.png"; //Initialisation @@ -92,17 +92,20 @@ int mainMenu(SDL_Renderer * renderer,SDL_Window * window, GeneralState * general *generalState = GS_Quit; break; case SDL_MOUSEBUTTONUP: - if(isHover(buttons,event.button.x,event.button.y)) + if(isHover(buttons)) printf("Nouvelle partie\n"); - if(isHover(&(buttons[2]),event.motion.x,event.motion.y)){ + if(isHover(&(buttons[2]))){ buttons[2].onClick(&(buttons[2])); break; } break; case SDL_MOUSEMOTION: - isHover(&(buttons[0]),event.motion.x,event.motion.y); - isHover(&(buttons[1]),event.motion.x,event.motion.y); - isHover(&(buttons[2]),event.motion.x,event.motion.y); + if(isButtonEntry(&(buttons[0]),event.motion.x,event.motion.y) || + isButtonEntry(&(buttons[1]),event.motion.x,event.motion.y) || + isButtonEntry(&(buttons[2]),event.motion.x,event.motion.y)){ + playSFX(SFX_menu_sound_effect, audioHandler); + printf("True\n"); + } break; default: break;