From 8cf39bb99eab911a171af3c0945a54594023a7f3 Mon Sep 17 00:00:00 2001 From: thmaillarb Date: Tue, 4 Jan 2022 10:07:37 +0100 Subject: [PATCH] Partially tested AudioHandler -> fade out doesn't work --- Pontu/entryPoints/test.c | 8 ++--- Pontu/include/engine/AudioHandler.h | 5 ++- Pontu/src/engine/AudioHandler.c | 21 +++++++------ Pontu/test/testAudio.c | 32 ------------------- Pontu/test/testAudioHandler.c | 48 +++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 49 deletions(-) delete mode 100644 Pontu/test/testAudio.c create mode 100644 Pontu/test/testAudioHandler.c diff --git a/Pontu/entryPoints/test.c b/Pontu/entryPoints/test.c index d4c2aaa..aedb171 100644 --- a/Pontu/entryPoints/test.c +++ b/Pontu/entryPoints/test.c @@ -1,6 +1,6 @@ //#include "../test/testTextureLoader.c" -#include "../test/testFontLoader.c" -// #include "../test/testAudio.c" +//#include "../test/testFontLoader.c" +#include "../test/testAudioHandler.c" // #include "../test/testGenerateurTexture.c" /* @@ -9,8 +9,8 @@ int main(/*int argc, char *argv[]*/) { //testTextureLoader(); - //testAudio(); - testFontLoader(); + testAudioHandler(); + //testFontLoader(); //testGenerateurTexture(); return 0; diff --git a/Pontu/include/engine/AudioHandler.h b/Pontu/include/engine/AudioHandler.h index cf64ba5..1b8fe30 100644 --- a/Pontu/include/engine/AudioHandler.h +++ b/Pontu/include/engine/AudioHandler.h @@ -105,7 +105,7 @@ void changeSFXVol(int volSFX); * \brief Frees the music and SFX, and un-initializes the audio. * \param[in,out] audioHandler A pointer to the AudioHandler to be freed. */ -void freeAudioHandler(AudioHandler audioHandler); +void freeAudioHandler(AudioHandler* audioHandler); /** * \brief Plays a music. If another music is currently playing, the previous one will fade out. @@ -117,10 +117,9 @@ void playMusic(EnumAudios music, AudioHandler audioHandler); /** * \brief Plays a sound effect. * \param[in] sfx The SFX to be played. Should be a value of #EnumAudios starting with SFX_. - * \param[in] channel The channel the SFX should be played on. * \param[in] audioHandler The AudioHandler used to store the SFX. */ -void playSFX(EnumAudios sfx, int channel, AudioHandler audioHandler); +void playSFX(EnumAudios sfx, AudioHandler audioHandler); #endif // AUDIO_H diff --git a/Pontu/src/engine/AudioHandler.c b/Pontu/src/engine/AudioHandler.c index 06e8ced..00f41b8 100644 --- a/Pontu/src/engine/AudioHandler.c +++ b/Pontu/src/engine/AudioHandler.c @@ -17,7 +17,7 @@ int fadeOut(void* args) { // Since args is a pointer to void // (the way C handles undefined types), // casting args to a pointer to Mix_Music - Mix_Music* = (Mix_Music*)args; + Mix_Music* music = (Mix_Music*)args; int ret; if(Mix_FadeOutMusic(500) == 0) { // Starting the fadeout @@ -29,7 +29,7 @@ int fadeOut(void* args) { Mix_HaltMusic(); } - ret = Mix_PlayMusic(argsCast->music,-1); + ret = Mix_PlayMusic(music,-1); if (ret != 0) { fprintf(stderr,"WARNING: %s\n",Mix_GetError()); } @@ -43,7 +43,7 @@ int fadeOut(void* args) { AudioHandler newAudioHandler(int volMusic, int volSFX) { AudioHandler audioHandler; - int nb_SFX = NB_AUDIO_DEFINED - NB_MUSC_DEFINED - 1; + int nb_SFX = NB_AUDIO_DEFINED - NB_MUSIC_DEFINED - 1; // Generating paths to musics and SFX files using macros char* musicsPaths[] = {MACRO_FOR_ALL_MUSICS(MACRO_TO_MUSIC_PATH)}; @@ -62,10 +62,11 @@ AudioHandler newAudioHandler(int volMusic, int volSFX) { // Loading musics for (size_t i = 0; i < NB_MUSIC_DEFINED; i++) { - if ((audioHandler.musics[i] = LoadMUS(musicPaths[i]) == NULL)) { + audioHandler.musics[i] = Mix_LoadMUS(musicsPaths[i]); + if (audioHandler.musics[i] == NULL) { fprintf(stderr,"WARNING: %s\n",Mix_GetError()); } else { - fprintf(stderr,"Loaded %s\n",musicPaths[i]); + fprintf(stderr,"Loaded %s\n",musicsPaths[i]); } } @@ -75,8 +76,9 @@ AudioHandler newAudioHandler(int volMusic, int volSFX) { Mix_AllocateChannels(NBCHANNELS); // Loading SFX - for (size_t i = 0; i < nb_SFX; i++) { - if ((audioHandler.sfx[i] = Mix_LoadWAV(sfxPaths[i])) == NULL) { + for (int i = 0; i < nb_SFX; i++) { + audioHandler.sfx[i] = Mix_LoadWAV(sfxPaths[i]); + if (audioHandler.sfx[i] == NULL) { fprintf(stderr,"WARNING: %s\n",Mix_GetError()); } else { fprintf(stderr,"Loaded %s\n",sfxPaths[i]); @@ -88,7 +90,7 @@ AudioHandler newAudioHandler(int volMusic, int volSFX) { } void changeSFXVol(int volSFX) { - for (size_t i = 0; i < NBCHANNELS; i++) { + for (int i = 0; i < NBCHANNELS; i++) { Mix_Volume(i, volSFX); } } @@ -145,13 +147,12 @@ void playMusic(EnumAudios music, AudioHandler audioHandler) { // No other music is playing: starting a music normally. } else { if (Mix_PlayMusic(audioHandler.musics[music],-1) != 0) { - fprintf("WARNING: %s\n",Mix_GetError()); + fprintf(stderr,"WARNING: %s\n",Mix_GetError()); } } } void playSFX(EnumAudios sfx, AudioHandler audioHandler) { - int nb_SFX = NB_AUDIO_DEFINED - NB_MUSIC_DEFINED - 1; int channel; Mix_Chunk* chunkSFX; diff --git a/Pontu/test/testAudio.c b/Pontu/test/testAudio.c deleted file mode 100644 index ad37449..0000000 --- a/Pontu/test/testAudio.c +++ /dev/null @@ -1,32 +0,0 @@ -#include "engine/Audio.h" -#include -#include -#include -#include - -void testAudio(void) { - char path1[] = "../rsrc/music/testMus.mp3"; - char path2[] = "../rsrc/music/base_tardi.mp3"; - Mix_Music* music = NULL; - - if (SDL_Init(SDL_INIT_AUDIO) != 0) { - perror("SDL"); - exit(errno); - } - - if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,MIX_DEFAULT_CHANNELS,1024) != 0) { - SDL_Quit(); - perror("SDL Mixer"); - exit(errno); - } - - switchMusic(music,path1); - SDL_Delay(5000); - switchMusic(music,path2); - SDL_Delay(5000); - - Mix_FreeMusic(music); - SDL_Quit(); -} - - diff --git a/Pontu/test/testAudioHandler.c b/Pontu/test/testAudioHandler.c new file mode 100644 index 0000000..b32afd0 --- /dev/null +++ b/Pontu/test/testAudioHandler.c @@ -0,0 +1,48 @@ +#include "engine/AudioHandler.h" +#include +#include +#include +#include + +void testAudioHandler(void) { + if (SDL_Init(SDL_INIT_AUDIO) != 0) { + perror("SDL"); + exit(errno); + } + + AudioHandler ah = newAudioHandler(100, 100); + if (ah.canPlayAudio == false) { + perror("AudioHandler"); + SDL_Quit(); + exit(1); + } + + printf("AudioHandler created!\n"); + + printf("Now (normally) playing base_tardi...\n"); + playMusic(MUSIC_base_tardi, ah); + + SDL_Delay(2000); + + printf("Switching to testMus\n"); + playMusic(MUSIC_testMus, ah); + + SDL_Delay(5000); + + printf("Playing SFX"); + + playSFX(SFX_testClick, ah); + + SDL_Delay(1000); + + printf("Freeing AudioHandler\n"); + freeAudioHandler(&ah); + + if (ah.canPlayAudio == true) { + printf("Failed to free AudioHandler!\n"); + } + + SDL_Quit(); +} + +