Partially tested AudioHandler -> fade out doesn't work

merge-requests/1/merge
thmaillarb 4 years ago
parent fcf2395d1d
commit 8cf39bb99e

@ -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;

@ -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

@ -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;

@ -1,32 +0,0 @@
#include "engine/Audio.h"
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL.h>
#include <stdio.h>
#include <errno.h>
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();
}

@ -0,0 +1,48 @@
#include "engine/AudioHandler.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
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();
}
Loading…
Cancel
Save